123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- <?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();
- }
- }
|