MemberCollectService.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services\Api;
  12. use App\Models\MemberCollectModel;
  13. use App\Services\BaseService;
  14. use App\Services\RedisService;
  15. /**
  16. * 用户收藏点赞管理-服务类
  17. * @author laravel开发员
  18. * @since 2020/11/11
  19. * Class MemberCollectService
  20. * @package App\Services\Api
  21. */
  22. class MemberCollectService extends BaseService
  23. {
  24. // 静态对象
  25. protected static $instance = null;
  26. /**
  27. * 构造函数
  28. * @author laravel开发员
  29. * @since 2020/11/11
  30. * MemberCollectService constructor.
  31. */
  32. public function __construct()
  33. {
  34. $this->model = new MemberCollectModel();
  35. }
  36. /**
  37. * 静态入口
  38. * @return static|null
  39. */
  40. public static function make()
  41. {
  42. if (!self::$instance) {
  43. self::$instance = (new static());
  44. }
  45. return self::$instance;
  46. }
  47. /**
  48. * 列表数据
  49. * @param $params
  50. * @param int $pageSize
  51. * @return array
  52. */
  53. public function getDataList($userId, $params, $pageSize = 15, $field='')
  54. {
  55. $where = ['a.mark' => 1,'a.status'=>1];
  56. $field = $field? $field : 'lev_a.id,lev_a.user_id,lev_a.type,lev_a.collect_uid,lev_b.nickname,lev_b.avatar,lev_c.nickname as c_nickname,lev_c.avatar as c_avatar';
  57. $sortType = isset($params['sort_type']) ? $params['sort_type'] : 1;
  58. $order = 'id desc';
  59. if($sortType == 1){
  60. $order = 'create_time desc, lev_a.id desc';
  61. }
  62. $list = $this->model->from('member_collect as a')
  63. ->leftJoin('member as b', function($join){
  64. $join->on('b.id', '=', 'a.user_id')
  65. ->where('b.status', 1)
  66. ->where('b.mark', 1);
  67. })
  68. ->leftJoin('member as c', function($join){
  69. $join->on('c.id', '=', 'a.collect_uid')
  70. ->where('c.status', 1)
  71. ->where('c.mark', 1);
  72. })
  73. ->where($where)
  74. ->where(function ($query) use ($params, $userId) {
  75. $status = isset($params['status'])? $params['status'] : 0;
  76. $status = $status>0? $status : 1;
  77. if ($status > 0) {
  78. $query->where('a.status', $status);
  79. }
  80. $followType = isset($params['follow_type'])? $params['follow_type'] : 0;
  81. if (in_array($followType,[1,2])) {
  82. // 我关注的人
  83. if($followType == 1){
  84. $query->where('a.user_id', $userId);
  85. }
  86. // 关注我的人
  87. else if($followType == 2){
  88. $query->where('a.collect_uid', $userId);
  89. }
  90. }else{
  91. // 我关注的和关注我的人
  92. $query->where(function($query) use($userId){
  93. $query->where('a.user_id',$userId)
  94. ->orWhere('a.collect_uid', $userId);
  95. });
  96. }
  97. })
  98. ->where(function ($query) use ($params) {
  99. $keyword = isset($params['kw']) ? $params['kw'] : '';
  100. $followType = isset($params['follow_type'])? $params['follow_type'] : 0;
  101. if ($keyword) {
  102. if($followType == 1){
  103. $query->where('b.nickname', 'like', "%{$keyword}%")->orWhere('b.id','like',"%{$keyword}%");
  104. }else if($followType == 2){
  105. $query->where('c.nickname', 'like', "%{$keyword}%")->orWhere('c.id','like',"%{$keyword}%");
  106. }else{
  107. $query->where('b.nickname', 'like', "%{$keyword}%")->orWhere('b.id','like',"%{$keyword}%")
  108. ->where('c.nickname', 'like', "%{$keyword}%")->orWhere('c.id','like',"%{$keyword}%");
  109. }
  110. }
  111. })
  112. ->selectRaw($field)
  113. ->orderByRaw($order)
  114. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  115. $list = $list ? $list->toArray() : [];
  116. if ($list) {
  117. foreach ($list['data'] as &$item) {
  118. $item['avatar'] = isset($item['avatar']) && $item['avatar'] ? get_image_url($item['avatar']) : '';
  119. $item['c_avatar'] = isset($item['c_avatar']) && $item['c_avatar'] ? get_image_url($item['c_avatar']) : '';
  120. // 我关注的用户
  121. if($userId == $item['user_id']){
  122. $item['nickname'] = $item['c_nickname'];
  123. $item['avatar'] = $item['c_avatar']? $item['c_avatar'] : get_image_url('/images/member/logo.png');
  124. }
  125. $item['checked'] = false;
  126. }
  127. }
  128. return [
  129. 'pageSize' => $pageSize,
  130. 'total' => isset($list['total']) ? $list['total'] : 0,
  131. 'list' => isset($list['data']) ? $list['data'] : []
  132. ];
  133. }
  134. /**
  135. * 是否已经收藏或点赞过
  136. * @param $userId
  137. * @param $collectUid
  138. * @param int $type 类型:
  139. * @return array|mixed
  140. */
  141. public function checkCollect($userId, $collectUid, $type=1)
  142. {
  143. $cacheKey = "caches:member:collect:u{$userId}_c{$collectUid}_{$type}";
  144. $data = RedisService::get($cacheKey);
  145. if($data){
  146. return $data? 1: 2;
  147. }
  148. $data = $this->model->where(['user_id'=> $userId,'collect_uid'=> $collectUid,'type'=> $type,'status'=>1,'mark'=>1])->value('id');
  149. if($data){
  150. RedisService::set($cacheKey, $data, rand(5, 10));
  151. }
  152. return $data? 1 : 2;
  153. }
  154. /**
  155. * 收藏点赞
  156. * @param $userId
  157. * @param $dynamicId
  158. * @param $type
  159. * @param int $status
  160. * @return mixed
  161. */
  162. public function follow($userId, $params)
  163. {
  164. $collectUid = isset($params['id'])? intval($params['id']) : 0;
  165. $type = isset($params['type'])? intval($params['type']) : 1;
  166. $status = isset($params['status'])? intval($params['status']) : 1;
  167. if($collectUid<=0 || !in_array($type, [1,2]) || !in_array($status, [1,2])){
  168. $this->error = 2501;
  169. return false;
  170. }
  171. $id = $this->model->where(['user_id'=> $userId,'collect_uid'=> $collectUid,'type'=> $type])->value('id');
  172. $data = [
  173. 'user_id'=> $userId,
  174. 'type'=> $type,
  175. 'collect_uid'=> $collectUid,
  176. 'update_time'=> time(),
  177. 'status'=> $status,
  178. 'mark'=> 1,
  179. ];
  180. if(!$id){
  181. $data['create_time'] = time();
  182. if(!$this->model->insertGetId($data)){
  183. return false;
  184. }
  185. }else{
  186. if(!$this->model->where('id', $id)->update($data)){
  187. return false;
  188. }
  189. }
  190. $this->error = 1002;
  191. RedisService::clear("caches:member:collect:u{$userId}_c{$collectUid}_{$type}");
  192. $isFans = $this->model->where(['user_id'=> $collectUid,'collect_uid'=> $userId,'type'=> $type])->value('id');
  193. return ['is_fans'=>$isFans? 1:0];
  194. }
  195. /**
  196. * 获取(被)收藏/关注/点赞数量
  197. * @param $userId 用户ID
  198. * @param int $type 类型:1-关注,2-收藏,3-点赞喜欢
  199. * @param int $ctype 查询类型:1-被关注/收藏/点赞,2-关注/收藏/点赞
  200. * @return array|mixed
  201. */
  202. public function getCount($userId, $type=1, $ctype=1)
  203. {
  204. $cacheKey = "caches:member:collect:{$userId}_{$type}_{$ctype}";
  205. $data = RedisService::get($cacheKey);
  206. if($data){
  207. return $data;
  208. }
  209. $field = 'collect_uid';
  210. if($ctype == 2){
  211. $field = 'user_id';
  212. }
  213. $data = $this->model->where([$field=> $userId,'type'=>$type,'status'=>1,'mark'=>1])->count('id');
  214. if($data){
  215. RedisService::set($cacheKey, $data, 3600);
  216. }
  217. return $data;
  218. }
  219. /**
  220. * 取消
  221. * @param $userId
  222. * @return bool
  223. */
  224. public function cancel($userId)
  225. {
  226. // 参数
  227. $ids = request()->post('ids','');
  228. $ids = $ids? explode(',', $ids) : [];
  229. $type = request()->post('type',0);
  230. if (empty($ids) && $type == 0) {
  231. $this->error = 1033;
  232. return false;
  233. }
  234. if($type){
  235. $this->model->where(['user_id'=> $userId,'status'=>1,'mark'=>1])->update(['status'=>2,'update_time'=>time()]);
  236. }else {
  237. $this->model->where(['user_id'=> $userId,'status'=>1,'mark'=>1])->whereIn('id', $ids)->update(['status'=>2,'update_time'=>time()]);
  238. }
  239. $this->error = 1002;
  240. return true;
  241. }
  242. }