SubjectController.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. namespace app\admin\controller\content;
  3. use app\controller\Curd;
  4. use app\model\Content;
  5. use app\model\Goods;
  6. use support\exception\BusinessException;
  7. use support\Request;
  8. use support\Response;
  9. use Tinywan\Jwt\JwtToken;
  10. class SubjectController extends Curd
  11. {
  12. public function __construct()
  13. {
  14. $this->model = new Content();
  15. }
  16. public function select(Request $request): Response
  17. {
  18. [$where, $format, $limit, $field, $order] = $this->selectInput($request);
  19. $where['content_category'] = 'SUBJECT';
  20. $field = 'content_addtimes';
  21. $order = 'DESC';
  22. $this->model = $this->model->with('user');
  23. $query = $this->doSelect($where, $field, $order);
  24. return $this->doFormat($query, $format, $limit);
  25. }
  26. /**
  27. * @Desc 详情
  28. * @Author Gorden
  29. * @Date 2024/11/8 13:24
  30. *
  31. * @param Request $request
  32. * @return Response
  33. */
  34. public function info(Request $request): Response
  35. {
  36. $primaryKey = $this->model->getKeyName();
  37. $data = $this->model->with('user')->where($primaryKey, $request->get('id'))->first();
  38. if (method_exists($this, "afterInfoQuery")) {
  39. $data = call_user_func([$this, "afterInfoQuery"], $data);
  40. }
  41. return json_success('', $data);
  42. }
  43. public function afterInfoQuery($item)
  44. {
  45. if (!empty($item->content_config_json)) {
  46. $contentConfigJson = json_decode($item->content_config_json, true);
  47. if (!empty($contentConfigJson['cate'])) {
  48. $item->cate = $contentConfigJson['cate'];
  49. }
  50. if (!empty($contentConfigJson['goods'])) {
  51. $goodsIds = array_column($contentConfigJson['goods'], 'goods_id');
  52. $goodsList = Goods::whereIn('goods_id', $goodsIds)
  53. ->select('goods_cover', 'goods_id', 'goods_name', 'goods_market_price', 'goods_sales_price')
  54. ->get()
  55. ->toArray();
  56. $formGoods = [];
  57. foreach ($goodsList as $key => $goods) {
  58. foreach ($contentConfigJson['goods'] as $cateGoods) {
  59. if ($cateGoods['goods_id'] == $goods['goods_id']) {
  60. $goodsList[$key]['cate'] = $cateGoods['cate'];
  61. $formGoods[] = [
  62. 'cate' => $cateGoods['cate'],
  63. 'goods_id' => $cateGoods['goods_id'],
  64. ];
  65. }
  66. }
  67. $goodsList[$key]['goods_cover'] = getenv('STORAGE_DOMAIN') . $goods['goods_cover'];
  68. }
  69. $item->goods = $formGoods;
  70. $item->goodsList = $goodsList;
  71. }
  72. if (!empty($contentConfigJson['slider'])) {
  73. $sliders = [];
  74. $originSliders = [];
  75. foreach ($contentConfigJson['slider'] as $slider) {
  76. if (!empty($slider['url'])) {
  77. $sliders[] = [
  78. 'url' => getenv('STORAGE_DOMAIN') . $slider['url'],
  79. 'href' => $slider['href'] ?? ''
  80. ];
  81. $originSliders[] = getenv('STORAGE_DOMAIN') . str_replace('/thumb', '', $slider['url']);
  82. }
  83. }
  84. $item->sliders = $sliders;
  85. $item->originSliders = $originSliders;
  86. }
  87. }
  88. return $item;
  89. }
  90. public function insertInput(Request $request): array
  91. {
  92. $params = $request->post();
  93. $data = $this->inputFilter($request->post());
  94. $contentConfigJson = [];
  95. if (!empty($params['sliders'])) {
  96. $contentConfigJson['slider'] = [];
  97. foreach ($params['sliders'] as $slider) {
  98. $contentConfigJson['slider'][] = [
  99. 'url' => str_replace(getenv('STORAGE_DOMAIN'), '', $slider['url']),
  100. 'href' => $slider['href']
  101. ];
  102. }
  103. }
  104. if (!empty($params['cate'])) {
  105. $contentConfigJson['cate'] = $params['cate'];
  106. }
  107. if (!empty($params['goods'])) {
  108. $contentConfigJson['goods'] = $params['goods'];
  109. }
  110. $data['content_config_json'] = json_encode($contentConfigJson);
  111. $data['creator_user_id'] = JwtToken::getCurrentId();
  112. $data['join_content_category_id'] = 207;
  113. return $data;
  114. }
  115. protected function updateInput(Request $request): array
  116. {
  117. $params = $request->post();
  118. $primary_key = $this->model->getKeyName();
  119. $id = $request->post($primary_key);
  120. $data = $this->inputFilter($request->post());
  121. $model = $this->model->find($id);
  122. if (!$model) {
  123. throw new BusinessException('记录不存在', 2);
  124. }
  125. $contentConfigJson = [];
  126. if (!empty($model->content_config_json)) {
  127. $contentConfigJson = json_decode($model->content_config_json, true);
  128. }
  129. if (!empty($params['sliders'])) {
  130. $contentConfigJson['slider'] = [];
  131. foreach ($params['sliders'] as $slider) {
  132. $contentConfigJson['slider'][] = [
  133. 'url' => str_replace(getenv('STORAGE_DOMAIN'), '', $slider['url']),
  134. 'href' => $slider['href']
  135. ];
  136. }
  137. }else{
  138. unset($contentConfigJson['slider']);
  139. }
  140. if (!empty($params['cate'])) {
  141. $contentConfigJson['cate'] = $params['cate'];
  142. }
  143. if (!empty($params['goods'])) {
  144. $contentConfigJson['goods'] = $params['goods'];
  145. }
  146. $data['content_config_json'] = json_encode($contentConfigJson);
  147. $data['content_updatetimes'] = time();
  148. unset($data[$primary_key]);
  149. return [$id, $data];
  150. }
  151. }