ArticleService.php 10 KB

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