MemberInterests.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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\admin\user;
  12. use app\common\repositories\user\MemberinterestsRepository;
  13. use app\common\repositories\user\UserBrokerageRepository;
  14. use app\validate\admin\UserBrokerageValidate;
  15. use crmeb\basic\BaseController;
  16. use think\App;
  17. use think\exception\ValidateException;
  18. class MemberInterests extends BaseController
  19. {
  20. protected $repository;
  21. public function __construct(App $app, MemberinterestsRepository $repository)
  22. {
  23. parent::__construct($app);
  24. $this->repository = $repository;
  25. }
  26. public function getLst()
  27. {
  28. [$page, $limit] = $this->getPage();
  29. $where = $this->request->params(['name',['type',$this->repository::TYPE_FREE]]);
  30. return app('json')->success($this->repository->getList($where, $page, $limit));
  31. }
  32. public function createForm()
  33. {
  34. return app('json')->success(formToData($this->repository->form()));
  35. }
  36. public function create()
  37. {
  38. $data = $this->checkParams();
  39. $this->repository->create($data);
  40. return app('json')->success('添加成功');
  41. }
  42. public function updateForm($id)
  43. {
  44. return app('json')->success(formToData($this->repository->form($id, $this->repository::TYPE_FREE)));
  45. }
  46. public function update($id)
  47. {
  48. $id = (int)$id;
  49. $data = $this->checkParams();
  50. if (!$id || !$this->repository->get($id)) {
  51. return app('json')->fail('数据不存在');
  52. }
  53. $this->repository->update($id, $data);
  54. return app('json')->success('修改成功');
  55. }
  56. public function detail($id)
  57. {
  58. $id = (int)$id;
  59. if (!$id || !$brokerage = $this->repository->get($id)) {
  60. return app('json')->fail('数据不存在');
  61. }
  62. return app('json')->success($brokerage->toArray());
  63. }
  64. public function delete($id)
  65. {
  66. $id = (int)$id;
  67. if (!$id || !$brokerage = $this->repository->get($id)) {
  68. return app('json')->fail('数据不存在');
  69. }
  70. $brokerage->delete();
  71. return app('json')->success('删除成功');
  72. }
  73. public function checkParams()
  74. {
  75. $data = $this->request->params(['brokerage_level', 'name', 'info', 'pic', 'type',['has_type',0],['link',''],['value',''],['on_pic','']]);
  76. if ($data['type'] == $this->repository::TYPE_FREE) {
  77. if(!$data['name'] || !$data['pic'] || empty($data['brokerage_level']))
  78. throw new ValidateException('请填写正确的权益信息');
  79. $count = app()->make(UserBrokerageRepository::class)->getWhereCount(['brokerage_level' => $data['brokerage_level'], 'type' => $data['type']]);
  80. if (!$count) throw new ValidateException('会员等级不存在');
  81. } else {
  82. if (mb_strlen($data['name']) > 6) throw new ValidateException('名称必须小于6个字符');
  83. if ($data['value'] < 0 && in_array($data['has_type'],[$this->repository::HAS_TYPE_SIGN,$this->repository::HAS_TYPE_PAY,$this->repository::HAS_TYPE_MEMBER])){
  84. throw new ValidateException('倍数不能位负数');
  85. }
  86. }
  87. return $data;
  88. }
  89. public function getSvipInterests()
  90. {
  91. $where['type'] = $this->repository::TYPE_SVIP;
  92. $data = $this->repository->getList($where,1,10);
  93. return app('json')->success($data['list']);
  94. }
  95. public function updateSvipForm($id)
  96. {
  97. return app('json')->success(formToData($this->repository->svipForm($id)));
  98. }
  99. public function switchWithStatus($id)
  100. {
  101. $status = $this->request->param('status') == 1 ? 1 :0;
  102. try{
  103. $this->repository->update($id,['status' => $status]);
  104. return app('json')->success('修改成功');
  105. }catch (\Exception $exception) {
  106. return app('json')->success('修改失败');
  107. }
  108. }
  109. }