StoreAttrTemplate.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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;
  12. use crmeb\basic\BaseController;
  13. use app\common\repositories\store\StoreAttrTemplateRepository;
  14. use app\validate\merchant\StoreAttrTemplateValidate;
  15. use think\App;
  16. use think\db\exception\DataNotFoundException;
  17. use think\db\exception\DbException;
  18. use think\db\exception\ModelNotFoundException;
  19. /**
  20. * Class StoreAttrTemplate
  21. * @package app\controller\merchant\store
  22. * @author xaboy
  23. * @day 2020-05-06
  24. */
  25. class StoreAttrTemplate extends BaseController
  26. {
  27. /**
  28. * @var StoreAttrTemplateRepository
  29. */
  30. protected $repository;
  31. /**
  32. * StoreAttrTemplate constructor.
  33. * @param App $app
  34. * @param StoreAttrTemplateRepository $repository
  35. */
  36. public function __construct(App $app, StoreAttrTemplateRepository $repository)
  37. {
  38. parent::__construct($app);
  39. $this->repository = $repository;
  40. }
  41. /**
  42. * 获取列表数据
  43. *
  44. * @return \think\response\Json
  45. */
  46. public function lst()
  47. {
  48. // 获取分页参数
  49. [$page, $limit] = $this->getPage();
  50. // 调用 repository 层获取列表数据
  51. $data = $this->repository->getList($this->request->merId(), [], $page, $limit);
  52. // 返回 JSON 格式的成功响应
  53. return app('json')->success($data);
  54. }
  55. public function getlist()
  56. {
  57. // 调用 repository 类的 list 方法获取列表数据,并通过 json 组件返回成功状态和数据
  58. return app('json')->success($this->repository->list($this->request->merId()));
  59. }
  60. /**
  61. * 创建商品属性模板
  62. *
  63. * @param StoreAttrTemplateValidate $validate 商品属性模板验证器
  64. * @return \Psr\Http\Message\ResponseInterface JSON格式的响应结果
  65. */
  66. public function create(StoreAttrTemplateValidate $validate)
  67. {
  68. // 调用checkParams方法获取验证后的数据
  69. $data = $this->checkParams($validate);
  70. // 设置商家ID
  71. $data['mer_id'] = $this->request->merId();
  72. // 调用repository的create方法创建商品属性模板
  73. $this->repository->create($data);
  74. // 返回JSON格式的成功响应结果
  75. return app('json')->success('添加成功');
  76. }
  77. /**
  78. * 更新属性模板
  79. *
  80. * @param int $id 属性模板ID
  81. * @param StoreAttrTemplateValidate $validate 验证器实例
  82. * @return \Psr\Http\Message\ResponseInterface JSON格式的响应结果
  83. */
  84. public function update($id, StoreAttrTemplateValidate $validate)
  85. {
  86. // 获取当前商家ID
  87. $merId = $this->request->merId();
  88. // 判断商家是否存在该属性模板
  89. if (!$this->repository->merExists($merId, $id))
  90. return app('json')->fail('数据不存在');
  91. // 校验参数并添加商家ID
  92. $data = $this->checkParams($validate);
  93. $data['mer_id'] = $merId;
  94. // 更新属性模板
  95. $this->repository->update($id, $data);
  96. // 返回成功响应
  97. return app('json')->success('编辑成功');
  98. }
  99. /**
  100. * 根据ID删除数据
  101. *
  102. * @param int $id 数据ID
  103. * @return \think\response\Json 返回JSON格式的响应结果
  104. */
  105. public function delete($id)
  106. {
  107. // 获取当前商家ID
  108. $merId = $this->request->merId();
  109. // 判断数据是否存在
  110. if (!$this->repository->merExists($merId, $id))
  111. return app('json')->fail('数据不存在');
  112. // 删除数据
  113. $this->repository->delete($id, $merId);
  114. // 返回成功响应结果
  115. return app('json')->success('删除成功');
  116. }
  117. /**
  118. * 检查参数是否符合要求
  119. *
  120. * @param StoreAttrTemplateValidate $validate 商品属性模板验证器
  121. * @return array 返回符合要求的参数数组
  122. */
  123. public function checkParams(StoreAttrTemplateValidate $validate)
  124. {
  125. // 从请求中获取参数
  126. $data = $this->request->params(['template_name', ['template_value', []]]);
  127. // 对参数进行验证
  128. $validate->check($data);
  129. // 返回符合要求的参数数组
  130. return $data;
  131. }
  132. }