[接口]鉴权authToken用户登录login-注册register-退出logout, 并将用户信息保存到redis中
This commit is contained in:
@@ -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_wight~t5_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) {
|
||||
// 忽略字段不存在
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成唯一标识 uid(12 位十六进制)
|
||||
*/
|
||||
public static function generateUid(): string
|
||||
{
|
||||
return strtoupper(substr(bin2hex(random_bytes(6)), 0, 12));
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户名 搜索
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user