// +---------------------------------------------------------------------- namespace App\Services; use App\Models\ArticleModel; use App\Models\CollectModel; use App\Models\DynamicModel; use App\Models\MemberModel; /** * 收藏管理-服务类 * @author wesmiler * @since 2020/11/11 * Class CollectService * @package App\Services */ class CollectService extends BaseService { protected static $instance = null; /** * 构造函数 * @author wesmiler * @since 2020/11/11 * CollectService constructor. */ public function __construct() { $this->model = new CollectModel(); } /** * 静态入口 * @return ArticleService|null */ public static function make(){ if(!self::$instance){ self::$instance = new ArticleService(); } return self::$instance; } /** * 获取列表 * @return array * @since 2020/11/11 * @author wesmiler */ public function getDataList($params) { $page = isset($params['pageSize']) ? intval($params['pageSize']) : PAGE; $pageSize = isset($params['pageSize']) ? intval($params['pageSize']) : PERPAGE; $dataList = $this->model::from('collect as a') ->leftJoin('article as c', 'a.source_id', '=', 'c.id') ->where(function ($query) use ($params) { $query->where(['a.mark'=> 1,'a.status'=> 1,'c.mark'=> 1,'c.status'=>1]); $type = isset($params['type']) ? intval($params['type']) : 0; if ($type > 0) { $query->where('a.type', $type); } $userId = isset($params['user_id']) ? intval($params['user_id']) : 0; if ($userId > 0) { $query->where('a.user_id', $userId); } }) ->select(['a.id', 'a.user_id','a.source_id','c.type', 'c.title as title', 'c.thumb', 'c.description', 'c.view_num', 'a.status', 'a.create_time']) ->orderBy('a.create_time', 'desc') ->paginate($pageSize); $dataList = $dataList ? $dataList->toArray() : []; if ($dataList) { foreach ($dataList['data'] as &$item) { $item['thumb'] = $item['thumb'] ? get_image_url($item['thumb']) : ''; $item['create_time'] = $item['create_time'] ? datetime($item['create_time'],'Y-m-d H:i:s') : ''; } unset($item); } return [ 'code' => 0, 'success'=> true, 'msg' => '操作成功', 'count' => isset($dataList['total']) ? $dataList['total'] : 0, 'data' => isset($dataList['data']) ? $dataList['data'] : 0, ]; } /** * 收藏 * @param $userId * @return array */ public function collect($userId){ $params = request()->all(); $id = isset($params['id'])? $params['id'] : 0; $type = isset($params['type'])? $params['type'] : 1; $status = isset($params['status'])? $params['status'] : 1; $type = $type<=0? 1 : $type; if($id<=0){ return message('参数错误', false); } if(!in_array($status, [1,2])){ return message('参数错误', false); } $name = $type==1? '收藏':'关注'; if($type == 2){ $info = DynamicModel::where(['id'=> $id,'mark'=> 1,'status'=> 1])->select(['id','user_id'])->first(); if(!$info){ return message("{$name}对象不存在", false); } if($info->user_id == $userId){ return message("不能{$name}自己的动态", false); } }else{ $info = ArticleModel::where(['id'=> $id,'mark'=> 1,'status'=> 1])->select(['id','user_id'])->first(); if(!$info){ return message("{$name}对象不存在", false); } if($userId && $info->user_id == $userId){ return message("不能{$name}自己发布的内容", false); } } $info = $this->model::where(['user_id'=> $userId, 'source_id'=> $id,'type'=> $type])->select(['id','status'])->first(); if($info && $info->status == 1 && $status == 1){ return message("您已{$name}过", false); }else if($info && $info->status == 2 && $status == 2){ return message("您已取消{$name}", false); }else if(!$info && $status == 2){ return message("您未{$name}过", false); } // 处理 if($info){ $info->status = $status; $info->create_time = time(); if($info->save()){ return message($status == 1? "{$name}成功":"取消{$name}成功", true); } }else{ $data = [ 'user_id'=> $userId, 'source_id'=> $id, 'type'=> $type, 'create_time'=> time(), 'status'=> 1, ]; if($this->model::insertGetId($data)){ return message("{$name}成功", true); } } return message('操作失败', false); } /** * 获取详情 * @param $id */ public function getDetail($id){ $info = $this->model::from('article as a') ->leftJoin('article_cates as c', 'a.cate_id', '=', 'c.id') ->where(['a.mark'=> 1,'a.status'=> 1,'a.id'=> $id]) ->select(['a.id','a.title','a.thumb','a.cate_id','c.name as cate_name','a.view_num','a.description','a.content','a.publish_at','a.create_time']) ->first(); $info = $info? $info->toArray() : []; if($info){ $info['thumb'] = $info['thumb']? get_image_url($info['thumb']) : ''; $info['publish_at'] = $info['publish_at']? $info['publish_at'] : datetime( $info['create_time'],'Y-m-d H:i:s'); } return $info; } /** * 添加或编辑 * @return array * @since 2020/11/11 * @author wesmiler */ public function edit() { $data = request()->all(); // 图片处理 $type = isset($data['type'])? intval($data['type']) : 0; $image = $data['thumb']? trim($data['thumb']) : ''; $id = isset($data['id']) ? $data['id'] : 0; if (!$id && !$image && $type == 1) { return message('请上传封面图片', false); } if (strpos($image, "temp")) { $data['thumb'] = save_image($image, 'item'); } else { $data['thumb'] = str_replace(IMG_URL, "", $data['thumb']); } $data['update_time'] = time(); $data['publish_at'] = isset($data['publish_at']) && $data['publish_at']? $data['publish_at'] : date('Y-m-d H:i:s'); return parent::edit($data); // TODO: Change the autogenerated stub } }