AdvController.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. namespace app\admin\controller\sys_manage;
  3. use app\admin\validate\sys_manage\AdvValidate;
  4. use app\controller\Curd;
  5. use app\model\Adv;
  6. use support\exception\BusinessException;
  7. use support\Request;
  8. use support\Response;
  9. class AdvController extends Curd
  10. {
  11. public function __construct()
  12. {
  13. $this->model = new Adv();
  14. $this->validate = true;
  15. $this->validateClass = new AdvValidate();
  16. }
  17. /** 列表
  18. * @Desc
  19. * @Author Gorden
  20. * @Date 2024/3/5 10:00
  21. *
  22. * @param Request $request
  23. * @return Response
  24. * @throws \support\exception\BusinessException
  25. */
  26. public function select(Request $request): Response
  27. {
  28. [$where, $format, $limit, $field, $order] = $this->selectInput($request);
  29. // $where['adv_category'] = 'adv';
  30. $order = $request->get('order', 'desc');
  31. $field = $field ?? 'adv_addtimes';
  32. $query = $this->doSelect($where, $field, $order);
  33. return $this->doFormat($query, $format, $limit);
  34. }
  35. protected function doSelect(array $where, string $field = null, string $order = 'desc')
  36. {
  37. $model = $this->model->with([
  38. 'category' => function ($query) {
  39. $query->select('category_id', 'category_name');
  40. }
  41. ]);
  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->adv_media_json)) {
  80. $mediaJson = json_decode($item->adv_media_json, true);
  81. if (isset($mediaJson['type'])) {
  82. $item->adv_media_json = $mediaJson['type'];
  83. }
  84. if (isset($mediaJson['advType'])) {
  85. $item->adv_type = $mediaJson['advType'];
  86. }
  87. if (isset($mediaJson['advMain'])) {
  88. $item->adv_main = $mediaJson['advMain'];
  89. }
  90. if (isset($mediaJson['cover'])) {
  91. $item->cover = getenv("STORAGE_DOMAIN") . $mediaJson['cover'];
  92. }
  93. if (empty($mediaJson)) {
  94. $item->adv_media_json = "";
  95. }
  96. }
  97. }
  98. return $items;
  99. }
  100. /**
  101. * @Desc
  102. * @Author Gorden
  103. * @Date 2024/3/27 10:24
  104. *
  105. * @param Request $request
  106. * @return array
  107. * @throws \support\exception\BusinessException
  108. */
  109. protected function insertInput(Request $request): array
  110. {
  111. $type = $request->post('adv_type', 0);
  112. $advMain = $request->post('adv_main', '');
  113. $cover = $request->post('cover', '');
  114. $data = $this->inputFilter($request->post());
  115. $mediaJson = [];
  116. if (!empty($data['adv_media'])) {
  117. $mediaJson = ['type' => $data['adv_media_json']];
  118. }
  119. if ($type != 0) {
  120. $mediaJson['advType'] = $type;
  121. }
  122. if ($type == 3) {
  123. $mediaJson['advMain'] = $advMain;
  124. }
  125. if (!empty($cover)) {
  126. $mediaJson['cover'] = str_replace(getenv("STORAGE_DOMAIN"), '', $cover);
  127. }
  128. $data['adv_media_json'] = json_encode($mediaJson);
  129. return $data;
  130. }
  131. protected function updateInput(Request $request): array
  132. {
  133. $type = $request->post('adv_type', 0);
  134. $advMain = $request->post('adv_main', '');
  135. $cover = $request->post('cover', '');
  136. $primary_key = $this->model->getKeyName();
  137. $id = $request->post($primary_key);
  138. $data = $this->inputFilter($request->post());
  139. $model = $this->model->find($id);
  140. $mediaJson = [];
  141. if (!empty($model->adv_media_json)) {
  142. $mediaJson = json_decode($model->adv_media_json, true);
  143. $mediaJson['type'] = $data['adv_media_json'];
  144. }
  145. if (!empty($data['adv_media_json'])) {
  146. $mediaJson['type'] = $data['adv_media_json'];
  147. }
  148. if ($type != 0) {
  149. $mediaJson['advType'] = $type;
  150. }
  151. if ($type == 3) {
  152. $mediaJson['advMain'] = $advMain;
  153. }
  154. if (!empty($cover)) {
  155. $mediaJson['cover'] = str_replace(getenv("STORAGE_DOMAIN"), '', $cover);
  156. }
  157. $data['adv_media_json'] = json_encode($mediaJson);
  158. if (!$model) {
  159. throw new BusinessException('记录不存在', 2);
  160. }
  161. unset($data[$primary_key]);
  162. return [$id, $data];
  163. }
  164. }