MemberCollectService.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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:members:collect:users_".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:member:collect:u{$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. $sourceType = $liveId? 2 : 1;
  201. $sourceId = $liveId? $liveId : $videoId;
  202. RedisService::clear("caches:member:collect:u{$userId}_c{$collectUid}_{$type}");
  203. RedisService::clear("caches:member:fans:{$userId}_{$sourceId}_{$sourceType}");
  204. $this->model->where(['user_id'=> $collectUid,'collect_uid'=> $userId])->update(['is_return'=> $data['is_return'],'update_time'=>time()]);
  205. return ['is_fans'=>$isFansId? 1:0];
  206. }
  207. /**
  208. * 获取播放/观看直播的粉丝数
  209. * @param $userId 用户
  210. * @param $sourceId 视频/直播ID
  211. * @param int $sourceType 来源:1-视频,2-直播
  212. * @return array|mixed
  213. */
  214. public function getViewFansCountByType($userId, $sourceId, $sourceType=1)
  215. {
  216. $cacheKey = "caches:member:fans:{$userId}_{$sourceId}_{$sourceType}";
  217. $count = RedisService::get($cacheKey);
  218. if($count || RedisService::exists($cacheKey)){
  219. return $count;
  220. }
  221. $where = ['a.collect_uid'=> $userId,'a.status'=>1,'a.mark'=>1];
  222. $count = $this->model->from('member_collect as a')
  223. ->leftJoin('video_collect as b',function($join) use($sourceType){
  224. $join->on('b.user_id','=','a.user_id')
  225. ->where(['b.source_type'=>$sourceType,'b.type'=>1,'b.status'=>1,'b.mark'=>1]);
  226. })
  227. ->where($where)
  228. ->where('b.id','>', 0)
  229. ->count('a.id');
  230. if($count){
  231. RedisService::set($cacheKey, $count, rand(5, 10));
  232. }
  233. return $count;
  234. }
  235. /**
  236. * 获取新增的粉丝数
  237. * @param $userId 用户
  238. * @param $sourceId 视频/直播ID
  239. * @param int $sourceType 来源:1-视频,2-直播
  240. * @return array|mixed
  241. */
  242. public function getNewFansCount($userId, $sourceId, $sourceType=1, $time=0)
  243. {
  244. $cacheKey = "caches:member:newFans:{$userId}_{$sourceId}_{$sourceType}_{$time}";
  245. $count = RedisService::get($cacheKey);
  246. if($count || RedisService::exists($cacheKey)){
  247. return $count;
  248. }
  249. $where = ['a.collect_uid'=> $userId,'a.status'=>1,'a.mark'=>1];
  250. if($sourceType == 1){
  251. // 视频
  252. $where['video_id'] = $sourceId;
  253. }else if($sourceType == 2){
  254. // 直播
  255. $where['live_id'] = $sourceId;
  256. }
  257. $count = $this->model->from('member_collect as a')
  258. ->where($where)
  259. ->where('a.create_time','>', $time)
  260. ->count('a.id');
  261. if($count){
  262. RedisService::set($cacheKey, $count, rand(3, 5));
  263. }
  264. return $count;
  265. }
  266. /**
  267. * 获取(被)收藏/关注/点赞数量
  268. * @param $userId 用户ID
  269. * @param int $type 类型:1-关注,2-收藏,3-点赞喜欢
  270. * @param int $ctype 查询类型:1-被关注/收藏/点赞,2-关注/收藏/点赞
  271. * @return array|mixed
  272. */
  273. public function getCount($userId, $type=1, $ctype=1)
  274. {
  275. $cacheKey = "caches:member:collect:{$userId}_{$type}_{$ctype}";
  276. $data = RedisService::get($cacheKey);
  277. if($data){
  278. return $data;
  279. }
  280. $field = 'collect_uid';
  281. if($ctype == 2){
  282. $field = 'user_id';
  283. }
  284. $data = $this->model->where([$field=> $userId,'type'=>$type,'status'=>1,'mark'=>1])->count('id');
  285. if($data){
  286. RedisService::set($cacheKey, $data, 3600);
  287. }
  288. return $data;
  289. }
  290. /**
  291. * 取消
  292. * @param $userId
  293. * @return bool
  294. */
  295. public function cancel($userId)
  296. {
  297. // 参数
  298. $ids = request()->post('ids','');
  299. $ids = $ids? explode(',', $ids) : [];
  300. $type = request()->post('type',0);
  301. if (empty($ids) && $type == 0) {
  302. $this->error = 1033;
  303. return false;
  304. }
  305. if($type){
  306. $this->model->where(['user_id'=> $userId,'status'=>1,'mark'=>1])->update(['status'=>2,'update_time'=>time()]);
  307. }else {
  308. $this->model->where(['user_id'=> $userId,'status'=>1,'mark'=>1])->whereIn('id', $ids)->update(['status'=>2,'update_time'=>time()]);
  309. }
  310. $this->error = 1002;
  311. return true;
  312. }
  313. }