Files

164 lines
5.4 KiB
PHP
Raw Permalink Normal View History

2025-11-07 09:56:20 +08:00
<?php
namespace app\api\controller;
use support\Request;
use support\Response;
use support\think\Db;
use hg\apidoc\annotation as Apidoc;
/**
2026-02-21 08:21:05 +08:00
* VIP
2025-11-07 09:56:20 +08:00
*/
2026-02-21 08:21:05 +08:00
class ThaliController extends BaseController{
2025-11-07 09:56:20 +08:00
/**
* 不需要鉴权的方法
* @var array
*/
public $noNeedAuth = ['*'];
/**
* 无需登录及鉴权的方法
* @var array
*/
2026-02-21 08:21:05 +08:00
public $noNeedLogin = ['recent','list'];
2025-11-07 09:56:20 +08:00
/**
* @Apidoc\Title("列表")
* @Apidoc\Method("GET")
* @Apidoc\Param("page", type="int",require=false, desc="页码")
* @Apidoc\Param("limit", type="int",require=false, desc="分页大小")
*/
public function list(Request $request){
$limit = $request->get('limit',10);
$kw = $request->get('kw');
2026-02-21 08:21:05 +08:00
$model = \app\model\Thali::with(['Role'])->where('status',1)->order('id asc');
2025-11-07 09:56:20 +08:00
if($limit == 'all' || $limit >= 999999){
$result = $model->select();
$result= \think\Paginator::make($result, 99999999999, 1, count($result));
}else{
$result = $model->paginate($limit);
}
return $this->success(__('successful'),$result);
}
2026-02-21 08:21:05 +08:00
/**
* @Apidoc\Title("当前角色信息")
* @Apidoc\Method("GET")
* @Apidoc\NotParse()
* @Apidoc\NotDebug()
*/
function detail(){
$user = \support\Jwt::getUser();
$data = \app\model\UserRole::where('id',$user->role_id)->field('name,id,price')->find();
return $this->success(__('successful'),$data);
}
2025-11-07 09:56:20 +08:00
/**
* @Apidoc\Title("最近购买列表")
* @Apidoc\Method("GET")
*/
public function recent(Request $request){
2026-02-21 08:21:05 +08:00
$list = (new \app\model\BalanceLog)->setSuffix('_score')
->where('type',\app\enum\BalanceType::PURCHASE_ROLE->value)
->order('created_at','desc')
->limit(0,10)
->field('user_id,created_at,memo')
->select();
2025-11-07 09:56:20 +08:00
$list = $list->toArray();
$data = [];
$role_list = \app\model\UserRole::where('id', '>',1)
->column('name','id');
foreach($list as $v){
$data[] = [
'username' => \app\model\User::where('id',$v['user_id'])->value('username'),
'created_at' => $v['created_at'],
'v' => $v,
//'role' => $role_list[str_replace('购买用户组:','',$v['memo'])]
'role' => 'K'.str_replace('购买用户组:','',$v['memo'])
];
}
return $this->success(__('successful'),$data);
}
/**
* @Apidoc\Title("购买")
* @Apidoc\Method("POST")
2026-02-21 08:21:05 +08:00
* @Apidoc\Param("id", type="string",require=true, desc="要购买的ID")
* @Apidoc\Param("quantity", type="string",require=true, desc="要购买的数量(单位月)")
2025-11-07 09:56:20 +08:00
* @Apidoc\Param("trade_password", type="string",require=true, desc="交易密码")
*/
function buy(Request $request): Response{
$user = \support\Jwt::getUser();
2026-02-21 08:21:05 +08:00
$id = (int)$request->post('id');
//数量
$quantity = (int)$request->post('quantity',1);
/**
* @var \app\model\Thali $thali
*/
$thali = \app\model\Thali::where('id',$id)->find();
if(!$thali){
return $this->fail(__('Role does not exist'));
2025-11-07 09:56:20 +08:00
}
2026-02-21 08:21:05 +08:00
$role_id = $thali->role_id;
2026-02-24 21:02:17 +08:00
if($user->role_id > $role_id){
2025-11-07 09:56:20 +08:00
return $this->fail(__('Your level is too high to purchase this character'));
}
2026-02-21 08:21:05 +08:00
$price = $thali->price;
2026-02-24 21:02:17 +08:00
2026-02-21 08:21:05 +08:00
if($quantity == 1){
$price = $thali->month_price;
}
if($quantity == 3){
$price = $thali->quarter_price;
2025-11-07 09:56:20 +08:00
}
2026-02-21 08:21:05 +08:00
if($quantity == 12){
$price = $thali->year_price;
}
2026-03-25 02:48:30 +08:00
//新开通
$isNew=false;
if(is_null($user->role_id)){
$isNew = true;
}
2026-02-24 21:02:17 +08:00
//升级
$isUpgrade=true;
//续费
if($user->role_id == $role_id){
$isUpgrade = false;
}
$amount = $price;
if($isUpgrade){
//按那个价格算,目前是按原价,剩余时间不做抵扣
}
//$amount = $price * $quantity;
2026-02-21 08:21:05 +08:00
if($amount <=0){
2025-11-07 09:56:20 +08:00
return $this->fail(__('This character group is not allowed to be sold'));
}
2026-02-21 08:21:05 +08:00
if($user->score < $amount){
2025-11-07 09:56:20 +08:00
return $this->fail(__('Insufficient balance'));
}
\support\Jwt::verify_trade_password($request->post('trade_password'));
2026-02-21 08:21:05 +08:00
$user = \support\Jwt::getUser();
$user->expire_at = ($user->expire_at>time() ? $user->expire_at : time())+86400* $quantity * 30;
2026-02-24 21:02:17 +08:00
if($isUpgrade){
$user->expire_at = (time())+86400* $quantity * 30;
$user->role_id = $role_id;
}
2026-02-21 08:21:05 +08:00
$user->save();
cache('user_role_'.$user->userID,[
'role_id'=>$role_id,'expire_at'=>$user->expire_at
],$user->expire_at-time());
2026-02-24 21:02:17 +08:00
\app\model\User::score($user->id,-$amount,\app\enum\BalanceType::PURCHASE_ROLE,json_encode(['role_id'=>$role_id,'quantity'=>$quantity,'role_name'=>$thali->title]));
2026-02-28 16:18:52 +08:00
cache('user_rights_'.$user->id,null);
2026-03-25 02:48:30 +08:00
if($isNew){
Hook('user.role_up', $user);
}
$data = [
'role_id' => $role_id,
'user_id' => $user->id,
'amount' => $amount,
];
Hook('user.role_buy', $data);
2026-02-21 08:21:05 +08:00
return $this->success(__('successful'),$user);
2025-11-07 09:56:20 +08:00
}
}