// +---------------------------------------------------------------------- namespace App\Services\Api; use App\Models\PostModel; use App\Models\PostRecordModel; use App\Services\BaseService; use App\Services\RedisService; /** * 发圈-服务类 * @author laravel开发员 * @since 2020/11/11 * @package App\Services\Api */ class SocialService extends BaseService { // 静态对象 protected static $instance = null; /** * 构造函数 * @author laravel开发员 * @since 2020/11/11 */ public function __construct() { $this->model = new PostModel(); } /** * 静态入口 */ public static function make() { if (!self::$instance) { self::$instance = new static(); } return self::$instance; } /** * 详情 * @param int $num * @return array|mixed */ public function getInfo($id, $userId=0) { $cacheKey = "caches:social:info_{$id}_{$userId}"; $datas = RedisService::get($cacheKey); if($datas){ return $datas; } $datas = $this->model->with(['user','isLike'=>function($query) use($userId){ $query->where('user_id', $userId); }]) ->where(['id'=>$id,'status'=>1,'mark'=>1]) ->orderBy('id','desc') ->first(); $datas = $datas? $datas->toArray() : []; if($datas){ RedisService::set($cacheKey, $datas, rand(10,20)); } return $datas; } /** * @param $params * @param int $pageSize * @return array */ public function getDataList($params, $pageSize = 15) { $query = $this->getQuery($params); $sortType = isset($params['sort_type'])?intval($params['sort_type']) : 0; if($sortType==1){ $query->orderBy('posts.views','desc'); }else{ $query->orderBy('posts.id','desc'); } $list = $query->select(['posts.*'])->withCount(['likes'])->paginate($pageSize > 0 ? $pageSize : 9999999); $list = $list? $list->toArray() :[]; return [ 'pageSize'=> $pageSize, 'total'=>isset($list['total'])? $list['total'] : 0, 'list'=> isset($list['data'])? $list['data'] : [] ]; } /** * 查询 * @param $params * @return mixed */ public function getQuery($params) { $where = ['posts.mark' => 1]; $status = isset($params['status'])? $params['status'] : 0; $type = isset($params['type'])? $params['type'] : 0; $userId = isset($params['user_id'])? $params['user_id'] : 0; if($status>0){ $where['posts.status'] = $status; } if($type>0){ $where['posts.type'] = $type; } return $this->model->with(['user','isLike'=>function($query) use($userId){ $query->where('user_id', $userId); }])->from('posts') ->where($where) ->where(function ($query) use($params){ $keyword = isset($params['keyword'])? $params['keyword'] : ''; if($keyword){ $query->where('posts.title','like',"%{$keyword}%") ->orWhere('posts.tags','like',"%{$keyword}%"); } $tag = isset($params['tag'])? $params['tag'] : ''; if($tag){ $query->where('posts.tags','like',"%{$tag},%"); } }); } /** * 发布笔记 * @param $userId * @param $params * @return array|false */ public function submit($userId, $params) { $title = isset($params['title']) ? trim($params['title']) : ''; $content = isset($params['content']) ? trim($params['content']) : ''; $type = isset($params['type']) ? intval($params['type']) : 1; $showType = isset($params['show_type']) ? intval($params['show_type']) : 1; if (empty($title)) { $this->error = '请添加标题'; return false; } if (empty($content)) { $this->error = '请添加内容'; return false; } $albums = isset($params['albums']) ? $params['albums'] : []; $cover = isset($albums[0])?get_image_path($albums[0]):''; $albums = $albums? get_format_images($albums, '') : ''; if ($showType == 1 && empty($albums)) { $this->error = '请添加图片'; return false; } $videoUrl = isset($params['video_url']) ? get_image_path($params['video_url']) : ''; if ($showType == 2 && empty($videoUrl)) { $this->error = '请添加视频'; return false; } $cacheKey = "caches:scoial:submit:{$userId}_".md5(json_encode($params,256)); if (RedisService::get($cacheKey."_lock")) { $this->error = '请不要重复提交'; return false; } $tags = isset($params['tags'])?$params['tags'] : []; $tags = array_filter($tags); $data = [ 'user_id' => $userId, 'title' => $title, 'type' => $type, 'show_type' => $showType, 'cover' => $cover, 'albums' => $albums, 'video_url' => $videoUrl, 'tags' => $tags? implode(',', $tags).',':'', 'content' => $content, 'create_time' => time(), 'update_time' => time(), 'status' => 1, 'mark' => 1, ]; RedisService::set($cacheKey."_lock", $data, rand(20,30)); if(!$id = $this->model->insertGetId($data)){ RedisService::clear($cacheKey."_lock"); $this->error = '发布失败'; return false; } $this->error = '发布成功'; RedisService::set($cacheKey, $data, 30); RedisService::clear($cacheKey."_lock"); return ['id' => $id]; } public function delete() { $ids = $this->model->where(['mark'=>0])->where('create_time','<', time() - 600)->pluck('id'); $ids = $ids? $ids->toArray() : []; $this->model->where(['mark'=>0])->where('create_time','<', time() - 600)->delete(); if($ids){ PostRecordModel::whereIn('source_id', $ids)->where('create_time','<', time() - 600)->delete(); } return parent::delete(); // TODO: Change the autogenerated stub } }