MemberCollectService.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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.nickname,lev_a.avatar,lev_a.member_level,lev_a.statuss';
  57. $sortType = isset($params['sort_type']) ? $params['sort_type'] : 1;
  58. $order = 'id desc';
  59. if($sortType == 1){
  60. $order = 'lev_a.create_time desc, lev_a.id desc';
  61. }
  62. $list = $this->model->from('member as a')
  63. ->where($where)
  64. ->where(function ($query) use ($params, $userId) {
  65. $followType = isset($params['follow_type'])? $params['follow_type'] : 0;
  66. if (in_array($followType,[1,2])) {
  67. // 我关注的人
  68. if($followType == 1){
  69. $uids = MemberCollectModel::where(['user_id'=> $userId,'status'=>1,'mark'=>1])->pluck('collect_uid');
  70. $uids = $uids? $uids : [0];
  71. $query->whereIn('a.id', $uids);
  72. }
  73. // 关注我的人
  74. else if($followType == 2){
  75. $uids = MemberCollectModel::where(['collect_uid'=> $userId,'status'=>1,'mark'=>1])->pluck('user_id');
  76. $uids = $uids? $uids : [0];
  77. $query->whereIn('a.id', $uids);
  78. }
  79. }else{
  80. // 我关注的和关注我的人
  81. $uids = MemberCollectModel::where(['status'=>1,'mark'=>1])
  82. ->where(function($query) use($userId){
  83. $query->where('user_id', $userId)->orWhere('collect_uid', $userId);
  84. })->pluck('user_id');
  85. $uids = $uids? $uids : [0];
  86. $query->whereIn('a.id', $uids);
  87. }
  88. })
  89. ->where(function ($query) use ($params) {
  90. $keyword = isset($params['kw']) ? $params['kw'] : '';
  91. if ($keyword) {
  92. $query->where('a.nickname', 'like', "%{$keyword}%")->orWhere('a.id','like',"%{$keyword}%");
  93. }
  94. })
  95. ->selectRaw($field)
  96. ->orderByRaw($order)
  97. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  98. $list = $list ? $list->toArray() : [];
  99. if ($list) {
  100. foreach ($list['data'] as &$item) {
  101. $item['avatar'] = isset($item['avatar']) && $item['avatar'] ? get_image_url($item['avatar']) : get_image_url('/images/member/logo.png');
  102. $item['checked'] = false;
  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 follow($userId, $params)
  140. {
  141. $collectUid = isset($params['id'])? intval($params['id']) : 0;
  142. $type = isset($params['type'])? intval($params['type']) : 1;
  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. if(!$this->model->insertGetId($data)){
  160. return false;
  161. }
  162. }else{
  163. if(!$this->model->where('id', $id)->update($data)){
  164. return false;
  165. }
  166. }
  167. $this->error = 1002;
  168. RedisService::clear("caches:member:collect:u{$userId}_c{$collectUid}_{$type}");
  169. $isFans = $this->model->where(['user_id'=> $collectUid,'collect_uid'=> $userId,'type'=> $type])->value('id');
  170. return ['is_fans'=>$isFans? 1:0];
  171. }
  172. /**
  173. * 获取(被)收藏/关注/点赞数量
  174. * @param $userId 用户ID
  175. * @param int $type 类型:1-关注,2-收藏,3-点赞喜欢
  176. * @param int $ctype 查询类型:1-被关注/收藏/点赞,2-关注/收藏/点赞
  177. * @return array|mixed
  178. */
  179. public function getCount($userId, $type=1, $ctype=1)
  180. {
  181. $cacheKey = "caches:member:collect:{$userId}_{$type}_{$ctype}";
  182. $data = RedisService::get($cacheKey);
  183. if($data){
  184. return $data;
  185. }
  186. $field = 'collect_uid';
  187. if($ctype == 2){
  188. $field = 'user_id';
  189. }
  190. $data = $this->model->where([$field=> $userId,'type'=>$type,'status'=>1,'mark'=>1])->count('id');
  191. if($data){
  192. RedisService::set($cacheKey, $data, 3600);
  193. }
  194. return $data;
  195. }
  196. /**
  197. * 取消
  198. * @param $userId
  199. * @return bool
  200. */
  201. public function cancel($userId)
  202. {
  203. // 参数
  204. $ids = request()->post('ids','');
  205. $ids = $ids? explode(',', $ids) : [];
  206. $type = request()->post('type',0);
  207. if (empty($ids) && $type == 0) {
  208. $this->error = 1033;
  209. return false;
  210. }
  211. if($type){
  212. $this->model->where(['user_id'=> $userId,'status'=>1,'mark'=>1])->update(['status'=>2,'update_time'=>time()]);
  213. }else {
  214. $this->model->where(['user_id'=> $userId,'status'=>1,'mark'=>1])->whereIn('id', $ids)->update(['status'=>2,'update_time'=>time()]);
  215. }
  216. $this->error = 1002;
  217. return true;
  218. }
  219. }