Browse Source

用户与好友

lizhen_gitee 9 months ago
parent
commit
ff3f6c10a7

+ 154 - 0
application/api/controller/tvuser/Friend.php

@@ -0,0 +1,154 @@
+<?php
+
+namespace app\api\controller\tvuser;
+
+use app\common\controller\Api;
+use think\Db;
+/**
+ * 好友
+ */
+class Friend extends Api
+{
+    protected $noNeedLogin = [];
+    protected $noNeedRight = ['*'];
+
+    //好友列表
+    public function friend_lists(){
+
+        $ids = Db::name('friend')->where('user_id',$this->auth->id)->column('to_user_id');
+        $ids2 = Db::name('friend')->where('to_user_id',$this->auth->id)->column('user_id');
+
+        $ids3 = array_merge($ids,$ids2);
+        $ids4 = array_unique($ids3);
+        /*dump($ids);
+        dump($ids2);
+        dump($ids3);
+        dump($ids4);*/
+
+        $list = [];
+        if(!empty($ids4)){
+            $order = 'field(id,'.implode(',',$ids4).')';
+            $list = Db::name('user')->field('id,nickname,avatar,tv_mobile')->where('id','IN',$ids4)->orderRaw($order)->autopage()->select();
+            $list = list_domain_image($list,['avatar']);
+        }
+
+        $this->success(1,$list);
+    }
+
+    //移除好友
+    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.tv_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,tv_mobile,avatar')->where('comefrom',2)->where('tv_mobile',$keyword)->where('id','neq',$this->auth->id)->select();
+        $list = list_domain_image($list,['avatar']);
+
+        if(!empty($list)){
+            foreach($list as $key => $val){
+                //是否申请过了
+                $val['is_apply'] = 0;
+                $check_apply = Db::name('friend_apply')->where('user_id',$this->auth->id)->where('to_user_id',$val['id'])->find();
+                if($check_apply){
+                    $val['is_apply'] = 1;
+                }
+
+                //是否是好友
+                $val['is_friend'] = 0;
+                $check_friend1 = Db::name('friend')->where('user_id',$this->auth->id)->where('to_user_id',$val['id'])->find();
+                $check_friend2 = Db::name('friend')->where('user_id',$val['id'])->where('to_user_id',$this->auth->id)->find();
+                if($check_friend1 || $check_friend2){
+                    $val['is_friend'] = 1;
+                }
+
+                //
+                $list[$key] = $val;
+            }
+        }
+
+        $this->success(1,$list);
+    }
+
+    //申请好友
+    public function apply(){
+        $user_id = input('user_id',0);
+        if(!$user_id){$this->error();}
+
+        if($user_id == $this->auth->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();
+    }
+
+}

+ 23 - 0
application/api/controller/tvuser/User.php

@@ -0,0 +1,23 @@
+<?php
+
+namespace app\api\controller\tvuser;
+
+use app\common\controller\Api;
+use think\Db;
+/**
+ * 会员接口
+ */
+class User extends Api
+{
+    protected $noNeedLogin = [];
+    protected $noNeedRight = '*';
+
+
+    //用户详细资料
+    public function userinfo(){
+        $info = $this->auth->getUserinfo_tv();
+
+        $this->success(__('success'),$info);
+    }
+
+}