Store.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. namespace app\controller\admin;
  3. use app\extra\basic\Base;
  4. use app\middleware\AuthMiddleware;
  5. use app\model\saas\SaasStore;
  6. use app\model\saas\SaasStoreCategory;
  7. use app\service\saas\GoodsService;
  8. use app\service\saas\StoreService;
  9. use DI\Attribute\Inject;
  10. use LinFly\Annotation\Attributes\Route\Controller;
  11. use LinFly\Annotation\Attributes\Route\GetMapping;
  12. use LinFly\Annotation\Attributes\Route\Middleware;
  13. use LinFly\Annotation\Attributes\Route\PostMapping;
  14. use support\Container;
  15. use support\Request;
  16. use support\Response;
  17. use Webman\RedisQueue\Redis;
  18. /**
  19. * POI门店
  20. */
  21. #[Controller("/api/store"),Middleware(AuthMiddleware::class)]
  22. class Store extends Base
  23. {
  24. #[Inject]
  25. protected StoreService $service;
  26. #[Inject]
  27. protected GoodsService $goodsService;
  28. #[Inject]
  29. protected SaasStore $model;
  30. /**
  31. * 数据列表
  32. * @param Request $request
  33. * @return Response
  34. */
  35. #[GetMapping('list')]
  36. public function getStoreData(Request $request): Response
  37. {
  38. try {
  39. $param = $request->all();
  40. $data = $this->service->setModel()->getList($param,null,true,['type'],['type' => function($query,$resp){
  41. if (empty($resp['product_type'])) return [];
  42. $productType = json_decode($resp['product_type'],true);
  43. $typeData = [];
  44. foreach ($this->goodsService->productType() as $key => $value) {
  45. if (in_array($value['key'],$productType)) {
  46. $typeData[] = $value['name'];
  47. }
  48. }
  49. return $typeData;
  50. }]);
  51. return successTrans(100010,pageFormat($data),200);
  52. } catch (\Throwable $th) {
  53. return error($th->getMessage());
  54. }
  55. }
  56. /**
  57. * 同步POI门店
  58. * @param Request $request
  59. * @return Response
  60. */
  61. #[GetMapping('sync')]
  62. public function syncStore(Request $request): Response
  63. {
  64. try {
  65. Redis::send("sync-store",[
  66. "appid" => sConf("wechat.appid"),
  67. "secret" => sConf("wechat.secret"),
  68. "account" => sConf("wechat.shop_id"),
  69. ]);
  70. return success("发起成功");
  71. } catch (\Throwable $throwable) {
  72. return error($throwable->getMessage());
  73. }
  74. }
  75. /**
  76. * 编辑代理信息
  77. * @param Request $request
  78. * @return Response
  79. */
  80. #[PostMapping("edit")]
  81. public function setListData(Request $request): Response
  82. {
  83. try {
  84. $param = $request->post();
  85. $factory = $this->model->where("id",$param['id'])->findOrEmpty();
  86. if ($factory->isEmpty()) return errorTrans("error.data");
  87. $state = $this->model->setAutoData($param);
  88. if (!$state) return errorTrans("error.data");
  89. return successTrans("success.data");
  90. } catch (\Throwable $throwable) {
  91. return error($throwable->getMessage());
  92. }
  93. }
  94. /**
  95. * 批量操作
  96. * @param Request $request
  97. * @return Response
  98. */
  99. #[PostMapping("batch")]
  100. public function setGoodsBatch(Request $request): Response
  101. {
  102. try {
  103. $param = $this->_valid([
  104. "id.require" => "参数错误",
  105. "value.require" => "参数错误",
  106. "field.require" => "参数错误",
  107. "type.require" => "参数错误"
  108. ],"post");
  109. if (!is_array($param)) return error($param);
  110. if ($param['type'] == "batch") {
  111. $state = $this->model->where("id","in",$param['id'])->save([$param['field'] => $param['value']]);
  112. } else {
  113. $state = $this->model->where("id",$param['id'])->save([$param['field'] => $param['value']]);
  114. }
  115. if (!$state) return errorTrans("error.data");
  116. return successTrans("success.data");
  117. } catch (\Throwable $throwable) {
  118. return error($throwable->getMessage());
  119. }
  120. }
  121. #[GetMapping("cate")]
  122. public function getCategory(): Response
  123. {
  124. try {
  125. $data = $this->goodsService->productType();
  126. return successTrans("success",compact('data'));
  127. } catch (\Throwable $throwable) {
  128. return error($throwable->getMessage());
  129. }
  130. }
  131. /**
  132. * 新增店铺品类授权
  133. * @param Request $request
  134. * @return Response
  135. */
  136. #[PostMapping("cate")]
  137. public function addCategory(Request $request): Response
  138. {
  139. try {
  140. $param = $this->_valid([
  141. "poi_id.require" => trans("empty.require"),
  142. "category.require" => trans("empty.require"),
  143. "product_type.require" => trans("empty.require"),
  144. ],$request->method());
  145. if (!is_array($param)) return error($param);
  146. $storeMode = (new SaasStoreCategory);
  147. $inData = [];
  148. foreach ($param['category'] as $key => $value) {
  149. if (!isset($value[2])) return error("请选择正确的品类");
  150. $lastId = $value[2];
  151. $storeCategory = $storeMode->where("category_id",$lastId)->where("poi_id",$param['poi_id'])->with(['cate'])->findOrEmpty();
  152. if (!$storeCategory->isEmpty()) return error("【{$storeCategory['cate']['name']}】已存在,请勿重复添加");
  153. $inData[$key] = [
  154. "category_id" => $value[2],
  155. "poi_id" => $param['poi_id'],
  156. "product_type" => json_encode($param["product_type"]),
  157. ];
  158. }
  159. $state = $storeMode->insertAll($inData);
  160. if (!$state) return errorTrans("error.data");
  161. return successTrans("success.data");
  162. } catch (\Throwable $throwable) {
  163. return error($throwable->getMessage());
  164. }
  165. }
  166. /**
  167. * 删除店铺品类授权
  168. * @param Request $request
  169. * @return Response
  170. */
  171. #[PostMapping("cate/del")]
  172. public function delCategory(Request $request): Response
  173. {
  174. try {
  175. $param = $this->_valid([
  176. "poi_id.require" => trans("empty.require"),
  177. "id.require" => trans("empty.require")
  178. ],$request->method());
  179. if (!is_array($param)) return error($param);
  180. $data = (new SaasStoreCategory)->where(['poi_id' => $param['poi_id'],'category_id' => $param['id']])->findOrEmpty();
  181. if ($data->isEmpty()) return errorTrans("error.data");
  182. $state = $data->delete();
  183. if (!$state) return errorTrans("error.data");
  184. return successTrans("success.data");
  185. } catch (\Throwable $throwable) {
  186. return error($throwable->getMessage());
  187. }
  188. }
  189. }