ProductLabel.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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\product;
  12. use app\common\repositories\store\product\ProductLabelRepository;
  13. use app\validate\admin\ProductLabelValidate;
  14. use think\App;
  15. use crmeb\basic\BaseController;
  16. class ProductLabel extends BaseController
  17. {
  18. protected $repository;
  19. /**
  20. * 构造函数
  21. *
  22. * @param App $app 应用实例
  23. * @param ProductLabelRepository $repository 商品标签仓库实例
  24. */
  25. public function __construct(App $app, ProductLabelRepository $repository)
  26. {
  27. // 调用父类构造函数
  28. parent::__construct($app);
  29. // 初始化商品标签仓库实例
  30. $this->repository = $repository;
  31. }
  32. /**
  33. * 获取商品标签列表
  34. *
  35. * @return \think\response\Json
  36. */
  37. public function lst()
  38. {
  39. // 获取分页参数
  40. [$page, $limit] = $this->getPage();
  41. // 获取查询条件
  42. $where = $this->request->params(['name', 'type', 'status']);
  43. // 添加商家ID查询条件
  44. $where['mer_id'] = $this->request->merId();
  45. // 获取商品标签列表
  46. $data = $this->repository->getList($where, $page, $limit);
  47. // 返回JSON格式的商品标签列表数据
  48. return app('json')->success($data);
  49. }
  50. /**
  51. * 创建商品标签表单
  52. *
  53. * @return \think\response\Json
  54. */
  55. public function createForm()
  56. {
  57. // 获取商品标签创建表单数据
  58. return app('json')->success(formToData($this->repository->form(null, 'merchantStoreProductLabelCreate')));
  59. }
  60. /**
  61. * 创建商品标签
  62. *
  63. * @param ProductLabelValidate $validate 商品标签验证器
  64. * @return \Psr\Http\Message\ResponseInterface JSON格式的响应结果
  65. */
  66. public function create(ProductLabelValidate $validate)
  67. {
  68. // 获取验证通过的参数
  69. $data = $this->checkParams($validate);
  70. // 检查标签名称是否已存在
  71. if (!$this->repository->check($data['label_name'], $this->request->merId()))
  72. return app('json')->fail('名称重复');
  73. // 设置商家ID
  74. $data['mer_id'] = $this->request->merId();
  75. // 创建商品标签
  76. $this->repository->create($data);
  77. // 返回操作成功的JSON响应
  78. return app('json')->success('添加成功');
  79. }
  80. /**
  81. * 更新商品标签表单
  82. *
  83. * @param int $id 商品标签ID
  84. * @return \Psr\Http\Message\ResponseInterface JSON格式的响应结果
  85. */
  86. public function updateForm($id)
  87. {
  88. // 返回更新表单的JSON响应
  89. return app('json')->success(formToData($this->repository->updateForm($id, 'merchantStoreProductLabelUpdate', $this->request->merId())));
  90. }
  91. /**
  92. * 更新商品标签
  93. * @param int $id 商品标签ID
  94. * @param ProductLabelValidate $validate 商品标签验证器
  95. * @return \think\response\Json
  96. */
  97. public function update($id, ProductLabelValidate $validate)
  98. {
  99. // 获取验证通过的参数
  100. $data = $this->checkParams($validate);
  101. // 检查商品标签名称是否重复
  102. if (!$this->repository->check($data['label_name'], $this->request->merId(), $id))
  103. return app('json')->fail('名称重复');
  104. // 获取指定ID的商品标签
  105. $getOne = $this->repository->getWhere(['product_label_id' => $id, 'mer_id' => $this->request->merId()]);
  106. // 如果商品标签不存在则返回错误信息
  107. if (!$getOne) return app('json')->fail('数据不存在');
  108. // 更新商品标签信息
  109. $this->repository->update($id, $data);
  110. // 返回操作成功信息
  111. return app('json')->success('编辑成功');
  112. }
  113. /**
  114. * 获取商品标签详情
  115. * @param int $id 商品标签ID
  116. * @return \think\response\Json
  117. */
  118. public function detail($id)
  119. {
  120. $getOne = $this->repository->getWhere(['product_label_id' => $id, 'mer_id' => $this->request->merId(), 'is_del' => 0]);
  121. // 如果商品标签不存在则返回错误信息
  122. if (!$getOne) return app('json')->fail('数据不存在');
  123. // 返回商品标签详情
  124. return app('json')->success($getOne);
  125. }
  126. /**
  127. * 根据ID删除商品标签
  128. * @param int $id 商品标签ID
  129. * @return \think\response\Json
  130. */
  131. public function delete($id)
  132. {
  133. // 从仓库中获取指定ID和商家ID的商品标签
  134. $getOne = $this->repository->getWhere(['product_label_id' => $id, 'mer_id' => $this->request->merId()]);
  135. // 如果没有获取到商品标签,则返回失败信息
  136. if (!$getOne) return app('json')->fail('数据不存在');
  137. // 调用仓库的删除方法删除商品标签
  138. $this->repository->delete($id);
  139. // 返回成功信息
  140. return app('json')->success('删除成功');
  141. }
  142. /**
  143. * 切换商品标签状态
  144. * @param int $id 商品标签ID
  145. * @return \think\response\Json
  146. */
  147. public function switchWithStatus($id)
  148. {
  149. // 获取请求参数中的状态值,如果没有则默认为0
  150. $status = $this->request->param('status') == 1 ? 1 : 0;
  151. // 从仓库中获取指定ID和商家ID的商品标签
  152. $getOne = $this->repository->getWhere(['product_label_id' => $id, 'mer_id' => $this->request->merId()]);
  153. // 如果没有获取到商品标签,则返回失败信息
  154. if (!$getOne) return app('json')->fail('数据不存在');
  155. // 调用仓库的更新方法更新商品标签状态
  156. $this->repository->update($id, ['status' => $status]);
  157. return app('json')->success('修改成功');
  158. }
  159. /**
  160. * 获取选项列表
  161. *
  162. * @return \think\response\Json
  163. */
  164. public function getOptions()
  165. {
  166. // 从仓库中获取选项数据
  167. $data = $this->repository->getOptions($this->request->merId());
  168. // 返回JSON格式的成功响应和数据
  169. return app('json')->success($data);
  170. }
  171. /**
  172. * 校验参数
  173. *
  174. * @param ProductLabelValidate $validate 产品标签验证器实例
  175. * @return array 校验通过的参数数组
  176. */
  177. public function checkParams(ProductLabelValidate $validate)
  178. {
  179. // 需要校验的参数列表
  180. $params = ['label_name', 'status', 'sort', 'info'];
  181. // 从请求中获取参数
  182. $data = $this->request->params($params);
  183. // 校验参数
  184. $validate->check($data);
  185. // 返回校验通过的参数数组
  186. return $data;
  187. }
  188. }