ArticleService.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. if ($status > 0) {
  58. $where['a.status'] = $status;
  59. }
  60. $list = $this->model->from('article as a')
  61. ->where($where)
  62. ->where(function ($query) use ($type) {
  63. if ($type && is_array($type)) {
  64. $query->whereIn('a.type', $type);
  65. } else if ($type) {
  66. $query->where('a.type', $type);
  67. }
  68. })
  69. ->where(function ($query) use ($params) {
  70. $keyword = isset($params['keyword']) ? $params['keyword'] : '';
  71. if ($keyword) {
  72. $query->where('a.title', 'like', "%{$keyword}%")->orWhere('a.tags', 'like', "%{$keyword}%");
  73. }
  74. })
  75. ->select(['a.*'])
  76. ->orderBy('a.create_time', 'desc')
  77. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  78. $list = $list ? $list->toArray() : [];
  79. if ($list) {
  80. foreach ($list['data'] as &$item) {
  81. $item['create_time'] = $item['create_time'] ? datetime($item['create_time'], 'Y-m-d H.i.s') : '';
  82. $item['cover'] = $item['cover'] ? get_image_url($item['cover']) : '';
  83. $item['content'] = $item['content'] ? get_format_content($item['content']) : '';
  84. }
  85. }
  86. return [
  87. 'pageSize' => $pageSize,
  88. 'total' => isset($list['total']) ? $list['total'] : 0,
  89. 'list' => isset($list['data']) ? $list['data'] : []
  90. ];
  91. }
  92. /**
  93. * 添加或编辑
  94. * @return array
  95. * @since 2020/11/11
  96. * @author laravel开发员
  97. */
  98. public function edit()
  99. {
  100. $data = request()->all();
  101. $content = isset($data['content']) ? $data['content'] : '';
  102. if ($content) {
  103. $data['content'] = set_format_content($content);
  104. }
  105. // 设置日志标题
  106. ActionLogModel::setTitle("发布文章或客服咨询信息");
  107. ActionLogModel::record();
  108. return parent::edit($data); // TODO: Change the autogenerated stub
  109. }
  110. public function importArticles($data)
  111. {
  112. DB::beginTransaction();
  113. try {
  114. foreach ($data as $item) {
  115. if (empty($item['title']) || empty($item['content'])) {
  116. throw new Exception("Title or content missing in import data");
  117. }
  118. DB::table('article')->insert([
  119. 'title' => $item['title'],
  120. 'content' => $item['content'],
  121. 'create_time' => time(),
  122. 'update_time' => time(),
  123. 'status' => 1,
  124. 'type' => 9,
  125. 'mark' => 1,
  126. ]);
  127. }
  128. DB::commit();
  129. return ['code' => 0, 'msg' => 'Import successful'];
  130. } catch (\Exception $e) {
  131. DB::rollBack();
  132. return ['code' => 1, 'msg' => $e->getMessage()];
  133. }
  134. }
  135. }