Svip.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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\api\user;
  12. use app\common\model\user\UserOrder;
  13. use app\common\repositories\store\coupon\StoreCouponRepository;
  14. use app\common\repositories\store\order\StoreOrderRepository;
  15. use app\common\repositories\store\product\ProductRepository;
  16. use app\common\repositories\store\product\SpuRepository;
  17. use app\common\repositories\system\groupData\GroupDataRepository;
  18. use app\common\repositories\system\groupData\GroupRepository;
  19. use app\common\repositories\system\serve\ServeOrderRepository;
  20. use app\common\repositories\user\MemberinterestsRepository;
  21. use app\common\repositories\user\UserBillRepository;
  22. use app\common\repositories\user\UserOrderRepository;
  23. use app\common\repositories\user\UserRepository;
  24. use crmeb\basic\BaseController;
  25. use app\common\repositories\user\FeedbackRepository;
  26. use think\App;
  27. use think\exception\ValidateException;
  28. class Svip extends BaseController
  29. {
  30. protected $repository;
  31. public function __construct(App $app, UserBillRepository $repository)
  32. {
  33. parent::__construct($app);
  34. $this->repository = $repository;
  35. if (!systemConfig('svip_switch_status')) throw new ValidateException('付费会员未开启');
  36. }
  37. /**
  38. * TODO 会员卡类型列表
  39. * @param GroupRepository $groupRepository
  40. * @param GroupDataRepository $groupDataRepository
  41. * @return \think\response\Json
  42. * @author Qinii
  43. * @day 2022/11/7
  44. */
  45. public function getTypeLst(GroupRepository $groupRepository,GroupDataRepository $groupDataRepository)
  46. {
  47. $group_id = $groupRepository->getSearch(['group_key' => 'svip_pay'])->value('group_id');
  48. $where['group_id'] = $group_id;
  49. $where['status'] = 1;
  50. $list = $groupDataRepository->getSearch($where)->field('group_data_id,value,sort,status')->order('sort DESC')->select();
  51. if ($this->request->isLogin() && $this->request->userInfo()->is_svip != -1) {
  52. foreach ($list as $item) {
  53. if ($item['value']['svip_type'] != 1) $res[] = $item;
  54. }
  55. }
  56. $list = $res ?? $list;
  57. $def = [];
  58. if ($list && isset($list[0])) {
  59. $def = $list[0] ? (['group_data_id' => $list[0]['group_data_id']] + $list[0]['value']) : [];
  60. }
  61. return app('json')->success(['def' => $def, 'list' => $list]);
  62. }
  63. /**
  64. * TODO 购买会员
  65. * @param $id
  66. * @param GroupDataRepository $groupDataRepository
  67. * @param ServeOrderRepository $serveOrderRepository
  68. * @return \think\response\Json|void
  69. * @author Qinii
  70. * @day 2022/11/7
  71. */
  72. public function createOrder($id, GroupDataRepository $groupDataRepository, UserOrderRepository $userOrderRepository)
  73. {
  74. $params = $this->request->params(['pay_type','return_url']);
  75. if (!in_array($params['pay_type'], ['weixin', 'routine', 'h5', 'alipay', 'alipayQr', 'weixinQr'], true))
  76. return app('json')->fail('请选择正确的支付方式');
  77. $res = $groupDataRepository->getWhere(['group_data_id' => $id, 'status' => 1]);
  78. if (!$res) return app('json')->fail('参数有误~');
  79. if ($this->request->userInfo()->is_svip == 3)
  80. return app('json')->fail('您已经是终身会员~');
  81. if ($this->request->userInfo()->is_svip !== -1 && $res['value']['svip_type'] == 1)
  82. return app('json')->fail('请选择其他会员类型');
  83. $params['is_app'] = $this->request->isApp();
  84. return $userOrderRepository->add($res,$this->request->userInfo(),$params);
  85. }
  86. /**
  87. * TODO 会员中心个人信息
  88. * @return \think\response\Json
  89. * @author Qinii
  90. * @day 2022/11/9
  91. */
  92. public function svipUserInfo()
  93. {
  94. if ($this->request->isLogin()) {
  95. $user = app()->make(UserRepository::class)->getSearch([])->field('uid,nickname,avatar,is_svip,svip_endtime,svip_save_money')->find($this->request->uid());
  96. if ($user && $user['is_svip'] == 3) $user['svip_endtime'] = date('Y-m-d H:i:s',strtotime("+100 year"));
  97. }
  98. $data['user'] = $user ?? new \stdClass();
  99. $data['interests'] = systemConfig('svip_switch_status') ? app()->make(MemberinterestsRepository::class)->getInterestsByLevel(MemberinterestsRepository::TYPE_SVIP) : [];
  100. return app('json')->success($data);
  101. }
  102. /**
  103. * TODO 获取会员优惠券列表
  104. * @param StoreCouponRepository $couponRepository
  105. * @return \think\response\Json
  106. * @author Qinii
  107. * @day 2022/11/17
  108. */
  109. public function svipCoupon(StoreCouponRepository $couponRepository)
  110. {
  111. $where['send_type'] = $couponRepository::GET_COUPON_TYPE_SVIP;
  112. $uid = $this->request->isLogin() ? $this->request->uid() : null;
  113. $data = $couponRepository->sviplist($where, $uid);
  114. return app('json')->success($data);
  115. }
  116. /**
  117. * TODO 领取会员优惠券
  118. * @param $id
  119. * @param StoreCouponRepository $couponRepository
  120. * @return \think\response\Json
  121. * @author Qinii
  122. * @day 2022/11/17
  123. */
  124. public function receiveCoupon($id, StoreCouponRepository $couponRepository)
  125. {
  126. if (!$this->request->userInfo()->is_svip)
  127. return app('json')->fail('您还不是付费会员');
  128. if (!$couponRepository->exists($id))
  129. return app('json')->fail('优惠券不存在');
  130. try {
  131. $couponRepository->receiveSvipCounpon($id, $this->request->uid());
  132. } catch (\Exception $e) {
  133. return app('json')->fail('优惠券已被领完');
  134. }
  135. return app('json')->success('领取成功');
  136. }
  137. /**
  138. * TODO 会员专属商品
  139. * @param SpuRepository $spuRepository
  140. * @return \think\response\Json
  141. * @author Qinii
  142. * @day 2022/11/17
  143. */
  144. public function svipProductList(SpuRepository $spuRepository)
  145. {
  146. [$page, $limit] = $this->getPage();
  147. $user = $this->request->isLogin() ? $this->request->userInfo() : null;
  148. $where['is_gift_bag'] = 0;
  149. $where['product_type'] = 0;
  150. $where['order'] = 'star';
  151. $where['svip'] = 1;
  152. $data = $spuRepository->getApiSearch($where, $page, $limit, $user);
  153. return app('json')->success($data);
  154. }
  155. }