| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210 |
- <?php
- namespace app\controller;
- use app\BaseController;
- use think\facade\Db;
- use phpu\facade\ThinkCaptcha;
- use Ramsey\Uuid\Uuid;
- use think\captcha\facade\Captcha;
- //VIP控制器
- class Vip extends BaseController
- {
- protected $noNeedLogin = [];
- private $platinumVip = [20, 54, 96, 120]; //铂金VIP价格
- private $diamondVip = [30, 81, 144, 180]; //钻石VIP价格
-
- //获取用户VIP信息,一般用于前端显示
- public function getUserVip()
- {
- $user = $this->getUser();
- $vip = Db::table('tb_user_vip')
- ->where(['user_id' => $user->user_id])
- ->find();
- if (!$vip) {
- $this->fail(500, '当前用户不是VIP');
- }
- if ($vip['expired_at'] < time()) {
- $this->fail(501, 'VIP已到期');
- }
- $this->success('success', $vip);
- }
-
- //获取VIP价格
- //铂金VIP,包月20R,包季度54R,包半年96R,包年120R
- //钻石VIP,包月30R,包季度81R,包半年144R,包年180R
- public function getVipPrice()
- {
- $type = $this->request->post('type'); //VIP类型 1:铂金 2:钻石
- if (!is_numeric($type)) {
- $this->fail(500, '参数校验错误');
- }
- $type = intval($type);
- if (!in_array($type, [1, 2])) {
- $this->fail(500, '参数校验错误');
- }
- $user = $this->getUser();
- $userVipPrice = Db::table('tb_user_vip_price')
- ->where(['user_id' => $user->user_id])
- ->where(['type' => $type])
- ->count();
- $priceList = $type == 1 ? $this->platinumVip : $this->diamondVip;
- if ($userVipPrice <= 0) {
- //插入月付价格
- Db::table('tb_user_vip_price')
- ->insert([
- 'user_id' => $user->user_id,
- 'type' => $type,
- 'duration' => 1,
- 'price' => $priceList[0],
- ]);
- //插入季付价格
- Db::table('tb_user_vip_price')
- ->insert([
- 'user_id' => $user->user_id,
- 'type' => $type,
- 'duration' => 3,
- 'price' => $priceList[1],
- ]);
- //插入半年付价格
- Db::table('tb_user_vip_price')
- ->insert([
- 'user_id' => $user->user_id,
- 'type' => $type,
- 'duration' => 6,
- 'price' => $priceList[2],
- ]);
- //插入一年付价格
- Db::table('tb_user_vip_price')
- ->insert([
- 'user_id' => $user->user_id,
- 'type' => $type,
- 'duration' => 12,
- 'price' => $priceList[3],
- ]);
- }
- $data = Db::table('tb_user_vip_price')
- ->where(['user_id' => $user->user_id])
- ->where(['type' => $type])
- ->select();
- $this->success('success', $data);
- }
-
- //购买开通VIP
- //铂金VIP,包月20R,包季度54R,包半年96R,包年120R
- //钻石VIP,包月30R,包季度81R,包半年144R,包年180R
- public function buyVip()
- {
- $type = $this->request->post('type'); //VIP类型 1:铂金 2:钻石
- $duration = $this->request->post('duration'); //周期 按月1,3,6,12
- if (!is_numeric($type) || !is_numeric($duration)) {
- $this->fail(500, '参数校验错误');
- }
- $type = intval($type);
- $duration = intval($duration);
- if (!in_array($type, [1, 2]) || !in_array($duration, [1, 3, 6, 12])) {
- $this->fail(500, '参数校验错误');
- }
- $user = $this->getUser();
- $userVipPrice = Db::table('tb_user_vip_price')
- ->where(['user_id' => $user->user_id])
- ->where(['type' => $type])
- ->count();
- $priceList = $type == 1 ? $this->platinumVip : $this->diamondVip;
- if ($userVipPrice <= 0) {
- //插入月付价格
- Db::table('tb_user_vip_price')
- ->insert([
- 'user_id' => $user->user_id,
- 'type' => $type,
- 'duration' => 1,
- 'price' => $priceList[0],
- ]);
- //插入季付价格
- Db::table('tb_user_vip_price')
- ->insert([
- 'user_id' => $user->user_id,
- 'type' => $type,
- 'duration' => 3,
- 'price' => $priceList[1],
- ]);
- //插入半年付价格
- Db::table('tb_user_vip_price')
- ->insert([
- 'user_id' => $user->user_id,
- 'type' => $type,
- 'duration' => 6,
- 'price' => $priceList[2],
- ]);
- //插入一年付价格
- Db::table('tb_user_vip_price')
- ->insert([
- 'user_id' => $user->user_id,
- 'type' => $type,
- 'duration' => 12,
- 'price' => $priceList[3],
- ]);
- }
- $data = Db::table('tb_user_vip_price')
- ->where(['user_id' => $user->user_id])
- ->where(['type' => $type])
- ->where(['duration' => $duration])
- ->find();
- $vipPrice = $data['price'];
- $balanceUser = Db::table('tb_user')
- ->where(['id' => $user->user_id])
- ->find();
- if ($balanceUser['balance'] < $vipPrice) {
- $this->fail(501, '余额不足无法开通');
- }
- //扣除余额
- Db::table('tb_user')
- ->where(['id' => $user->user_id])
- ->update([
- 'balance' => $balanceUser['balance'] - $vipPrice,
- ]);
- //开通VIP
- $vip = Db::table('tb_user_vip')
- ->where(['user_id' => $user->user_id])
- ->find();
- $startTime = 0;
- $expiredTime = 0;
- if (!$vip) {
- $startTime = time();
- $expiredTime = time() + 60 * 60 * 24 * 30 * $duration;
- Db::table('tb_user_vip')
- ->insert([
- 'user_id' => $user->user_id,
- 'type' => $type,
- 'created_at' => time(),
- 'expired_at' => time() + 60 * 60 * 24 * 30 * $duration,
- ]);
- } else {
- $expired = false;
- if ($vip['expired_at'] < time()) {
- $expired = true;
- }
- $startTime = time();
- $expiredTime = $expired ? time() + 60 * 60 * 24 * 30 * $duration : $vip['expired_at'] + 60 * 60 * 24 * 30 * $duration;
- Db::table('tb_user_vip')
- ->where(['user_id' => $user->user_id])
- ->update([
- 'type' => $type,
- 'expired_at' => $expired ? time() + 60 * 60 * 24 * 30 * $duration : $vip['expired_at'] + 60 * 60 * 24 * 30 * $duration,
- ]);
- }
- //写VIP购买记录
- Db::table('tb_user_vip_record')
- ->insert([
- 'user_id' => $user->user_id,
- 'price' => $vipPrice,
- 'type' => $type,
- 'duration' => $duration,
- 'start_time' => $startTime,
- 'expired_at' => $expiredTime,
- 'created_at' => time(),
- ]);
- $this->success('success', null);
- }
- }
|