| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- <?php
- // +----------------------------------------------------------------------
- // | Laravel框架 [ Laravel ]
- // +----------------------------------------------------------------------
- // | 版权所有 2017~2021 Laravel研发中心
- // +----------------------------------------------------------------------
- // | 官方网站: http://www.laravel.cn
- // +----------------------------------------------------------------------
- // | Author: wesmiler <12345678@qq.com>
- // +----------------------------------------------------------------------
- namespace App\Services;
- use App\Models\CollectModel;
- /**
- * 收藏管理-服务类
- * @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.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? '收藏':'关注';
- $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
- }
- }
|