| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369 |
- <?php
- // +----------------------------------------------------------------------
- // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
- // +----------------------------------------------------------------------
- // | 版权所有 2017~2021 LARAVEL研发中心
- // +----------------------------------------------------------------------
- // | 官方网站: http://www.laravel.cn
- // +----------------------------------------------------------------------
- // | Author: laravel开发员 <laravel.qq.com>
- // +----------------------------------------------------------------------
- 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)
- ->whereNotIn('a.id', [$userId])
- ->where(function ($query) use ($params, $userId) {
- $followType = isset($params['follow_type'])? $params['follow_type'] : 0;
- $uid = isset($params['uid']) && $params['uid']? $params['uid'] : $userId;
- if (in_array($followType,[1,2,3])) {
- // 互关的人
- if($followType == 1){
- $uids = $this->getCollectUsers(['user_id'=> $uid,'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'=> $uid,'status'=>1,'mark'=>1],'user_id');
- $uids = $uids? $uids : [0];
- $query->whereIn('a.id', $uids);
- }
- // 我关注的人
- else if($followType == 3){
- $uids = $this->getCollectUsers(['user_id'=> $uid,'status'=>1,'mark'=>1],'collect_uid');
- $uids = $uids? $uids : [0];
- $query->whereIn('a.id', $uids);
- }
- }else if($followType>=0){
- // 我关注的和关注我的人
- $uids = $this->getCollectUsers(['user_id'=> $uid,'is_return'=>1,'status'=>1,'mark'=>1],'collect_uid');
- $uids = $uids? $uids : [0];
- $uids1 = $this->getCollectUsers(['collect_uid'=> $uid,'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.username', 'like', "%{$keyword}%")->orWhere('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'=>'普通用户'];
- $item['nickname_short'] = $item['nickname']? mb_substr($item['nickname'], 0,6,'utf-8') : $item['id'];
- 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;
- }
- }
|