Browse Source

好友接口

lizhen_gitee 10 months ago
parent
commit
d20d10d76d
1 changed files with 143 additions and 0 deletions
  1. 143 0
      application/api/controller/Friend.php

+ 143 - 0
application/api/controller/Friend.php

@@ -0,0 +1,143 @@
+<?php
+
+namespace app\api\controller;
+
+use app\common\controller\Api;
+use think\Db;
+/**
+ * 好友
+ */
+class Friend extends Api
+{
+    protected $noNeedLogin = [];
+    protected $noNeedRight = ['*'];
+
+    //好友列表
+    public function lists(){
+        $field = '
+            friend.id,friend.user_id,friend.to_user_id,
+            u.avatar as u_avatar,u.nickname as u_nickname,u.mobile as u_mobile,
+            tu.avatar as tu_avatar,tu.nickname as tu_nickname,tu.mobile as tu_mobile
+            ';
+        $list = Db::name('friend')->field($field)
+            ->join('user u'  ,'friend.user_id    = u.id','LEFT')
+            ->join('user tu' ,'friend.to_user_id = tu.id','LEFT')
+            ->where('(friend.user_id = '.$this->auth->id.') or (friend.to_user_id = '.$this->auth->id.')')
+            ->order('friend.id desc')->autopage()
+            ->select();
+        $result = [];
+
+        if(!empty($list)){
+            foreach($list as $key => $val){
+                $newval = [];
+                if($val['user_id'] == $this->auth->id){
+                    $newval = [
+                        'id'       => $val['to_user_id'],
+                        'nickname' => $val['tu_nickname'],
+                        'avatar'   => $val['tu_avatar'],
+                        'mobile'   => $val['tu_mobile'],
+                    ];
+                }else{
+                    $newval = [
+                        'id'       => $val['user_id'],
+                        'nickname' => $val['u_nickname'],
+                        'avatar'   => $val['u_avatar'],
+                        'mobile'   => $val['u_mobile'],
+                    ];
+                }
+                $result[] = $newval;
+            }
+        }
+
+        $this->success(1,$result);
+    }
+
+    //移除好友
+    public function remove(){
+        $user_id = input('user_id',0);
+        if(empty($user_id)){$this->error();}
+
+        $rs3 = Db::name('friend')->where('user_id',$user_id)->where('to_user_id',$this->auth->id)->delete();
+        $rs4 = Db::name('friend')->where('user_id',$this->auth->id)->where('to_user_id',$user_id)->delete();
+
+        $this->success('操作成功');
+    }
+
+    //申请列表
+    public function apply_list(){
+        $list = Db::name('friend_apply')->field('user.id,user.avatar,user.nickname,user.mobile')
+            ->join('user','friend_apply.user_id = user.id','LEFT')
+            ->where('friend_apply.to_user_id',$this->auth->id)->where('friend_apply.status',0)->autopage()->select();
+
+        $list = list_domain_image($list,['avatar']);
+
+        $this->success(1,$list);
+    }
+
+    //同意与拒绝
+    public function apply_audit(){
+        $user_id = input('user_id',0);
+        $status = input('status',0);
+        if(empty($user_id) || empty($status)){
+            $this->error();
+        }
+
+        if($status == 1){
+            //同意
+            Db::startTrans();
+            $rs1 = Db::name('friend_apply')->where('user_id',$user_id)->where('to_user_id',$this->auth->id)->delete();
+            $rs2 = Db::name('friend_apply')->where('user_id',$this->auth->id)->where('to_user_id',$user_id)->delete();
+            $rs3 = Db::name('friend')->where('user_id',$user_id)->where('to_user_id',$this->auth->id)->delete();
+            $rs4 = Db::name('friend')->where('user_id',$this->auth->id)->where('to_user_id',$user_id)->delete();
+
+            $data = [
+                'user_id'    => $user_id,
+                'to_user_id' => $this->auth->id,
+            ];
+            $rs5 = Db::name('friend')->insertGetId($data);
+
+            if($rs1 !== false && $rs2 !== false && $rs3 !== false && $rs4 !== false && $rs5 !== false){
+                Db::commit();
+                $this->success('操作成功');
+            }else{
+                Db::rollback();
+                $this->error('操作失败');
+            }
+        }else{
+            //拒绝
+            Db::name('friend_apply')->where('user_id',$user_id)->where('to_user_id',$this->auth->id)->delete();
+            $this->success();
+        }
+
+    }
+
+    //搜索
+    public function search(){
+        $keyword = input('keyword',0);
+        if(!$keyword){$this->error();}
+
+        $list = Db::name('user')->field('id,nickname,mobile,avatar')->where('mobile',$keyword)->select();
+        $list = list_domain_image($list,['avatar']);
+
+        $this->success(1,$list);
+    }
+
+    //申请好友
+    public function apply(){
+        $user_id = input('user_id',0);
+        if(!$user_id){$this->error();}
+
+        $check = Db::name('friend_apply')->where('user_id',$this->auth->id)->where('to_user_id',$user_id)->find();
+        if(!$check){
+            $data = [
+                'user_id' => $this->auth->id,
+                'to_user_id' => $user_id,
+            ];
+
+            $id = Db::name('friend_apply')->insertGetId($data);
+        }
+
+        $this->success(1);
+    }
+
+}