Goods.php 11 KB

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