Explorar o código

关系的两两唯一性

lizhen_gitee hai 1 ano
pai
achega
05e93b457d
Modificáronse 1 ficheiros con 44 adicións e 4 borrados
  1. 44 4
      application/api/controller/Relation.php

+ 44 - 4
application/api/controller/Relation.php

@@ -69,6 +69,7 @@ class Relation extends Api
         if(empty($relation)){
             $this->error('不存在的关系');
         }
+        $relation_name = $relation['name'];
 
         //检查关系卡数量
         $cardnum = Db::name('user_decorate_relation')->where('user_id',$this->auth->id)->where('is_using',0)->find();
@@ -76,7 +77,8 @@ class Relation extends Api
             $this->error('关系卡数量不足');
         }
 
-        //检查已有关系
+
+        //检查这两个人的已有关系
         $where = '(uid = '.$this->auth->id.' and to_uid = '.$to_uid.') or (uid = '.$to_uid.' and to_uid = '.$this->auth->id.')';
         $check = Db::name('user_relation')->where($where)->find();
 
@@ -84,19 +86,19 @@ class Relation extends Api
             $now_relation = Db::name('relation')->where('id',$check['relation_id'])->value('name');
 
             if($check['status'] == 1){
-                if($check['uid'] = $this->auth->id){
+                if($check['uid'] == $this->auth->id){
                     $this->error('您已经与该用户建立'.$now_relation.'关系');
                 }else{
                     $this->error('该用户已经与您建立'.$now_relation.'关系');
                 }
             }elseif($check['status'] == 0){
-                if($check['uid'] = $this->auth->id){
+                if($check['uid'] == $this->auth->id){
                     $this->error('您已经申请建立'.$now_relation.'关系,待对方同意');
                 }else{
                     $this->error('对方已经申请建立'.$now_relation.'关系,待您同意');
                 }
             }elseif($check['status'] == 2){
-                $this->error($now_relation.'关系的申请已被拒绝,七日内不能申请');
+                $this->error($now_relation.'关系的申请已被拒绝,七日内不能再像TA申请');
             }else{
                 //可以申请
             }
@@ -104,6 +106,44 @@ class Relation extends Api
             //可以申请
         }
 
+        //检查我 关系的唯一性
+        $check_map1 = 'relation_id = '.$relation_id.' and (uid = '.$this->auth->id.' or to_uid = '.$this->auth->id.')';
+        $check_res1 = Db::name('user_relation')->where($check_map1)->find();
+        if($check_res1){
+            if($check_res1['status'] == 1){
+                if($check_res1['uid'] == $this->auth->id){
+                    $this->error('您已经与其他用户建立'.$relation_name.'关系');
+                }else{
+                    $this->error('其他用户已经与您建立'.$relation_name.'关系');
+                }
+            }elseif($check_res1['status'] == 0){
+                if($check_res1['uid'] == $this->auth->id){
+                    $this->error('您已经与其他用户申请建立'.$relation_name.'关系,待对方审核中');
+                }else{
+                    $this->error('其他用户已经与您申请建立'.$relation_name.'关系,待您审核中');
+                }
+            }
+        }
+
+        //检查对方 关系的唯一性
+        $check_map2 = 'relation_id = '.$relation_id.' and (uid = '.$to_uid.' or to_uid = '.$to_uid.')';
+        $check_res2 = Db::name('user_relation')->where($check_map2)->find();
+        if($check_res2){
+            if($check_res2['status'] == 1){
+                if($check_res2['uid'] == $to_uid){
+                    $this->error('TA已经与其他用户建立'.$relation_name.'关系');
+                }else{
+                    $this->error('其他用户已经与TA建立'.$relation_name.'关系');
+                }
+            }elseif($check_res2['status'] == 0){
+                if($check_res2['uid'] == $to_uid){
+                    $this->error('TA已经与其他用户申请建立'.$relation_name.'关系,您可尝试换个关系');
+                }else{
+                    $this->error('其他用户已经与TA申请建立'.$relation_name.'关系,您可尝试换个关系');
+                }
+            }
+        }
+
         Db::startTrans();
 
         //扣掉一个关系卡