HealthyClassroomController.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. namespace app\admin\controller\sys_manage;
  3. use app\admin\validate\sys_manage\AdvValidate;
  4. use app\admin\validate\sys_manage\ArticleValidate;
  5. use app\admin\validate\sys_manage\ContentValidate;
  6. use app\controller\Curd;
  7. use app\model\Adv;
  8. use app\model\Article;
  9. use app\model\Content;
  10. use support\exception\BusinessException;
  11. use support\Request;
  12. use support\Response;
  13. class HealthyClassroomController extends Curd
  14. {
  15. public function __construct()
  16. {
  17. $this->model = new Content();
  18. $this->validate = true;
  19. $this->validateClass = new ContentValidate();
  20. }
  21. /** 列表
  22. * @Desc
  23. * @Author Gorden
  24. * @Date 2024/3/5 10:00
  25. *
  26. * @param Request $request
  27. * @return Response
  28. * @throws \support\exception\BusinessException
  29. */
  30. public function select(Request $request): Response
  31. {
  32. [$where, $format, $limit, $field, $order] = $this->selectInput($request);
  33. $where['content_category'] = 'CLASSROOM';
  34. $order = $request->get('order', 'desc');
  35. $field = $field ?? 'content_addtimes';
  36. $query = $this->doSelect($where, $field, $order);
  37. return $this->doFormat($query, $format, $limit);
  38. }
  39. protected function doSelect(array $where, string $field = null, string $order = 'desc')
  40. {
  41. $model = $this->model->with('category');
  42. foreach ($where as $column => $value) {
  43. if (is_array($value)) {
  44. if ($value[0] === 'like' || $value[0] === 'not like') {
  45. $model = $model->where($column, $value[0], "%$value[1]%");
  46. } elseif (in_array($value[0], ['>', '=', '<', '<>'])) {
  47. $model = $model->where($column, $value[0], $value[1]);
  48. } elseif ($value[0] == 'in' && !empty($value[1])) {
  49. $valArr = $value[1];
  50. if (is_string($value[1])) {
  51. $valArr = explode(",", trim($value[1]));
  52. }
  53. $model = $model->whereIn($column, $valArr);
  54. } elseif ($value[0] == 'not in' && !empty($value[1])) {
  55. $valArr = $value[1];
  56. if (is_string($value[1])) {
  57. $valArr = explode(",", trim($value[1]));
  58. }
  59. $model = $model->whereNotIn($column, $valArr);
  60. } elseif ($value[0] == 'null') {
  61. $model = $model->whereNull($column);
  62. } elseif ($value[0] == 'not null') {
  63. $model = $model->whereNotNull($column);
  64. } elseif ($value[0] !== '' || $value[1] !== '') {
  65. $model = $model->whereBetween($column, $value);
  66. }
  67. } else {
  68. $model = $model->where($column, $value);
  69. }
  70. }
  71. if ($field) {
  72. $model = $model->orderBy($field, $order);
  73. }
  74. return $model;
  75. }
  76. public function afterQuery($items)
  77. {
  78. foreach ($items as &$item) {
  79. if (!empty($item->content_title_prefix)) {
  80. $item->content_title_prefix = ltrim($item->content_title_prefix, '【');
  81. $item->content_title_prefix = rtrim($item->content_title_prefix, '】');
  82. }
  83. if (!empty($item->content_config_json)) {
  84. $configJson = json_decode($item->content_config_json, true);
  85. foreach ($configJson as $key => $item1) {
  86. if (substr($item1, 0, 4) != 'http') {
  87. $configJson[$key] = getenv('STORAGE_DOMAIN') . $item1;
  88. }
  89. }
  90. $item->content_config_json = $configJson;
  91. }
  92. }
  93. return $items;
  94. }
  95. public function insert(Request $request): Response
  96. {
  97. if ($this->validate && !$this->validateClass->scene('add')->check($request->post())) {
  98. return json_fail($this->validateClass->getError());
  99. }
  100. try {
  101. $data = $this->insertInput($request);
  102. $this->doInsert($data);
  103. } catch (BusinessException $customException) {
  104. return json_fail($customException->getMessage());
  105. } catch (\Exception $e) {
  106. dump($e->getMessage());
  107. return json_fail('数据写入失败11');
  108. }
  109. return json_success('success');
  110. }
  111. /**
  112. * @Desc
  113. * @Author Gorden
  114. * @Date 2024/3/27 10:24
  115. *
  116. * @param Request $request
  117. * @return array
  118. * @throws \support\exception\BusinessException
  119. */
  120. protected function insertInput(Request $request): array
  121. {
  122. $params = $request->post();
  123. if (!empty($params['content_config_json'])){
  124. foreach ($params['content_config_json'] as $key => $item){
  125. $params['content_config_json'][$key] = str_replace(getenv("STORAGE_DOMAIN"),'',$item);
  126. }
  127. $params['content_config_json'] = json_encode($params['content_config_json'], JSON_UNESCAPED_UNICODE);
  128. }
  129. $data = $this->inputFilter($params);
  130. $data['content_title_prefix'] = '【'.$data['content_title_prefix'].'】';
  131. $data['content_category'] = 'CLASSROOM';
  132. return $data;
  133. }
  134. protected function updateInput(Request $request): array
  135. {
  136. $primary_key = $this->model->getKeyName();
  137. $id = $request->post($primary_key);
  138. $params = $request->post();
  139. if (!empty($params['content_config_json'])){
  140. foreach ($params['content_config_json'] as $key => $item){
  141. $params['content_config_json'][$key] = str_replace(getenv("STORAGE_DOMAIN"),'',$item);
  142. }
  143. $params['content_config_json'] = json_encode($params['content_config_json'], JSON_UNESCAPED_UNICODE);
  144. }
  145. $data = $this->inputFilter($params);
  146. $data['content_title_prefix'] = '【'.$data['content_title_prefix'].'】';
  147. $model = $this->model->find($id);
  148. if (!$model) {
  149. throw new BusinessException('记录不存在', 2);
  150. }
  151. unset($data[$primary_key]);
  152. return [$id, $data];
  153. }
  154. }