AdvController.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. if (!empty($item->adv_config_json)) {
  98. $configJson = json_decode($item->adv_config_json, true);
  99. $item->adv_cover_type = 'cover';
  100. if (!empty($configJson['sliderList'])) {
  101. $item->adv_cover_type = 'slider';
  102. $sliderList = [];
  103. foreach ($configJson['sliderList'] as $slider) {
  104. $sliderList[] = [
  105. 'cover' => getenv('STORAGE_DOMAIN') . $slider['url'],
  106. 'href' => $slider['href']
  107. ];
  108. }
  109. $item->slider_list = $sliderList;
  110. }
  111. if (!empty($configJson['cover'])) {
  112. $item->cover = getenv('STORAGE_DOMAIN') . $configJson['cover'];
  113. }
  114. }
  115. }
  116. return $items;
  117. }
  118. /**
  119. * @Desc
  120. * @Author Gorden
  121. * @Date 2024/3/27 10:24
  122. *
  123. * @param Request $request
  124. * @return array
  125. * @throws \support\exception\BusinessException
  126. */
  127. protected function insertInput(Request $request): array
  128. {
  129. $type = $request->post('adv_type', 0);
  130. $advMain = $request->post('adv_main', '');
  131. $cover = $request->post('cover', '');
  132. $advCoverType = $request->post('adv_cover_type');
  133. $sliderList = $request->post('slider_list');
  134. $data = $this->inputFilter($request->post());
  135. $mediaJson = [];
  136. if (!empty($data['adv_media'])) {
  137. $mediaJson = ['type' => $data['adv_media_json']];
  138. }
  139. if ($type != 0) {
  140. $mediaJson['advType'] = $type;
  141. }
  142. if ($type == 3) {
  143. $mediaJson['advMain'] = $advMain;
  144. }
  145. if (!empty($cover)) {
  146. $mediaJson['cover'] = str_replace(getenv("STORAGE_DOMAIN"), '', $cover);
  147. }
  148. $data['adv_media_json'] = json_encode($mediaJson);
  149. $configJson = [];
  150. // 轮播
  151. if ($advCoverType == 'slider') {
  152. $data['adv_href'] = '';
  153. foreach ($sliderList as $slider) {
  154. $configJson['sliderList'][] = [
  155. 'url' => str_replace(getenv('STORAGE_DOMAIN'), '', $slider['cover']),
  156. 'href' => $slider['href']
  157. ];
  158. }
  159. } elseif ($advCoverType == 'cover') {
  160. $configJson['cover'] = str_replace(getenv('STORAGE_DOMAIN'), '', $cover);
  161. }
  162. $data['adv_config_json'] = json_encode($configJson);
  163. return $data;
  164. }
  165. protected function updateInput(Request $request): array
  166. {
  167. $type = $request->post('adv_type', 0);
  168. $advMain = $request->post('adv_main', '');
  169. $cover = $request->post('cover', '');
  170. $advCoverType = $request->post('adv_cover_type');
  171. $sliderList = $request->post('slider_list');
  172. $primary_key = $this->model->getKeyName();
  173. $id = $request->post($primary_key);
  174. $data = $this->inputFilter($request->post());
  175. $model = $this->model->find($id);
  176. $configJson = [];
  177. // if (!empty($model->adv_config_json)) {
  178. // $configJson = json_decode($model->adv_config_json, true);
  179. // }
  180. $mediaJson = [];
  181. if (!empty($model->adv_media_json)) {
  182. $mediaJson = json_decode($model->adv_media_json, true);
  183. $mediaJson['type'] = $data['adv_media_json'];
  184. }
  185. if (!empty($data['adv_media_json'])) {
  186. $mediaJson['type'] = $data['adv_media_json'];
  187. }
  188. if ($type != 0) {
  189. $mediaJson['advType'] = $type;
  190. }
  191. if ($type == 3) {
  192. $mediaJson['advMain'] = $advMain;
  193. }
  194. if (!empty($cover)) {
  195. $mediaJson['cover'] = str_replace(getenv("STORAGE_DOMAIN"), '', $cover);
  196. }
  197. $data['adv_media_json'] = json_encode($mediaJson);
  198. // 轮播
  199. if ($advCoverType == 'slider') {
  200. $data['adv_href'] = '';
  201. unset($configJson['cover']);
  202. foreach ($sliderList as $slider) {
  203. $configJson['sliderList'][] = [
  204. 'url' => str_replace(getenv('STORAGE_DOMAIN'), '', $slider['cover']),
  205. 'href' => $slider['href']
  206. ];
  207. }
  208. } elseif ($advCoverType == 'cover') {
  209. unset($configJson['sliderList']);
  210. $configJson['cover'] = str_replace(getenv('STORAGE_DOMAIN'), '', $cover);
  211. }
  212. $data['adv_config_json'] = json_encode($configJson);
  213. if (!$model) {
  214. throw new BusinessException('记录不存在', 2);
  215. }
  216. unset($data[$primary_key]);
  217. return [$id, $data];
  218. }
  219. }