// +---------------------------------------------------------------------- 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,'a.user_id'=> $userId,'b.mark'=>1]; $field = $field? $field : 'lev_a.id,lev_a.user_id,lev_a.type,lev_a.collect_uid,lev_a.create_time,lev_b.nickname,lev_b.avatar'; $sortType = isset($params['sort_type']) ? $params['sort_type'] : 1; $order = 'id desc'; if($sortType == 1){ $order = 'create_time desc, lev_a.id desc'; } $list = $this->model->with(['mechanic']) ->from('member_collect as a') ->leftJoin('member as b', 'b.id', '=', 'a.collect_uid') ->where($where) ->where(function ($query) use ($params) { $userId = isset($params['user_id']) ? $params['user_id'] : 0; if ($userId > 0) { $query->where('a.user_id', $userId); } $status = isset($params['status'])? $params['status'] : 0; $status = $status>0? $status : 1; if ($status > 0) { $query->where('a.status', $status); } $type = isset($params['type'])? $params['type'] : 0; $type = $type>0? $type : 1; if ($type > 0) { $query->where('a.type', $type); } }) ->where(function ($query) use ($params) { $keyword = isset($params['kw']) ? $params['kw'] : ''; if ($keyword) { $query->where('b.nickname', 'like', "%{$keyword}%")->orWhere('b.username','like',"%{$keyword}%"); } }) ->selectRaw($field) ->orderByRaw($order) ->paginate($pageSize > 0 ? $pageSize : 9999999); $list = $list ? $list->toArray() : []; if ($list) { foreach ($list['data'] as &$item) { $item['time_text'] = isset($item['create_time']) && $item['create_time']? dateFormat($item['create_time']) : ''; $item['avatar'] = isset($item['avatar']) && $item['avatar'] ? get_image_url($item['avatar']) : ''; $mechaic = isset($item['mechanic'])? $item['mechanic'] : []; if($mechaic){ $mechaic['avatar'] = isset($mechaic['avatar'])? get_image_url($mechaic['avatar']) : ''; $mechaic['nickname'] = isset($mechaic['nickname']) && $mechaic['nickname']? $mechaic['nickname']: $mechaic['realname']; } $item['checked'] = false; $item['mechanic'] = $mechaic; } } 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 collect($userId, $params) { $collectUid = isset($params['id'])? intval($params['id']) : 0; $type = isset($params['type'])? intval($params['type']) : 2; $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(); $this->error = '1002'; return $this->model->insertGetId($data); }else{ if($status == 2){ RedisService::clear("caches:member:collect:u{$userId}_c{$collectUid}_{$type}"); } $this->error = '1002'; return $this->model->where('id', $id)->update($data); } } /** * 获取(被)收藏/关注/点赞数量 * @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; } }