208 lines
7.2 KiB
PHP
208 lines
7.2 KiB
PHP
<?php
|
||
declare(strict_types=1);
|
||
|
||
namespace app\api\controller;
|
||
|
||
use support\Request;
|
||
use support\Response;
|
||
use app\api\cache\UserCache;
|
||
use app\api\logic\UserLogic;
|
||
use app\api\util\ReturnCode;
|
||
use app\dice\model\play_record\DicePlayRecord;
|
||
use app\dice\model\player_wallet_record\DicePlayerWalletRecord;
|
||
use plugin\saiadmin\basic\OpenController;
|
||
|
||
/**
|
||
* API 用户登录/注册
|
||
* 需先携带 auth-token,登录/注册成功后返回 user-token 与用户信息,用户信息已写入 Redis(key=base64(user_id),value=加密)
|
||
*/
|
||
class UserController extends OpenController
|
||
{
|
||
/**
|
||
* 登录
|
||
* POST /api/user/login
|
||
* body: phone (+60), password
|
||
*/
|
||
public function login(Request $request): Response
|
||
{
|
||
$phone = $request->post('phone', '');
|
||
$password = $request->post('password', '');
|
||
if ($phone === '' || $password === '') {
|
||
return $this->fail('请填写手机号和密码', ReturnCode::PARAMS_ERROR);
|
||
}
|
||
$logic = new UserLogic();
|
||
$data = $logic->login($phone, $password);
|
||
return $this->success([
|
||
'user' => $data['user'],
|
||
'user-token' => $data['user-token'],
|
||
'user_id' => $data['user_id'],
|
||
]);
|
||
}
|
||
|
||
/**
|
||
* 注册
|
||
* POST /api/user/register
|
||
* body: phone (+60), password, nickname(可选)
|
||
*/
|
||
public function register(Request $request): Response
|
||
{
|
||
$phone = $request->post('phone', '');
|
||
$password = $request->post('password', '');
|
||
$nickname = $request->post('nickname');
|
||
if ($phone === '' || $password === '') {
|
||
return $this->fail('请填写手机号和密码', ReturnCode::PARAMS_ERROR);
|
||
}
|
||
$logic = new UserLogic();
|
||
$data = $logic->register($phone, $password, $nickname ? (string) $nickname : null);
|
||
return $this->success([
|
||
'user' => $data['user'],
|
||
'user-token' => $data['user-token'],
|
||
'user_id' => $data['user_id'],
|
||
]);
|
||
}
|
||
|
||
/**
|
||
* 退出登录
|
||
* POST /api/user/logout
|
||
* header: user-token(由 CheckUserTokenMiddleware 校验并注入 request->userToken)
|
||
*/
|
||
public function logout(Request $request): Response
|
||
{
|
||
$token = $request->userToken ?? UserLogic::getTokenFromRequest($request);
|
||
if ($token === '' || !UserLogic::logout($token)) {
|
||
return $this->fail('退出失败或 token 已失效', ReturnCode::TOKEN_INVALID);
|
||
}
|
||
return $this->success('已退出登录');
|
||
}
|
||
|
||
/**
|
||
* 获取当前用户信息
|
||
* GET /api/user/info
|
||
* header: user-token(由 CheckUserTokenMiddleware 校验并注入 request->user_id)
|
||
* 返回:id, username, phone, uid, name, coin, total_draw_count
|
||
*/
|
||
public function info(Request $request): Response
|
||
{
|
||
$userId = UserLogic::getUserIdFromRequest($request) ?? 0;
|
||
$user = UserLogic::getCachedUser($userId);
|
||
if (empty($user)) {
|
||
return $this->fail('用户不存在', ReturnCode::NOT_FOUND);
|
||
}
|
||
$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(由 CheckUserTokenMiddleware 校验并注入 request->user_id)
|
||
*/
|
||
public function balance(Request $request): Response
|
||
{
|
||
$userId = UserLogic::getUserIdFromRequest($request) ?? 0;
|
||
$user = UserLogic::getCachedUser($userId);
|
||
if (empty($user)) {
|
||
return $this->fail('用户不存在', ReturnCode::NOT_FOUND);
|
||
}
|
||
$coin = $user['coin'] ?? 0;
|
||
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'] ?? '',
|
||
]);
|
||
}
|
||
|
||
/**
|
||
* 玩家钱包流水
|
||
* GET /api/user/walletRecord
|
||
* header: user-token(由 CheckUserTokenMiddleware 校验并注入 request->user_id)
|
||
* 参数: page 页码(默认1), limit 每页条数(默认10), create_time_min/create_time_max 创建时间范围(可选)
|
||
*/
|
||
public function walletRecord(Request $request): Response
|
||
{
|
||
$userId = UserLogic::getUserIdFromRequest($request) ?? 0;
|
||
$page = (int) $request->post('page', 1);
|
||
$limit = (int) $request->post('limit', 10);
|
||
if ($page < 1) {
|
||
$page = 1;
|
||
}
|
||
if ($limit < 1) {
|
||
$limit = 10;
|
||
}
|
||
|
||
$query = DicePlayerWalletRecord::where('player_id', $userId)->order('id', 'desc');
|
||
|
||
$createTimeMin = $request->post('create_time_min', '');
|
||
$createTimeMax = $request->post('create_time_max', '');
|
||
if ($createTimeMin !== '' && $createTimeMin !== null) {
|
||
$query->where('create_time', '>=', $createTimeMin);
|
||
}
|
||
if ($createTimeMax !== '' && $createTimeMax !== null) {
|
||
$query->where('create_time', '<=', $createTimeMax);
|
||
}
|
||
|
||
$total = $query->count();
|
||
$list = $query->page($page, $limit)->select()->toArray();
|
||
$totalPage = $limit > 0 ? (int) ceil($total / $limit) : 0;
|
||
|
||
return $this->success([
|
||
'list' => $list,
|
||
'total_count' => $total,
|
||
'total_page' => $totalPage,
|
||
'current_page' => $page,
|
||
'limit' => $limit,
|
||
]);
|
||
}
|
||
|
||
/**
|
||
* 游玩记录
|
||
* GET /api/user/playGameRecord
|
||
* header: user-token(由 CheckUserTokenMiddleware 校验并注入 request->user_id)
|
||
* 参数: page 页码(默认1), limit 每页条数(默认10), create_time_min/create_time_max 创建时间范围(可选)
|
||
*/
|
||
public function playGameRecord(Request $request): Response
|
||
{
|
||
$userId = UserLogic::getUserIdFromRequest($request) ?? 0;
|
||
$page = (int) $request->post('page', 1);
|
||
$limit = (int) $request->post('limit', 10);
|
||
if ($page < 1) {
|
||
$page = 1;
|
||
}
|
||
if ($limit < 1) {
|
||
$limit = 10;
|
||
}
|
||
|
||
$query = DicePlayRecord::where('player_id', $userId)->order('id', 'desc');
|
||
|
||
$createTimeMin = $request->post('create_time_min', '');
|
||
$createTimeMax = $request->post('create_time_max', '');
|
||
if ($createTimeMin !== '' && $createTimeMin !== null) {
|
||
$query->where('create_time', '>=', $createTimeMin);
|
||
}
|
||
if ($createTimeMax !== '' && $createTimeMax !== null) {
|
||
$query->where('create_time', '<=', $createTimeMax);
|
||
}
|
||
|
||
$total = $query->count();
|
||
$list = $query->page($page, $limit)->select()->toArray();
|
||
$totalPage = $limit > 0 ? (int) ceil($total / $limit) : 0;
|
||
|
||
return $this->success([
|
||
'list' => $list,
|
||
'total_count' => $total,
|
||
'total_page' => $totalPage,
|
||
'current_page' => $page,
|
||
'limit' => $limit,
|
||
]);
|
||
}
|
||
}
|