Relation.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. /**
  6. * 关系
  7. */
  8. class Relation extends Api
  9. {
  10. protected $noNeedLogin = ['*'];
  11. protected $noNeedRight = ['*'];
  12. //关系列表
  13. public function lists(){
  14. $uid = input('uid',$this->auth->id);
  15. //所有
  16. $list = Db::name('relation')->where('is_show',1)->order('weigh desc')->select();
  17. //已有关系
  18. $user_relation = Db::name('user_relation')->alias('ur')
  19. ->field('ur.*,u.avatar,u.nickname,to.avatar as to_avatar,to.nickname as to_nickname')
  20. ->join('user u','ur.uid = u.id','LEFT')
  21. ->join('user to','ur.to_uid = to.id','LEFT')
  22. ->where('ur.uid|ur.to_uid',$uid)->where('ur.status',1)->select();
  23. $user_relation = list_domain_image($user_relation,['avatar','to_avatar']);
  24. $new_user_relation = array_column($user_relation,null,'relation_id');
  25. //处理,留对方的头像和昵称
  26. foreach($list as $key => &$val){
  27. $val['info'] = [];
  28. if(isset($new_user_relation[$val['id']])){
  29. $info = $new_user_relation[$val['id']];
  30. if($info['uid'] == $uid){
  31. $info['avatar'] = $info['to_avatar'];
  32. $info['nickname'] = $info['to_nickname'];
  33. unset($info['to_avatar']);
  34. unset($info['to_nickname']);
  35. }else{
  36. unset($info['to_avatar']);
  37. unset($info['to_nickname']);
  38. }
  39. $val['info'] = $info;
  40. }
  41. }
  42. $this->success('success',$list);
  43. }
  44. //新增
  45. public function addone(){
  46. $to_uid = input('to_uid',0);
  47. if($to_uid == $this->auth->id){
  48. $this->error('不能跟自己建立关系');
  49. }
  50. //关系检测
  51. $relation_id = input('relation_id','');
  52. $relation = Db::name('relation')->where('id',$relation_id)->find();
  53. if(empty($relation)){
  54. $this->error('不存在的关系');
  55. }
  56. //检查已有关系
  57. $where = '(uid = '.$this->auth->id.' and to_uid = '.$to_uid.') or (uid = '.$to_uid.' and to_uid = '.$this->auth->id.')';
  58. $check = Db::name('user_relation')->where($where)->find();
  59. if($check){
  60. $now_relation = Db::name('relation')->where('id',$check['relation_id'])->value('name');
  61. if($check['status'] == 1){
  62. if($check['uid'] = $this->auth->id){
  63. $this->error('您已经与该用户建立'.$now_relation.'关系');
  64. }else{
  65. $this->error('该用户已经与您建立'.$now_relation.'关系');
  66. }
  67. }elseif($check['status'] == 0){
  68. if($check['uid'] = $this->auth->id){
  69. $this->error('您已经申请建立'.$now_relation.'关系,待对方同意');
  70. }else{
  71. $this->error('对方已经申请建立'.$now_relation.'关系,待您同意');
  72. }
  73. }elseif($check['status'] == 2){
  74. $this->error($now_relation.'关系的申请已被拒绝,七日内不能申请');
  75. }else{
  76. //可以申请
  77. }
  78. }else{
  79. //可以申请
  80. }
  81. //扣钱或道具
  82. //新增
  83. $data = [
  84. 'uid' => $this->auth->id,
  85. 'relation_id' => $relation_id,
  86. 'to_uid' => $to_uid,
  87. 'status' => 0,
  88. 'createtime' => time(),
  89. 'updatetime' => time(),
  90. ];
  91. $id = Db::name('user_relation')->insertGetId($data);
  92. //给对方一条消息
  93. $message = [
  94. 'user_id' => $to_uid,
  95. 'title' => '关系审核',
  96. 'content' => '有人想和你成为'.$relation['name'],
  97. 'createtime' => time(),
  98. 'status' => 0,
  99. 'infotype' => 'newrelation',//建立关系
  100. 'infotype_id' => 0,
  101. ];
  102. Db::name('message')->insertGetId($message);
  103. $this->success('申请成功,等待对方同意',$id);
  104. }
  105. //待审核关系
  106. public function audit_lists(){
  107. $list = Db::name('user_relation')->alias('ur')
  108. ->field('ur.*,relation.name as relation_name,user.nickname,user.avatar,user.gender,user.birthday,user.attribute,uw.vip_endtime')
  109. ->join('relation','ur.relation_id = relation.id','LEFT')
  110. ->join('user','ur.uid = user.id','LEFT')
  111. ->join('user_wallet uw','ur.uid = uw.user_id','LEFT')
  112. ->where('ur.to_uid',$this->auth->id)
  113. ->where('ur.status',0)
  114. ->order('ur.id desc')->autopage()->select();
  115. $list = list_domain_image($list,['avatar']);
  116. if(!empty($list)){
  117. foreach($list as $key => &$val){
  118. //用户年龄
  119. $val['age'] = birthtime_to_age($val['birthday']);
  120. unset($val['birthday']);
  121. //用户vip
  122. $val['is_vip'] = $val['vip_endtime'] > time() ? 1 : 0;
  123. unset($val['vip_endtime']);
  124. }
  125. }
  126. $this->success('success',$list);
  127. }
  128. //审核
  129. public function audit(){
  130. $id = input('id',0);
  131. $status = input('status',1);
  132. //查询
  133. $info = Db::name('user_relation')->alias('ur')
  134. ->where('ur.id',$id)
  135. ->where('ur.to_uid',$this->auth->id)
  136. ->where('ur.status',0)
  137. ->find();
  138. if(!$info){
  139. $this->error('请刷新重试');
  140. }
  141. //更新
  142. $data = [
  143. 'status' => $status,
  144. 'updatetime' => time(),
  145. ];
  146. Db::name('user_relation')->where('id',$id)->update($data);
  147. if($status == 1){
  148. $remark = '您已同意建立关系';
  149. $remark2 = '已同意';
  150. }else{
  151. $remark = '已拒绝';
  152. $remark2 = '已拒绝';
  153. }
  154. //给发起方一条消息
  155. $relation_name = Db::name('relation')->where('id',$info['relation_id'])->value('name');
  156. $message = [
  157. 'user_id' => $info['uid'],
  158. 'title' => '关系申请结果',
  159. 'content' => '和'.$this->auth->nickname.'成为'.$relation_name.':'.$remark2,
  160. 'createtime' => time(),
  161. 'status' => 0,
  162. 'infotype' => '',//建立关系
  163. 'infotype_id' => 0,
  164. ];
  165. Db::name('message')->insertGetId($message);
  166. //
  167. $this->success($remark);
  168. }
  169. }