HealthyClassroomController.php 6.1 KB

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