MemberInterests.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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\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. /**
  19. * 我的等级 / 付费会员 权益
  20. * app\controller\admin\user
  21. * MemberInterests
  22. */
  23. class MemberInterests extends BaseController
  24. {
  25. protected $repository;
  26. public function __construct(App $app, MemberinterestsRepository $repository)
  27. {
  28. parent::__construct($app);
  29. $this->repository = $repository;
  30. }
  31. /**
  32. * 列表
  33. * @return \think\response\Json
  34. * @author Qinii
  35. */
  36. public function getLst()
  37. {
  38. [$page, $limit] = $this->getPage();
  39. $where = $this->request->params(['name', ['type', $this->repository::TYPE_FREE]]);
  40. return app('json')->success($this->repository->getList($where, $page, $limit));
  41. }
  42. /**
  43. * 创建表单
  44. * @return \think\response\Json
  45. * @author Qinii
  46. */
  47. public function createForm()
  48. {
  49. return app('json')->success(formToData($this->repository->form()));
  50. }
  51. /**
  52. * 创建
  53. * @return \think\response\Json
  54. * @author Qinii
  55. */
  56. public function create()
  57. {
  58. $data = $this->checkParams();
  59. $this->repository->create($data);
  60. return app('json')->success('添加成功');
  61. }
  62. /**
  63. * 修改表单
  64. * @param $id
  65. * @return \think\response\Json
  66. * @author Qinii
  67. */
  68. public function updateForm($id)
  69. {
  70. return app('json')->success(formToData($this->repository->form($id, $this->repository::TYPE_FREE)));
  71. }
  72. /**
  73. * 修改
  74. * @param $id
  75. * @return \think\response\Json
  76. * @author Qinii
  77. */
  78. public function update($id)
  79. {
  80. $id = (int)$id;
  81. $data = $this->checkParams();
  82. if (!$id || !$this->repository->get($id)) {
  83. return app('json')->fail('数据不存在');
  84. }
  85. $this->repository->update($id, $data);
  86. return app('json')->success('修改成功');
  87. }
  88. /**
  89. * 详情
  90. * @param $id
  91. * @return \think\response\Json
  92. * @author Qinii
  93. */
  94. public function detail($id)
  95. {
  96. $id = (int)$id;
  97. if (!$id || !$brokerage = $this->repository->get($id)) {
  98. return app('json')->fail('数据不存在');
  99. }
  100. return app('json')->success($brokerage->toArray());
  101. }
  102. /**
  103. * 删除
  104. * @param $id
  105. * @return \think\response\Json
  106. * @author Qinii
  107. */
  108. public function delete($id)
  109. {
  110. $id = (int)$id;
  111. if (!$id || !$brokerage = $this->repository->get($id)) {
  112. return app('json')->fail('数据不存在');
  113. }
  114. $brokerage->delete();
  115. return app('json')->success('删除成功');
  116. }
  117. /**
  118. * 验证数据
  119. * @return array
  120. * @author Qinii
  121. */
  122. public function checkParams()
  123. {
  124. $data = $this->request->params(['brokerage_level', 'name', 'info', 'pic', 'type', ['has_type', 0], ['link', ''], ['value', ''], ['on_pic', '']]);
  125. if ($data['type'] == $this->repository::TYPE_FREE) {
  126. if (!$data['name'] || !$data['pic'] || empty($data['brokerage_level']))
  127. throw new ValidateException('请填写正确的权益信息');
  128. $count = app()->make(UserBrokerageRepository::class)->getWhereCount(['brokerage_level' => $data['brokerage_level'], 'type' => $data['type']]);
  129. if (!$count) throw new ValidateException('会员等级不存在');
  130. } else {
  131. if (mb_strlen($data['name']) > 6) throw new ValidateException('名称必须小于6个字符');
  132. if ($data['value'] < 0 && in_array($data['has_type'], [$this->repository::HAS_TYPE_SIGN, $this->repository::HAS_TYPE_PAY, $this->repository::HAS_TYPE_MEMBER])) {
  133. throw new ValidateException('倍数不能位负数');
  134. }
  135. }
  136. return $data;
  137. }
  138. /**
  139. * 获取付费会员权益
  140. * @return \think\response\Json
  141. * @author Qinii
  142. */
  143. public function getSvipInterests()
  144. {
  145. $where['type'] = $this->repository::TYPE_SVIP;
  146. $data = $this->repository->getList($where, 1, 10);
  147. return app('json')->success($data['list']);
  148. }
  149. /**
  150. * 付费会员权益修改表单
  151. * @param $id
  152. * @return \think\response\Json
  153. * @author Qinii
  154. */
  155. public function updateSvipForm($id)
  156. {
  157. return app('json')->success(formToData($this->repository->svipForm($id)));
  158. }
  159. /**
  160. * 付费会员权益修改
  161. * @param $id
  162. * @return \think\response\Json
  163. * @author Qinii
  164. */
  165. public function switchWithStatus($id)
  166. {
  167. $status = $this->request->param('status') == 1 ? 1 : 0;
  168. try {
  169. $this->repository->update($id, ['status' => $status]);
  170. return app('json')->success('修改成功');
  171. } catch (\Exception $exception) {
  172. return app('json')->success('修改失败');
  173. }
  174. }
  175. }