[接口]新增获取用户信息接口user/info, 获取钱包余额接口
This commit is contained in:
20
server/app/api/cache/UserCache.php
vendored
20
server/app/api/cache/UserCache.php
vendored
@@ -75,6 +75,26 @@ class UserCache
|
||||
return is_array($data) ? $data : [];
|
||||
}
|
||||
|
||||
/**
|
||||
* 仅从缓存读取用户平台币 coin(不查库,低延迟)
|
||||
* @return int|float|null 余额,缓存未命中返回 null(缓存中 coin 可能为字符串,统一转为数值)
|
||||
*/
|
||||
public static function getUserCoin(int $userId): int|float|null
|
||||
{
|
||||
$user = self::getUser($userId);
|
||||
if (empty($user) || !array_key_exists('coin', $user)) {
|
||||
return null;
|
||||
}
|
||||
$coin = $user['coin'];
|
||||
if (is_int($coin) || is_float($coin)) {
|
||||
return $coin;
|
||||
}
|
||||
if (is_string($coin) && is_numeric($coin)) {
|
||||
return str_contains($coin, '.') ? (float) $coin : (int) $coin;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/** 删除用户缓存 */
|
||||
public static function deleteUser(int $userId): bool
|
||||
{
|
||||
|
||||
@@ -5,6 +5,7 @@ namespace app\api\controller;
|
||||
|
||||
use support\Request;
|
||||
use support\Response;
|
||||
use app\api\cache\UserCache;
|
||||
use app\api\logic\UserLogic;
|
||||
use plugin\saiadmin\basic\OpenController;
|
||||
|
||||
@@ -80,4 +81,77 @@ class UserController extends OpenController
|
||||
}
|
||||
return $this->fail('退出失败或 token 已失效');
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前用户信息
|
||||
* GET /api/user/info
|
||||
* header: user-token(或 Authorization: Bearer <user-token>)
|
||||
* 返回:id, username, phone, uid, name, coin, total_draw_count
|
||||
*/
|
||||
public function info(Request $request): Response
|
||||
{
|
||||
$token = $request->header('user-token');
|
||||
if (empty($token)) {
|
||||
$auth = $request->header('authorization');
|
||||
if ($auth && stripos($auth, 'Bearer ') === 0) {
|
||||
$token = trim(substr($auth, 7));
|
||||
}
|
||||
}
|
||||
if (empty($token)) {
|
||||
return $this->fail('请携带 user-token');
|
||||
}
|
||||
$userId = UserLogic::getUserIdFromToken($token);
|
||||
if ($userId === null) {
|
||||
return $this->fail('user-token 无效或已过期');
|
||||
}
|
||||
$user = UserLogic::getCachedUser($userId);
|
||||
if (empty($user)) {
|
||||
return $this->fail('用户不存在');
|
||||
}
|
||||
$fields = ['id', 'username', 'phone', 'uid', 'name', 'coin', 'total_draw_count'];
|
||||
$info = [];
|
||||
foreach ($fields as $field) {
|
||||
if (array_key_exists($field, $user)) {
|
||||
$info[$field] = $user[$field];
|
||||
}
|
||||
}
|
||||
return $this->success($info);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取钱包余额(仅读缓存,不查库,低延迟)
|
||||
* GET /api/user/balance
|
||||
* header: user-token(或 Authorization: Bearer <user-token>)
|
||||
* 返回:coin, phone, username(登录时已写入缓存,本接口只从缓存读取)
|
||||
*/
|
||||
public function balance(Request $request): Response
|
||||
{
|
||||
$token = $request->header('user-token');
|
||||
if (empty($token)) {
|
||||
$auth = $request->header('authorization');
|
||||
if ($auth && stripos($auth, 'Bearer ') === 0) {
|
||||
$token = trim(substr($auth, 7));
|
||||
}
|
||||
}
|
||||
if (empty($token)) {
|
||||
return $this->fail('请携带 user-token');
|
||||
}
|
||||
$userId = UserLogic::getUserIdFromToken($token);
|
||||
if ($userId === null) {
|
||||
return $this->fail('user-token 无效或已过期');
|
||||
}
|
||||
$user = UserCache::getUser($userId);
|
||||
if (empty($user)) {
|
||||
return $this->fail('缓存已过期,请重新登录');
|
||||
}
|
||||
$coin = $user['coin'] ?? null;
|
||||
if (is_string($coin) && is_numeric($coin)) {
|
||||
$coin = str_contains($coin, '.') ? (float) $coin : (int) $coin;
|
||||
}
|
||||
return $this->success([
|
||||
'coin' => $coin,
|
||||
'phone' => $user['phone'] ?? '',
|
||||
'username' => $user['username'] ?? '',
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,6 +21,8 @@ Route::group('/api', function () {
|
||||
Route::post('/user/login', [app\api\controller\UserController::class, 'login']);
|
||||
Route::post('/user/register', [app\api\controller\UserController::class, 'register']);
|
||||
Route::post('/user/logout', [app\api\controller\UserController::class, 'logout']);
|
||||
Route::get('/user/info', [app\api\controller\UserController::class, 'info']);
|
||||
Route::get('/user/balance', [app\api\controller\UserController::class, 'balance']);
|
||||
})->middleware([CheckApiAuthMiddleware::class]);
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user