Browse Source

建立关系,扣关系卡

lizhen_gitee 1 year ago
parent
commit
4766088af3
1 changed files with 19 additions and 1 deletions
  1. 19 1
      application/api/controller/Relation.php

+ 19 - 1
application/api/controller/Relation.php

@@ -70,6 +70,12 @@ class Relation extends Api
             $this->error('不存在的关系');
         }
 
+        //检查关系卡数量
+        $cardnum = Db::name('user_decorate_relation')->where('user_id',$this->auth->id)->where('is_using',0)->find();
+        if(empty($cardnum)){
+            $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();
@@ -98,7 +104,14 @@ class Relation extends Api
             //可以申请
         }
 
-        //扣钱或道具
+        Db::startTrans();
+
+        //扣掉一个关系卡
+        $use_card = Db::name('user_decorate_relation')->where('id',$cardnum['id'])->update(['is_using'=>1,'updatetime'=>time()]);
+        if($use_card === false){
+            Db::rollback();
+            $this->error('关系申请失败');
+        }
 
         //新增
         $data = [
@@ -111,6 +124,10 @@ class Relation extends Api
         ];
 
         $id = Db::name('user_relation')->insertGetId($data);
+        if(!$id){
+            Db::rollback();
+            $this->error('关系申请失败');
+        }
 
         //给对方一条消息
         $message = [
@@ -124,6 +141,7 @@ class Relation extends Api
         ];
         Db::name('message')->insertGetId($message);
 
+        Db::commit();
 
         $this->success('申请成功,等待对方同意',$id);
     }