ShippingTemplate.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\controller\merchant\store\shipping;
  12. use think\App;
  13. use crmeb\basic\BaseController;
  14. use app\validate\merchant\ShippingTemplateValidate as validate;
  15. use app\common\repositories\store\shipping\ShippingTemplateRepository as repository;
  16. class ShippingTemplate extends BaseController
  17. {
  18. protected $repository;
  19. /**
  20. * ShippingTemplate constructor.
  21. * @param App $app
  22. * @param repository $repository
  23. */
  24. public function __construct(App $app, repository $repository)
  25. {
  26. parent::__construct($app);
  27. $this->repository = $repository;
  28. }
  29. /**
  30. * 获取列表数据
  31. * @return \think\response\Json
  32. */
  33. public function lst()
  34. {
  35. // 获取分页参数
  36. [$page, $limit] = $this->getPage();
  37. // 获取查询条件
  38. $where = $this->request->params(['type', 'name']);
  39. // 调用 repository 类的 search 方法获取数据并返回 JSON 格式的成功响应
  40. return app('json')->success($this->repository->search($this->request->merId(), $where, $page, $limit));
  41. }
  42. /**
  43. * 获取列表数据
  44. *
  45. * @return \Illuminate\Http\JsonResponse
  46. */
  47. public function getList()
  48. {
  49. // 调用 repository 类的 getList 方法获取数据并返回 JSON 格式的成功响应
  50. // 调用 repository 类的 getList 方法获取数据并返回 JSON 格式的成功响应
  51. return app('json')->success($this->repository->getList($this->request->merId()));
  52. }
  53. /**
  54. * 创建数据
  55. * @param validate $validate 验证器实例
  56. * @return \think\response\Json
  57. */
  58. public function create(validate $validate)
  59. {
  60. // 校验参数
  61. $data = $this->checkParams($validate);
  62. // 设置商家 ID
  63. $data['mer_id'] = $this->request->merId();
  64. // 调用 repository 类的 create 方法创建数据并返回 JSON 格式的成功响应
  65. $this->repository->create($data);
  66. return app('json')->success('添加成功');
  67. }
  68. /**
  69. * 获取指定ID的商品详情
  70. *
  71. * @param int $id 商品ID
  72. * @return \think\response\Json
  73. */
  74. public function detail($id)
  75. {
  76. if (!$this->repository->merExists($this->request->merId(), $id))
  77. // 如果不存在则返回失败信息
  78. return app('json')->fail('数据不存在');
  79. // 如果存在则返回商品详情
  80. return app('json')->success($this->repository->getOne($id));
  81. }
  82. /**
  83. * 更新指定ID的数据
  84. *
  85. * @param int $id 数据ID
  86. * @param \think\Validate $validate 验证器实例
  87. * @return \think\response\Json 操作成功的JSON格式结果
  88. */
  89. public function update($id, validate $validate)
  90. {
  91. // 获取验证通过的数据
  92. // 获取验证通过的数据
  93. $data = $this->checkParams($validate);
  94. // 判断数据是否存在
  95. if (!$this->repository->merExists($this->request->merId(), $id))
  96. return app('json')->fail('数据不存在');
  97. // 更新数据
  98. $this->repository->update($id, $data, $this->request->merId());
  99. // 返回操作成功的JSON格式结果
  100. // 返回操作成功的JSON格式结果
  101. return app('json')->success('编辑成功');
  102. }
  103. /**
  104. * 根据ID删除商品模板
  105. *
  106. * @param int $id 商品模板ID
  107. * @return \Illuminate\Http\JsonResponse 返回JSON格式的操作结果
  108. */
  109. public function delete($id)
  110. {
  111. if (!$this->repository->merExists($this->request->merId(), $id))
  112. return app('json')->fail('数据不存在');
  113. if ($this->repository->merDefaultExists($this->request->merId(), $id))
  114. return app('json')->fail('默认模板不能删除');
  115. if ($this->repository->getProductUse($this->request->merId(), $id))
  116. return app('json')->fail('模板使用中,不能删除');
  117. // 删除商品模板
  118. $this->repository->delete($id);
  119. // 返回操作成功的JSON格式结果
  120. return app('json')->success('删除成功');
  121. }
  122. /**
  123. * 检查参数是否符合要求
  124. *
  125. * @param validate $validate 验证器对象
  126. * @return array 返回符合要求的参数数组
  127. */
  128. public function checkParams(validate $validate)
  129. {
  130. $data = $this->request->params(['name', 'type', 'appoint', 'undelivery', 'region', 'free', 'undelives', 'sort', 'info']);
  131. // 使用验证器对参数进行校验
  132. $validate->check($data);
  133. // 返回符合要求的参数数组
  134. return $data;
  135. }
  136. /**
  137. * 设置默认模板
  138. * @param $id
  139. * @return \think\response\Json
  140. *
  141. * @date 2023/10/07
  142. * @author yyw
  143. */
  144. public function setDefault($id)
  145. {
  146. if (!$this->repository->merExists($this->request->merId(), $id))
  147. return app('json')->fail('数据不存在');
  148. if ($this->repository->merDefaultExists($this->request->merId(), $id))
  149. return app('json')->fail('当前模板已是默认模板了');
  150. $this->repository->setDefault($this->request->merId(), $id);
  151. return app('json')->success('设置成功');
  152. }
  153. }