// +---------------------------------------------------------------------- namespace App\Services\Common; use App\Models\ActionLogModel; use App\Models\ArticleModel; use App\Services\BaseService; use Exception; use Illuminate\Support\Facades\DB; /** * 文章管理-服务类 * @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; $paperType = isset($params['paper_type']) ? $params['paper_type'] : 0; if ($status > 0) { $where['a.status'] = $status; } $list = $this->model->from('article as a') ->where($where) ->where(function ($query) use ($type) { if ($type && is_array($type)) { $query->whereIn('a.type', $type); } else if ($type) { $query->where('a.type', $type); } }) ->where(function ($query) use ($paperType) { if ($paperType) { $query->where('a.paper_type', $paperType); } }) ->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.create_time', 'desc') ->paginate($pageSize > 0 ? $pageSize : 9999999); $list = $list ? $list->toArray() : []; if ($list) { foreach ($list['data'] as &$item) { $item['create_time'] = $item['create_time'] ? datetime($item['create_time'], 'Y-m-d H.i.s') : ''; $item['cover'] = $item['cover'] ? get_image_url($item['cover']) : ''; $item['content'] = $item['content'] ? get_format_content($item['content']) : ''; } } 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(); // 处理封面图片 if (!empty($data['cover'])) { $data['cover'] = get_image_path(trim($data['cover'])); } $content = isset($data['content']) ? $data['content'] : ''; if ($content) { $data['content'] = set_format_content($content); } // 设置日志标题 ActionLogModel::setTitle("发布文章或客服咨询信息"); ActionLogModel::record(); return parent::edit($data); } public function importArticles($data) { DB::beginTransaction(); try { foreach ($data as $item) { if (empty($item['title']) || empty($item['content'])) { throw new Exception("Title or content missing in import data"); } DB::table('article')->insert([ 'title' => $item['title'], 'content' => $item['content'], 'create_time' => time(), 'update_time' => time(), 'status' => 1, 'type' => 9, 'mark' => 1, ]); } DB::commit(); return ['code' => 0, 'msg' => 'Import successful']; } catch (\Exception $e) { DB::rollBack(); return ['code' => 1, 'msg' => $e->getMessage()]; } } /** * 导入复习资料 * @param array $data * @return array */ public function importReviewMaterials($data) { DB::beginTransaction(); try { $articles = $data['articles'] ?? []; foreach ($articles as $item) { if (empty($item['title']) || empty($item['content'])) { throw new Exception("Title or content missing in import data"); } DB::table('article')->insert([ 'title' => $item['title'], 'content' => $item['content'], 'paper_type' => $item['paper_type'], 'type' => 21, // 复习资料类型 'author' => '', 'tags' => '', 'cover' => '', 'description' => '', 'cate_id' => 0, 'sort' => 0, 'view_num' => 0, 'status' => 1, 'create_time' => time(), 'update_time' => time(), 'mark' => 1, ]); } DB::commit(); return ['code' => 0, 'msg' => '成功导入 ' . count($articles) . ' 条复习资料']; } catch (\Exception $e) { DB::rollBack(); return ['code' => 1, 'msg' => '导入失败: ' . $e->getMessage()]; } } /** * 删除七天之前标记软删除的数据 */ public function delete() { // 设置日志标题 ActionLogModel::setRecord(session('userId'), ['type' => 1, 'title' => "删除文章信息", 'content' => json_encode(request()->post(), 256), 'module' => 'admin']); ActionLogModel::record(); $this->model->where('mark', 0)->where('update_time', '<=', time() - 7 * 86400)->delete(); return parent::delete(); } }