Category.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace app\controller\admin;
  3. use app\extra\basic\Base;
  4. use app\extra\tools\DataExtend;
  5. use app\middleware\AuthMiddleware;
  6. use app\model\saas\SaasCategory;
  7. use app\service\saas\CategoryService;
  8. use DI\Attribute\Inject;
  9. use LinFly\Annotation\Attributes\Route\Controller;
  10. use LinFly\Annotation\Attributes\Route\GetMapping;
  11. use LinFly\Annotation\Attributes\Route\Middleware;
  12. use LinFly\Annotation\Attributes\Route\PostMapping;
  13. use support\Request;
  14. use support\Response;
  15. use Webman\RedisQueue\Redis;
  16. #[Controller("/api/category"),Middleware(AuthMiddleware::class)]
  17. class Category extends Base
  18. {
  19. #[Inject]
  20. protected CategoryService $service;
  21. #[Inject]
  22. protected SaasCategory $model;
  23. /**
  24. * 数据列表
  25. * @param Request $request
  26. * @return Response
  27. */
  28. #[GetMapping('list')]
  29. public function getStoreCategory(Request $request): Response
  30. {
  31. try {
  32. $data = $this->service->setModel()->getList($request->all(),null,false);
  33. $tree = DataExtend::arr2tree($data,"category_id","parent_id");
  34. return successTrans(100010,$tree,200);
  35. } catch (\Throwable $th) {
  36. return error($th->getMessage());
  37. }
  38. }
  39. /**
  40. * 同步门店分类
  41. * @param Request $request
  42. * @return Response
  43. */
  44. #[GetMapping('sync')]
  45. public function syncStore(Request $request): Response
  46. {
  47. try {
  48. Redis::send("sync-category",[
  49. "appid" => sConf("wechat.appid"),
  50. "secret" => sConf("wechat.secret"),
  51. "account" => sConf("wechat.shop_id"),
  52. ]);
  53. return success("发起成功");
  54. } catch (\Throwable $throwable) {
  55. return error($throwable->getMessage());
  56. }
  57. }
  58. /**
  59. * 编辑代理信息
  60. * @param Request $request
  61. * @return Response
  62. */
  63. #[PostMapping("edit")]
  64. public function setListData(Request $request): Response
  65. {
  66. try {
  67. $param = $request->post();
  68. $factory = $this->model->where("id",$param['id'])->findOrEmpty();
  69. if ($factory->isEmpty()) return errorTrans("error.data");
  70. $state = $this->model->setAutoData($param);
  71. if (!$state) return errorTrans("error.data");
  72. return successTrans("success.data");
  73. } catch (\Throwable $throwable) {
  74. return error($throwable->getMessage());
  75. }
  76. }
  77. /**
  78. * 批量操作
  79. * @param Request $request
  80. * @return Response
  81. */
  82. #[PostMapping("batch")]
  83. public function setGoodsBatch(Request $request): Response
  84. {
  85. try {
  86. $param = $this->_valid([
  87. "id.require" => "参数错误",
  88. "value.require" => "参数错误",
  89. "field.require" => "参数错误",
  90. "type.require" => "参数错误"
  91. ],"post");
  92. if (!is_array($param)) return error($param);
  93. if ($param['type'] == "batch") {
  94. $state = $this->model->where("id","in",$param['id'])->save([$param['field'] => $param['value']]);
  95. } else {
  96. $state = $this->model->where("id",$param['id'])->save([$param['field'] => $param['value']]);
  97. }
  98. if (!$state) return errorTrans("error.data");
  99. return successTrans("success.data");
  100. } catch (\Throwable $throwable) {
  101. return error($throwable->getMessage());
  102. }
  103. }
  104. }