MemberCollectService.php 13 KB

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