ArticleService.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. $paperType = isset($params['paper_type']) ? $params['paper_type'] : 0;
  58. if ($status > 0) {
  59. $where['a.status'] = $status;
  60. }
  61. $list = $this->model->from('article as a')
  62. ->where($where)
  63. ->where(function ($query) use ($type) {
  64. if ($type && is_array($type)) {
  65. $query->whereIn('a.type', $type);
  66. } else if ($type) {
  67. $query->where('a.type', $type);
  68. }
  69. })
  70. ->where(function ($query) use ($paperType) {
  71. if ($paperType) {
  72. $query->where('a.paper_type', $paperType);
  73. }
  74. })
  75. ->where(function ($query) use ($params) {
  76. $keyword = isset($params['keyword']) ? $params['keyword'] : '';
  77. if ($keyword) {
  78. $query->where('a.title', 'like', "%{$keyword}%")->orWhere('a.tags', 'like', "%{$keyword}%");
  79. }
  80. })
  81. ->select(['a.*'])
  82. ->orderBy('a.create_time', 'desc')
  83. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  84. $list = $list ? $list->toArray() : [];
  85. if ($list) {
  86. foreach ($list['data'] as &$item) {
  87. $item['create_time'] = $item['create_time'] ? datetime($item['create_time'], 'Y-m-d H.i.s') : '';
  88. $item['cover'] = $item['cover'] ? get_image_url($item['cover']) : '';
  89. $item['content'] = $item['content'] ? get_format_content($item['content']) : '';
  90. }
  91. }
  92. return [
  93. 'pageSize' => $pageSize,
  94. 'total' => isset($list['total']) ? $list['total'] : 0,
  95. 'list' => isset($list['data']) ? $list['data'] : []
  96. ];
  97. }
  98. /**
  99. * 添加或编辑
  100. * @return array
  101. * @since 2020/11/11
  102. * @author laravel开发员
  103. */
  104. public function edit()
  105. {
  106. $data = request()->all();
  107. // 处理封面图片
  108. if (!empty($data['cover'])) {
  109. $data['cover'] = get_image_path(trim($data['cover']));
  110. }
  111. $content = isset($data['content']) ? $data['content'] : '';
  112. if ($content) {
  113. $data['content'] = set_format_content($content);
  114. }
  115. // 设置日志标题
  116. ActionLogModel::setTitle("发布文章或客服咨询信息");
  117. ActionLogModel::record();
  118. return parent::edit($data);
  119. }
  120. public function importArticles($data)
  121. {
  122. DB::beginTransaction();
  123. try {
  124. foreach ($data as $item) {
  125. if (empty($item['title']) || empty($item['content'])) {
  126. throw new Exception("Title or content missing in import data");
  127. }
  128. DB::table('article')->insert([
  129. 'title' => $item['title'],
  130. 'content' => $item['content'],
  131. 'create_time' => time(),
  132. 'update_time' => time(),
  133. 'status' => 1,
  134. 'type' => 9,
  135. 'mark' => 1,
  136. ]);
  137. }
  138. DB::commit();
  139. return ['code' => 0, 'msg' => 'Import successful'];
  140. } catch (\Exception $e) {
  141. DB::rollBack();
  142. return ['code' => 1, 'msg' => $e->getMessage()];
  143. }
  144. }
  145. /**
  146. * 导入复习资料
  147. * @param array $data
  148. * @return array
  149. */
  150. public function importReviewMaterials($data)
  151. {
  152. DB::beginTransaction();
  153. try {
  154. $articles = $data['articles'] ?? [];
  155. foreach ($articles as $item) {
  156. if (empty($item['title']) || empty($item['content'])) {
  157. throw new Exception("Title or content missing in import data");
  158. }
  159. DB::table('article')->insert([
  160. 'title' => $item['title'],
  161. 'content' => $item['content'],
  162. 'paper_type' => $item['paper_type'],
  163. 'type' => 21, // 复习资料类型
  164. 'author' => '',
  165. 'tags' => '',
  166. 'cover' => '',
  167. 'description' => '',
  168. 'cate_id' => 0,
  169. 'sort' => 0,
  170. 'view_num' => 0,
  171. 'status' => 1,
  172. 'create_time' => time(),
  173. 'update_time' => time(),
  174. 'mark' => 1,
  175. ]);
  176. }
  177. DB::commit();
  178. return ['code' => 0, 'msg' => '成功导入 ' . count($articles) . ' 条复习资料'];
  179. } catch (\Exception $e) {
  180. DB::rollBack();
  181. return ['code' => 1, 'msg' => '导入失败: ' . $e->getMessage()];
  182. }
  183. }
  184. /**
  185. * 删除七天之前标记软删除的数据
  186. */
  187. public function delete()
  188. {
  189. // 设置日志标题
  190. ActionLogModel::setRecord(session('userId'), ['type' => 1, 'title' => "删除文章信息", 'content' => json_encode(request()->post(), 256), 'module' => 'admin']);
  191. ActionLogModel::record();
  192. $this->model->where('mark', 0)->where('update_time', '<=', time() - 7 * 86400)->delete();
  193. return parent::delete();
  194. }
  195. }