UserBrokerage.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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\admin\user;
  12. use app\common\repositories\user\UserBrokerageRepository;
  13. use app\validate\admin\UserBrokerageValidate;
  14. use crmeb\basic\BaseController;
  15. use think\App;
  16. /**
  17. * Class UserBrokerage
  18. * app\controller\admin\user
  19. * 分销员 / 等级 / svip 相关等级设置
  20. */
  21. class UserBrokerage extends BaseController
  22. {
  23. protected $repository;
  24. public function __construct(App $app, UserBrokerageRepository $repository)
  25. {
  26. parent::__construct($app);
  27. $this->repository = $repository;
  28. }
  29. /**
  30. * 根据类型获取等级列表
  31. * @return \think\response\Json
  32. * @author Qinii
  33. */
  34. public function options()
  35. {
  36. $where = $this->request->params(['type']);
  37. return app('json')->success($this->repository->options($where));
  38. }
  39. /**
  40. * 列表
  41. * @param $id
  42. * @return \think\response\Json
  43. * @author Qinii
  44. */
  45. public function getLst()
  46. {
  47. [$page, $limit] = $this->getPage();
  48. $where = $this->request->params(['brokerage_name', 'type']);
  49. return app('json')->success($this->repository->getList($where, $page, $limit));
  50. }
  51. /**
  52. * 添加表单
  53. * @return \think\response\Json
  54. * @author Qinii
  55. */
  56. public function createForm()
  57. {
  58. return app('json')->success(formToData($this->repository->form()));
  59. }
  60. /**
  61. * 添加
  62. * @return \think\response\Json
  63. * @author Qinii
  64. */
  65. public function create()
  66. {
  67. $data = $this->checkParams();
  68. if ($this->repository->fieldExists('brokerage_level', $data['brokerage_level'], null, $data['type'])) {
  69. return app('json')->fail('会员等级已存在');
  70. }
  71. if ($data['type']) {
  72. $data['brokerage_rule'] = [
  73. 'image' => $data['image'],
  74. 'value' => $data['value'],
  75. ];
  76. }
  77. unset($data['image'], $data['value']);
  78. $this->repository->create($data);
  79. return app('json')->success('添加成功');
  80. }
  81. /**
  82. * 修改表单
  83. * @param $id
  84. * @return \think\response\Json
  85. * @author Qinii
  86. */
  87. public function updateForm($id)
  88. {
  89. return app('json')->success(formToData($this->repository->form($id)));
  90. }
  91. /**
  92. * 修改
  93. * @param $id
  94. * @return \think\response\Json
  95. * @author Qinii
  96. */
  97. public function update($id)
  98. {
  99. $id = (int)$id;
  100. $data = $this->checkParams();
  101. if (!$id || !$this->repository->get($id)) {
  102. return app('json')->fail('数据不存在');
  103. }
  104. if ($this->repository->fieldExists('brokerage_level', $data['brokerage_level'], $id, $data['type'])) {
  105. return app('json')->fail('会员等级已存在');
  106. }
  107. if ($data['type']) {
  108. $data['brokerage_rule'] = [
  109. 'image' => $data['image'],
  110. 'value' => $data['value'],
  111. ];
  112. }
  113. unset($data['image'], $data['value']);
  114. $data['brokerage_rule'] = json_encode($data['brokerage_rule'], JSON_UNESCAPED_UNICODE);
  115. $this->repository->update($id, $data);
  116. return app('json')->success('修改成功');
  117. }
  118. /**
  119. * 详情
  120. * @param $id
  121. * @return \think\response\Json
  122. * @author Qinii
  123. */
  124. public function detail($id)
  125. {
  126. $id = (int)$id;
  127. if (!$id || !$brokerage = $this->repository->get($id)) {
  128. return app('json')->fail('数据不存在');
  129. }
  130. return app('json')->success($brokerage->toArray());
  131. }
  132. /**
  133. * 删除
  134. * @param $id
  135. * @return \think\response\Json
  136. * @author Qinii
  137. */
  138. public function delete($id)
  139. {
  140. $id = (int)$id;
  141. if (!$id || !$brokerage = $this->repository->get($id)) {
  142. return app('json')->fail('数据不存在');
  143. }
  144. if ($brokerage->user_num > 0) {
  145. return app('json')->fail('该等级下有数据,不能进行删除操作!');
  146. }
  147. $brokerage->delete();
  148. return app('json')->success('删除成功');
  149. }
  150. /**
  151. * 验证数据
  152. * @return array|mixed|string|string[]
  153. * @author Qinii
  154. */
  155. public function checkParams()
  156. {
  157. $data = $this->request->params(['brokerage_level', 'brokerage_name', 'brokerage_icon', 'brokerage_rule', 'extension_one', 'extension_two', 'image', 'value', ['type', 0]]);
  158. app()->make(UserBrokerageValidate::class)->check($data);
  159. return $data;
  160. }
  161. }