10
This commit is contained in:
Executable
+142
@@ -0,0 +1,142 @@
|
||||
<?php
|
||||
namespace app\api\controller;
|
||||
use support\Request;
|
||||
use support\Response;
|
||||
use support\think\Db;
|
||||
use hg\apidoc\annotation as Apidoc;
|
||||
|
||||
/**
|
||||
* VIP
|
||||
*/
|
||||
class ThaliController extends BaseController{
|
||||
/**
|
||||
* 不需要鉴权的方法
|
||||
* @var array
|
||||
*/
|
||||
public $noNeedAuth = ['*'];
|
||||
/**
|
||||
* 无需登录及鉴权的方法
|
||||
* @var array
|
||||
*/
|
||||
public $noNeedLogin = ['recent','list'];
|
||||
|
||||
/**
|
||||
* @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');
|
||||
|
||||
$model = \app\model\Thali::with(['Role'])->where('status',1)->order('id asc');
|
||||
|
||||
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);
|
||||
}
|
||||
/**
|
||||
* @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);
|
||||
}
|
||||
|
||||
/**
|
||||
* @Apidoc\Title("最近购买列表")
|
||||
* @Apidoc\Method("GET")
|
||||
*/
|
||||
public function recent(Request $request){
|
||||
$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();
|
||||
$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")
|
||||
* @Apidoc\Param("id", type="string",require=true, desc="要购买的ID")
|
||||
* @Apidoc\Param("quantity", type="string",require=true, desc="要购买的数量(单位月)")
|
||||
* @Apidoc\Param("trade_password", type="string",require=true, desc="交易密码")
|
||||
*/
|
||||
function buy(Request $request): Response{
|
||||
$user = \support\Jwt::getUser();
|
||||
$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'));
|
||||
}
|
||||
$role_id = $thali->role_id;
|
||||
if($user->role_id >= $role_id){
|
||||
return $this->fail(__('Your level is too high to purchase this character'));
|
||||
}
|
||||
$price = $thali->price;
|
||||
if($quantity == 1){
|
||||
$price = $thali->month_price;
|
||||
}
|
||||
if($quantity == 3){
|
||||
$price = $thali->quarter_price;
|
||||
}
|
||||
if($quantity == 12){
|
||||
$price = $thali->year_price;
|
||||
}
|
||||
$amount = $price * $quantity;
|
||||
if($amount <=0){
|
||||
return $this->fail(__('This character group is not allowed to be sold'));
|
||||
}
|
||||
if($user->score < $amount){
|
||||
return $this->fail(__('Insufficient balance'));
|
||||
}
|
||||
\support\Jwt::verify_trade_password($request->post('trade_password'));
|
||||
$user = \support\Jwt::getUser();
|
||||
$user->role_id = $role_id;
|
||||
$user->expire_at = ($user->expire_at>time() ? $user->expire_at : time())+86400* $quantity * 30;
|
||||
$user->save();
|
||||
cache('user_role_'.$user->userID,[
|
||||
'role_id'=>$role_id,'expire_at'=>$user->expire_at
|
||||
],$user->expire_at-time());
|
||||
|
||||
\app\model\User::score($user->id,-$amount,\app\enum\BalanceType::PURCHASE_ROLE,$role_id);
|
||||
//Hook('user.roleup', $user);
|
||||
// $data = [
|
||||
// 'role_id' => $role_id,
|
||||
// 'user_id' => $user->id,
|
||||
// 'parent_id' => $user->parent_id,
|
||||
// 'amount' => $amount,
|
||||
// ];
|
||||
// Hook('role.buy', $data);
|
||||
return $this->success(__('successful'),$user);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user