[接口]鉴权authToken用户登录login-注册register-退出logout, 并将用户信息保存到redis中

This commit is contained in:
2026-03-04 11:39:11 +08:00
parent ad56d6d4ce
commit 77a898df22
10 changed files with 585 additions and 7 deletions

View File

@@ -7,6 +7,7 @@
namespace app\dice\model\player;
use plugin\saiadmin\basic\think\BaseModel;
use app\dice\model\lottery_config\DiceLotteryConfig;
/**
* 大富翁-玩家模型
@@ -16,6 +17,7 @@ use plugin\saiadmin\basic\think\BaseModel;
* @property $id ID
* @property $username 用户名
* @property $phone 手机
* @property $uid uid
* @property $name 昵称
* @property $password 密码
* @property $status 状态
@@ -47,6 +49,68 @@ class DicePlayer extends BaseModel
*/
protected $table = 'dice_player';
/**
* 新增前:生成唯一 uid昵称 name 默认使用 uid
* 用 try-catch 避免表尚未含 uid 时 getAttr/getData 抛 InvalidArgumentException
*/
public static function onBeforeInsert($model): void
{
parent::onBeforeInsert($model);
try {
$uid = $model->getAttr('uid');
} catch (\Throwable $e) {
$uid = null;
}
if ($uid === null || $uid === '') {
$uid = self::generateUid();
$model->setAttr('uid', $uid);
}
try {
$name = $model->getAttr('name');
} catch (\Throwable $e) {
$name = null;
}
if ($name === null || $name === '') {
$model->setAttr('name', $uid);
}
// 彩金池权重默认取 type=0 的奖池配置
self::setDefaultWeightsFromLotteryConfig($model);
}
/**
* 从 DiceLotteryConfig type=0 取 t1_wightt5_wight 作为玩家未设置时的默认值
*/
protected static function setDefaultWeightsFromLotteryConfig(DicePlayer $model): void
{
$config = DiceLotteryConfig::where('type', 0)->find();
if (!$config) {
return;
}
$fields = ['t1_wight', 't2_wight', 't3_wight', 't4_wight', 't5_wight'];
foreach ($fields as $field) {
try {
$val = $model->getAttr($field);
} catch (\Throwable $e) {
$val = null;
}
if ($val === null || $val === '') {
try {
$model->setAttr($field, $config->getAttr($field));
} catch (\Throwable $e) {
// 忽略字段不存在
}
}
}
}
/**
* 生成唯一标识 uid12 位十六进制)
*/
public static function generateUid(): string
{
return strtoupper(substr(bin2hex(random_bytes(6)), 0, 12));
}
/**
* 用户名 搜索
*/