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; } //升级 $isUpgrade=true; //续费 if($user->role_id == $role_id){ $isUpgrade = false; } $amount = $price; if($isUpgrade){ //按那个价格算,目前是按原价,剩余时间不做抵扣 } //$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->expire_at = ($user->expire_at>time() ? $user->expire_at : time())+86400* $quantity * 30; if($isUpgrade){ $user->expire_at = (time())+86400* $quantity * 30; $user->role_id = $role_id; } $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,json_encode(['role_id'=>$role_id,'quantity'=>$quantity,'role_name'=>$thali->title])); cache('user_rights_'.$user->id,null); //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); } }