HealthyClassroomController.php 6.0 KB

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