// +---------------------------------------------------------------------- namespace App\Services\Api; use App\Models\MemberCollectModel; use App\Services\BaseService; use App\Services\RedisService; /** * 用户收藏点赞管理-服务类 * @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.user_id,lev_a.type,lev_a.collect_uid,lev_b.nickname,lev_b.avatar,lev_c.nickname as c_nickname,lev_c.avatar as c_avatar'; $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 = $this->model->from('member_collect as a') ->leftJoin('member as b', function($join){ $join->on('b.id', '=', 'a.user_id') ->where('b.status', 1) ->where('b.mark', 1); }) ->leftJoin('member as c', function($join){ $join->on('c.id', '=', 'a.collect_uid') ->where('c.status', 1) ->where('c.mark', 1); }) ->where($where) ->where(function ($query) use ($params, $userId) { $status = isset($params['status'])? $params['status'] : 0; $status = $status>0? $status : 1; if ($status > 0) { $query->where('a.status', $status); } $followType = isset($params['follow_type'])? $params['follow_type'] : 0; if (in_array($followType,[1,2])) { // 我关注的人 if($followType == 1){ $query->where('a.user_id', $userId); } // 关注我的人 else if($followType == 2){ $query->where('a.collect_uid', $userId); } }else{ // 我关注的和关注我的人 $query->where(function($query) use($userId){ $query->where('a.user_id',$userId) ->orWhere('a.collect_uid', $userId); }); } }) ->where(function ($query) use ($params) { $keyword = isset($params['kw']) ? $params['kw'] : ''; $followType = isset($params['follow_type'])? $params['follow_type'] : 0; if ($keyword) { if($followType == 1){ $query->where('b.nickname', 'like', "%{$keyword}%")->orWhere('b.id','like',"%{$keyword}%"); }else if($followType == 2){ $query->where('c.nickname', 'like', "%{$keyword}%")->orWhere('c.id','like',"%{$keyword}%"); }else{ $query->where('b.nickname', 'like', "%{$keyword}%")->orWhere('b.id','like',"%{$keyword}%") ->where('c.nickname', 'like', "%{$keyword}%")->orWhere('c.id','like',"%{$keyword}%"); } } }) ->selectRaw($field) ->orderByRaw($order) ->paginate($pageSize > 0 ? $pageSize : 9999999); $list = $list ? $list->toArray() : []; if ($list) { 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['c_avatar'] = isset($item['c_avatar']) && $item['c_avatar'] ? get_image_url($item['c_avatar']) : get_image_url('/images/member/logo.png'); // 我关注的用户 if($userId == $item['user_id']){ $item['nickname'] = $item['c_nickname']; $item['avatar'] = $item['c_avatar']; $item['user_id'] = $item['collect_uid']; } $item['checked'] = false; } } return [ 'pageSize' => $pageSize, 'total' => isset($list['total']) ? $list['total'] : 0, 'list' => isset($list['data']) ? $list['data'] : [] ]; } /** * 是否已经收藏或点赞过 * @param $userId * @param $collectUid * @param int $type 类型: * @return array|mixed */ public function checkCollect($userId, $collectUid, $type=1) { $cacheKey = "caches:member:collect:u{$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'); $data = [ 'user_id'=> $userId, 'type'=> $type, 'collect_uid'=> $collectUid, 'update_time'=> time(), '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::clear("caches:member:collect:u{$userId}_c{$collectUid}_{$type}"); $isFans = $this->model->where(['user_id'=> $collectUid,'collect_uid'=> $userId,'type'=> $type])->value('id'); return ['is_fans'=>$isFans? 1:0]; } /** * 获取(被)收藏/关注/点赞数量 * @param $userId 用户ID * @param int $type 类型:1-关注,2-收藏,3-点赞喜欢 * @param int $ctype 查询类型:1-被关注/收藏/点赞,2-关注/收藏/点赞 * @return array|mixed */ public function getCount($userId, $type=1, $ctype=1) { $cacheKey = "caches:member: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, 3600); } 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; } }