LabelRule.php 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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\user;
  12. use app\common\repositories\user\LabelRuleRepository;
  13. use app\common\repositories\user\UserLabelRepository;
  14. use app\validate\merchant\LabelRuleValidate;
  15. use crmeb\basic\BaseController;
  16. use think\App;
  17. use think\exception\ValidateException;
  18. class LabelRule extends BaseController
  19. {
  20. protected $repository;
  21. public function __construct(App $app, LabelRuleRepository $repository)
  22. {
  23. parent::__construct($app);
  24. $this->repository = $repository;
  25. }
  26. public function getList()
  27. {
  28. $where = $this->request->params(['keyword', 'type']);
  29. $where['mer_id'] = $this->request->merId();
  30. [$page, $limit] = $this->getPage();
  31. return app('json')->success($this->repository->getList($where, $page, $limit));
  32. }
  33. public function create()
  34. {
  35. $data = $this->checkParams();
  36. $data['mer_id'] = $this->request->merId();
  37. if (app()->make(UserLabelRepository::class)->existsName($data['label_name'], $data['mer_id'], 1))
  38. return app('json')->fail('标签名已存在');
  39. $this->repository->create($data);
  40. return app('json')->success('添加成功');
  41. }
  42. public function update($id)
  43. {
  44. $data = $this->checkParams();
  45. $mer_id = $this->request->merId();
  46. if (!$label = $this->repository->getWhere(['label_rule_id' => $id, 'mer_id' => $mer_id]))
  47. return app('json')->fail('数据不存在');
  48. if (app()->make(UserLabelRepository::class)->existsName($data['label_name'], $mer_id, 1, $label->label_id))
  49. return app('json')->fail('标签名已存在');
  50. $this->repository->update(intval($id), $data);
  51. return app('json')->success('编辑成功');
  52. }
  53. public function delete($id)
  54. {
  55. if (!$this->repository->existsWhere(['label_rule_id' => $id, 'mer_id' => $this->request->merId()]))
  56. return app('json')->fail('数据不存在');
  57. $this->repository->delete(intval($id));
  58. return app('json')->success('删除成功');
  59. }
  60. public function sync($id)
  61. {
  62. if (!$this->repository->existsWhere(['label_rule_id' => $id, 'mer_id' => $this->request->merId()]))
  63. return app('json')->fail('数据不存在');
  64. $this->repository->syncUserNum(intval($id));
  65. return app('json')->success('更新成功');
  66. }
  67. /**
  68. * @return array
  69. * @author xaboy
  70. * @day 2020/10/21
  71. */
  72. public function checkParams()
  73. {
  74. $data = $this->request->params(['label_name', 'min', 'max', 'type', 'data']);
  75. app()->make(LabelRuleValidate::class)->check($data);
  76. if (!$data['type']) {
  77. if (false === filter_var($data['min'], FILTER_VALIDATE_INT) || false === filter_var($data['max'], FILTER_VALIDATE_INT)) {
  78. throw new ValidateException('数值必须为整数');
  79. }
  80. }
  81. return $data;
  82. }
  83. }