| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375 |
- <?php
- // +----------------------------------------------------------------------
- // | Laravel框架 [ Laravel ]
- // +----------------------------------------------------------------------
- // | 版权所有 2017~2021 Laravel研发中心
- // +----------------------------------------------------------------------
- // | 官方网站: http://www.laravel.cn
- // +----------------------------------------------------------------------
- // | Author: wesmiler <12345678@qq.com>
- // +----------------------------------------------------------------------
- namespace App\Services;
- use App\Models\ArticleModel;
- use App\Models\CollectModel;
- use App\Models\MemberModel;
- /**
- * 文章管理-服务类
- * @author wesmiler
- * @since 2020/11/11
- * Class ArticleService
- * @package App\Services
- */
- class ArticleService extends BaseService
- {
- protected static $instance = null;
- /**
- * 构造函数
- * @author wesmiler
- * @since 2020/11/11
- * ArticleService constructor.
- */
- public function __construct()
- {
- $this->model = new ArticleModel();
- }
- /**
- * 静态入口
- * @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 getList()
- {
- $params = request()->all();
- $page = isset($params['pageSize']) ? intval($params['pageSize']) : PAGE;
- $pageSize = isset($params['pageSize']) ? intval($params['pageSize']) : PERPAGE;
- $dataList = $this->model::from('article as a')
- ->leftJoin('article_cates as c', 'a.cate_id', '=', 'c.id')
- ->where(function ($query) use ($params) {
- $query->where('a.mark', 1);
- $title = isset($params['title']) ? trim($params['title']) : '';
- if (!empty($title)) {
- $query->where('a.title', 'like', "%{$title}%");
- }
- $cateId = isset($params['cate_id']) ? intval($params['cate_id']) : 0;
- if ($cateId > 0) {
- $query->where('a.cate_id', $cateId);
- } else if ($cateId == -1) {
- $query->where('a.is_recommand', 1);
- }
- $isTop = isset($params['is_top']) ? intval($params['is_top']) : 0;
- if ($isTop > 0) {
- $query->where('a.is_top', $isTop);
- }
- $type = isset($params['type']) ? intval($params['type']) : 0;
- if ($type > 0) {
- $query->where('a.type', $type);
- }
- $status = isset($params['status']) ? $params['status'] : 0;
- if ($status > 0) {
- $query->where('a.status', $status);
- } else {
- $query->whereIn('a.status', [1, 2, 3]);
- }
- })
- ->select(['a.id', 'a.cate_id', 'c.name as cate_name', 'a.title', 'a.is_form', 'a.is_recommand', 'a.view_num', 'a.thumb', 'a.status', 'a.create_time', 'a.update_time', 'a.description', 'a.sort', 'a.content','a.publish_at'])
- ->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['content'] = $item['content'] ? str_replace("\n",'<br>',$item['content']) : '';
- $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,
- ];
- }
- /**
- * 访问量
- * @return mixed
- */
- public function updateVisit($userId=0){
- $id = request()->get('id');
- $cacheKey = "caches:article:visit:{$userId}_{$id}";
- $check = RedisService::get($cacheKey);
- if($id && !$check){
- RedisService::set($cacheKey, $id, 3600);
- return $this->model::where(['id'=> $id])->increment('view_num', 1);
- }
- }
- /**
- * 获取列表
- * @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('article as a')
- ->leftJoin('article_cates as c', 'a.cate_id', '=', 'c.id')
- ->where(function ($query) use ($params) {
- $query->where(['a.mark'=>1]);
- $status = isset($params['status'])? $params['status'] : 0;
- if($status){
- $status = is_array($status)? $status : [$status];
- $query->whereIn('a.status', $status);
- }else{
- $query->where('a.status', 1);
- }
- $keyword = isset($params['keyword']) ? trim($params['keyword']) : '';
- $cateId = isset($params['cate_id']) ? intval($params['cate_id']) : 0;
- if ($cateId > 0) {
- $query->where('a.cate_id', $cateId);
- } else if ($cateId==0) {
- $query->where('a.is_recommand', 1);
- }
- $isTop = isset($params['is_top']) ? intval($params['is_top']) : 0;
- if ($isTop > 0) {
- $query->where('a.is_top', $isTop);
- }
- $userId = isset($params['user_id']) ? intval($params['user_id']) : 0;
- if ($userId > 0) {
- $query->where('a.user_id', $userId);
- }
- $type = isset($params['type']) ? intval($params['type']) : 0;
- if ($type > 0) {
- $query->where('a.type', $type);
- }
- })
- ->where(function($query) use($params){
- $keyword = isset($params['keyword']) ? trim($params['keyword']) : '';
- if (!empty($keyword)) {
- $query->where('a.title', 'like', "%{$keyword}%")->orWhere('c.name','like',"%{$keyword}%");
- }
- })
- ->select(['a.id', 'a.cate_id','a.user_id', 'c.name as cate_name','a.author', 'a.title', 'a.is_form', 'a.is_recommand', 'a.view_num', 'a.thumb', 'a.status', 'a.create_time', 'a.update_time', 'a.description', 'a.sort','a.publish_at'])
- ->orderBy('a.sort', 'desc')
- ->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['author'] = $item['author'] ? $item['author'] : '报恩寺';
- $item['create_time'] = $item['create_time'] ? datetime($item['create_time'],'Y-m-d H:i:s') : '';
- $item['publish_at'] = $item['publish_at'] ? $item['publish_at'] : $item['create_at'];
- }
- unset($item);
- }
- return [
- 'code' => 0,
- 'success'=> true,
- 'msg' => '操作成功',
- 'count' => isset($dataList['total']) ? $dataList['total'] : 0,
- 'data' => isset($dataList['data']) ? $dataList['data'] : 0,
- ];
- }
- /**
- * 相关推荐列表
- * @return array
- * @since 2020/11/11
- * @author wesmiler
- */
- public function getRelationList($params)
- {
- $page = isset($params['pageSize']) ? intval($params['pageSize']) : PAGE;
- $pageSize = isset($params['pageSize']) ? intval($params['pageSize']) : PERPAGE;
- $id = isset($params['id']) ? intval($params['id']) : PERPAGE;
- $info = $this->model::from('article as a')
- ->leftJoin('article_cates as c', 'a.cate_id', '=', 'c.id')
- ->where(['a.id'=> $id,'a.mark'=> 1,'a.status'=> 1])
- ->select(['a.id','a.cate_id','a.type'])
- ->first();
- $dataList = $this->model::from('article as a')
- ->leftJoin('article_cates as c', 'a.cate_id', '=', 'c.id')
- ->where(function ($query) use ($params, $info) {
- $query->where(['a.mark'=>1,'a.status'=> 1]);
- $cateId = isset($info['cate_id']) ? intval($info['cate_id']) : 0;
- if ($cateId > 0) {
- $query->where('a.cate_id', $cateId);
- }
- $type = isset($info['type']) ? intval($info['type']) : 0;
- if ($type > 0) {
- $query->where('a.type', $type);
- }
- })
- ->where(function($query) use($params){
- $keyword = isset($params['keyword']) ? trim($params['keyword']) : '';
- if (!empty($keyword)) {
- $query->where('a.title', 'like', "%{$keyword}%")->orWhere('c.name','like',"%{$keyword}%");
- }
- })
- ->select(['a.id', 'a.cate_id', 'c.name as cate_name', 'a.title', 'a.is_form', 'a.is_recommand', 'a.view_num', 'a.thumb', 'a.status', 'a.create_time', 'a.update_time', 'a.description', 'a.sort','a.publish_at'])
- ->orderBy('a.sort', 'desc')
- ->orderBy('a.update_time', 'desc')
- ->paginate($pageSize);
- $dataList = $dataList ? $dataList->toArray() : [];
- if(empty($dataList)){
- $dataList = $this->model::from('article as a')
- ->leftJoin('article_cates as c', 'a.cate_id', '=', 'c.id')
- ->where(['a.mark'=>1,'a.status'=> 1])
- ->select(['a.id', 'a.cate_id', 'c.name as cate_name', 'a.title', 'a.is_form', 'a.is_recommand', 'a.view_num', 'a.thumb', 'a.status', 'a.create_time', 'a.update_time', 'a.description', 'a.sort','a.publish_at'])
- ->orderBy(\DB::raw('rand()'))
- ->orderBy('a.update_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') : '';
- $item['publish_at'] = $item['publish_at'] ? $item['publish_at'] : $item['create_at'];
- }
- unset($item);
- }
- return [
- 'code' => 0,
- 'success'=> true,
- 'msg' => '操作成功',
- 'count' => isset($dataList['total']) ? $dataList['total'] : 0,
- 'data' => isset($dataList['data']) ? $dataList['data'] : 0,
- ];
- }
- /**
- * 获取详情
- * @param $id
- */
- public function getDetail($id, $userId=0){
- $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.author','a.user_id','a.cate_id','c.name as cate_name','a.view_num','a.description','a.is_form','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');
- $info['content'] = $info['content'] ? str_replace("\n",'<br>',$info['content']) : '';
- if(empty($info['author']) && $info['user_id']){
- $author = MemberModel::where(['id'=> $info['user_id']])->value('nickname');
- $info['author'] = $author? $author : '报恩寺';
- }
- // 是否已收藏
- $info['is_collect'] = 0;
- if($userId){
- if(CollectModel::where(['user_id'=> $userId,'source_id'=> $id,'type'=> 1,'mark'=> 1,'status'=> 1])->value('id')){
- $info['is_collect'] = 1;
- }
- }
- }
- 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'] = is_array($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
- }
- /**
- * 发布
- * @param $params
- * @return array
- */
- public function send($params){
- $data = [
- 'type'=> 3,
- 'id'=> isset($params['id'])? $params['id'] : 0,
- 'user_id'=> isset($params['user_id'])? $params['user_id'] : 0,
- 'title'=> isset($params['title'])? $params['title'] : '',
- 'author'=> isset($params['author'])? $params['author'] : '',
- 'thumb'=> isset($params['thumb'])? $params['thumb'] : '',
- 'content'=> isset($params['content'])? $params['content'] : '',
- 'create_time'=> time(),
- 'mark'=> 1,
- 'status'=> 2,
- ];
- if($data['thumb']){
- $data['thumb'] = is_array($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
- }
- }
|