SubjectController.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. $sliders[] = getenv('STORAGE_DOMAIN') . $slider['url'];
  77. $originSliders[] = getenv('STORAGE_DOMAIN').str_replace('/thumb','',$slider['url']);
  78. }
  79. $item->sliders = $sliders;
  80. $item->originSliders = $originSliders;
  81. }
  82. }
  83. return $item;
  84. }
  85. public function insertInput(Request $request): array
  86. {
  87. $params = $request->post();
  88. $data = $this->inputFilter($request->post());
  89. $contentConfigJson = [];
  90. if (!empty($params['sliders'])) {
  91. $sliders = explode(',', $params['sliders']);
  92. foreach ($sliders as $slider) {
  93. $contentConfigJson['slider'][] = ['url' => str_replace(getenv('STORAGE_DOMAIN'), '', $slider)];
  94. }
  95. }
  96. if (!empty($params['cate'])) {
  97. $contentConfigJson['cate'] = $params['cate'];
  98. }
  99. if (!empty($params['goods'])) {
  100. $contentConfigJson['goods'] = $params['goods'];
  101. }
  102. $data['content_config_json'] = json_encode($contentConfigJson);
  103. $data['creator_user_id'] = JwtToken::getCurrentId();
  104. $data['join_content_category_id'] = 207;
  105. return $data;
  106. }
  107. protected function updateInput(Request $request): array
  108. {
  109. $params = $request->post();
  110. $primary_key = $this->model->getKeyName();
  111. $id = $request->post($primary_key);
  112. $data = $this->inputFilter($request->post());
  113. $model = $this->model->find($id);
  114. if (!$model) {
  115. throw new BusinessException('记录不存在', 2);
  116. }
  117. $contentConfigJson = [];
  118. if (!empty($model->content_config_json)){
  119. $contentConfigJson = json_decode($model->content_config_json,true);
  120. }
  121. if (!empty($params['sliders'])) {
  122. $contentConfigJson['slider'] = [];
  123. $sliders = explode(',', $params['sliders']);
  124. foreach ($sliders as $slider) {
  125. $contentConfigJson['slider'][] = ['url' => str_replace(getenv('STORAGE_DOMAIN'), '', $slider)];
  126. }
  127. }
  128. if (!empty($params['cate'])) {
  129. $contentConfigJson['cate'] = $params['cate'];
  130. }
  131. if (!empty($params['goods'])) {
  132. $contentConfigJson['goods'] = $params['goods'];
  133. }
  134. $data['content_config_json'] = json_encode($contentConfigJson);
  135. $data['content_updatetimes'] = time();
  136. unset($data[$primary_key]);
  137. return [$id, $data];
  138. }
  139. }