Userfollow.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. /**
  6. * 关注
  7. */
  8. class Userfollow extends Api
  9. {
  10. protected $noNeedLogin = [];
  11. protected $noNeedRight = ['*'];
  12. //我的关注列表/好友列表
  13. public function my_follow_list(){
  14. $type = input('type', 0, 'intval'); //类型:0关注 1好友
  15. $where = [];
  16. if ($type == 1) {
  17. $where['follow.status'] = 1;
  18. }
  19. $list = Db::name('user_follow')
  20. ->alias('follow')
  21. ->join('user','follow.follow_uid = user.id','LEFT')
  22. ->join('user_remark ur', 'ur.to_user_id = user.id and ur.user_id = '.$this->auth->id, 'LEFT')
  23. ->join('user_wallet uw','follow.follow_uid = uw.id','LEFT')
  24. ->field('user.id,user.nickname,user.avatar,user.real_status,user.idcard_status,user.birthday,user.gender,follow.status,uw.vip_endtime,uw.vip_level,ur.nickname_remark')
  25. ->where('follow.uid',$this->auth->id)->where($where)->order('follow.id desc')->autopage()->select();
  26. $list = list_domain_image($list,['avatar']);
  27. $list = list_birthday_age($list);
  28. if ($list) {
  29. /*$mt_user_wallet = Db::name('user_wallet'); //钱包
  30. $mt_wealth_level = Db::name('wealth_level'); //财富等级
  31. $mt_charm_level = Db::name('charm_level'); //魅力等级*/
  32. foreach ($list as &$val) {
  33. //查询财富等级和魅力等级
  34. /*$wallet_info = $mt_user_wallet->where(['user_id' => $val['id']])->find();
  35. $wealth_level = $mt_wealth_level->where(['value' => ['elt', $wallet_info['pay_money']]])->order('id desc')->find();
  36. if ($wealth_level) {
  37. $val['wealth_level'] = $wealth_level['name'];
  38. } else {
  39. $val['wealth_level'] = '';
  40. }
  41. $charm_level = $mt_charm_level->where(['value' => ['elt', $wallet_info['get_money']]])->order('id desc')->find();
  42. if ($charm_level) {
  43. $val['charm_level'] = $charm_level['name'];
  44. } else {
  45. $val['charm_level'] = '';
  46. }*/
  47. $val['is_vip'] = $this->is_vip($val['vip_endtime'],$val['vip_level']);
  48. $val['nickname'] = !empty($val['nickname_remark']) ? $val['nickname_remark'] : $val['nickname'];
  49. }
  50. }
  51. $this->success('success',$list);
  52. }
  53. //我的粉丝列表
  54. public function my_fans_list(){
  55. $list = Db::name('user_follow')
  56. ->alias('follow')
  57. ->join('user','follow.uid = user.id','LEFT')
  58. ->join('user_wallet uw','follow.follow_uid = uw.id','LEFT')
  59. ->join('user_remark ur', 'ur.to_user_id = user.id and ur.user_id = '.$this->auth->id, 'LEFT')
  60. ->field('user.id,user.nickname,user.avatar,user.real_status,user.idcard_status,user.birthday,user.gender,follow.status,uw.vip_endtime,uw.vip_level,ur.nickname_remark')
  61. ->where('follow.follow_uid',$this->auth->id)->order('follow.id desc')->autopage()->select();
  62. $list = list_domain_image($list,['avatar']);
  63. $list = list_birthday_age($list);
  64. if ($list) {
  65. /*$mt_user_wallet = Db::name('user_wallet'); //钱包
  66. $mt_wealth_level = Db::name('wealth_level'); //财富等级
  67. $mt_charm_level = Db::name('charm_level'); //魅力等级*/
  68. foreach ($list as &$val) {
  69. //查询财富等级和魅力等级
  70. /*$wallet_info = $mt_user_wallet->where(['user_id' => $val['id']])->find();
  71. $wealth_level = $mt_wealth_level->where(['value' => ['elt', $wallet_info['pay_money']]])->order('id desc')->find();
  72. if ($wealth_level) {
  73. $val['wealth_level'] = $wealth_level['name'];
  74. } else {
  75. $val['wealth_level'] = '';
  76. }
  77. $charm_level = $mt_charm_level->where(['value' => ['elt', $wallet_info['get_money']]])->order('id desc')->find();
  78. if ($charm_level) {
  79. $val['charm_level'] = $charm_level['name'];
  80. } else {
  81. $val['charm_level'] = '';
  82. }*/
  83. $val['is_vip'] = $this->is_vip($val['vip_endtime'],$val['vip_level']);
  84. $val['nickname'] = !empty($val['nickname_remark']) ? $val['nickname_remark'] : $val['nickname'];
  85. }
  86. }
  87. $this->success('success',$list);
  88. }
  89. //关注某人
  90. public function follow_one(){
  91. // 接口防并发
  92. if (!$this->apiLimit(1, 1)) {
  93. $this->error(__('Operation frequently'));
  94. }
  95. $follow_uid = input('follow_uid',0);
  96. // $gift_id = input('gift_id');// 礼物ID
  97. // $number = input('number',1,'intval');//数量
  98. if(!$follow_uid){
  99. $this->error(__('Invalid parameters'));
  100. }
  101. $checkuser = Db::name('user')->find($follow_uid);
  102. if(empty($checkuser)){
  103. $this->error('此用户不存在');
  104. }
  105. if($follow_uid == $this->auth->id){
  106. $this->error('不能关注自己');
  107. }
  108. $map = [
  109. 'uid' => $this->auth->id,
  110. 'follow_uid' => $follow_uid,
  111. ];
  112. $check = Db::name('user_follow')->where($map)->find();
  113. if($check){
  114. $this->error('已经关注此人');
  115. }
  116. //查询对方是否关注自己
  117. $where = [
  118. 'uid' => $follow_uid,
  119. 'follow_uid' => $this->auth->id
  120. ];
  121. $count = Db::name('user_follow')->where($where)->count('id');
  122. if ($count) {
  123. $map['status'] = 1; //状态:0=关注,1=互为关注
  124. }
  125. $return_data['money'] = ''; //获得金额
  126. $return_data['level_remark'] = ''; //亲密度等级提示语
  127. //开启事务
  128. Db::startTrans();
  129. $id = Db::name('user_follow')->insertGetId($map);
  130. if(!$id){
  131. Db::rollback();
  132. $this->error('您的网络开小差啦~');
  133. }
  134. if ($count) {
  135. //若对方已关注, 则将状态改为 互为关注
  136. $rs = Db::name('user_follow')->where($where)->where(['status' => 0])->setField('status', 1);
  137. if (!$rs) {
  138. Db::rollback();
  139. $this->error('您的网络开小差啦~');
  140. }
  141. }
  142. //系统消息
  143. $msg_id = \app\common\model\Message::addMessage($this->auth->id,'喜欢','您已喜欢用户' . $checkuser['nickname']);
  144. Db::commit();
  145. $this->success('操作成功', $return_data);
  146. }
  147. //取关某人
  148. public function un_follow_one(){
  149. $follow_uid = input('follow_uid',0);
  150. if(!$follow_uid){
  151. $this->error(__('Invalid parameters'));
  152. }
  153. $checkuser = Db::name('user')->find($follow_uid);
  154. if(empty($checkuser)){
  155. $this->error('此用户不存在');
  156. }
  157. $map = [
  158. 'uid' => $this->auth->id,
  159. 'follow_uid' => $follow_uid,
  160. ];
  161. $info = Db::name('user_follow')->where($map)->find();
  162. if (!$info) {
  163. $this->error('您尚未关注此人~');
  164. }
  165. //开启事务
  166. Db::startTrans();
  167. $rs = Db::name('user_follow')->where($map)->delete();
  168. if (!$rs) {
  169. Db::rollback();
  170. $this->error('您的网络开小差啦~');
  171. }
  172. $where = [
  173. 'uid' => $follow_uid,
  174. 'follow_uid' => $this->auth->id,
  175. ];
  176. $count = Db::name('user_follow')->where($where)->count('id');
  177. if ($count) {
  178. $rt = Db::name('user_follow')->where($where)->where(['status' => 1])->setField('status', 0);
  179. if (!$rt) {
  180. Db::rollback();
  181. $this->error('您的网络开小差啦~');
  182. }
  183. }
  184. Db::commit();
  185. $this->success('操作成功');
  186. }
  187. }