// +---------------------------------------------------------------------- namespace App\Services\Api; use App\Models\LiveModel; use App\Models\MemberCollectModel; use App\Models\MemberModel; use App\Models\VideoModel; use App\Services\BaseService; use App\Services\RedisService; use BN\Red; /** * 用户收藏点赞管理-服务类 * @author laravel开发员 * @since 2020/11/11 * Class MemberCollectService * @package App\Services\Api */ class MemberCollectService extends BaseService { // 静态对象 protected static $instance = null; /** * 构造函数 * @author laravel开发员 * @since 2020/11/11 * MemberCollectService constructor. */ public function __construct() { $this->model = new MemberCollectModel(); } /** * 静态入口 * @return static|null */ public static function make() { if (!self::$instance) { self::$instance = (new static()); } return self::$instance; } /** * 列表数据 * @param $params * @param int $pageSize * @return array */ public function getDataList($userId, $params, $pageSize = 15, $field='') { $where = ['a.mark' => 1,'a.status'=>1]; $field = $field? $field : 'lev_a.id,lev_a.nickname,lev_a.avatar,lev_a.member_level,lev_a.status'; $sortType = isset($params['sort_type']) ? $params['sort_type'] : 1; $order = 'id desc'; if($sortType == 1){ $order = 'lev_a.create_time desc, lev_a.id desc'; } $list = MemberModel::with(['level'])->from('member as a') ->where($where) ->where(function ($query) use ($params, $userId) { $followType = isset($params['follow_type'])? $params['follow_type'] : 0; if (in_array($followType,[1,2,3])) { // 互关的人 if($followType == 1){ $uids = $this->getCollectUsers(['user_id'=> $userId,'is_return'=>1,'status'=>1,'mark'=>1],'collect_uid'); $uids = $uids? $uids : [0]; $query->whereIn('a.id', $uids); } // 关注我的人 else if($followType == 2){ $uids = $this->getCollectUsers(['collect_uid'=> $userId,'status'=>1,'mark'=>1],'user_id'); $uids = $uids? $uids : [0]; $query->whereIn('a.id', $uids); } // 我关注的人 else if($followType == 3){ $uids = $this->getCollectUsers(['user_id'=> $userId,'status'=>1,'mark'=>1],'collect_uid'); $uids = $uids? $uids : [0]; $query->whereIn('a.id', $uids); } }else{ // 我关注的和关注我的人 $uids = $this->getCollectUsers(['user_id'=> $userId,'is_return'=>1,'status'=>1,'mark'=>1],'collect_uid'); $uids = $uids? $uids : [0]; $uids1 = $this->getCollectUsers(['collect_uid'=> $userId,'status'=>1,'mark'=>1],'user_id'); $uids1 = $uids1? $uids1 : [0]; $query->whereIn('a.id', $uids)->orWhereIn('a.id', $uids1); } }) ->where(function ($query) use ($params) { $keyword = isset($params['kw']) ? $params['kw'] : ''; if ($keyword) { $query->where('a.nickname', 'like', "%{$keyword}%")->orWhere('a.id','like',"%{$keyword}%"); } }) ->selectRaw($field) ->orderByRaw($order) ->paginate($pageSize > 0 ? $pageSize : 9999999); $list = $list ? $list->toArray() : []; if ($list) { $showType = isset($params['show_type'])? $params['show_type'] : 0; foreach ($list['data'] as &$item) { $item['avatar'] = isset($item['avatar']) && $item['avatar'] ? get_image_url($item['avatar']) : get_image_url('/images/member/logo.png'); $item['checked'] = false; $item['level'] = $item['level']? $item['level'] : ['id'=>0,'name'=>'普通用户']; if($showType == 1) { $item['is_follow'] = (int)MemberCollectService::make()->checkCollect($userId, $item['id'], 1); // 是否关注了 $item['is_fans'] = (int)MemberCollectService::make()->checkCollect($item['id'], $userId, 1); // 是否我的粉丝 $item['live_id'] = (int)LiveModel::where(['user_id' => $item['id'], 'status' => 1, 'mark' => 1])->orderBy('create_time', 'desc')->value('id'); // 是否正在直播 $item['video_count'] = (int)VideoModel::where(['user_id' => $item['id'], 'status' => 2, 'mark' => 1])->count('id'); // 视频数量 } } } return [ 'pageSize' => $pageSize, 'total' => isset($list['total']) ? $list['total'] : 0, 'list' => isset($list['data']) ? $list['data'] : [] ]; } /** * 获取关注表用户ID * @param $where * @param string $field 返回字段:关注的人或被关注的人 * @return array|mixed */ public function getCollectUsers($where, $field='user_id') { $cacheKey = "caches:m_collect:u_".md5(json_encode($where).$field); $datas = RedisService::get($cacheKey); if($datas || RedisService::exists($cacheKey)){ return $datas; } $uids = MemberCollectModel::where($where)->pluck($field); $uids = $uids? $uids->toArray() : []; if($uids){ RedisService::set($cacheKey, $uids, rand(5, 10)); } return $uids; } /** * 是否已经收藏或点赞过 * @param $userId * @param $collectUid * @param int $type 类型: * @return array|mixed */ public function checkCollect($userId, $collectUid, $type=1) { $cacheKey = "caches:m_collect:{$userId}_c{$collectUid}_{$type}"; $data = RedisService::get($cacheKey); if($data){ return $data? 1: 2; } $data = $this->model->where(['user_id'=> $userId,'collect_uid'=> $collectUid,'type'=> $type,'status'=>1,'mark'=>1])->value('id'); if($data){ RedisService::set($cacheKey, $data, rand(5, 10)); } return $data? 1 : 2; } /** * 收藏点赞 * @param $userId * @param $dynamicId * @param $type * @param int $status * @return mixed */ public function follow($userId, $params) { $collectUid = isset($params['id'])? intval($params['id']) : 0; $type = isset($params['type'])? intval($params['type']) : 1; $status = isset($params['status'])? intval($params['status']) : 1; if($collectUid<=0 || !in_array($type, [1,2]) || !in_array($status, [1,2])){ $this->error = 2501; return false; } $id = $this->model->where(['user_id'=> $userId,'collect_uid'=> $collectUid,'type'=> $type])->value('id'); $isFansId = $this->model->where(['user_id'=> $collectUid,'collect_uid'=> $userId,'type'=> $type,'status'=>1,'mark'=>1])->value('id'); $liveId = isset($params['live_id'])? intval($params['live_id']) : 0; $videoId = isset($params['video_id'])? intval($params['video_id']) : 0; $data = [ 'user_id'=> $userId, 'type'=> $type, 'live_id'=> $liveId, 'video_id'=> $videoId, 'collect_uid'=> $collectUid, 'update_time'=> time(), 'is_return'=> $isFansId? ($status==1? 1 : 0) : 0, // 互关 'status'=> $status, 'mark'=> 1, ]; if(!$id){ $data['create_time'] = time(); if(!$this->model->insertGetId($data)){ return false; } }else{ if(!$this->model->where('id', $id)->update($data)){ return false; } } $this->error = 1002; RedisService::keyDel("caches:m_fans:*"); RedisService::keyDel("caches:m_collect:*"); $this->model->where(['user_id'=> $collectUid,'collect_uid'=> $userId])->update(['is_return'=> $data['is_return'],'update_time'=>time()]); if($status == 1){ $url = '/pages/user/home?id='.$userId; $userNickname = MemberModel::where(['id'=> $userId])->value('nickname'); MessageService::make()->pushMessage($collectUid, '有人对你感兴趣',"{$userNickname}关注了你", 5, $userId, $url); // 关注用户任务处理 TaskService::make()->updateTask($userId,3, $collectUid); } return ['is_fans'=>$isFansId? 1:0]; } /** * 获取播放/观看直播的粉丝数 * @param $userId 用户 * @param $sourceId 视频/直播ID * @param int $sourceType 来源:1-视频,2-直播 * @return array|mixed */ public function getViewFansCountByType($userId, $sourceId, $sourceType=1) { $cacheKey = "caches:m_fans:{$userId}_{$sourceId}_{$sourceType}"; $count = RedisService::get($cacheKey); if($count || RedisService::exists($cacheKey)){ return $count; } $where = ['a.collect_uid'=> $userId,'a.status'=>1,'a.mark'=>1]; $count = $this->model->from('member_collect as a') ->leftJoin('video_collect as b',function($join) use($sourceType){ $join->on('b.user_id','=','a.user_id') ->where(['b.source_type'=>$sourceType,'b.type'=>1,'b.status'=>1,'b.mark'=>1]); }) ->where($where) ->where('b.id','>', 0) ->count('a.id'); if($count){ RedisService::set($cacheKey, $count, rand(5, 10)); } return $count; } /** * 获取新增的粉丝数 * @param $userId 用户 * @param $sourceId 视频/直播ID * @param int $sourceType 来源:1-视频,2-直播 * @return array|mixed */ public function getNewFansCount($userId, $sourceId, $sourceType=1, $time=0) { $cacheKey = "caches:m_newFans:{$userId}_{$sourceId}_{$sourceType}_{$time}"; $count = RedisService::get($cacheKey); if($count || RedisService::exists($cacheKey)){ return $count; } $where = ['a.collect_uid'=> $userId,'a.status'=>1,'a.mark'=>1]; if($sourceType == 1){ // 视频 $where['video_id'] = $sourceId; }else if($sourceType == 2){ // 直播 $where['live_id'] = $sourceId; } $count = $this->model->from('member_collect as a') ->where($where) ->where('a.create_time','>', $time) ->count('a.id'); if($count){ RedisService::set($cacheKey, $count, rand(3, 5)); } return $count; } /** * 获取(被)收藏/关注/点赞数量 * @param $userId 用户ID * @param int $type 类型:1-关注 * @param int $ctype 查询类型:1-被关注/收藏/点赞,2-关注/收藏/点赞 * @return array|mixed */ public function getCount($userId, $type=1, $ctype=1) { $cacheKey = "caches:m_collect:{$userId}_{$type}_{$ctype}"; $data = RedisService::get($cacheKey); if($data){ return $data; } $field = 'collect_uid'; if($ctype == 2){ $field = 'user_id'; } $data = $this->model->where([$field=> $userId,'type'=>$type,'status'=>1,'mark'=>1])->count('id'); if($data){ RedisService::set($cacheKey, $data, rand(300,600)); } return $data; } /** * 取消 * @param $userId * @return bool */ public function cancel($userId) { // 参数 $ids = request()->post('ids',''); $ids = $ids? explode(',', $ids) : []; $type = request()->post('type',0); if (empty($ids) && $type == 0) { $this->error = 1033; return false; } if($type){ $this->model->where(['user_id'=> $userId,'status'=>1,'mark'=>1])->update(['status'=>2,'update_time'=>time()]); }else { $this->model->where(['user_id'=> $userId,'status'=>1,'mark'=>1])->whereIn('id', $ids)->update(['status'=>2,'update_time'=>time()]); } $this->error = 1002; return true; } }