// +---------------------------------------------------------------------- namespace App\Services\Common; use App\Models\ActionLogModel; use App\Models\ArticleModel; use App\Services\BaseService; /** * 文章管理-服务类 * @author laravel开发员 * @since 2020/11/11 * Class ArticleService * @package App\Services\Common */ class ArticleService extends BaseService { /** * 构造函数 * @author laravel开发员 * @since 2020/11/11 * ArticleService constructor. */ public function __construct() { $this->model = new ArticleModel(); } /** * 静态入口 * @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($params, $pageSize = 15) { $where = ['a.mark' => 1]; $status = isset($params['status']) ? $params['status'] : 0; $type = isset($params['type']) ? $params['type'] : 0; $cateId = isset($params['cate_id']) ? $params['cate_id'] : 0; if ($status > 0) { $where['a.status'] = $status; } if ($cateId > 0) { $where['a.cate_id'] = $cateId; } $list = $this->model->with(['category'])->from('article as a') ->where($where) ->where(function ($query) use ($type) { if ($type>0) { $query->where('a.type', $type); } else if ($type<0) { $query->whereIn('a.type', [1,2]); } else { $query->where('a.type','>=',3)->whereNotIn('a.type',[9]); } }) ->where(function ($query) use ($params) { $keyword = isset($params['keyword']) ? $params['keyword'] : ''; if ($keyword) { $query->where('a.title', 'like', "%{$keyword}%")->orWhere('a.tags', 'like', "%{$keyword}%"); } }) ->select(['a.*']) ->orderBy('a.sort', 'desc') ->orderBy('a.publish_at', 'desc') ->orderBy('a.id', 'desc') ->paginate($pageSize > 0 ? $pageSize : 9999999); $list = $list ? $list->toArray() : []; if ($list) { $typrArr = [ 1=>'行业资讯中心', 2=>'行业政策专栏', 3=>'用户注册协议', 4=>'提现协议', 5=>'隐私协议', 9=>'关于我们', 10=>'营业执照' ]; foreach ($list['data'] as &$item) { $item['create_time'] = $item['publish_at'] ? datetime($item['publish_at']) : datetime($item['create_time']); $item['publish_at'] = $item['publish_at'] ? datetime($item['publish_at']) : datetime($item['create_time']); $item['cover'] = $item['cover'] ? get_image_url($item['cover']) : ''; $item['content'] = $item['content'] ? ($item['content_type']==2? json_decode(format_content($item['content']),true):get_format_content($item['content'])) : ''; $item['type_name'] = isset($typrArr[$item['type']]) ? $typrArr[$item['type']] : '其他'; } } return [ 'pageSize' => $pageSize, 'total' => isset($list['total']) ? $list['total'] : 0, 'list' => isset($list['data']) ? $list['data'] : [] ]; } /** * 添加或编辑 * @return array * @since 2020/11/11 * @author laravel开发员 */ public function edit() { $data = request()->all(); $content = isset($data['content']) ? $data['content'] : ''; if ($content) { $data['content'] = $data['content_type'] == 2? set_format_content(json_encode($data['content'], 256)) : set_format_content($content); } else { // 确保content字段存在(即使为空字符串) $data['content'] = ''; } $data['author'] = isset($data['author']) && $data['author']?$data['author'] : \App\Services\ConfigService::make()->getConfigByCode('app_name'); $data['cover'] = $data['cover']? get_image_path($data['cover']) : ''; $data['publish_at'] = !empty($data['publish_at'])? datetime($data['publish_at']) : date('Y-m-d H:i:s'); // 设置日志标题 ActionLogModel::setTitle("编辑文章信息"); ActionLogModel::record(); return parent::edit($data); // TODO: Change the autogenerated stub } /** * 删除 * @return array */ public function delete() { $id = request()->post('id'); if (!$id) { return ['code' => 1, 'msg' => '参数错误']; } // 支持批量删除 if (is_array($id)) { $result = $this->model->whereIn('id', $id)->update(['mark' => 0]); } else { $result = $this->model->where('id', $id)->update(['mark' => 0]); } if ($result) { // 设置日志标题 ActionLogModel::setTitle("删除文章内容"); ActionLogModel::record(); return ['code' => 0, 'msg' => '删除成功']; } return ['code' => 1, 'msg' => '删除失败']; } }