ProductLabel.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 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. public function __construct(App $app, ProductLabelRepository $repository)
  20. {
  21. parent::__construct($app);
  22. $this->repository = $repository;
  23. }
  24. public function lst()
  25. {
  26. [$page, $limit] = $this->getPage();
  27. $where = $this->request->params(['name', 'type', 'status']);
  28. $where['mer_id'] = $this->request->merId();
  29. $data = $this->repository->getList($where,$page, $limit);
  30. return app('json')->success($data);
  31. }
  32. public function createForm()
  33. {
  34. return app('json')->success(formToData($this->repository->form(null, 'merchantStoreProductLabelCreate')));
  35. }
  36. public function create(ProductLabelValidate $validate)
  37. {
  38. $data = $this->checkParams($validate);
  39. if (!$this->repository->check($data['label_name'], $this->request->merId()))
  40. return app('json')->fail('名称重复');
  41. $data['mer_id'] = $this->request->merId();
  42. $this->repository->create($data);
  43. return app('json')->success('添加成功');
  44. }
  45. public function updateForm($id)
  46. {
  47. return app('json')->success(formToData($this->repository->updateForm($id, 'merchantStoreProductLabelUpdate', $this->request->merId())));
  48. }
  49. public function update($id, ProductLabelValidate $validate)
  50. {
  51. $data = $this->checkParams($validate);
  52. if (!$this->repository->check($data['label_name'], $this->request->merId(),$id))
  53. return app('json')->fail('名称重复');
  54. $getOne = $this->repository->getWhere(['product_label_id' => $id,'mer_id' => $this->request->merId()]);
  55. if (!$getOne) return app('json')->fail('数据不存在');
  56. $this->repository->update($id, $data);
  57. return app('json')->success('编辑成功');
  58. }
  59. public function detail($id)
  60. {
  61. $getOne = $this->repository->getWhere(['product_label_id' => $id,'mer_id' => $this->request->merId(), 'is_del' => 0]);
  62. if (!$getOne) return app('json')->fail('数据不存在');
  63. return app('json')->success($getOne);
  64. }
  65. public function delete($id)
  66. {
  67. $getOne = $this->repository->getWhere(['product_label_id' => $id,'mer_id' => $this->request->merId()]);
  68. if (!$getOne) return app('json')->fail('数据不存在');
  69. $this->repository->delete($id);
  70. return app('json')->success('删除成功');
  71. }
  72. public function switchWithStatus($id)
  73. {
  74. $status = $this->request->param('status') == 1 ? 1 : 0;
  75. $getOne = $this->repository->getWhere(['product_label_id' => $id,'mer_id' => $this->request->merId()]);
  76. if (!$getOne) return app('json')->fail('数据不存在');
  77. $this->repository->update($id,['status' => $status]);
  78. return app('json')->success('修改成功');
  79. }
  80. public function getOptions()
  81. {
  82. $data = $this->repository->getOptions($this->request->merId());
  83. return app('json')->success($data);
  84. }
  85. public function checkParams(ProductLabelValidate $validate)
  86. {
  87. $params = ['label_name', 'status', 'sort', 'info'];
  88. $data = $this->request->params($params);
  89. $validate->check($data);
  90. return $data;
  91. }
  92. }