ArticleService.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. /**
  16. * 文章管理-服务类
  17. * @author laravel开发员
  18. * @since 2020/11/11
  19. * Class ArticleService
  20. * @package App\Services\Common
  21. */
  22. class ArticleService extends BaseService
  23. {
  24. /**
  25. * 构造函数
  26. * @author laravel开发员
  27. * @since 2020/11/11
  28. * ArticleService constructor.
  29. */
  30. public function __construct()
  31. {
  32. $this->model = new ArticleModel();
  33. }
  34. /**
  35. * 静态入口
  36. * @return static|null
  37. */
  38. public static function make()
  39. {
  40. if (!self::$instance) {
  41. self::$instance = (new static());
  42. }
  43. return self::$instance;
  44. }
  45. /**
  46. * @param $params
  47. * @param int $pageSize
  48. * @return array
  49. */
  50. public function getDataList($params, $pageSize = 15)
  51. {
  52. $where = ['a.mark' => 1];
  53. $status = isset($params['status']) ? $params['status'] : 0;
  54. $type = isset($params['type']) ? $params['type'] : 0;
  55. $cateId = isset($params['cate_id']) ? $params['cate_id'] : 0;
  56. if ($status > 0) {
  57. $where['a.status'] = $status;
  58. }
  59. if ($cateId > 0) {
  60. $where['a.cate_id'] = $cateId;
  61. }
  62. $list = $this->model->with(['category'])->from('article as a')
  63. ->where($where)
  64. ->where(function ($query) use ($type) {
  65. if ($type>0) {
  66. $query->where('a.type', $type);
  67. } else if ($type<0) {
  68. $query->whereIn('a.type', [1,2]);
  69. } else {
  70. $query->where('a.type','>=',3)->whereNotIn('a.type',[9]);
  71. }
  72. })
  73. ->where(function ($query) use ($params) {
  74. $keyword = isset($params['keyword']) ? $params['keyword'] : '';
  75. if ($keyword) {
  76. $query->where('a.title', 'like', "%{$keyword}%")->orWhere('a.tags', 'like', "%{$keyword}%");
  77. }
  78. })
  79. ->select(['a.*'])
  80. ->orderBy('a.sort', 'desc')
  81. ->orderBy('a.create_time', 'desc')
  82. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  83. $list = $list ? $list->toArray() : [];
  84. if ($list) {
  85. $typrArr = [
  86. 1=>'行业资讯中心',
  87. 2=>'行业政策专栏',
  88. 3=>'用户注册协议',
  89. 4=>'提现协议',
  90. 5=>'隐私协议',
  91. 9=>'关于我们',
  92. 10=>'营业执照'
  93. ];
  94. foreach ($list['data'] as &$item) {
  95. $item['create_time'] = $item['create_time'] ? datetime($item['create_time'], 'Y-m-d H.i.s') : '';
  96. $item['cover'] = $item['cover'] ? get_image_url($item['cover']) : '';
  97. $item['content'] = $item['content'] ? ($item['content_type']==2? json_decode(format_content($item['content']),true):get_format_content($item['content'])) : '';
  98. $item['type_name'] = isset($typrArr[$item['type']]) ? $typrArr[$item['type']] : '其他';
  99. }
  100. }
  101. return [
  102. 'pageSize' => $pageSize,
  103. 'total' => isset($list['total']) ? $list['total'] : 0,
  104. 'list' => isset($list['data']) ? $list['data'] : []
  105. ];
  106. }
  107. /**
  108. * 添加或编辑
  109. * @return array
  110. * @since 2020/11/11
  111. * @author laravel开发员
  112. */
  113. public function edit()
  114. {
  115. $data = request()->all();
  116. $content = isset($data['content']) ? $data['content'] : '';
  117. if ($content) {
  118. $data['content'] = $data['content_type'] == 2? set_format_content(json_encode($data['content'], 256)) : set_format_content($content);
  119. } else {
  120. // 确保content字段存在(即使为空字符串)
  121. $data['content'] = '';
  122. }
  123. $data['author'] = isset($data['author']) && $data['author']?$data['author'] : \App\Services\ConfigService::make()->getConfigByCode('app_name');
  124. $data['cover'] = $data['cover']? get_image_path($data['cover']) : '';
  125. // 设置日志标题
  126. ActionLogModel::setTitle("编辑文章信息");
  127. ActionLogModel::record();
  128. return parent::edit($data); // TODO: Change the autogenerated stub
  129. }
  130. /**
  131. * 删除
  132. * @return array
  133. */
  134. public function delete()
  135. {
  136. $id = request()->post('id');
  137. if (!$id) {
  138. return ['code' => 1, 'msg' => '参数错误'];
  139. }
  140. // 支持批量删除
  141. if (is_array($id)) {
  142. $result = $this->model->whereIn('id', $id)->update(['mark' => 0]);
  143. } else {
  144. $result = $this->model->where('id', $id)->update(['mark' => 0]);
  145. }
  146. if ($result) {
  147. // 设置日志标题
  148. ActionLogModel::setTitle("删除文章内容");
  149. ActionLogModel::record();
  150. return ['code' => 0, 'msg' => '删除成功'];
  151. }
  152. return ['code' => 1, 'msg' => '删除失败'];
  153. }
  154. }