ArticleService.php 12 KB

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