v1.1
This commit is contained in:
144
app/main/api/internal/logic/user/authlogic.go
Normal file
144
app/main/api/internal/logic/user/authlogic.go
Normal file
@@ -0,0 +1,144 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"ycc-server/app/main/api/internal/svc"
|
||||
"ycc-server/app/main/api/internal/types"
|
||||
"ycc-server/app/main/model"
|
||||
"ycc-server/common/xerr"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
type AuthLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewAuthLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AuthLogic {
|
||||
return &AuthLogic{ctx: ctx, svcCtx: svcCtx}
|
||||
}
|
||||
|
||||
func (l *AuthLogic) Auth(req *types.AuthReq) (*types.AuthResp, error) {
|
||||
var userID string
|
||||
var userType int64
|
||||
var authType string
|
||||
var authKey string
|
||||
|
||||
switch req.Platform {
|
||||
case model.PlatformH5:
|
||||
authType = model.UserAuthTypeUUID
|
||||
authKey = uuid.NewString()
|
||||
user, err := l.findOrCreateUserByAuth(authType, authKey)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
userID = user.Id
|
||||
userType = l.getUserType(user)
|
||||
case model.PlatformWxH5:
|
||||
openid, err := l.svcCtx.VerificationService.GetWechatH5OpenID(l.ctx, req.Code)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "获取WxH5 OpenID失败: %v", err)
|
||||
}
|
||||
authType = model.UserAuthTypeWxh5OpenID
|
||||
authKey = openid
|
||||
userAuth, err := l.svcCtx.UserAuthModel.FindOneByAuthTypeAuthKey(l.ctx, authType, authKey)
|
||||
if err != nil && !errors.Is(err, model.ErrNotFound) {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "查询用户授权失败: %v", err)
|
||||
}
|
||||
if userAuth != nil {
|
||||
user, _ := l.svcCtx.UserModel.FindOne(l.ctx, userAuth.UserId)
|
||||
userID = user.Id
|
||||
userType = l.getUserType(user)
|
||||
} else {
|
||||
user, err := l.createUserWithAuth(authType, authKey)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
userID = user.Id
|
||||
userType = model.UserTypeTemp
|
||||
}
|
||||
case model.PlatformWxMini:
|
||||
openid, err := l.svcCtx.VerificationService.GetWechatMiniOpenID(l.ctx, req.Code)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "获取WxMini OpenID失败: %v", err)
|
||||
}
|
||||
authType = model.UserAuthTypeWxMiniOpenID
|
||||
authKey = openid
|
||||
userAuth, err := l.svcCtx.UserAuthModel.FindOneByAuthTypeAuthKey(l.ctx, authType, authKey)
|
||||
if err != nil && !errors.Is(err, model.ErrNotFound) {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "查询用户授权失败: %v", err)
|
||||
}
|
||||
if userAuth != nil {
|
||||
user, _ := l.svcCtx.UserModel.FindOne(l.ctx, userAuth.UserId)
|
||||
userID = user.Id
|
||||
userType = l.getUserType(user)
|
||||
} else {
|
||||
user, err := l.createUserWithAuth(authType, authKey)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
userID = user.Id
|
||||
userType = model.UserTypeTemp
|
||||
}
|
||||
default:
|
||||
return nil, errors.Wrapf(xerr.NewErrMsg("不支持的平台类型"), "platform=%s", req.Platform)
|
||||
}
|
||||
|
||||
token, err := l.svcCtx.UserService.GeneralUserToken(l.ctx, userID, userType)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "生成Token失败: %v", err)
|
||||
}
|
||||
now := time.Now().Unix()
|
||||
user, _ := l.svcCtx.UserModel.FindOne(l.ctx, userID)
|
||||
hasMobile := user.Mobile.Valid
|
||||
isAgent := false
|
||||
if hasMobile {
|
||||
agent, _ := l.svcCtx.AgentModel.FindOneByUserId(l.ctx, userID)
|
||||
isAgent = agent != nil
|
||||
}
|
||||
return &types.AuthResp{
|
||||
AccessToken: token,
|
||||
AccessExpire: now + l.svcCtx.Config.JwtAuth.AccessExpire,
|
||||
RefreshAfter: now + l.svcCtx.Config.JwtAuth.RefreshAfter,
|
||||
UserType: userType,
|
||||
HasMobile: hasMobile,
|
||||
IsAgent: isAgent,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (l *AuthLogic) findOrCreateUserByAuth(authType, authKey string) (*model.User, error) {
|
||||
userAuth, err := l.svcCtx.UserAuthModel.FindOneByAuthTypeAuthKey(l.ctx, authType, authKey)
|
||||
if err != nil && !errors.Is(err, model.ErrNotFound) {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "查找用户授权失败: %v", err)
|
||||
}
|
||||
if userAuth != nil {
|
||||
user, err := l.svcCtx.UserModel.FindOne(l.ctx, userAuth.UserId)
|
||||
return user, err
|
||||
}
|
||||
return l.createUserWithAuth(authType, authKey)
|
||||
}
|
||||
|
||||
func (l *AuthLogic) createUserWithAuth(authType, authKey string) (*model.User, error) {
|
||||
user := &model.User{Id: uuid.NewString()}
|
||||
_, err := l.svcCtx.UserModel.Insert(l.ctx, nil, user)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ua := &model.UserAuth{Id: uuid.NewString(), UserId: user.Id, AuthType: authType, AuthKey: authKey}
|
||||
_, err = l.svcCtx.UserAuthModel.Insert(l.ctx, nil, ua)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return l.svcCtx.UserModel.FindOne(l.ctx, user.Id)
|
||||
}
|
||||
|
||||
func (l *AuthLogic) getUserType(user *model.User) int64 {
|
||||
if user.Mobile.Valid {
|
||||
return model.UserTypeNormal
|
||||
}
|
||||
return model.UserTypeTemp
|
||||
}
|
||||
@@ -14,9 +14,11 @@ import (
|
||||
"ycc-server/common/xerr"
|
||||
"ycc-server/pkg/lzkit/crypto"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"github.com/zeromicro/go-zero/core/stores/redis"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
)
|
||||
|
||||
type BindMobileLogic struct {
|
||||
@@ -34,75 +36,207 @@ func NewBindMobileLogic(ctx context.Context, svcCtx *svc.ServiceContext) *BindMo
|
||||
}
|
||||
|
||||
func (l *BindMobileLogic) BindMobile(req *types.BindMobileReq) (resp *types.BindMobileResp, err error) {
|
||||
// 从上下文中获取当前登录态的用户声明(可能是临时用户或正式用户),包含UserId/AuthType/AuthKey
|
||||
claims, err := ctxdata.GetClaimsFromCtx(l.ctx)
|
||||
if err != nil && !errors.Is(err, ctxdata.ErrNoInCtx) {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "绑定手机号, %v", err)
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "获取用户信息失败: %v", err)
|
||||
}
|
||||
|
||||
// 当前登录用户信息(用于后续合并/绑定)
|
||||
currentUserID := claims.UserId
|
||||
currentAuthType := claims.AuthType
|
||||
currentAuthKey := claims.AuthKey
|
||||
|
||||
// 加密手机号(所有手机号以密文存储)
|
||||
secretKey := l.svcCtx.Config.Encrypt.SecretKey
|
||||
encryptedMobile, err := crypto.EncryptMobile(req.Mobile, secretKey)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "绑定手机号, 加密手机号失败: %v", err)
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "加密手机号失败: %v", err)
|
||||
}
|
||||
// 开发环境下跳过验证码校验
|
||||
// 非开发环境下校验短信验证码(从Redis读取并比对)
|
||||
if os.Getenv("ENV") != "development" {
|
||||
// 检查手机号是否在一分钟内已发送过验证码
|
||||
redisKey := fmt.Sprintf("%s:%s", "bindMobile", encryptedMobile)
|
||||
cacheCode, err := l.svcCtx.Redis.Get(redisKey)
|
||||
if err != nil {
|
||||
if errors.Is(err, redis.Nil) {
|
||||
return nil, errors.Wrapf(xerr.NewErrMsg("验证码已过期"), "手机登录, 验证码过期: %s", encryptedMobile)
|
||||
return nil, errors.Wrapf(xerr.NewErrMsg("验证码已过期"), "")
|
||||
}
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "手机登录, 读取验证码redis缓存失败, mobile: %s, err: %+v", encryptedMobile, err)
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "读取验证码失败: %v", err)
|
||||
}
|
||||
if cacheCode != req.Code {
|
||||
return nil, errors.Wrapf(xerr.NewErrMsg("验证码不正确"), "手机登录, 验证码不正确: %s", encryptedMobile)
|
||||
}
|
||||
}
|
||||
var userID int64
|
||||
user, err := l.svcCtx.UserModel.FindOneByMobile(l.ctx, sql.NullString{String: encryptedMobile, Valid: true})
|
||||
if err != nil && !errors.Is(err, model.ErrNotFound) {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "绑定手机号, %v", err)
|
||||
}
|
||||
if user != nil {
|
||||
// 进行平台绑定
|
||||
if claims != nil {
|
||||
if req.Mobile != "18889793585" {
|
||||
if claims.UserType == model.UserTypeTemp {
|
||||
userTemp, err := l.svcCtx.UserTempModel.FindOne(l.ctx, claims.UserId)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "绑定手机号, 读取临时用户失败: %v", err)
|
||||
}
|
||||
userAuth, err := l.svcCtx.UserAuthModel.FindOneByUserIdAuthType(l.ctx, user.Id, userTemp.AuthType)
|
||||
if err != nil && !errors.Is(err, model.ErrNotFound) {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "绑定手机号, 读取用户认证失败: %v", err)
|
||||
}
|
||||
if userAuth != nil && userAuth.AuthKey != userTemp.AuthKey {
|
||||
return nil, errors.Wrapf(xerr.NewErrMsg("该手机号已绑定其他微信号"), "绑定手机号, 临时用户已注册: %s", encryptedMobile)
|
||||
}
|
||||
err = l.svcCtx.UserService.TempUserBindUser(l.ctx, nil, user.Id)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "绑定手机号, 临时用户绑定用户失败: %+v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
userID = user.Id
|
||||
} else {
|
||||
// 创建账号,并绑定手机号
|
||||
userID, err = l.svcCtx.UserService.RegisterUser(l.ctx, encryptedMobile)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "绑定手机号, 注册用户失败: %+v", err)
|
||||
return nil, errors.Wrapf(xerr.NewErrMsg("验证码不正确"), "")
|
||||
}
|
||||
}
|
||||
|
||||
token, err := l.svcCtx.UserService.GeneralUserToken(l.ctx, userID, model.UserTypeNormal)
|
||||
// 通过加密后的手机号查找目标用户(手机号用户视为正式用户)
|
||||
targetUser, err := l.svcCtx.UserModel.FindOneByMobile(l.ctx, sql.NullString{String: encryptedMobile, Valid: true})
|
||||
if err != nil && !errors.Is(err, model.ErrNotFound) {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "查找用户失败: %v", err)
|
||||
}
|
||||
|
||||
var finalUserID string
|
||||
if targetUser == nil {
|
||||
// 手机号不存在:直接将当前用户升级为正式用户(写入mobile与mobile认证)
|
||||
finalUserID = currentUserID
|
||||
currentUser, err := l.svcCtx.UserModel.FindOne(l.ctx, currentUserID)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "查找当前用户失败: %v", err)
|
||||
}
|
||||
currentUser.Mobile = sql.NullString{String: encryptedMobile, Valid: true}
|
||||
if _, err := l.svcCtx.UserModel.Update(l.ctx, nil, currentUser); err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "更新手机号失败: %v", err)
|
||||
}
|
||||
// 记录mobile认证(确保后续可通过手机号登录)
|
||||
if _, err := l.svcCtx.UserAuthModel.Insert(l.ctx, nil, &model.UserAuth{Id: uuid.NewString(), UserId: finalUserID, AuthType: model.UserAuthTypeMobile, AuthKey: encryptedMobile}); err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "创建手机号认证失败: %v", err)
|
||||
}
|
||||
// 发放正式用户token
|
||||
token, err := l.svcCtx.UserService.GeneralUserToken(l.ctx, finalUserID, model.UserTypeNormal)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "生成Token失败: %v", err)
|
||||
}
|
||||
now := time.Now().Unix()
|
||||
return &types.BindMobileResp{AccessToken: token, AccessExpire: now + l.svcCtx.Config.JwtAuth.AccessExpire, RefreshAfter: now + l.svcCtx.Config.JwtAuth.RefreshAfter}, nil
|
||||
}
|
||||
|
||||
// 手机号已存在:进入账号合并或快捷登录流程
|
||||
finalUserID = targetUser.Id
|
||||
// 保护校验:若将不同用户进行合并,确保源用户不存在代理记录(临时用户不应为代理)
|
||||
if currentUserID != finalUserID {
|
||||
agent, err := l.svcCtx.AgentModel.FindOneByUserId(l.ctx, currentUserID)
|
||||
if err != nil && !errors.Is(err, model.ErrNotFound) {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "查询代理信息失败: %v", err)
|
||||
}
|
||||
if agent != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrMsg("账号数据异常:源用户存在代理记录,请联系技术支持"), "")
|
||||
}
|
||||
}
|
||||
// 查找当前登录态使用的认证(例如uuid或微信openid)是否已存在
|
||||
existingAuth, err := l.svcCtx.UserAuthModel.FindOneByAuthTypeAuthKey(l.ctx, currentAuthType, currentAuthKey)
|
||||
if err != nil && !errors.Is(err, model.ErrNotFound) {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "查找认证信息失败: %v", err)
|
||||
}
|
||||
// 如果当前认证已属于目标手机号用户,直接发放token(无需合并)
|
||||
if existingAuth != nil && existingAuth.UserId == finalUserID {
|
||||
token, err := l.svcCtx.UserService.GeneralUserToken(l.ctx, finalUserID, model.UserTypeNormal)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "生成Token失败: %v", err)
|
||||
}
|
||||
now := time.Now().Unix()
|
||||
return &types.BindMobileResp{AccessToken: token, AccessExpire: now + l.svcCtx.Config.JwtAuth.AccessExpire, RefreshAfter: now + l.svcCtx.Config.JwtAuth.RefreshAfter}, nil
|
||||
}
|
||||
|
||||
// 微信唯一性约束(按类型):
|
||||
// - H5 与 小程序各自只能绑定一个 openid(互不影响)
|
||||
if currentAuthType == model.UserAuthTypeWxh5OpenID {
|
||||
wxh5Auth, err := l.svcCtx.UserAuthModel.FindOneByUserIdAuthType(l.ctx, finalUserID, model.UserAuthTypeWxh5OpenID)
|
||||
if err != nil && !errors.Is(err, model.ErrNotFound) {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "查找认证信息失败: %v", err)
|
||||
}
|
||||
if wxh5Auth != nil && wxh5Auth.AuthKey != currentAuthKey {
|
||||
return nil, errors.Wrapf(xerr.NewErrMsg("该手机号已绑定其他H5微信号"), "")
|
||||
}
|
||||
}
|
||||
if currentAuthType == model.UserAuthTypeWxMiniOpenID {
|
||||
wxminiAuth, err := l.svcCtx.UserAuthModel.FindOneByUserIdAuthType(l.ctx, finalUserID, model.UserAuthTypeWxMiniOpenID)
|
||||
if err != nil && !errors.Is(err, model.ErrNotFound) {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "查找认证信息失败: %v", err)
|
||||
}
|
||||
if wxminiAuth != nil && wxminiAuth.AuthKey != currentAuthKey {
|
||||
return nil, errors.Wrapf(xerr.NewErrMsg("该手机号已绑定其他小程序微信号"), "")
|
||||
}
|
||||
}
|
||||
|
||||
// 事务处理:
|
||||
// - 将当前登录态的认证(uuid / 微信openid 等)绑定到目标手机号用户(finalUserID)
|
||||
// - 将源用户(currentUserID)的业务数据(订单、报告)迁移到目标用户,避免数据分裂
|
||||
// - 对源用户执行软删除,清理无主临时账号,保持数据一致性
|
||||
// 注意:所有步骤必须在同一个事务中执行,任何一步失败均会回滚,确保原子性
|
||||
err = l.svcCtx.UserModel.Trans(l.ctx, func(ctx context.Context, session sqlx.Session) error {
|
||||
// 1) 认证绑定处理(UUID替换策略)
|
||||
if currentAuthType == model.UserAuthTypeUUID {
|
||||
targetUUIDAuth, _ := l.svcCtx.UserAuthModel.FindOneByUserIdAuthType(ctx, finalUserID, model.UserAuthTypeUUID)
|
||||
if existingAuth != nil && existingAuth.UserId != finalUserID {
|
||||
if targetUUIDAuth != nil {
|
||||
if targetUUIDAuth.AuthKey != currentAuthKey {
|
||||
if err := l.svcCtx.UserAuthModel.Delete(ctx, session, existingAuth.Id); err != nil {
|
||||
return errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "删除旧UUID认证失败: %v", err)
|
||||
}
|
||||
targetUUIDAuth.AuthKey = currentAuthKey
|
||||
if _, err := l.svcCtx.UserAuthModel.Update(ctx, session, targetUUIDAuth); err != nil {
|
||||
return errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "更新目标UUID认证失败: %v", err)
|
||||
}
|
||||
} else {
|
||||
if err := l.svcCtx.UserAuthModel.Delete(ctx, session, existingAuth.Id); err != nil {
|
||||
return errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "删除重复UUID认证失败: %v", err)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
existingAuth.UserId = finalUserID
|
||||
if _, err := l.svcCtx.UserAuthModel.Update(ctx, session, existingAuth); err != nil {
|
||||
return errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "迁移UUID认证失败: %v", err)
|
||||
}
|
||||
}
|
||||
} else if existingAuth == nil {
|
||||
if targetUUIDAuth != nil {
|
||||
if targetUUIDAuth.AuthKey != currentAuthKey {
|
||||
targetUUIDAuth.AuthKey = currentAuthKey
|
||||
if _, err := l.svcCtx.UserAuthModel.Update(ctx, session, targetUUIDAuth); err != nil {
|
||||
return errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "更新目标UUID认证失败: %v", err)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
_, err := l.svcCtx.UserAuthModel.Insert(ctx, session, &model.UserAuth{Id: uuid.NewString(), UserId: finalUserID, AuthType: currentAuthType, AuthKey: currentAuthKey})
|
||||
if err != nil {
|
||||
return errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "创建UUID认证失败: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if existingAuth != nil && existingAuth.UserId != finalUserID {
|
||||
existingAuth.UserId = finalUserID
|
||||
if _, err := l.svcCtx.UserAuthModel.Update(ctx, session, existingAuth); err != nil {
|
||||
return errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "更新认证绑定失败: %v", err)
|
||||
}
|
||||
} else if existingAuth == nil {
|
||||
_, err := l.svcCtx.UserAuthModel.Insert(ctx, session, &model.UserAuth{Id: uuid.NewString(), UserId: finalUserID, AuthType: currentAuthType, AuthKey: currentAuthKey})
|
||||
if err != nil {
|
||||
return errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "创建认证绑定失败: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 2) 业务数据迁移
|
||||
// 当源用户与目标用户不同时,迁移源用户的订单与报告归属到finalUserID,避免合并后数据仍挂在旧用户
|
||||
if currentUserID != finalUserID {
|
||||
if err := l.svcCtx.OrderModel.UpdateUserIDWithSession(ctx, session, currentUserID, finalUserID); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := l.svcCtx.QueryModel.UpdateUserIDWithSession(ctx, session, currentUserID, finalUserID); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 3) 源用户软删除
|
||||
// 软删源用户(通常为临时用户),防止遗留无效账号;软删可保留历史痕迹,满足审计需求
|
||||
currentUser, err := l.svcCtx.UserModel.FindOne(ctx, currentUserID)
|
||||
if err != nil {
|
||||
return errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "查找当前用户失败: %v", err)
|
||||
}
|
||||
if err := l.svcCtx.UserModel.DeleteSoft(ctx, session, currentUser); err != nil {
|
||||
return errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "删除当前用户失败: %v", err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "绑定手机号, 生成token失败: %+v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 合并完成后生成并返回正式用户token
|
||||
token, err := l.svcCtx.UserService.GeneralUserToken(l.ctx, finalUserID, model.UserTypeNormal)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "生成Token失败: %v", err)
|
||||
}
|
||||
now := time.Now().Unix()
|
||||
return &types.BindMobileResp{
|
||||
AccessToken: token,
|
||||
AccessExpire: now + l.svcCtx.Config.JwtAuth.AccessExpire,
|
||||
RefreshAfter: now + l.svcCtx.Config.JwtAuth.RefreshAfter,
|
||||
}, nil
|
||||
return &types.BindMobileResp{AccessToken: token, AccessExpire: now + l.svcCtx.Config.JwtAuth.AccessExpire, RefreshAfter: now + l.svcCtx.Config.JwtAuth.RefreshAfter}, nil
|
||||
}
|
||||
|
||||
@@ -53,22 +53,18 @@ func (l *MobileCodeLoginLogic) MobileCodeLogin(req *types.MobileCodeLoginReq) (r
|
||||
return nil, errors.Wrapf(xerr.NewErrMsg("验证码不正确"), "手机登录, 验证码不正确: %s", encryptedMobile)
|
||||
}
|
||||
}
|
||||
var userID int64
|
||||
var userID string
|
||||
user, findUserErr := l.svcCtx.UserModel.FindOneByMobile(l.ctx, sql.NullString{String: encryptedMobile, Valid: true})
|
||||
if findUserErr != nil && findUserErr != model.ErrNotFound {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "手机登录, 读取数据库获取用户失败, mobile: %s, err: %+v", encryptedMobile, err)
|
||||
}
|
||||
if user == nil {
|
||||
userID, err = l.svcCtx.UserService.RegisterUser(l.ctx, encryptedMobile)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "手机登录, 注册用户失败: %+v", err)
|
||||
}
|
||||
} else {
|
||||
userID = user.Id
|
||||
return nil, errors.Wrapf(xerr.NewErrMsg("用户不存在"), "手机登录, 用户不存在: %s", encryptedMobile)
|
||||
}
|
||||
userID = user.Id
|
||||
token, err := l.svcCtx.UserService.GeneralUserToken(l.ctx, userID, model.UserTypeNormal)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "手机登录, 生成token失败 : %d", userID)
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "手机登录, 生成token失败 : %s", userID)
|
||||
}
|
||||
|
||||
// 获取当前时间戳
|
||||
|
||||
@@ -1,21 +1,22 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"context"
|
||||
"ycc-server/app/main/model"
|
||||
"ycc-server/common/xerr"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"time"
|
||||
"context"
|
||||
"ycc-server/app/main/model"
|
||||
"ycc-server/common/xerr"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/google/uuid"
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"ycc-server/app/main/api/internal/svc"
|
||||
"ycc-server/app/main/api/internal/types"
|
||||
"ycc-server/app/main/api/internal/svc"
|
||||
"ycc-server/app/main/api/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type WxH5AuthLogic struct {
|
||||
@@ -46,41 +47,29 @@ func (l *WxH5AuthLogic) WxH5Auth(req *types.WXH5AuthReq) (resp *types.WXH5AuthRe
|
||||
}
|
||||
|
||||
// Step 3: 处理用户信息
|
||||
var userID int64
|
||||
var userType int64
|
||||
if userAuth != nil {
|
||||
// 已存在用户,直接登录
|
||||
userID = userAuth.UserId
|
||||
userType = model.UserTypeNormal
|
||||
} else {
|
||||
// 检查临时用户表
|
||||
userTemp, err := l.svcCtx.UserTempModel.FindOneByAuthTypeAuthKey(l.ctx, model.UserAuthTypeWxh5OpenID, accessTokenResp.Openid)
|
||||
if err != nil && !errors.Is(err, model.ErrNotFound) {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "查询用户临时信息失败: %v", err)
|
||||
}
|
||||
|
||||
if userTemp == nil {
|
||||
// 创建临时用户记录
|
||||
userTemp = &model.UserTemp{
|
||||
AuthType: model.UserAuthTypeWxh5OpenID,
|
||||
AuthKey: accessTokenResp.Openid,
|
||||
}
|
||||
result, err := l.svcCtx.UserTempModel.Insert(l.ctx, nil, userTemp)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "创建临时用户信息失败: %v", err)
|
||||
}
|
||||
userID, err = result.LastInsertId()
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "获取新创建的临时用户ID失败: %v", err)
|
||||
}
|
||||
} else {
|
||||
userID = userTemp.Id
|
||||
}
|
||||
userType = model.UserTypeTemp
|
||||
}
|
||||
var userID string
|
||||
var userType int64
|
||||
if userAuth != nil {
|
||||
// 已存在用户,直接登录
|
||||
userID = userAuth.UserId
|
||||
userType = model.UserTypeNormal
|
||||
} else {
|
||||
user := &model.User{Id: uuid.NewString()}
|
||||
_, err := l.svcCtx.UserModel.Insert(l.ctx, nil, user)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "创建用户失败: %v", err)
|
||||
}
|
||||
ua := &model.UserAuth{Id: uuid.NewString(), UserId: user.Id, AuthType: model.UserAuthTypeWxh5OpenID, AuthKey: accessTokenResp.Openid}
|
||||
_, err = l.svcCtx.UserAuthModel.Insert(l.ctx, nil, ua)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "创建用户授权失败: %v", err)
|
||||
}
|
||||
userID = user.Id
|
||||
userType = model.UserTypeTemp
|
||||
}
|
||||
|
||||
// Step 4: 生成JWT Token
|
||||
token, err := l.svcCtx.UserService.GeneralUserToken(l.ctx, userID, userType)
|
||||
token, err := l.svcCtx.UserService.GeneralUserToken(l.ctx, userID, userType)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "生成JWT token失败: %v", err)
|
||||
}
|
||||
|
||||
@@ -1,20 +1,21 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"time"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"ycc-server/app/main/api/internal/svc"
|
||||
"ycc-server/app/main/api/internal/types"
|
||||
"ycc-server/app/main/model"
|
||||
"ycc-server/common/xerr"
|
||||
"ycc-server/app/main/api/internal/svc"
|
||||
"ycc-server/app/main/api/internal/types"
|
||||
"ycc-server/app/main/model"
|
||||
"ycc-server/common/xerr"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"github.com/google/uuid"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type WxMiniAuthLogic struct {
|
||||
@@ -45,41 +46,29 @@ func (l *WxMiniAuthLogic) WxMiniAuth(req *types.WXMiniAuthReq) (resp *types.WXMi
|
||||
}
|
||||
|
||||
// 3. 处理用户信息
|
||||
var userID int64
|
||||
var userType int64
|
||||
if userAuth != nil {
|
||||
// 已存在用户,直接登录
|
||||
userID = userAuth.UserId
|
||||
userType = model.UserTypeNormal
|
||||
} else {
|
||||
// 注册临时用户
|
||||
userTemp, err := l.svcCtx.UserTempModel.FindOneByAuthTypeAuthKey(l.ctx, model.UserAuthTypeWxMiniOpenID, sessionKeyResp.Openid)
|
||||
if err != nil && !errors.Is(err, model.ErrNotFound) {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "查询用户临时信息失败: %v", err)
|
||||
}
|
||||
if userTemp == nil {
|
||||
// 创建新的临时用户
|
||||
userTemp = &model.UserTemp{}
|
||||
userTemp.AuthType = model.UserAuthTypeWxMiniOpenID
|
||||
userTemp.AuthKey = sessionKeyResp.Openid
|
||||
result, err := l.svcCtx.UserTempModel.Insert(l.ctx, nil, userTemp)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "创建临时用户信息失败: %v", err)
|
||||
}
|
||||
// 获取新创建的临时用户ID
|
||||
userID, err = result.LastInsertId()
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "获取新创建的临时用户ID失败: %v", err)
|
||||
}
|
||||
} else {
|
||||
// 使用已存在的临时用户ID
|
||||
userID = userTemp.Id
|
||||
}
|
||||
userType = model.UserTypeTemp
|
||||
}
|
||||
var userID string
|
||||
var userType int64
|
||||
if userAuth != nil {
|
||||
// 已存在用户,直接登录
|
||||
userID = userAuth.UserId
|
||||
userType = model.UserTypeNormal
|
||||
} else {
|
||||
user := &model.User{Id: uuid.NewString()}
|
||||
_, err := l.svcCtx.UserModel.Insert(l.ctx, nil, user)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "创建用户失败: %v", err)
|
||||
}
|
||||
ua := &model.UserAuth{Id: uuid.NewString(), UserId: user.Id, AuthType: model.UserAuthTypeWxMiniOpenID, AuthKey: sessionKeyResp.Openid}
|
||||
_, err = l.svcCtx.UserAuthModel.Insert(l.ctx, nil, ua)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "创建用户授权失败: %v", err)
|
||||
}
|
||||
userID = user.Id
|
||||
userType = model.UserTypeTemp
|
||||
}
|
||||
|
||||
// 4. 生成JWT Token
|
||||
token, err := l.svcCtx.UserService.GeneralUserToken(l.ctx, userID, userType)
|
||||
token, err := l.svcCtx.UserService.GeneralUserToken(l.ctx, userID, userType)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "生成JWT Token失败: %v", err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user