MemberCollectService.php 13 KB

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