ArticleService.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services\Common;
  12. use App\Models\ActionLogModel;
  13. use App\Models\ArticleModel;
  14. use App\Services\BaseService;
  15. use Exception;
  16. use Illuminate\Support\Facades\DB;
  17. /**
  18. * 文章管理-服务类
  19. * @author laravel开发员
  20. * @since 2020/11/11
  21. * Class ArticleService
  22. * @package App\Services\Common
  23. */
  24. class ArticleService extends BaseService
  25. {
  26. /**
  27. * 构造函数
  28. * @author laravel开发员
  29. * @since 2020/11/11
  30. * ArticleService constructor.
  31. */
  32. public function __construct()
  33. {
  34. $this->model = new ArticleModel();
  35. }
  36. /**
  37. * 静态入口
  38. * @return static|null
  39. */
  40. public static function make()
  41. {
  42. if (!self::$instance) {
  43. self::$instance = (new static());
  44. }
  45. return self::$instance;
  46. }
  47. /**
  48. * @param $params
  49. * @param int $pageSize
  50. * @return array
  51. */
  52. public function getDataList($params, $pageSize = 15)
  53. {
  54. $where = ['a.mark' => 1];
  55. $status = isset($params['status']) ? $params['status'] : 0;
  56. $type = isset($params['type']) ? $params['type'] : 0;
  57. $cateId = isset($params['cate_id']) ? $params['cate_id'] : 0;
  58. $articleCateType = isset($params['article_cate_type']) ? $params['article_cate_type'] : 0;
  59. if ($status > 0) {
  60. $where['a.status'] = $status;
  61. }
  62. $query = $this->model->from('article as a')
  63. ->leftJoin('article_cates as ac', 'a.cate_id', '=', 'ac.id')
  64. ->where($where)
  65. ->where(function ($query) use ($type) {
  66. if ($type && is_array($type)) {
  67. $query->whereIn('a.type', $type);
  68. } else if ($type) {
  69. $query->where('a.type', $type);
  70. }
  71. })
  72. ->where(function ($query) use ($cateId) {
  73. if ($cateId) {
  74. $query->where('a.cate_id', $cateId);
  75. }
  76. })
  77. ->where(function ($query) use ($articleCateType) {
  78. if ($articleCateType) {
  79. $query->where('ac.type', $articleCateType);
  80. }
  81. })
  82. ->where(function ($query) use ($params) {
  83. $keyword = isset($params['keyword']) ? $params['keyword'] : '';
  84. if ($keyword) {
  85. $query->where('a.title', 'like', "%{$keyword}%")->orWhere('a.tags', 'like', "%{$keyword}%");
  86. }
  87. })
  88. ->select(['a.*', 'ac.name as cate_name'])
  89. ->orderBy('a.create_time', 'desc');
  90. $list = $query->paginate($pageSize > 0 ? $pageSize : 9999999);
  91. $list = $list ? $list->toArray() : [];
  92. if ($list) {
  93. foreach ($list['data'] as &$item) {
  94. $item['create_time'] = $item['create_time'] ? datetime($item['create_time'], 'Y-m-d H.i.s') : '';
  95. $item['cover'] = $item['cover'] ? get_image_url($item['cover']) : '';
  96. $item['content'] = $item['content'] ? get_format_content($item['content']) : '';
  97. // 处理file_url,转换为完整URL
  98. if (isset($item['file_url']) && $item['file_url']) {
  99. $item['file_url'] = get_image_url($item['file_url']);
  100. }
  101. }
  102. }
  103. return [
  104. 'pageSize' => $pageSize,
  105. 'total' => isset($list['total']) ? $list['total'] : 0,
  106. 'list' => isset($list['data']) ? $list['data'] : []
  107. ];
  108. }
  109. /**
  110. * 添加或编辑
  111. * @return array
  112. * @since 2020/11/11
  113. * @author laravel开发员
  114. */
  115. public function edit()
  116. {
  117. $data = request()->all();
  118. // 处理封面图片
  119. if (!empty($data['cover'])) {
  120. $data['cover'] = get_image_path(trim($data['cover']));
  121. }
  122. // 处理文件URL(Word文档或图片)
  123. if (!empty($data['file_url'])) {
  124. $data['file_url'] = get_image_path(trim($data['file_url']));
  125. }
  126. // 处理内容
  127. $content = isset($data['content']) ? $data['content'] : '';
  128. if ($content) {
  129. $data['content'] = set_format_content($content);
  130. }
  131. // 设置日志标题
  132. ActionLogModel::setTitle("发布文章或客服咨询信息");
  133. ActionLogModel::record();
  134. return parent::edit($data);
  135. }
  136. public function importArticles($data)
  137. {
  138. DB::beginTransaction();
  139. try {
  140. foreach ($data as $item) {
  141. if (empty($item['title']) || empty($item['content'])) {
  142. throw new Exception("Title or content missing in import data");
  143. }
  144. DB::table('article')->insert([
  145. 'title' => $item['title'],
  146. 'content' => $item['content'],
  147. 'create_time' => time(),
  148. 'update_time' => time(),
  149. 'status' => 1,
  150. 'type' => 9,
  151. 'mark' => 1,
  152. ]);
  153. }
  154. DB::commit();
  155. return ['code' => 0, 'msg' => 'Import successful'];
  156. } catch (\Exception $e) {
  157. DB::rollBack();
  158. return ['code' => 1, 'msg' => $e->getMessage()];
  159. }
  160. }
  161. /**
  162. * 导入复习资料
  163. * @param array $data
  164. * @return array
  165. */
  166. public function importReviewMaterials($data)
  167. {
  168. DB::beginTransaction();
  169. try {
  170. $articles = $data['articles'] ?? [];
  171. foreach ($articles as $item) {
  172. if (empty($item['title']) || empty($item['content'])) {
  173. throw new Exception("Title or content missing in import data");
  174. }
  175. // 格式化内容
  176. $item['content'] = set_format_content($item['content']);
  177. DB::table('article')->insert([
  178. 'title' => $item['title'],
  179. 'content' => $item['content'],
  180. 'cate_id' => $item['cate_id'] ?? 0,
  181. 'type' => 21, // 复习资料类型
  182. 'author' => '',
  183. 'tags' => '',
  184. 'cover' => '',
  185. 'description' => '',
  186. 'sort' => 0,
  187. 'view_num' => 0,
  188. 'status' => 1,
  189. 'create_time' => time(),
  190. 'update_time' => time(),
  191. 'mark' => 1,
  192. ]);
  193. }
  194. DB::commit();
  195. return ['code' => 0, 'msg' => '成功导入 ' . count($articles) . ' 条复习资料'];
  196. } catch (\Exception $e) {
  197. DB::rollBack();
  198. return ['code' => 1, 'msg' => '导入失败: ' . $e->getMessage()];
  199. }
  200. }
  201. /**
  202. * 获取记录详情
  203. * @return array
  204. * @since 2020/11/11
  205. * @author laravel开发员
  206. */
  207. public function info()
  208. {
  209. // 记录ID
  210. $id = request()->input("id", 0);
  211. $info = [];
  212. if ($id) {
  213. $info = $this->model->getInfo($id);
  214. // 确保内容不被格式化,保持原始HTML格式供TinyMCE编辑器使用
  215. if (isset($info['content'])) {
  216. $info['content'] = $info['content'] ? get_format_content($info['content']) : '';
  217. }
  218. // 确保file_url返回完整URL
  219. if (isset($info['file_url'])) {
  220. $info['file_url'] = $info['file_url'] ? get_image_url($info['file_url']) : '';
  221. }
  222. }
  223. return message(MESSAGE_OK, true, $info);
  224. }
  225. /**
  226. * 删除七天之前标记软删除的数据
  227. */
  228. public function delete()
  229. {
  230. // 设置日志标题
  231. ActionLogModel::setRecord(session('userId'), ['type' => 1, 'title' => "删除文章信息", 'content' => json_encode(request()->post(), 256), 'module' => 'admin']);
  232. ActionLogModel::record();
  233. $this->model->where('mark', 0)->where('update_time', '<=', time() - 7 * 86400)->delete();
  234. return parent::delete();
  235. }
  236. /**
  237. * 解析Word文档内容
  238. * @param string $url 文档URL
  239. * @param string $fileName 文件名
  240. * @return string 解析后的HTML内容
  241. */
  242. public function parseWordDocument($url, $fileName)
  243. {
  244. try {
  245. // 这里可以集成Word文档解析库,如PhpOffice\PhpWord
  246. // 暂时返回模拟的解析结果
  247. // 模拟解析Word文档内容
  248. $content = '<div class="word-document">';
  249. $content .= '<h2>Word文档内容解析</h2>';
  250. $content .= '<h3>文档信息</h3>';
  251. $content .= '<p><strong>文件名:</strong>' . htmlspecialchars($fileName) . '</p>';
  252. $content .= '<p><strong>文档路径:</strong>' . htmlspecialchars($url) . '</p>';
  253. $content .= '<p><strong>解析时间:</strong>' . date('Y-m-d H:i:s') . '</p>';
  254. $content .= '<hr>';
  255. $content .= '<h3>文档内容</h3>';
  256. $content .= '<p>这里是Word文档的解析内容...</p>';
  257. $content .= '<p>实际项目中,这里会显示从Word文档中提取的文本内容。</p>';
  258. $content .= '<p>您可以继续在富文本编辑器中编辑这些内容。</p>';
  259. $content .= '<hr>';
  260. $content .= '<p><em>注意:这是模拟的解析结果。实际项目中需要集成Word文档解析库。</em></p>';
  261. $content .= '</div>';
  262. return $content;
  263. } catch (\Exception $e) {
  264. throw new \Exception('Word文档解析失败: ' . $e->getMessage());
  265. }
  266. }
  267. }