ArticleService.php 11 KB

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