ArticleService.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. }
  98. }
  99. return [
  100. 'pageSize' => $pageSize,
  101. 'total' => isset($list['total']) ? $list['total'] : 0,
  102. 'list' => isset($list['data']) ? $list['data'] : []
  103. ];
  104. }
  105. /**
  106. * 添加或编辑
  107. * @return array
  108. * @since 2020/11/11
  109. * @author laravel开发员
  110. */
  111. public function edit()
  112. {
  113. $data = request()->all();
  114. // 处理封面图片
  115. if (!empty($data['cover'])) {
  116. $data['cover'] = get_image_path(trim($data['cover']));
  117. }
  118. $content = isset($data['content']) ? $data['content'] : '';
  119. if ($content) {
  120. $data['content'] = set_format_content($content);
  121. }
  122. // 设置日志标题
  123. ActionLogModel::setTitle("发布文章或客服咨询信息");
  124. ActionLogModel::record();
  125. return parent::edit($data);
  126. }
  127. public function importArticles($data)
  128. {
  129. DB::beginTransaction();
  130. try {
  131. foreach ($data as $item) {
  132. if (empty($item['title']) || empty($item['content'])) {
  133. throw new Exception("Title or content missing in import data");
  134. }
  135. DB::table('article')->insert([
  136. 'title' => $item['title'],
  137. 'content' => $item['content'],
  138. 'create_time' => time(),
  139. 'update_time' => time(),
  140. 'status' => 1,
  141. 'type' => 9,
  142. 'mark' => 1,
  143. ]);
  144. }
  145. DB::commit();
  146. return ['code' => 0, 'msg' => 'Import successful'];
  147. } catch (\Exception $e) {
  148. DB::rollBack();
  149. return ['code' => 1, 'msg' => $e->getMessage()];
  150. }
  151. }
  152. /**
  153. * 导入复习资料
  154. * @param array $data
  155. * @return array
  156. */
  157. public function importReviewMaterials($data)
  158. {
  159. DB::beginTransaction();
  160. try {
  161. $articles = $data['articles'] ?? [];
  162. foreach ($articles as $item) {
  163. if (empty($item['title']) || empty($item['content'])) {
  164. throw new Exception("Title or content missing in import data");
  165. }
  166. // 格式化内容
  167. $item['content'] = set_format_content($item['content']);
  168. DB::table('article')->insert([
  169. 'title' => $item['title'],
  170. 'content' => $item['content'],
  171. 'cate_id' => $item['cate_id'] ?? 0,
  172. 'type' => 21, // 复习资料类型
  173. 'author' => '',
  174. 'tags' => '',
  175. 'cover' => '',
  176. 'description' => '',
  177. 'sort' => 0,
  178. 'view_num' => 0,
  179. 'status' => 1,
  180. 'create_time' => time(),
  181. 'update_time' => time(),
  182. 'mark' => 1,
  183. ]);
  184. }
  185. DB::commit();
  186. return ['code' => 0, 'msg' => '成功导入 ' . count($articles) . ' 条复习资料'];
  187. } catch (\Exception $e) {
  188. DB::rollBack();
  189. return ['code' => 1, 'msg' => '导入失败: ' . $e->getMessage()];
  190. }
  191. }
  192. /**
  193. * 获取记录详情
  194. * @return array
  195. * @since 2020/11/11
  196. * @author laravel开发员
  197. */
  198. public function info()
  199. {
  200. // 记录ID
  201. $id = request()->input("id", 0);
  202. $info = [];
  203. if ($id) {
  204. $info = $this->model->getInfo($id);
  205. // 确保内容不被格式化,保持原始HTML格式供TinyMCE编辑器使用
  206. if (isset($info['content'])) {
  207. $info['content'] = $info['content'] ? get_format_content($info['content']) : '';
  208. }
  209. }
  210. return message(MESSAGE_OK, true, $info);
  211. }
  212. /**
  213. * 删除七天之前标记软删除的数据
  214. */
  215. public function delete()
  216. {
  217. // 设置日志标题
  218. ActionLogModel::setRecord(session('userId'), ['type' => 1, 'title' => "删除文章信息", 'content' => json_encode(request()->post(), 256), 'module' => 'admin']);
  219. ActionLogModel::record();
  220. $this->model->where('mark', 0)->where('update_time', '<=', time() - 7 * 86400)->delete();
  221. return parent::delete();
  222. }
  223. /**
  224. * 解析Word文档内容
  225. * @param string $url 文档URL
  226. * @param string $fileName 文件名
  227. * @return string 解析后的HTML内容
  228. */
  229. public function parseWordDocument($url, $fileName)
  230. {
  231. try {
  232. // 这里可以集成Word文档解析库,如PhpOffice\PhpWord
  233. // 暂时返回模拟的解析结果
  234. // 模拟解析Word文档内容
  235. $content = '<div class="word-document">';
  236. $content .= '<h2>Word文档内容解析</h2>';
  237. $content .= '<h3>文档信息</h3>';
  238. $content .= '<p><strong>文件名:</strong>' . htmlspecialchars($fileName) . '</p>';
  239. $content .= '<p><strong>文档路径:</strong>' . htmlspecialchars($url) . '</p>';
  240. $content .= '<p><strong>解析时间:</strong>' . date('Y-m-d H:i:s') . '</p>';
  241. $content .= '<hr>';
  242. $content .= '<h3>文档内容</h3>';
  243. $content .= '<p>这里是Word文档的解析内容...</p>';
  244. $content .= '<p>实际项目中,这里会显示从Word文档中提取的文本内容。</p>';
  245. $content .= '<p>您可以继续在富文本编辑器中编辑这些内容。</p>';
  246. $content .= '<hr>';
  247. $content .= '<p><em>注意:这是模拟的解析结果。实际项目中需要集成Word文档解析库。</em></p>';
  248. $content .= '</div>';
  249. return $content;
  250. } catch (\Exception $e) {
  251. throw new \Exception('Word文档解析失败: ' . $e->getMessage());
  252. }
  253. }
  254. }