feat: 设备登录新增 base_payload 字段,前端传入后存储到 user_device 表
All checks were successful
Build docker and publish / build (20.15.1) (push) Successful in 4m48s

This commit is contained in:
shanshanzhong 2026-04-20 20:08:20 -07:00
parent c0d839deb9
commit c8258dc93b
6 changed files with 44 additions and 27 deletions

View File

@ -149,11 +149,12 @@ type (
State string `form:"state"` State string `form:"state"`
} }
DeviceLoginRequest { DeviceLoginRequest {
Identifier string `json:"identifier" validate:"required"` Identifier string `json:"identifier" validate:"required"`
IP string `header:"X-Original-Forwarded-For"` IP string `header:"X-Original-Forwarded-For"`
UserAgent string `json:"user_agent" validate:"required"` UserAgent string `json:"user_agent" validate:"required"`
CfToken string `json:"cf_token,optional"` CfToken string `json:"cf_token,optional"`
ShortCode string `json:"short_code,optional"` ShortCode string `json:"short_code,optional"`
BasePayload string `json:"base_payload,optional"`
} }
GenerateCaptchaResponse { GenerateCaptchaResponse {
Id string `json:"id"` Id string `json:"id"`

View File

@ -0,0 +1 @@
ALTER TABLE `user_device` DROP COLUMN `base_payload`;

View File

@ -0,0 +1 @@
ALTER TABLE `user_device` ADD COLUMN `base_payload` TEXT DEFAULT NULL COMMENT 'Base Payload' AFTER `short_code`;

View File

@ -96,6 +96,17 @@ func (l *DeviceLoginLogic) DeviceLogin(req *types.DeviceLoginRequest) (resp *typ
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "query user failed: %v", err.Error()) return nil, errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "query user failed: %v", err.Error())
} }
// Update base_payload if provided
if req.BasePayload != "" && req.BasePayload != deviceInfo.BasePayload {
deviceInfo.BasePayload = req.BasePayload
if updateErr := l.svcCtx.UserModel.UpdateDevice(l.ctx, deviceInfo); updateErr != nil {
l.Errorw("update device base_payload failed",
logger.Field("device_id", deviceInfo.Id),
logger.Field("error", updateErr.Error()),
)
}
}
// 注销后 device auth_method 被删除,重新登录时需要补回 // 注销后 device auth_method 被删除,重新登录时需要补回
hasDeviceAuth := false hasDeviceAuth := false
for _, am := range userInfo.AuthMethods { for _, am := range userInfo.AuthMethods {
@ -220,13 +231,14 @@ func (l *DeviceLoginLogic) registerUserAndDevice(req *types.DeviceLoginRequest)
// Insert device record // Insert device record
deviceInfo := &user.Device{ deviceInfo := &user.Device{
Ip: req.IP, Ip: req.IP,
UserId: userInfo.Id, UserId: userInfo.Id,
UserAgent: req.UserAgent, UserAgent: req.UserAgent,
Identifier: req.Identifier, Identifier: req.Identifier,
ShortCode: req.ShortCode, ShortCode: req.ShortCode,
Enabled: true, BasePayload: req.BasePayload,
Online: false, Enabled: true,
Online: false,
} }
if err := db.Create(deviceInfo).Error; err != nil { if err := db.Create(deviceInfo).Error; err != nil {
l.Errorw("failed to insert device", l.Errorw("failed to insert device",

View File

@ -130,16 +130,17 @@ func (*AuthMethods) TableName() string {
} }
type Device struct { type Device struct {
Id int64 `gorm:"primaryKey"` Id int64 `gorm:"primaryKey"`
Ip string `gorm:"type:varchar(255);not null;comment:Device IP"` Ip string `gorm:"type:varchar(255);not null;comment:Device IP"`
UserId int64 `gorm:"index:idx_user_id;not null;comment:User ID"` UserId int64 `gorm:"index:idx_user_id;not null;comment:User ID"`
UserAgent string `gorm:"default:null;comment:UserAgent."` UserAgent string `gorm:"default:null;comment:UserAgent."`
Identifier string `gorm:"type:varchar(255);unique;index:idx_identifier;default:'';comment:Device Identifier"` Identifier string `gorm:"type:varchar(255);unique;index:idx_identifier;default:'';comment:Device Identifier"`
ShortCode string `gorm:"type:varchar(255);default:'';comment:Short Code"` ShortCode string `gorm:"type:varchar(255);default:'';comment:Short Code"`
Online bool `gorm:"default:false;not null;comment:Online"` BasePayload string `gorm:"type:text;default:null;comment:Base Payload"`
Enabled bool `gorm:"default:true;not null;comment:Enabled"` Online bool `gorm:"default:false;not null;comment:Online"`
CreatedAt time.Time `gorm:"<-:create;comment:Creation Time"` Enabled bool `gorm:"default:true;not null;comment:Enabled"`
UpdatedAt time.Time `gorm:"comment:Update Time"` CreatedAt time.Time `gorm:"<-:create;comment:Creation Time"`
UpdatedAt time.Time `gorm:"comment:Update Time"`
} }
func (*Device) TableName() string { func (*Device) TableName() string {

View File

@ -637,11 +637,12 @@ type DeviceAuthticateConfig struct {
} }
type DeviceLoginRequest struct { type DeviceLoginRequest struct {
Identifier string `json:"identifier" validate:"required"` Identifier string `json:"identifier" validate:"required"`
IP string `header:"X-Original-Forwarded-For"` IP string `header:"X-Original-Forwarded-For"`
UserAgent string `json:"user_agent" validate:"required"` UserAgent string `json:"user_agent" validate:"required"`
CfToken string `json:"cf_token,optional"` CfToken string `json:"cf_token,optional"`
ShortCode string `json:"short_code,optional"` ShortCode string `json:"short_code,optional"`
BasePayload string `json:"base_payload,optional"`
} }
type DissolveFamilyRequest struct { type DissolveFamilyRequest struct {