ProductUnit.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. namespace app\controller\merchant\store\product;
  3. use app\common\repositories\store\product\ProductUnitRepository as repository;
  4. use app\validate\admin\ProductUnitValidate;
  5. use crmeb\basic\BaseController;
  6. use think\App;
  7. class ProductUnit extends BaseController
  8. {
  9. protected $repository;
  10. /**
  11. * Product constructor.
  12. * @param App $app
  13. * @param repository $repository
  14. */
  15. public function __construct(App $app, repository $repository)
  16. {
  17. parent::__construct($app);
  18. $this->repository = $repository;
  19. }
  20. /**
  21. * 获取列表数据
  22. *
  23. * @return \think\response\Json
  24. */
  25. public function list()
  26. {
  27. // 获取分页参数
  28. [$page, $limit] = $this->getPage();
  29. // 获取查询条件
  30. $where = $this->request->params(['value']);
  31. // 设置商家ID
  32. $where['mer_id'] = $this->request->merId();
  33. // 调用仓库方法获取数据
  34. $data = $this->repository->list($where, $page, $limit);
  35. // 返回JSON格式的数据
  36. return app('json')->success($data);
  37. }
  38. /**
  39. * 创建表单
  40. *
  41. * @return \think\response\Json
  42. */
  43. public function createForm()
  44. {
  45. // 调用仓库方法获取表单数据
  46. return app('json')->success(formToData($this->repository->createForm($this->request->merId())));
  47. }
  48. /**
  49. * 创建产品单位
  50. *
  51. * @param ProductUnitValidate $validate 验证器实例
  52. * @return \think\response\Json
  53. */
  54. public function create(ProductUnitValidate $validate)
  55. {
  56. // 验证参数
  57. $data = $this->checkParams($validate);
  58. // 调用仓库方法创建产品单位
  59. $this->repository->create($this->request->merId(), $data);
  60. return app('json')->success('添加成功');
  61. }
  62. /**
  63. * 更新表单
  64. *
  65. * @param int $id 表单ID
  66. * @return \Illuminate\Http\JsonResponse
  67. */
  68. public function updateForm($id)
  69. {
  70. // 调用 repository 的 updateForm 方法更新表单数据,并将结果转换为 JSON 格式返回
  71. return app('json')->success(formToData($this->repository->updateForm((int)$id, $this->request->merId())));
  72. }
  73. /**
  74. * 更新商品单位
  75. *
  76. * @param int $id 商品单位ID
  77. * @param ProductUnitValidate $validate 商品单位验证器
  78. * @return \Illuminate\Http\JsonResponse
  79. */
  80. public function update($id, ProductUnitValidate $validate)
  81. {
  82. // 获取验证通过的参数数据
  83. $data = $this->checkParams($validate);
  84. // 调用 repository 的 update 方法更新商品单位数据
  85. $this->repository->update($id, $this->request->merId(), $data);
  86. // 返回操作成功的 JSON 格式响应
  87. return app('json')->success('编辑成功');
  88. }
  89. /**
  90. * 删除商品单位
  91. *
  92. * @param int $id 商品单位ID
  93. * @return \Illuminate\Http\JsonResponse
  94. */
  95. public function delete($id)
  96. {
  97. // 调用 repository 的 delete 方法删除商品单位数据
  98. $this->repository->delete($id, $this->request->merId());
  99. return app('json')->success('删除成功');
  100. }
  101. /**
  102. * 获取选择列表
  103. *
  104. * @return array
  105. */
  106. public function getSelectList()
  107. {
  108. // 调用 repository 类的 getSelectList 方法获取选择列表数据,并通过 json 组件返回成功状态和数据
  109. return app('json')->success($this->repository->getSelectList($this->request->merId()));
  110. }
  111. /**
  112. * 校验参数
  113. *
  114. * @param ProductUnitValidate $validate 参数校验器实例
  115. * @return array 校验后的参数数组
  116. */
  117. public function checkParams(ProductUnitValidate $validate)
  118. {
  119. // 需要校验的参数列表
  120. $params = ['value', 'sort'];
  121. // 从请求中获取参数数组
  122. $data = $this->request->params($params);
  123. // 使用参数校验器对参数进行校验
  124. $validate->check($data);
  125. // 返回校验后的参数数组
  126. return $data;
  127. }
  128. }