ArticleService.php 5.4 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. $showType = isset($params['show_type']) ? $params['show_type'] : 1; // 1:协议 2:公告
  56. if ($status > 0) {
  57. $where['a.status'] = $status;
  58. }
  59. $list = $this->model->from('article as a')
  60. ->where($where)
  61. ->where(function ($query) use ($type, $showType) {
  62. // 根据show_type区分协议和公告
  63. if ($showType == 1) {
  64. // 协议类型:1-5
  65. $query->whereIn('a.type', [1, 2, 3, 4, 5]);
  66. } else if ($showType == 2) {
  67. // 公告类型:固定为6
  68. $query->where('a.type', 6);
  69. }
  70. // 如果指定了具体类型,则进一步筛选(仅用于协议)
  71. if ($showType == 1 && $type && is_array($type)) {
  72. $query->whereIn('a.type', $type);
  73. } else if ($showType == 1 && $type) {
  74. $query->where('a.type', $type);
  75. }
  76. })
  77. ->where(function ($query) use ($params) {
  78. $keyword = isset($params['keyword']) ? $params['keyword'] : '';
  79. if ($keyword) {
  80. $query->where('a.title', 'like', "%{$keyword}%")->orWhere('a.tags', 'like', "%{$keyword}%");
  81. }
  82. })
  83. ->select(['a.*'])
  84. ->orderBy('a.create_time', 'desc')
  85. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  86. $list = $list ? $list->toArray() : [];
  87. if ($list) {
  88. $typrArr = [
  89. '',
  90. '商家入驻协议',
  91. '代理申请协议',
  92. '提现协议',
  93. '提现说明',
  94. '充值说明',
  95. '公告'
  96. ];
  97. foreach ($list['data'] as &$item) {
  98. $item['create_time'] = $item['create_time'] ? datetime($item['create_time'], 'Y-m-d H.i.s') : '';
  99. $item['cover'] = $item['cover'] ? get_image_url($item['cover']) : '';
  100. $item['content'] = $item['content'] ? get_format_content($item['content']) : '';
  101. $item['type_name'] = isset($typrArr[$item['type']]) ? $typrArr[$item['type']] : '其他';
  102. }
  103. }
  104. return [
  105. 'pageSize' => $pageSize,
  106. 'total' => isset($list['total']) ? $list['total'] : 0,
  107. 'list' => isset($list['data']) ? $list['data'] : []
  108. ];
  109. }
  110. /**
  111. * 添加或编辑
  112. * @return array
  113. * @since 2020/11/11
  114. * @author laravel开发员
  115. */
  116. public function edit()
  117. {
  118. $data = request()->all();
  119. $content = isset($data['content']) ? $data['content'] : '';
  120. if ($content) {
  121. $data['content'] = set_format_content($content);
  122. } else {
  123. // 确保content字段存在(即使为空字符串)
  124. $data['content'] = '';
  125. }
  126. // 设置日志标题
  127. ActionLogModel::setTitle("发布文章或客服咨询信息");
  128. ActionLogModel::record();
  129. return parent::edit($data); // TODO: Change the autogenerated stub
  130. }
  131. /**
  132. * 删除
  133. * @return array
  134. */
  135. public function delete()
  136. {
  137. $id = request()->post('id');
  138. if (!$id) {
  139. return ['code' => 1, 'msg' => '参数错误'];
  140. }
  141. // 支持批量删除
  142. if (is_array($id)) {
  143. $result = $this->model->whereIn('id', $id)->update(['mark' => 0]);
  144. } else {
  145. $result = $this->model->where('id', $id)->update(['mark' => 0]);
  146. }
  147. if ($result) {
  148. // 设置日志标题
  149. ActionLogModel::setTitle("删除协议内容");
  150. ActionLogModel::record();
  151. return ['code' => 0, 'msg' => '删除成功'];
  152. }
  153. return ['code' => 1, 'msg' => '删除失败'];
  154. }
  155. }