ArticleService.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. if ($status > 0) {
  56. $where['a.status'] = $status;
  57. }
  58. $list = $this->model->with(['category'])->from('article as a')
  59. ->where($where)
  60. ->where(function ($query) use ($type) {
  61. if ($type>0) {
  62. $query->where('a.type', $type);
  63. } else {
  64. $query->where('a.type','>=',3)->where('a.type','<',9);
  65. }
  66. })
  67. ->where(function ($query) use ($params) {
  68. $keyword = isset($params['keyword']) ? $params['keyword'] : '';
  69. if ($keyword) {
  70. $query->where('a.title', 'like', "%{$keyword}%")->orWhere('a.tags', 'like', "%{$keyword}%");
  71. }
  72. })
  73. ->select(['a.*'])
  74. ->orderBy('a.create_time', 'desc')
  75. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  76. $list = $list ? $list->toArray() : [];
  77. if ($list) {
  78. $typrArr = [
  79. 1=>'行业资讯中心',
  80. 2=>'行业政策专栏',
  81. 3=>'用户注册协议',
  82. 4=>'提现协议',
  83. 5=>'隐私协议',
  84. 9=>'关于我们'
  85. ];
  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. $item['type_name'] = isset($typrArr[$item['type']]) ? $typrArr[$item['type']] : '其他';
  91. }
  92. }
  93. return [
  94. 'pageSize' => $pageSize,
  95. 'total' => isset($list['total']) ? $list['total'] : 0,
  96. 'list' => isset($list['data']) ? $list['data'] : []
  97. ];
  98. }
  99. /**
  100. * 添加或编辑
  101. * @return array
  102. * @since 2020/11/11
  103. * @author laravel开发员
  104. */
  105. public function edit()
  106. {
  107. $data = request()->all();
  108. $content = isset($data['content']) ? $data['content'] : '';
  109. if ($content) {
  110. $data['content'] = set_format_content($content);
  111. } else {
  112. // 确保content字段存在(即使为空字符串)
  113. $data['content'] = '';
  114. }
  115. // 设置日志标题
  116. ActionLogModel::setTitle("编辑文章信息");
  117. ActionLogModel::record();
  118. return parent::edit($data); // TODO: Change the autogenerated stub
  119. }
  120. /**
  121. * 删除
  122. * @return array
  123. */
  124. public function delete()
  125. {
  126. $id = request()->post('id');
  127. if (!$id) {
  128. return ['code' => 1, 'msg' => '参数错误'];
  129. }
  130. // 支持批量删除
  131. if (is_array($id)) {
  132. $result = $this->model->whereIn('id', $id)->update(['mark' => 0]);
  133. } else {
  134. $result = $this->model->where('id', $id)->update(['mark' => 0]);
  135. }
  136. if ($result) {
  137. // 设置日志标题
  138. ActionLogModel::setTitle("删除文章内容");
  139. ActionLogModel::record();
  140. return ['code' => 0, 'msg' => '删除成功'];
  141. }
  142. return ['code' => 1, 'msg' => '删除失败'];
  143. }
  144. }