| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301 |
- <?php
- // +----------------------------------------------------------------------
- // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
- // +----------------------------------------------------------------------
- // | 版权所有 2017~2021 LARAVEL研发中心
- // +----------------------------------------------------------------------
- // | 官方网站: http://www.laravel.cn
- // +----------------------------------------------------------------------
- // | Author: laravel开发员 <laravel.qq.com>
- // +----------------------------------------------------------------------
- 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;
- $cateId = isset($params['cate_id']) ? $params['cate_id'] : 0;
- $articleCateType = isset($params['article_cate_type']) ? $params['article_cate_type'] : 0;
- if ($status > 0) {
- $where['a.status'] = $status;
- }
- $query = $this->model->from('article as a')
- ->leftJoin('article_cates as ac', 'a.cate_id', '=', 'ac.id')
- ->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 ($cateId,$articleCateType) {
- if ($articleCateType && $articleCateType!=1) {
- if($cateId>0){
- $query->where('a.cate_id', $cateId);
- }else{
- $query->where('a.cate_id', '>', 0);
- }
- }else if($articleCateType==1){
- $query->where('a.cate_id', 0);
- }
- })
- ->where(function ($query) use ($articleCateType) {
- if ($articleCateType>1) {
- $query->where('ac.type', $articleCateType);
- }
- })
- ->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.*', 'ac.name as cate_name'])
- ->orderBy('a.create_time', 'desc');
- $list = $query->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']) : '';
- // 处理file_url,转换为完整URL
- if (isset($item['file_url']) && $item['file_url']) {
- $item['file_url'] = get_image_url($item['file_url']);
- }
- }
- }
- 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']));
- }
- // 处理文件URL(Word文档或图片)
- if (!empty($data['file_url'])) {
- $data['file_url'] = get_image_path(trim($data['file_url']));
- }
- // 处理内容
- $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");
- }
- // 格式化内容
- $item['content'] = set_format_content($item['content']);
- DB::table('article')->insert([
- 'title' => $item['title'],
- 'content' => $item['content'],
- 'cate_id' => $item['cate_id'] ?? 0,
- 'type' => 21, // 复习资料类型
- 'author' => '',
- 'tags' => '',
- 'cover' => '',
- 'description' => '',
- '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()];
- }
- }
- /**
- * 获取记录详情
- * @return array
- * @since 2020/11/11
- * @author laravel开发员
- */
- public function info()
- {
- // 记录ID
- $id = request()->input("id", 0);
- $info = [];
- if ($id) {
- $info = $this->model->getInfo($id);
- // 确保内容不被格式化,保持原始HTML格式供TinyMCE编辑器使用
- if (isset($info['content'])) {
- $info['content'] = $info['content'] ? get_format_content($info['content']) : '';
- }
- // 确保file_url返回完整URL
- if (isset($info['file_url'])) {
- $info['file_url'] = $info['file_url'] ? get_image_url($info['file_url']) : '';
- }
- }
- return message(MESSAGE_OK, true, $info);
- }
- /**
- * 删除七天之前标记软删除的数据
- */
- 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();
- }
- /**
- * 解析Word文档内容
- * @param string $url 文档URL
- * @param string $fileName 文件名
- * @return string 解析后的HTML内容
- */
- public function parseWordDocument($url, $fileName)
- {
- try {
- // 这里可以集成Word文档解析库,如PhpOffice\PhpWord
- // 暂时返回模拟的解析结果
- // 模拟解析Word文档内容
- $content = '<div class="word-document">';
- $content .= '<h2>Word文档内容解析</h2>';
- $content .= '<h3>文档信息</h3>';
- $content .= '<p><strong>文件名:</strong>' . htmlspecialchars($fileName) . '</p>';
- $content .= '<p><strong>文档路径:</strong>' . htmlspecialchars($url) . '</p>';
- $content .= '<p><strong>解析时间:</strong>' . date('Y-m-d H:i:s') . '</p>';
- $content .= '<hr>';
- $content .= '<h3>文档内容</h3>';
- $content .= '<p>这里是Word文档的解析内容...</p>';
- $content .= '<p>实际项目中,这里会显示从Word文档中提取的文本内容。</p>';
- $content .= '<p>您可以继续在富文本编辑器中编辑这些内容。</p>';
- $content .= '<hr>';
- $content .= '<p><em>注意:这是模拟的解析结果。实际项目中需要集成Word文档解析库。</em></p>';
- $content .= '</div>';
- return $content;
- } catch (\Exception $e) {
- throw new \Exception('Word文档解析失败: ' . $e->getMessage());
- }
- }
- }
|