HealthyClassroomController.php 6.2 KB

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