123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244 |
- <?php
- namespace app\api\controller;
- use app\common\controller\Api;
- use think\Db;
- /**
- * 关系
- */
- class Relation extends Api
- {
- protected $noNeedLogin = ['*'];
- protected $noNeedRight = ['*'];
- //关系列表
- public function lists(){
- $uid = input('uid',$this->auth->id);
- //所有
- $list = Db::name('relation')->where('is_show',1)->order('weigh desc')->select();
- //已有关系
- $user_relation = Db::name('user_relation')->alias('ur')
- ->field('ur.*,u.avatar,u.nickname,to.avatar as to_avatar,to.nickname as to_nickname')
- ->join('user u','ur.uid = u.id','LEFT')
- ->join('user to','ur.to_uid = to.id','LEFT')
- ->where('ur.uid|ur.to_uid',$uid)->where('ur.status',1)->select();
- $user_relation = list_domain_image($user_relation,['avatar','to_avatar']);
- $new_user_relation = array_column($user_relation,null,'relation_id');
- //处理,留对方的头像和昵称
- foreach($list as $key => &$val){
- $val['info'] = [];
- if(isset($new_user_relation[$val['id']])){
- $info = $new_user_relation[$val['id']];
- if($info['uid'] == $uid){
- $info['avatar'] = $info['to_avatar'];
- $info['nickname'] = $info['to_nickname'];
- unset($info['to_avatar']);
- unset($info['to_nickname']);
- }else{
- unset($info['to_avatar']);
- unset($info['to_nickname']);
- }
- //是否能取消
- $info['can_cancel'] = 0;
- if($info['uid'] == $this->auth->id || $info['to_uid'] == $this->auth->id){
- $info['can_cancel'] = 1;
- }
- $val['info'] = $info;
- }
- }
- $this->success('success',$list);
- }
- //新增
- public function addone(){
- $to_uid = input('to_uid',0);
- if($to_uid == $this->auth->id){
- $this->error('不能跟自己建立关系');
- }
- //关系检测
- $relation_id = input('relation_id','');
- $relation = Db::name('relation')->where('id',$relation_id)->find();
- if(empty($relation)){
- $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();
- if($check){
- $now_relation = Db::name('relation')->where('id',$check['relation_id'])->value('name');
- if($check['status'] == 1){
- 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){
- $this->error('您已经申请建立'.$now_relation.'关系,待对方同意');
- }else{
- $this->error('对方已经申请建立'.$now_relation.'关系,待您同意');
- }
- }elseif($check['status'] == 2){
- $this->error($now_relation.'关系的申请已被拒绝,七日内不能申请');
- }else{
- //可以申请
- }
- }else{
- //可以申请
- }
- //扣钱或道具
- //新增
- $data = [
- 'uid' => $this->auth->id,
- 'relation_id' => $relation_id,
- 'to_uid' => $to_uid,
- 'status' => 0,
- 'createtime' => time(),
- 'updatetime' => time(),
- ];
- $id = Db::name('user_relation')->insertGetId($data);
- //给对方一条消息
- $message = [
- 'user_id' => $to_uid,
- 'title' => '关系审核',
- 'content' => '有人想和你成为'.$relation['name'],
- 'createtime' => time(),
- 'status' => 0,
- 'infotype' => 'newrelation',//建立关系
- 'infotype_id' => 0,
- ];
- Db::name('message')->insertGetId($message);
- $this->success('申请成功,等待对方同意',$id);
- }
- //待审核关系
- public function audit_lists(){
- $list = Db::name('user_relation')->alias('ur')
- ->field('ur.*,relation.name as relation_name,user.nickname,user.avatar,user.gender,user.birthday,user.attribute,uw.vip_endtime')
- ->join('relation','ur.relation_id = relation.id','LEFT')
- ->join('user','ur.uid = user.id','LEFT')
- ->join('user_wallet uw','ur.uid = uw.user_id','LEFT')
- ->where('ur.to_uid',$this->auth->id)
- ->where('ur.status',0)
- ->order('ur.id desc')->autopage()->select();
- $list = list_domain_image($list,['avatar']);
- if(!empty($list)){
- foreach($list as $key => &$val){
- //用户年龄
- $val['age'] = birthtime_to_age($val['birthday']);
- unset($val['birthday']);
- //用户vip
- $val['is_vip'] = $val['vip_endtime'] > time() ? 1 : 0;
- unset($val['vip_endtime']);
- }
- }
- $this->success('success',$list);
- }
- //审核
- public function audit(){
- $id = input('id',0);
- $status = input('status',1);
- //查询
- $info = Db::name('user_relation')->alias('ur')
- ->where('ur.id',$id)
- ->where('ur.to_uid',$this->auth->id)
- ->where('ur.status',0)
- ->find();
- if(!$info){
- $this->error('请刷新重试');
- }
- //更新
- $data = [
- 'status' => $status,
- 'updatetime' => time(),
- ];
- Db::name('user_relation')->where('id',$id)->update($data);
- if($status == 1){
- $remark = '您已同意建立关系';
- $remark2 = '已同意';
- }else{
- $remark = '已拒绝';
- $remark2 = '已拒绝';
- }
- //给发起方一条消息
- $relation_name = Db::name('relation')->where('id',$info['relation_id'])->value('name');
- $message = [
- 'user_id' => $info['uid'],
- 'title' => '关系申请结果',
- 'content' => '和'.$this->auth->nickname.'成为'.$relation_name.':'.$remark2,
- 'createtime' => time(),
- 'status' => 0,
- 'infotype' => '',//建立关系
- 'infotype_id' => 0,
- ];
- Db::name('message')->insertGetId($message);
- //
- $this->success($remark);
- }
- //取消
- public function cancel(){
- $id = input('id',0);
- $relation_id = input('relation_id',0);
- $where = [
- 'id' => $id,
- 'relation_id' => $relation_id
- ];
- $where2 = 'uid = '.$this->auth->id.' or to_uid = '.$this->auth->id;
- $info = Db::name('user_relation')->where($where)->where($where2)->find();
- if(empty($info)){
- $this->error('不存在的关系');
- }
- Db::name('user_relation')->where($where)->delete();
- //给发起方一条消息
- $relation_name = Db::name('relation')->where('id',$relation_id)->value('name');
- $message = [
- 'user_id' => ($info['uid'] == $this->auth->id) ? $info['to_uid'] : $info['uid'],
- 'title' => '关系取消',
- 'content' => $this->auth->nickname.'已取消和你的'.$relation_name.'关系',
- 'createtime' => time(),
- 'status' => 0,
- 'infotype' => '',//建立关系
- 'infotype_id' => 0,
- ];
- Db::name('message')->insertGetId($message);
- //
- $this->success('关系已取消');
- }
- }
|