MemberCollectService.php 13 KB

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