Goods.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. if (!empty($param['status'])) $param['status'] = intval($param['status']+1);
  36. $productType = $this->service->productType();
  37. $data = $this->service->setModel()->getList($param,null,true,['types'],['types' => function($query,$resp) use($productType){
  38. $productTypeArr = [];
  39. foreach ($productType as $val) {
  40. $productTypeArr[$val['key']] = $val['name'];
  41. }
  42. return $productTypeArr[$resp['product_type']]??'';
  43. }]);
  44. return successTrans(100010,pageFormat($data),200);
  45. } catch (\Throwable $th) {
  46. return error($th->getMessage());
  47. }
  48. }
  49. /**
  50. * 商品详情
  51. * @param Request $request
  52. * @return Response
  53. */
  54. #[GetMapping('detail')]
  55. public function getGoodDetail(Request $request): Response
  56. {
  57. try {
  58. $param = $this->_valid([
  59. "id.require" => trans("empty.require"),
  60. "product_id.require" => trans("empty.require"),
  61. ],$request->method());
  62. if (!is_array($param)) return error($param);
  63. $detail = $this->model->where("id",$param["id"])->with(['skuSpecs'])->findOrEmpty();
  64. if ($detail->isEmpty()) return errorTrans("empty.data");
  65. if ($detail['poi_id'] <> $request->user['store_id']) return errorTrans("error.param");
  66. $detail['sold_start_times'] = [(string)$detail['sold_start_time'],(string)$detail['sold_end_time']];
  67. $detail['use_times'] = [(string)$detail['use_time_start'],(string)$detail['use_time_end']];
  68. return successTrans('success.data',$detail->toArray());
  69. } catch (\Throwable $throwable) {
  70. return error($throwable->getMessage());
  71. }
  72. }
  73. #[PostMapping("template")]
  74. public function getTemplate(Request $request): Response
  75. {
  76. try {
  77. $param = $this->_valid([
  78. "product_type.require" => trans("empty.require"),
  79. "category.require" => trans("empty.require"),
  80. ],$request->method());
  81. if (!is_array($param)) return error($param);
  82. $data = (new BaseData)->config([
  83. "appid" => sConf("wechat.appid"),
  84. "secret" => sConf("wechat.secret"),
  85. "account" => sConf("wechat.shop_id"),
  86. ])->token()->getStoreCategoryTemplate($param['category'][2],$param['product_type']);
  87. return success("ok",$data);
  88. } catch (\Throwable $throwable) {
  89. return error($throwable->getMessage());
  90. }
  91. }
  92. #[GetMapping('type')]
  93. public function getProductType(Request $request): Response
  94. {
  95. try {
  96. $poiId = $request->user['store_id'];
  97. $productType = $this->store->where("poi_id", $poiId)->value("product_type");
  98. if (empty($productType)) return error("未配置");
  99. $typeData = [];
  100. foreach ($this->service->productType() as $key => $value) {
  101. if (in_array($value['key'],json_decode($productType,true))) {
  102. $typeData[] = $value;
  103. }
  104. }
  105. return success('ok', $typeData);
  106. } catch (\Throwable $th) {
  107. return error($th->getMessage());
  108. }
  109. }
  110. /**
  111. * 新增商品
  112. * @param Request $request
  113. * @return Response
  114. */
  115. #[PostMapping("save")]
  116. public function saveGoods(Request $request): Response
  117. {
  118. try {
  119. $param = $request->post();
  120. if (empty($param['auto_renew'])) $param['auto_renew'] = 0;
  121. $param['image_list'] = empty($param['image_list']) ? [] : json_encode($param['image_list']);
  122. $param['detail_image_list'] = empty($param['detail_image_list']) ? [] : json_encode($param['detail_image_list']);
  123. $param['environment_image_list'] = empty($param['environment_image_list']) ? [] : json_encode($param['environment_image_list']);
  124. $param['notification'] = empty($param['notification']) ? [] : json_encode($param['notification']);
  125. if (empty($param['specs'])) return error("至少添加一组搭配信息");
  126. if (is_array($param['specs'])) $param['specs'] = json_encode($param['specs']);
  127. if (is_array($param['category'])) {
  128. $param['category_id'] = $param['category'][2]??0;
  129. $param['category'] = json_encode($param['category']);
  130. }
  131. if (!isset($param['id'])) {
  132. $param['product_id'] = strtoupper(CodeExtend::random(18,3));
  133. }
  134. $param['price'] = $param['price']*100;
  135. $param['poi_id'] = $request->user['store_id'];
  136. if (!empty($param['no_use_weeks']) && count($param['no_use_weeks']) == 7) return error("至少需要保留一天可用");
  137. $skuData = [];
  138. if (is_array($param['skuSpecs'])) {
  139. foreach ($param['skuSpecs'] as $key => $value) {
  140. $skuData[$key] = $value;
  141. $skuData[$key]['product_id'] = $param['product_id'];
  142. $skuData[$key]['specs'] = json_encode($value['specs']);
  143. }
  144. }
  145. $param['spu_id'] = CodeExtend::uniqidDate(18);
  146. unset($param['skuSpecs']);
  147. $param['line_price'] = $param['line_price'] * 100;
  148. if (empty($param['notification'])) return error("请最少添加一组添加使用规则");
  149. foreach (json_decode($param['notification'],true) as $key => $value) {
  150. if (empty($value['title'])) return error("规则标题不能为空");
  151. if (empty($value['content'])) return error("规则内容不能为空");
  152. }
  153. $store = (new SaasStore)->where("poi_id",$request->user['store_id'])->findOrEmpty();
  154. if ($store->isEmpty()) return errorTrans("error.data");
  155. // 获取attr_key_value_map参数
  156. $valMap = (new BaseData)->config([
  157. "appid" => sConf("wechat.appid"),
  158. "secret" => sConf("wechat.secret"),
  159. "account" => sConf("wechat.shop_id"),
  160. ])->token()->getStoreCategoryTemplate($param['category_id'],$param['product_type']);
  161. if (empty($valMap['product_attrs'])) return errorTrans("error.data");
  162. $data = (new BaseData)->config([
  163. "appid" => sConf("wechat.mini_appid"),
  164. "secret" => sConf("wechat.mini_secret"),
  165. ])->token()->createGoodsData($param,$skuData,$store->toArray(),$valMap);
  166. // return errorTrans("error.data");
  167. if (empty($data['product_id'])) return error($data['description']);
  168. $param['goods_id'] = $data['product_id'];
  169. // return errorTrans("error.data");
  170. $stateSku = false;
  171. if (!empty($skuData)) {
  172. [$stateSku,$msg] = $this->skuData($skuData);
  173. if (!$stateSku) return error($msg);
  174. }
  175. $state = $this->model->setAutoData($param);
  176. if (!$state && !$stateSku) return errorTrans("error.data");
  177. return successTrans("success.data");
  178. } catch (\Throwable $throwable) {
  179. echo $throwable->getLine()."\n";
  180. echo $throwable->getFile()."\n";
  181. echo $throwable->getMessage()."\n";
  182. return error($throwable->getMessage());
  183. }
  184. }
  185. /**
  186. * SKU编辑
  187. * @param array $skuData
  188. * @return array
  189. */
  190. protected function skuData(array $skuData = []): array
  191. {
  192. try {
  193. if (empty($skuData)) return [0,trans('empty.require')];
  194. foreach ($skuData as $vo) {
  195. $sku = $this->skuModel->where("sku_id",$vo['sku_id'])->findOrEmpty();
  196. if ($sku->isEmpty()) {
  197. $sku->insertGetId($vo);
  198. } else {
  199. $sku->update($vo);
  200. }
  201. }
  202. return [1,'success'];
  203. } catch (\Throwable $throwable) {
  204. return [0,$throwable->getMessage()];
  205. }
  206. }
  207. /**
  208. * 商品下架
  209. * @param Request $request
  210. * @return Response
  211. */
  212. #[PostMapping("off")]
  213. public function offGood2Life(Request $request): Response
  214. {
  215. try {
  216. $param = $this->_valid([
  217. "id.require" => trans("empty.require"),
  218. ],$request->method());
  219. if (!is_array($param)) return error($param);
  220. $detail = $this->model->where("id",$param["id"])->with(['skuSpecs'])->findOrEmpty();
  221. if ($detail->isEmpty()) return errorTrans("empty.data");
  222. if ($detail['poi_id'] <> $request->user['store_id']) return errorTrans("error.param");
  223. $store = (new SaasStore)->where("poi_id",$request->user['store_id'])->findOrEmpty();
  224. if ($store->isEmpty()) return errorTrans("error.data");
  225. $data = (new BaseData)->config([
  226. "appid" => sConf("wechat.mini_appid"),
  227. "secret" => sConf("wechat.mini_secret"),
  228. ])->token()->goodsOffOn(2,$store['store_id'],$detail['goods_id']);
  229. if ($data['error_code'] <> 0) return error("操作失败,请稍后重试");
  230. $detail->status = 2;
  231. $detail->save();
  232. return successTrans("success.data");
  233. } catch (\Throwable $throwable) {
  234. return error($throwable->getMessage());
  235. }
  236. }
  237. }