MemberCollectService.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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,'a.user_id'=> $userId,'b.mark'=>1];
  56. $field = $field? $field : 'lev_a.id,lev_a.user_id,lev_a.type,lev_a.collect_uid,lev_a.create_time,lev_b.nickname,lev_b.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->with(['mechanic'])
  63. ->from('member_collect as a')
  64. ->leftJoin('member as b', 'b.id', '=', 'a.collect_uid')
  65. ->where($where)
  66. ->where(function ($query) use ($params) {
  67. $userId = isset($params['user_id']) ? $params['user_id'] : 0;
  68. if ($userId > 0) {
  69. $query->where('a.user_id', $userId);
  70. }
  71. $status = isset($params['status'])? $params['status'] : 0;
  72. $status = $status>0? $status : 1;
  73. if ($status > 0) {
  74. $query->where('a.status', $status);
  75. }
  76. $type = isset($params['type'])? $params['type'] : 0;
  77. $type = $type>0? $type : 1;
  78. if ($type > 0) {
  79. $query->where('a.type', $type);
  80. }
  81. })
  82. ->where(function ($query) use ($params) {
  83. $keyword = isset($params['kw']) ? $params['kw'] : '';
  84. if ($keyword) {
  85. $query->where('b.nickname', 'like', "%{$keyword}%")->orWhere('b.username','like',"%{$keyword}%");
  86. }
  87. })
  88. ->selectRaw($field)
  89. ->orderByRaw($order)
  90. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  91. $list = $list ? $list->toArray() : [];
  92. if ($list) {
  93. foreach ($list['data'] as &$item) {
  94. $item['time_text'] = isset($item['create_time']) && $item['create_time']? dateFormat($item['create_time']) : '';
  95. $item['avatar'] = isset($item['avatar']) && $item['avatar'] ? get_image_url($item['avatar']) : '';
  96. $mechaic = isset($item['mechanic'])? $item['mechanic'] : [];
  97. if($mechaic){
  98. $mechaic['avatar'] = isset($mechaic['avatar'])? get_image_url($mechaic['avatar']) : '';
  99. $mechaic['nickname'] = isset($mechaic['nickname']) && $mechaic['nickname']? $mechaic['nickname']: $mechaic['realname'];
  100. }
  101. $item['checked'] = false;
  102. $item['mechanic'] = $mechaic;
  103. }
  104. }
  105. return [
  106. 'pageSize' => $pageSize,
  107. 'total' => isset($list['total']) ? $list['total'] : 0,
  108. 'list' => isset($list['data']) ? $list['data'] : []
  109. ];
  110. }
  111. /**
  112. * 是否已经收藏或点赞过
  113. * @param $userId
  114. * @param $collectUid
  115. * @param int $type 类型:
  116. * @return array|mixed
  117. */
  118. public function checkCollect($userId, $collectUid, $type=1)
  119. {
  120. $cacheKey = "caches:member:collect:u{$userId}_c{$collectUid}_{$type}";
  121. $data = RedisService::get($cacheKey);
  122. if($data){
  123. return $data? 1: 2;
  124. }
  125. $data = $this->model->where(['user_id'=> $userId,'collect_uid'=> $collectUid,'type'=> $type,'status'=>1,'mark'=>1])->value('id');
  126. if($data){
  127. RedisService::set($cacheKey, $data, rand(5, 10));
  128. }
  129. return $data? 1 : 2;
  130. }
  131. /**
  132. * 收藏点赞
  133. * @param $userId
  134. * @param $dynamicId
  135. * @param $type
  136. * @param int $status
  137. * @return mixed
  138. */
  139. public function collect($userId, $params)
  140. {
  141. $collectUid = isset($params['id'])? intval($params['id']) : 0;
  142. $type = isset($params['type'])? intval($params['type']) : 2;
  143. $status = isset($params['status'])? intval($params['status']) : 1;
  144. if($collectUid<=0 || !in_array($type, [1,2]) || !in_array($status, [1,2])){
  145. $this->error = 2501;
  146. return false;
  147. }
  148. $id = $this->model->where(['user_id'=> $userId,'collect_uid'=> $collectUid,'type'=> $type])->value('id');
  149. $data = [
  150. 'user_id'=> $userId,
  151. 'type'=> $type,
  152. 'collect_uid'=> $collectUid,
  153. 'update_time'=> time(),
  154. 'status'=> $status,
  155. 'mark'=> 1,
  156. ];
  157. if(!$id){
  158. $data['create_time'] = time();
  159. $this->error = '1002';
  160. return $this->model->insertGetId($data);
  161. }else{
  162. if($status == 2){
  163. RedisService::clear("caches:member:collect:u{$userId}_c{$collectUid}_{$type}");
  164. }
  165. $this->error = '1002';
  166. return $this->model->where('id', $id)->update($data);
  167. }
  168. }
  169. /**
  170. * 获取(被)收藏/关注/点赞数量
  171. * @param $userId 用户ID
  172. * @param int $type 类型:1-关注,2-收藏,3-点赞喜欢
  173. * @param int $ctype 查询类型:1-被关注/收藏/点赞,2-关注/收藏/点赞
  174. * @return array|mixed
  175. */
  176. public function getCount($userId, $type=1, $ctype=1)
  177. {
  178. $cacheKey = "caches:member:collect:{$userId}_{$type}_{$ctype}";
  179. $data = RedisService::get($cacheKey);
  180. if($data){
  181. return $data;
  182. }
  183. $field = 'collect_uid';
  184. if($ctype == 2){
  185. $field = 'user_id';
  186. }
  187. $data = $this->model->where([$field=> $userId,'type'=>$type,'status'=>1,'mark'=>1])->count('id');
  188. if($data){
  189. RedisService::set($cacheKey, $data, 3600);
  190. }
  191. return $data;
  192. }
  193. /**
  194. * 取消
  195. * @param $userId
  196. * @return bool
  197. */
  198. public function cancel($userId)
  199. {
  200. // 参数
  201. $ids = request()->post('ids','');
  202. $ids = $ids? explode(',', $ids) : [];
  203. $type = request()->post('type',0);
  204. if (empty($ids) && $type == 0) {
  205. $this->error = 1033;
  206. return false;
  207. }
  208. if($type){
  209. $this->model->where(['user_id'=> $userId,'status'=>1,'mark'=>1])->update(['status'=>2,'update_time'=>time()]);
  210. }else {
  211. $this->model->where(['user_id'=> $userId,'status'=>1,'mark'=>1])->whereIn('id', $ids)->update(['status'=>2,'update_time'=>time()]);
  212. }
  213. $this->error = 1002;
  214. return true;
  215. }
  216. }