Goods.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. namespace app\controller\merchant;
  3. use app\extra\basic\Base;
  4. use app\extra\dyLife\data\BaseData;
  5. use app\extra\tools\CodeExtend;
  6. use app\middleware\AuthMiddleware;
  7. use app\model\saas\SaasGoods;
  8. use app\model\saas\SaasGoodsSku;
  9. use app\model\saas\SaasStore;
  10. use app\service\saas\GoodsService;
  11. use DI\Attribute\Inject;
  12. use LinFly\Annotation\Attributes\Route\Controller;
  13. use LinFly\Annotation\Attributes\Route\GetMapping;
  14. use LinFly\Annotation\Attributes\Route\Middleware;
  15. use LinFly\Annotation\Attributes\Route\PostMapping;
  16. use support\Request;
  17. use support\Response;
  18. #[Controller("/api/merchant/goods"),Middleware(AuthMiddleware::class)]
  19. class Goods extends Base
  20. {
  21. #[Inject]
  22. protected GoodsService $service;
  23. #[Inject]
  24. protected SaasGoods $model;
  25. #[Inject]
  26. protected SaasGoodsSku $skuModel;
  27. #[Inject]
  28. protected SaasStore $store;
  29. #[GetMapping('list')]
  30. public function getDataList(Request $request): Response
  31. {
  32. try {
  33. $param = $request->all();
  34. $param['poi_id'] = $request->user['store_id'];
  35. $data = $this->service->setModel()->getList($request->all());
  36. return successTrans(100010,pageFormat($data),200);
  37. } catch (\Throwable $th) {
  38. return error($th->getMessage());
  39. }
  40. }
  41. /**
  42. * 商品详情
  43. * @param Request $request
  44. * @return Response
  45. */
  46. #[GetMapping('detail')]
  47. public function getGoodDetail(Request $request): Response
  48. {
  49. try {
  50. $param = $this->_valid([
  51. "id.require" => trans("empty.require"),
  52. "product_id.require" => trans("empty.require"),
  53. ],$request->method());
  54. if (!is_array($param)) return error($param);
  55. $detail = $this->model->where("id",$param["id"])->with(['skuSpecs'])->findOrEmpty();
  56. if ($detail->isEmpty()) return errorTrans("empty.data");
  57. if ($detail['poi_id'] <> $request->user['store_id']) return errorTrans("error.param");
  58. $detail['sold_start_times'] = [(string)$detail['sold_start_time'],(string)$detail['sold_end_time']];
  59. $detail['use_times'] = [(string)$detail['use_time_start'],(string)$detail['use_time_end']];
  60. return successTrans('success.data',$detail->toArray());
  61. } catch (\Throwable $throwable) {
  62. return error($throwable->getMessage());
  63. }
  64. }
  65. #[PostMapping("template")]
  66. public function getTemplate(Request $request): Response
  67. {
  68. try {
  69. $param = $this->_valid([
  70. "product_type.require" => trans("empty.require"),
  71. "category.require" => trans("empty.require"),
  72. ],$request->method());
  73. if (!is_array($param)) return error($param);
  74. $data = (new BaseData)->config([
  75. "appid" => sConf("wechat.appid"),
  76. "secret" => sConf("wechat.secret"),
  77. "account" => sConf("wechat.shop_id"),
  78. ])->token()->getStoreCategoryTemplate($param['category'][2],$param['product_type']);
  79. return success("ok",$data);
  80. } catch (\Throwable $throwable) {
  81. return error($throwable->getMessage());
  82. }
  83. }
  84. #[GetMapping('type')]
  85. public function getProductType(Request $request): Response
  86. {
  87. try {
  88. $poiId = $request->user['store_id'];
  89. $productType = $this->store->where("poi_id", $poiId)->value("product_type");
  90. if (empty($productType)) return error("未配置");
  91. $typeData = [];
  92. foreach ($this->service->productType() as $key => $value) {
  93. if (in_array($value['key'],json_decode($productType,true))) {
  94. $typeData[] = $value;
  95. }
  96. }
  97. return success('ok', $typeData);
  98. } catch (\Throwable $th) {
  99. return error($th->getMessage());
  100. }
  101. }
  102. /**
  103. * 新增商品
  104. * @param Request $request
  105. * @return Response
  106. */
  107. #[PostMapping("save")]
  108. public function saveGoods(Request $request): Response
  109. {
  110. try {
  111. $param = $request->post();
  112. if (empty($param['auto_renew'])) $param['auto_renew'] = 0;
  113. $param['image_list'] = empty($param['image_list']) ? [] : json_encode($param['image_list']);
  114. $param['detail_image_list'] = empty($param['detail_image_list']) ? [] : json_encode($param['detail_image_list']);
  115. $param['environment_image_list'] = empty($param['environment_image_list']) ? [] : json_encode($param['environment_image_list']);
  116. if (is_array($param['specs'])) $param['specs'] = json_encode($param['specs']);
  117. if (is_array($param['category'])) {
  118. $param['category_id'] = $param['category'][2]??0;
  119. $param['category'] = json_encode($param['category']);
  120. }
  121. if (!isset($param['id'])) {
  122. $param['product_id'] = strtoupper(CodeExtend::random(18,3));
  123. }
  124. $param['price'] = $param['price']*100;
  125. $param['poi_id'] = $request->user['store_id'];
  126. $skuData = [];
  127. if (is_array($param['skuSpecs'])) {
  128. foreach ($param['skuSpecs'] as $key => $value) {
  129. $skuData[$key] = $value;
  130. $skuData[$key]['product_id'] = $param['product_id'];
  131. $skuData[$key]['specs'] = json_encode($value['specs']);
  132. }
  133. }
  134. unset($param['skuSpecs']);
  135. $param['line_price'] = $param['line_price'] * 100;
  136. if (!empty($skuData)) {
  137. [$state,$msg] = $this->skuData($skuData);
  138. if (!$state) return error($msg);
  139. }
  140. $state = $this->model->setAutoData($param);
  141. if (!$state) return errorTrans("error.data");
  142. return successTrans("success.data");
  143. } catch (\Throwable $throwable) {
  144. echo $throwable->getLine()."\n";
  145. return error($throwable->getMessage());
  146. }
  147. }
  148. /**
  149. * SKU编辑
  150. * @param array $skuData
  151. * @return array
  152. */
  153. protected function skuData(array $skuData = []): array
  154. {
  155. try {
  156. if (empty($skuData)) return [0,trans('empty.require')];
  157. foreach ($skuData as $vo) {
  158. $sku = $this->skuModel->where("sku_id",$vo['sku_id'])->findOrEmpty();
  159. if ($sku->isEmpty()) {
  160. $sku->insertGetId($vo);
  161. } else {
  162. $sku->update($vo);
  163. }
  164. }
  165. return [1,'success'];
  166. } catch (\Throwable $throwable) {
  167. return [0,$throwable->getMessage()];
  168. }
  169. }
  170. }