MemberCollectService.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. use BN\Red;
  16. /**
  17. * 用户收藏点赞管理-服务类
  18. * @author laravel开发员
  19. * @since 2020/11/11
  20. * Class MemberCollectService
  21. * @package App\Services\Api
  22. */
  23. class MemberCollectService extends BaseService
  24. {
  25. // 静态对象
  26. protected static $instance = null;
  27. /**
  28. * 构造函数
  29. * @author laravel开发员
  30. * @since 2020/11/11
  31. * MemberCollectService constructor.
  32. */
  33. public function __construct()
  34. {
  35. $this->model = new MemberCollectModel();
  36. }
  37. /**
  38. * 静态入口
  39. * @return static|null
  40. */
  41. public static function make()
  42. {
  43. if (!self::$instance) {
  44. self::$instance = (new static());
  45. }
  46. return self::$instance;
  47. }
  48. /**
  49. * 列表数据
  50. * @param $params
  51. * @param int $pageSize
  52. * @return array
  53. */
  54. public function getDataList($userId, $params, $pageSize = 15, $field='')
  55. {
  56. $where = ['a.mark' => 1,'a.status'=>1];
  57. $field = $field? $field : 'lev_a.id,lev_a.nickname,lev_a.avatar,lev_a.member_level,lev_a.status';
  58. $sortType = isset($params['sort_type']) ? $params['sort_type'] : 1;
  59. $order = 'id desc';
  60. if($sortType == 1){
  61. $order = 'lev_a.create_time desc, lev_a.id desc';
  62. }
  63. $list = $this->model->from('member as a')
  64. ->where($where)
  65. ->where(function ($query) use ($params, $userId) {
  66. $followType = isset($params['follow_type'])? $params['follow_type'] : 0;
  67. if (in_array($followType,[1,2,3])) {
  68. // 互关的人
  69. if($followType == 1){
  70. $uids = $this->getCollectUsers(['user_id'=> $userId,'is_return'=>1,'status'=>1,'mark'=>1],'collect_uid');
  71. $uids = $uids? $uids : [0];
  72. $query->whereIn('a.id', $uids);
  73. }
  74. // 关注我的人
  75. else if($followType == 2){
  76. $uids = $this->getCollectUsers(['collect_uid'=> $userId,'status'=>1,'mark'=>1],'user_id');
  77. $uids = $uids? $uids : [0];
  78. $query->whereIn('a.id', $uids);
  79. }
  80. // 我关注的人
  81. else if($followType == 3){
  82. $uids = $this->getCollectUsers(['user_id'=> $userId,'status'=>1,'mark'=>1],'collect_uid');
  83. $uids = $uids? $uids : [0];
  84. $query->whereIn('a.id', $uids);
  85. }
  86. }else{
  87. // 我关注的和关注我的人
  88. $uids = $this->getCollectUsers(['user_id'=> $userId,'is_return'=>1,'status'=>1,'mark'=>1],'collect_uid');
  89. $uids = $uids? $uids : [0];
  90. $uids1 = $this->getCollectUsers(['collect_uid'=> $userId,'status'=>1,'mark'=>1],'user_id');
  91. $uids1 = $uids1? $uids1 : [0];
  92. $query->whereIn('a.id', $uids)->orWhereIn('a.id', $uids1);
  93. }
  94. })
  95. ->where(function ($query) use ($params) {
  96. $keyword = isset($params['kw']) ? $params['kw'] : '';
  97. if ($keyword) {
  98. $query->where('a.nickname', 'like', "%{$keyword}%")->orWhere('a.id','like',"%{$keyword}%");
  99. }
  100. })
  101. ->selectRaw($field)
  102. ->orderByRaw($order)
  103. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  104. $list = $list ? $list->toArray() : [];
  105. if ($list) {
  106. foreach ($list['data'] as &$item) {
  107. $item['avatar'] = isset($item['avatar']) && $item['avatar'] ? get_image_url($item['avatar']) : get_image_url('/images/member/logo.png');
  108. $item['checked'] = false;
  109. }
  110. }
  111. return [
  112. 'pageSize' => $pageSize,
  113. 'total' => isset($list['total']) ? $list['total'] : 0,
  114. 'list' => isset($list['data']) ? $list['data'] : []
  115. ];
  116. }
  117. /**
  118. * 获取关注表用户ID
  119. * @param $where
  120. * @param string $field 返回字段:关注的人或被关注的人
  121. * @return array|mixed
  122. */
  123. public function getCollectUsers($where, $field='user_id')
  124. {
  125. $cacheKey = "caches:m_collect:u_".md5(json_encode($where).$field);
  126. $datas = RedisService::get($cacheKey);
  127. if($datas || RedisService::exists($cacheKey)){
  128. return $datas;
  129. }
  130. $uids = MemberCollectModel::where($where)->pluck($field);
  131. $uids = $uids? $uids->toArray() : [];
  132. if($uids){
  133. RedisService::set($cacheKey, $uids, rand(5, 10));
  134. }
  135. return $uids;
  136. }
  137. /**
  138. * 是否已经收藏或点赞过
  139. * @param $userId
  140. * @param $collectUid
  141. * @param int $type 类型:
  142. * @return array|mixed
  143. */
  144. public function checkCollect($userId, $collectUid, $type=1)
  145. {
  146. $cacheKey = "caches:m_collect:{$userId}_c{$collectUid}_{$type}";
  147. $data = RedisService::get($cacheKey);
  148. if($data){
  149. return $data? 1: 2;
  150. }
  151. $data = $this->model->where(['user_id'=> $userId,'collect_uid'=> $collectUid,'type'=> $type,'status'=>1,'mark'=>1])->value('id');
  152. if($data){
  153. RedisService::set($cacheKey, $data, rand(5, 10));
  154. }
  155. return $data? 1 : 2;
  156. }
  157. /**
  158. * 收藏点赞
  159. * @param $userId
  160. * @param $dynamicId
  161. * @param $type
  162. * @param int $status
  163. * @return mixed
  164. */
  165. public function follow($userId, $params)
  166. {
  167. $collectUid = isset($params['id'])? intval($params['id']) : 0;
  168. $type = isset($params['type'])? intval($params['type']) : 1;
  169. $status = isset($params['status'])? intval($params['status']) : 1;
  170. if($collectUid<=0 || !in_array($type, [1,2]) || !in_array($status, [1,2])){
  171. $this->error = 2501;
  172. return false;
  173. }
  174. $id = $this->model->where(['user_id'=> $userId,'collect_uid'=> $collectUid,'type'=> $type])->value('id');
  175. $isFansId = $this->model->where(['user_id'=> $collectUid,'collect_uid'=> $userId,'type'=> $type,'status'=>1,'mark'=>1])->value('id');
  176. $liveId = isset($params['live_id'])? intval($params['live_id']) : 0;
  177. $videoId = isset($params['video_id'])? intval($params['video_id']) : 0;
  178. $data = [
  179. 'user_id'=> $userId,
  180. 'type'=> $type,
  181. 'live_id'=> $liveId,
  182. 'video_id'=> $videoId,
  183. 'collect_uid'=> $collectUid,
  184. 'update_time'=> time(),
  185. 'is_return'=> $isFansId? ($status==1? 1 : 0) : 0, // 互关
  186. 'status'=> $status,
  187. 'mark'=> 1,
  188. ];
  189. if(!$id){
  190. $data['create_time'] = time();
  191. if(!$this->model->insertGetId($data)){
  192. return false;
  193. }
  194. }else{
  195. if(!$this->model->where('id', $id)->update($data)){
  196. return false;
  197. }
  198. }
  199. $this->error = 1002;
  200. RedisService::keyDel("caches:m_fans:*");
  201. RedisService::keyDel("caches:m_collect:*");
  202. $this->model->where(['user_id'=> $collectUid,'collect_uid'=> $userId])->update(['is_return'=> $data['is_return'],'update_time'=>time()]);
  203. if($status == 1){
  204. MessageService::make()->pushMessage($collectUid, '有人对你感兴趣','有人关注了你', 5, $userId);
  205. // 关注用户任务处理
  206. TaskService::make()->updateTask($userId,3, $collectUid);
  207. }
  208. return ['is_fans'=>$isFansId? 1:0];
  209. }
  210. /**
  211. * 获取播放/观看直播的粉丝数
  212. * @param $userId 用户
  213. * @param $sourceId 视频/直播ID
  214. * @param int $sourceType 来源:1-视频,2-直播
  215. * @return array|mixed
  216. */
  217. public function getViewFansCountByType($userId, $sourceId, $sourceType=1)
  218. {
  219. $cacheKey = "caches:m_fans:{$userId}_{$sourceId}_{$sourceType}";
  220. $count = RedisService::get($cacheKey);
  221. if($count || RedisService::exists($cacheKey)){
  222. return $count;
  223. }
  224. $where = ['a.collect_uid'=> $userId,'a.status'=>1,'a.mark'=>1];
  225. $count = $this->model->from('member_collect as a')
  226. ->leftJoin('video_collect as b',function($join) use($sourceType){
  227. $join->on('b.user_id','=','a.user_id')
  228. ->where(['b.source_type'=>$sourceType,'b.type'=>1,'b.status'=>1,'b.mark'=>1]);
  229. })
  230. ->where($where)
  231. ->where('b.id','>', 0)
  232. ->count('a.id');
  233. if($count){
  234. RedisService::set($cacheKey, $count, rand(5, 10));
  235. }
  236. return $count;
  237. }
  238. /**
  239. * 获取新增的粉丝数
  240. * @param $userId 用户
  241. * @param $sourceId 视频/直播ID
  242. * @param int $sourceType 来源:1-视频,2-直播
  243. * @return array|mixed
  244. */
  245. public function getNewFansCount($userId, $sourceId, $sourceType=1, $time=0)
  246. {
  247. $cacheKey = "caches:m_newFans:{$userId}_{$sourceId}_{$sourceType}_{$time}";
  248. $count = RedisService::get($cacheKey);
  249. if($count || RedisService::exists($cacheKey)){
  250. return $count;
  251. }
  252. $where = ['a.collect_uid'=> $userId,'a.status'=>1,'a.mark'=>1];
  253. if($sourceType == 1){
  254. // 视频
  255. $where['video_id'] = $sourceId;
  256. }else if($sourceType == 2){
  257. // 直播
  258. $where['live_id'] = $sourceId;
  259. }
  260. $count = $this->model->from('member_collect as a')
  261. ->where($where)
  262. ->where('a.create_time','>', $time)
  263. ->count('a.id');
  264. if($count){
  265. RedisService::set($cacheKey, $count, rand(3, 5));
  266. }
  267. return $count;
  268. }
  269. /**
  270. * 获取(被)收藏/关注/点赞数量
  271. * @param $userId 用户ID
  272. * @param int $type 类型:1-关注
  273. * @param int $ctype 查询类型:1-被关注/收藏/点赞,2-关注/收藏/点赞
  274. * @return array|mixed
  275. */
  276. public function getCount($userId, $type=1, $ctype=1)
  277. {
  278. $cacheKey = "caches:m_collect:{$userId}_{$type}_{$ctype}";
  279. $data = RedisService::get($cacheKey);
  280. if($data){
  281. return $data;
  282. }
  283. $field = 'collect_uid';
  284. if($ctype == 2){
  285. $field = 'user_id';
  286. }
  287. $data = $this->model->where([$field=> $userId,'type'=>$type,'status'=>1,'mark'=>1])->count('id');
  288. if($data){
  289. RedisService::set($cacheKey, $data, rand(300,600));
  290. }
  291. return $data;
  292. }
  293. /**
  294. * 取消
  295. * @param $userId
  296. * @return bool
  297. */
  298. public function cancel($userId)
  299. {
  300. // 参数
  301. $ids = request()->post('ids','');
  302. $ids = $ids? explode(',', $ids) : [];
  303. $type = request()->post('type',0);
  304. if (empty($ids) && $type == 0) {
  305. $this->error = 1033;
  306. return false;
  307. }
  308. if($type){
  309. $this->model->where(['user_id'=> $userId,'status'=>1,'mark'=>1])->update(['status'=>2,'update_time'=>time()]);
  310. }else {
  311. $this->model->where(['user_id'=> $userId,'status'=>1,'mark'=>1])->whereIn('id', $ids)->update(['status'=>2,'update_time'=>time()]);
  312. }
  313. $this->error = 1002;
  314. return true;
  315. }
  316. }