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); } }