初始化代码
This commit is contained in:
340
application/api/model/Vip.php
Normal file
340
application/api/model/Vip.php
Normal file
@@ -0,0 +1,340 @@
|
||||
<?php
|
||||
|
||||
namespace app\api\model;
|
||||
|
||||
use think\Db;
|
||||
use think\Model;
|
||||
|
||||
class Vip extends Model
|
||||
{
|
||||
|
||||
//会员类型列表
|
||||
public function get_vip_list($uid){
|
||||
$config = get_uncache_system_config();
|
||||
$user_info = db::name('user')->where('uid', $uid)->find();
|
||||
if(!$user_info){
|
||||
return ['code' => 201, 'msg' => '参数错误', 'data' => null];
|
||||
}
|
||||
|
||||
if($user_info['vid'] > 0){
|
||||
$is_vip = 1;
|
||||
}else{
|
||||
$is_vip = 2;
|
||||
}
|
||||
|
||||
$vip_list = db::name('vip')->field('vid,name,day,price')->order('day asc')->select();
|
||||
|
||||
$data = [];
|
||||
$data['is_vip'] = $is_vip;
|
||||
$data['vip_end_time'] = $user_info['vip_end_time'];
|
||||
$data['user_vid'] = $user_info['vid'];
|
||||
$data['vip_list'] = $vip_list;
|
||||
$data['update_inviter_price'] = $config['update_inviter_price'];
|
||||
|
||||
return ['code' => 200, 'msg' => '获取成功', 'data' => $data];
|
||||
}
|
||||
|
||||
//购买会员
|
||||
public function pay_vip_order($uid, $vid){
|
||||
$user_info = db::name('user')->find($uid);
|
||||
if($user_info['is_teenager'] == 1){
|
||||
return ['code' => 201, 'msg' => '已开启青少年模式', 'data' => null];
|
||||
}
|
||||
$pay_money = 0;
|
||||
$give_integral = 0;
|
||||
|
||||
// if (!in_array($pay_type, [1, 2])) {
|
||||
// return ['code' => 201, 'msg' => '充值方式参数非法', 'data' => null];
|
||||
// }
|
||||
|
||||
if(!empty($vid)){
|
||||
$map = [];
|
||||
$map[] = ['vid', '=', $vid];
|
||||
$vip_info = db::name('vip')->where($map)->find();
|
||||
if(empty($vip_info)){
|
||||
return ['code' => 201, 'msg' => '会员卡类型不存在', 'data' => null];
|
||||
}
|
||||
if ($vip_info['price'] <= 0) {
|
||||
return ['code' => 201, 'msg' => '会员卡购买金额参数非法', 'data' => null];
|
||||
}
|
||||
$pay_money = $vip_info['price'];
|
||||
}else{
|
||||
return ['code' => 201, 'msg' => '会员卡类型不存在', 'data' => null];
|
||||
}
|
||||
|
||||
Db::startTrans();
|
||||
try {
|
||||
$pay_integral = $pay_money;
|
||||
|
||||
if($user_info['integral'] < $pay_integral){
|
||||
return ['code' => 201, 'msg' => '当前金币不足', 'data' => null];
|
||||
}
|
||||
//扣除对应金币
|
||||
|
||||
$reslut = model('admin/User')->change_user_money_by_uid($uid, -$pay_integral, 2, 36, '购买会员消费金币', $uid, 0, 0);
|
||||
if ($reslut['code'] == 201) {
|
||||
Db::rollback();
|
||||
return ['code' => 201, 'msg' => $reslut['msg'], 'data' => null];
|
||||
}
|
||||
|
||||
//增加用户会员
|
||||
$reslut = model('api/Vip')->add_user_vip($uid, $vid);
|
||||
if($reslut['code'] == 201){
|
||||
Db::rollback();
|
||||
return ['code' => 201, 'msg' => $reslut['msg'], 'data' => null];
|
||||
}
|
||||
|
||||
Db::commit();
|
||||
return ['code' => 200, 'msg' => "成功", 'data' => null];
|
||||
} catch (\Exception $e) {
|
||||
dump($e);
|
||||
//回滚事务
|
||||
Db::rollback();
|
||||
return ['code' => 201, 'msg' => "失败", 'data' => null];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//增加用户会员
|
||||
public function add_user_vip($uid, $vid){
|
||||
$user_info = db::name('user')->where('uid', $uid)->find();
|
||||
if(!$user_info){
|
||||
return ['code' => 201, 'msg' => '参数错误', 'data' => null];
|
||||
}
|
||||
|
||||
$vip_info = db::name('vip')->where('vid', $vid)->find();
|
||||
if(!$vip_info){
|
||||
return ['code' => 201, 'msg' => '该会员类型不存在', 'data' => null];
|
||||
}
|
||||
|
||||
Db::startTrans();
|
||||
try {
|
||||
//当前用户是否有会员
|
||||
if(empty($user_info['vid'])){
|
||||
$update = [];
|
||||
$update['vid'] = $vid;
|
||||
$update['vip_end_time'] = time() + (60*60*24*$vip_info['day']);
|
||||
$update['update_time'] = time();
|
||||
$reslut = db::name('user')->where('uid', $uid)->update($update);
|
||||
|
||||
}else{
|
||||
$update = [];
|
||||
$update['vid'] = $vid;
|
||||
if($user_info['vip_end_time'] > time()){
|
||||
$vip_end_time = $user_info['vip_end_time'] + (60*60*24*$vip_info['day']);
|
||||
}else{
|
||||
$vip_end_time = time() + (60*60*24*$vip_info['day']);
|
||||
}
|
||||
$update['vip_end_time'] = $vip_end_time;
|
||||
$update['update_time'] = time();
|
||||
$reslut = db::name('user')->where('uid', $uid)->update($update);
|
||||
|
||||
}
|
||||
if(!$reslut){
|
||||
Db::rollback();
|
||||
return ['code' => 201, 'msg' => '失败', 'data' => null];
|
||||
}
|
||||
|
||||
//记录
|
||||
$insert = [];
|
||||
$insert['uid'] = $uid;
|
||||
$insert['vid'] = $vid;
|
||||
$insert['day'] = $vip_info['day'];
|
||||
$insert['price'] = $vip_info['price'];
|
||||
$insert['add_time'] = time();
|
||||
$reslut = db::name('user_vip_log')->insert($insert);
|
||||
if(!$reslut){
|
||||
Db::rollback();
|
||||
return ['code' => 201, 'msg' => '失败', 'data' => null];
|
||||
}
|
||||
|
||||
Db::commit();
|
||||
return ['code' => 200, 'msg' => "成功", 'data' => null];
|
||||
} catch (\Exception $e) {
|
||||
dump($e);
|
||||
//回滚事务
|
||||
Db::rollback();
|
||||
return ['code' => 201, 'msg' => "失败", 'data' => null];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//会员邀请收益
|
||||
public function vip_invite_earnings($uid, $money, $integral){
|
||||
$user_info = db::name('user')->where('uid', $uid)->find();
|
||||
if(!$user_info){
|
||||
return ['code' => 201, 'msg' => '参数错误', 'data' => null];
|
||||
}
|
||||
|
||||
Db::startTrans();
|
||||
try {
|
||||
//是否有上级
|
||||
if(!empty($user_info['pid'])){
|
||||
//上级是否是会员
|
||||
$pid_info = db::name('user')->where('uid', $user_info['pid'])->find();
|
||||
if(!empty($pid_info)){
|
||||
$send_money = $money * 0.009;
|
||||
if($send_money > 0){
|
||||
$reslut = model('admin/User')->change_user_money_by_uid($user_info['pid'], $send_money, 1, 22, "会员邀请收益", $uid, 0);
|
||||
if ($reslut['code'] != 200) {
|
||||
Db::rollback();
|
||||
return ['code' => 201, 'msg' => $reslut['msg'], 'data' => null];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Db::commit();
|
||||
return ['code' => 200, 'msg' => "成功", 'data' => null];
|
||||
} catch (\Exception $e) {
|
||||
//回滚事务
|
||||
Db::rollback();
|
||||
return ['code' => 201, 'msg' => "失败", 'data' => null];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//会员换绑回调
|
||||
public function user_update_inviter($uid, $invite_uid){
|
||||
$user_info = db::name('user')->where('uid', $uid)->find();
|
||||
if(!$user_info){
|
||||
return ['code' => 201, 'msg' => '参数错误', 'data' => null];
|
||||
}
|
||||
|
||||
$inviter_info = db::name('user')->where('uid', $invite_uid)->find();
|
||||
if(!$inviter_info){
|
||||
return ['code' => 201, 'msg' => '参数错误', 'data' => null];
|
||||
}
|
||||
|
||||
$path = $inviter_info['path'].','.$uid;
|
||||
$update = [];
|
||||
$update['pid'] = $invite_uid;
|
||||
$update['path'] = $path;
|
||||
$update['update_time'] = time();
|
||||
$reslut = db::name('user')->where('uid', $uid)->update($update);
|
||||
if($reslut){
|
||||
return ['code' => 200, 'msg' => '成功', 'data' => null];
|
||||
}else{
|
||||
return ['code' => 201, 'msg' => '失败', 'data' => null];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//换绑信息
|
||||
public function get_update_invite_info($uid){
|
||||
$config = get_uncache_system_config();
|
||||
$user_info = db::name('user')->where('uid', $uid)->find();
|
||||
if(!$user_info){
|
||||
return ['code' => 201, 'msg' => '参数错误', 'data' => null];
|
||||
}
|
||||
|
||||
$inviter_nick_name = '';
|
||||
if(!empty($user_info['pid'])){
|
||||
$inviter_info = db::name('user')->where('uid', $user_info['pid'])->find();
|
||||
$inviter_nick_name = mb_convert_encoding(base64_decode($inviter_info['base64_nick_name']), 'UTF-8', 'UTF-8');
|
||||
}
|
||||
|
||||
|
||||
$data = [];
|
||||
$data['update_inviter_price'] = $config['update_inviter_price'];
|
||||
$data['invite_uid'] = $user_info['pid'];
|
||||
$data['inviter_nick_name'] = $inviter_nick_name;
|
||||
return ['code' => 200, 'msg' => '获取成功', 'data' => $data];
|
||||
}
|
||||
|
||||
//更换邀请人
|
||||
public function update_user_inviter($uid, $user_id, $sms_code){
|
||||
$config = get_uncache_system_config();
|
||||
|
||||
$user_info = db::name('user')->where('uid', $uid)->find();
|
||||
if(!$user_info){
|
||||
return ['code' => 201, 'msg' => '参数错误', 'data' => null];
|
||||
}
|
||||
if($user_info['is_teenager'] == 1){
|
||||
return ['code' => 201, 'msg' => '已开启青少年模式', 'data' => null];
|
||||
}
|
||||
|
||||
if($user_info['user_name'] == $uid){
|
||||
return ['code' => 201, 'msg' => '该用户尚未绑定手机号', 'data' => null];
|
||||
}
|
||||
|
||||
$reslut = model('sms')->verification_code($user_info['user_name'], $sms_code);
|
||||
if ($reslut['code'] == 201) {
|
||||
return ajaxReturn($reslut['code'], $reslut['msg'], $reslut['data']);
|
||||
}
|
||||
|
||||
$pay_money = 0;
|
||||
$give_integral = 0;
|
||||
|
||||
$inviter_info = db::name('user')->where('uid', $user_id)->find();
|
||||
if(!$inviter_info){
|
||||
return ['code' => 201, 'msg' => '更换邀请人不存在', 'data' => null];
|
||||
}
|
||||
|
||||
if(empty($inviter_info['vid'])){
|
||||
return ['code' => 201, 'msg' => '更换邀请人不是会员', 'data' => null];
|
||||
}
|
||||
|
||||
if($inviter_info['vip_end_time'] - time() < (60*60*24*30)){
|
||||
return ['code' => 201, 'msg' => '更换邀请人会员少于30天', 'data' => null];
|
||||
}
|
||||
|
||||
// if (!in_array($pay_type, [1, 2])) {
|
||||
// return ['code' => 201, 'msg' => '充值方式参数非法', 'data' => null];
|
||||
// }
|
||||
|
||||
if(empty($config['update_inviter_price']) && $config['update_inviter_price'] < 0){
|
||||
return ['code' => 201, 'msg' => '换绑支付金额不能为空且不能小于0', 'data' => null];
|
||||
}
|
||||
|
||||
$pay_money = $config['update_inviter_price'];
|
||||
|
||||
Db::startTrans();
|
||||
try {
|
||||
//扣除对应金币
|
||||
$pay_integral = $pay_money;
|
||||
if($user_info['integral'] < $pay_integral){
|
||||
return ['code' => 201, 'msg' => '当前金币不足', 'data' => null];
|
||||
}
|
||||
$reslut = model('admin/User')->change_user_money_by_uid($uid, -$pay_integral, 2, 37, '更换邀请人消费金币', $uid, 0, 0);
|
||||
if ($reslut['code'] == 201) {
|
||||
Db::rollback();
|
||||
return ['code' => 201, 'msg' => $reslut['msg'], 'data' => null];
|
||||
}
|
||||
|
||||
//更换邀请人
|
||||
$reslut = model('api/Vip')->user_update_inviter($uid, $user_id);
|
||||
if ($reslut['code'] == 201) {
|
||||
Db::rollback();
|
||||
return ['code' => 201, 'msg' => $reslut['msg'], 'data' => null];
|
||||
}
|
||||
|
||||
Db::commit();
|
||||
return ['code' => 200, 'msg' => "成功", 'data' => null];
|
||||
} catch (\Exception $e) {
|
||||
//回滚事务
|
||||
Db::rollback();
|
||||
return ['code' => 201, 'msg' => "失败", 'data' => null];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//清除到期用户vip
|
||||
public function close_user_vip(){
|
||||
set_time_limit(0);
|
||||
ini_set('memory_limit', '1024M');
|
||||
|
||||
$map = [];
|
||||
$map[] = ['vid', '>', 0];
|
||||
$map[] = ['vip_end_time', '<', time()];
|
||||
$vip_list = db::name('user')->where($map)->select();
|
||||
if(!empty($vip_list)){
|
||||
db::name('user')->where($map)->update(['vid' => 0, 'vip_end_time' => 0, 'update_time' => 0]);
|
||||
}
|
||||
|
||||
echo date('Y-m-d H:i:s').'执行成功';
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user