Goods.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. if (isset($param['id'])) {
  146. $param['spu_id'] = CodeExtend::uniqidDate(18);
  147. }
  148. unset($param['skuSpecs']);
  149. $param['line_price'] = $param['line_price'] * 100;
  150. if (empty($param['notification'])) return error("请最少添加一组添加使用规则");
  151. foreach (json_decode($param['notification'],true) as $key => $value) {
  152. if (empty($value['title'])) return error("规则标题不能为空");
  153. if (empty($value['content'])) return error("规则内容不能为空");
  154. }
  155. $store = (new SaasStore)->where("poi_id",$request->user['store_id'])->findOrEmpty();
  156. if ($store->isEmpty()) return errorTrans("error.data");
  157. // 获取attr_key_value_map参数
  158. $valMap = (new BaseData)->config([
  159. "appid" => sConf("wechat.appid"),
  160. "secret" => sConf("wechat.secret"),
  161. "account" => sConf("wechat.shop_id"),
  162. ])->token()->getStoreCategoryTemplate($param['category_id'],$param['product_type']);
  163. if (empty($valMap['product_attrs'])) return errorTrans("error.data");
  164. $data = (new BaseData)->config([
  165. "appid" => sConf("wechat.mini_appid"),
  166. "secret" => sConf("wechat.mini_secret"),
  167. ])->token()->createGoodsData($param,$skuData,$store->toArray(),$valMap);
  168. // return errorTrans("error.data");
  169. if (empty($data['product_id'])) return error($data['description']);
  170. $param['goods_id'] = $data['product_id'];
  171. // return errorTrans("error.data");
  172. $stateSku = false;
  173. if (!empty($skuData)) {
  174. [$stateSku,$msg] = $this->skuData($skuData);
  175. if (!$stateSku) return error($msg);
  176. }
  177. $state = $this->model->setAutoData($param);
  178. if (!$state && !$stateSku) return errorTrans("error.data");
  179. return successTrans("success.data");
  180. } catch (\Throwable $throwable) {
  181. echo $throwable->getLine()."\n";
  182. echo $throwable->getFile()."\n";
  183. echo $throwable->getMessage()."\n";
  184. return error($throwable->getMessage());
  185. }
  186. }
  187. /**
  188. * SKU编辑
  189. * @param array $skuData
  190. * @return array
  191. */
  192. protected function skuData(array $skuData = []): array
  193. {
  194. try {
  195. if (empty($skuData)) return [0,trans('empty.require')];
  196. foreach ($skuData as $vo) {
  197. $sku = $this->skuModel->where("sku_id",$vo['sku_id'])->findOrEmpty();
  198. if ($sku->isEmpty()) {
  199. $sku->insertGetId($vo);
  200. } else {
  201. $sku->update($vo);
  202. }
  203. }
  204. return [1,'success'];
  205. } catch (\Throwable $throwable) {
  206. return [0,$throwable->getMessage()];
  207. }
  208. }
  209. /**
  210. * 商品下架
  211. * @param Request $request
  212. * @return Response
  213. */
  214. #[PostMapping("off")]
  215. public function offGood2Life(Request $request): Response
  216. {
  217. try {
  218. $param = $this->_valid([
  219. "id.require" => trans("empty.require"),
  220. ],$request->method());
  221. if (!is_array($param)) return error($param);
  222. $detail = $this->model->where("id",$param["id"])->with(['skuSpecs'])->findOrEmpty();
  223. if ($detail->isEmpty()) return errorTrans("empty.data");
  224. if ($detail['poi_id'] <> $request->user['store_id']) return errorTrans("error.param");
  225. $store = (new SaasStore)->where("poi_id",$request->user['store_id'])->findOrEmpty();
  226. if ($store->isEmpty()) return errorTrans("error.data");
  227. $data = (new BaseData)->config([
  228. "appid" => sConf("wechat.mini_appid"),
  229. "secret" => sConf("wechat.mini_secret"),
  230. ])->token()->goodsOffOn(2,$store['store_id'],$detail['goods_id']);
  231. if ($data['error_code'] <> 0) return error("操作失败,请稍后重试");
  232. $detail->status = 2;
  233. $detail->save();
  234. return successTrans("success.data");
  235. } catch (\Throwable $throwable) {
  236. return error($throwable->getMessage());
  237. }
  238. }
  239. }