Like.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. namespace app\api\controller;
  3. use think\Db;
  4. /**
  5. * 喜欢接口
  6. */
  7. class Like extends Common
  8. {
  9. protected $noNeedLogin = [];
  10. protected $noNeedRight = '*';
  11. public function _initialize()
  12. {
  13. parent::_initialize();
  14. }
  15. /**
  16. * 添加喜欢列表
  17. * @date 2020-10-27
  18. * @createby 虎嗅网络科技
  19. */
  20. public function addLikeList() {
  21. $user_id = $this->request->request('user_id',0,"intval"); // 喜欢的用户ID
  22. $fans_id = $this->auth->id; // 添加到我喜欢,我就是粉丝
  23. if (!$user_id || !$fans_id) {
  24. $this->error(__('Invalid parameters'));
  25. }
  26. if($user_id == $fans_id) {
  27. $this->error(__('自己对自己 不能心动哦?'));
  28. }
  29. $userLikeModel = new \app\common\model\UserLike();
  30. $where = [];
  31. $where["user_id"] = $user_id;
  32. $where["fans_id"] = $fans_id;
  33. $userlikeInfo = $userLikeModel->where($where)->find();
  34. $data = [];
  35. if($userlikeInfo) {
  36. $data = $userlikeInfo;
  37. $res = true;
  38. } else {
  39. // 获取是否是有眼缘
  40. // $fate = \app\common\model\UserFate::where(['user_id'=>$fans_id,'fate_user_id'=>$user_id])->find();
  41. $data["user_id"] = $user_id;
  42. $data["fans_id"] = $fans_id;
  43. // $data["is_fate"] = $fate?1:0;
  44. $data["createtime"] = time();
  45. $res = $userLikeModel->insert($data);
  46. }
  47. if ($res) {
  48. // +message
  49. $user_info = \app\common\model\User::where(['id'=>$user_id])->find();
  50. $title = '喜欢通知!';
  51. $content = $user_info->nickname.': 我关注了你哦,找我聊聊吧~';
  52. \app\common\model\SysMsg::sendSysMsg($user_id,6,$title,$content);
  53. $this->success(__('添加成功!'), $data);
  54. } else {
  55. $this->success(__('添加失败!'));
  56. }
  57. }
  58. /**
  59. * 获取喜欢我的
  60. * @date 2020-10-27
  61. * @createby 虎嗅网络科技
  62. */
  63. public function getFansList() {
  64. $page = $this->request->request('page',1); // 分页
  65. $pageNum = $this->request->request('pageNum',10); // 分页
  66. // 分页搜索构建
  67. $pageStart = ($page-1)*$pageNum;
  68. $userLikeModel = new \app\common\model\UserLike();
  69. $fansList = $userLikeModel->getFansList($this->auth->id,$pageStart,$pageNum);
  70. if ($fansList) {
  71. $fate = \app\common\model\UserFate::where(['user_id'=>$this->auth->id])->select();
  72. $fate_user_id = array_column($fate,'fate_user_id');
  73. foreach($fansList as $k => $v) {
  74. if(in_array($v['user_id'],$fate_user_id)) {
  75. $fansList[$k]['is_fate'] = 1;
  76. } else {
  77. $fansList[$k]['is_fate'] = 0;
  78. }
  79. }
  80. $this->success(__('获取成功!'), $fansList);
  81. } else {
  82. $this->success(__('数据为空!'),[]);
  83. }
  84. }
  85. /**
  86. * 获取我喜欢的
  87. * @date 2020-10-27
  88. * @createby 虎嗅网络科技
  89. */
  90. public function getLikeList() {
  91. $page = $this->request->request('page',1); // 分页
  92. $pageNum = $this->request->request('pageNum',10); // 分页
  93. // 分页搜索构建
  94. $pageStart = ($page-1)*$pageNum;
  95. $userLikeModel = new \app\common\model\UserLike();
  96. $idolList = $userLikeModel->getUserList($this->auth->id,$pageStart,$pageNum);
  97. if ($idolList) {
  98. $fate = \app\common\model\UserFate::where(['user_id'=>$this->auth->id])->select();
  99. $fate_user_id = array_column($fate,'fate_user_id');
  100. foreach($idolList as $k => $v) {
  101. if(in_array($v['user_id'],$fate_user_id)) {
  102. $idolList[$k]['is_fate'] = 1;
  103. } else {
  104. $idolList[$k]['is_fate'] = 0;
  105. }
  106. }
  107. $this->success(__('获取成功!'), $idolList);
  108. } else {
  109. $this->success(__('数据为空!'),[]);
  110. }
  111. }
  112. /**
  113. * 删除我喜欢
  114. */
  115. public function delLike() {
  116. $id = $this->request->request('id',1); // 喜欢ID
  117. if($id <= 0) $this->error('参数错误!');
  118. $userLikeModel = new \app\common\model\UserLike();
  119. $where = [];
  120. $where['id'] = $id;
  121. $where['fans_id'] = $this->auth->id;
  122. $userLikeInfo = $userLikeModel->where($where)->find();
  123. if(!$userLikeInfo) $this->error('数据未找到,或无权限!');
  124. $res = $userLikeInfo->delete();
  125. if ($res) {
  126. $this->success(__('删除成功!'));
  127. } else {
  128. $this->success(__('删除失败!'));
  129. }
  130. }
  131. /**
  132. * 删除我喜欢
  133. */
  134. public function delLikeByUserId() {
  135. $user_id = $this->request->request('user_id',1); // 被喜欢用户ID
  136. if($user_id <= 0) $this->error('参数错误!');
  137. $userLikeModel = new \app\common\model\UserLike();
  138. $where = [];
  139. $where['user_id'] = $user_id;
  140. $where['fans_id'] = $this->auth->id;
  141. $userLikeInfo = $userLikeModel->where($where)->find();
  142. if(!$userLikeInfo) $this->error('数据未找到,或无权限!');
  143. $res = $userLikeInfo->delete();
  144. if ($res) {
  145. $this->success(__('删除成功!'));
  146. } else {
  147. $this->success(__('删除失败!'));
  148. }
  149. }
  150. }