feat: minilogin改造
This commit is contained in:
@@ -13,6 +13,7 @@ import (
|
||||
|
||||
"sundynix-micro-go/app/auth/api/internal/svc"
|
||||
"sundynix-micro-go/app/auth/api/internal/types"
|
||||
plantPb "sundynix-micro-go/app/plant/rpc/plant"
|
||||
sysPb "sundynix-micro-go/app/system/rpc/system"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
@@ -54,9 +55,20 @@ func (l *LoginByPhoneLogic) LoginByPhone(req *types.LoginByPhoneReq) (resp *type
|
||||
_ = jsonData
|
||||
_ = url.Values{}
|
||||
|
||||
// 2. 通过 user-rpc 查询用户
|
||||
userResp, err := l.svcCtx.SystemRpc.GetUserByOpenId(l.ctx, &sysPb.GetUserByOpenIdReq{
|
||||
OpenId: req.OpenId,
|
||||
// 2. 通过 plant-rpc 和 system-rpc 获取用户
|
||||
if l.svcCtx.PlantRpc == nil {
|
||||
return nil, fmt.Errorf("植物服务不可用")
|
||||
}
|
||||
profileResp, err := l.svcCtx.PlantRpc.FindOrCreateUserByOpenId(l.ctx, &plantPb.FindOrCreateUserByOpenIdReq{
|
||||
OpenId: req.OpenId,
|
||||
ClientId: req.ClientId,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("微信登录解析失败")
|
||||
}
|
||||
|
||||
userResp, err := l.svcCtx.SystemRpc.GetUserById(l.ctx, &sysPb.GetUserByIdReq{
|
||||
Id: profileResp.UserId,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("用户不存在")
|
||||
|
||||
@@ -112,46 +112,39 @@ func (l *MiniLoginLogic) MiniLogin(req *types.MiniLoginReq) (resp *types.LoginRe
|
||||
return nil, fmt.Errorf("openid为空")
|
||||
}
|
||||
|
||||
// 4. 通过 system-rpc 创建或获取基础用户
|
||||
createResp, err := l.svcCtx.SystemRpc.CreateUser(l.ctx, &sysPb.CreateUserReq{
|
||||
Name: "",
|
||||
OpenId: wxResp.Openid,
|
||||
SessionKey: wxResp.SessionKey,
|
||||
ClientId: req.ClientId,
|
||||
// 4. 根据 clientId 路由到对应业务服务,获取或注册其扩展用户并得到 userId
|
||||
var userId string
|
||||
switch req.ClientId {
|
||||
case ClientIdPlant:
|
||||
if l.svcCtx.PlantRpc == nil {
|
||||
return nil, fmt.Errorf("植物小程序服务不可用")
|
||||
}
|
||||
profileResp, err := l.svcCtx.PlantRpc.FindOrCreateUserByOpenId(l.ctx, &plantPb.FindOrCreateUserByOpenIdReq{
|
||||
OpenId: wxResp.Openid,
|
||||
SessionKey: wxResp.SessionKey,
|
||||
UnionId: wxResp.Unionid,
|
||||
ClientId: req.ClientId,
|
||||
})
|
||||
if err != nil {
|
||||
l.Errorf("微信登录失败(植物服务):%v", err)
|
||||
return nil, fmt.Errorf("登录失败")
|
||||
}
|
||||
userId = profileResp.UserId
|
||||
default:
|
||||
return nil, fmt.Errorf("暂不支持该客户端: %s", req.ClientId)
|
||||
}
|
||||
|
||||
// 5. 根据 userId 获取系统核心基础用户信息
|
||||
userResp, err := l.svcCtx.SystemRpc.GetUserById(l.ctx, &sysPb.GetUserByIdReq{
|
||||
Id: userId,
|
||||
})
|
||||
if err != nil {
|
||||
l.Errorf("创建用户失败: %v", err)
|
||||
l.Errorf("获取系统基础用户信息失败: userId=%s, err=%v", userId, err)
|
||||
return nil, fmt.Errorf("登录失败")
|
||||
}
|
||||
|
||||
userId := createResp.User.Id
|
||||
|
||||
// 5. 异步初始化各业务服务的用户扩展表
|
||||
// 新增小程序:在 switch 里加一个 case 即可
|
||||
go func(uid, clientId string) {
|
||||
bgCtx := context.Background()
|
||||
switch clientId {
|
||||
case ClientIdPlant:
|
||||
if l.svcCtx.PlantRpc == nil {
|
||||
l.Errorf("[MiniLogin] PlantRpc 未配置,跳过 plant profile 初始化")
|
||||
return
|
||||
}
|
||||
if _, err := l.svcCtx.PlantRpc.GetUserProfile(bgCtx, &plantPb.GetProfileReq{
|
||||
UserId: uid,
|
||||
}); err != nil {
|
||||
l.Errorf("[MiniLogin] 初始化 plant profile 失败: userId=%s, err=%v", uid, err)
|
||||
} else {
|
||||
l.Infof("[MiniLogin] plant profile 初始化成功: userId=%s", uid)
|
||||
}
|
||||
// case ClientIdRadio:
|
||||
// l.svcCtx.RadioRpc.GetUserProfile(bgCtx, &radioPb.GetProfileReq{UserId: uid})
|
||||
default:
|
||||
// 未知 clientId 或无需扩展表的场景,跳过
|
||||
}
|
||||
}(userId, req.ClientId)
|
||||
|
||||
// 6. 生成JWT Token
|
||||
token, err := generateToken(l.svcCtx.Config.Auth.AccessSecret, l.svcCtx.Config.Auth.AccessExpire, createResp.User)
|
||||
token, err := generateToken(l.svcCtx.Config.Auth.AccessSecret, l.svcCtx.Config.Auth.AccessExpire, userResp.User)
|
||||
if err != nil {
|
||||
l.Errorf("生成Token失败: %v", err)
|
||||
return nil, fmt.Errorf("登录失败")
|
||||
@@ -159,6 +152,6 @@ func (l *MiniLoginLogic) MiniLogin(req *types.MiniLoginReq) (resp *types.LoginRe
|
||||
|
||||
return &types.LoginResp{
|
||||
Token: token,
|
||||
UserInfo: createResp.User,
|
||||
UserInfo: userResp.User,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -15,10 +15,6 @@ type SundynixUser struct {
|
||||
Password string `gorm:"size:100;column:password" json:"-"`
|
||||
NickName string `gorm:"size:100;column:nick_name" json:"nickName"`
|
||||
Phone string `gorm:"size:20;column:phone" json:"phone"`
|
||||
SessionKey string `gorm:"size:80;column:session_key" json:"-"`
|
||||
UnionID string `gorm:"size:80;column:union_id" json:"unionId"`
|
||||
OpenID string `gorm:"size:80;column:open_id;index" json:"openId"`
|
||||
SaOpenID string `gorm:"size:80;column:sa_open_id" json:"saOpenId"`
|
||||
AvatarID string `gorm:"size:50;column:avatar_id" json:"avatarId"`
|
||||
Gender int `gorm:"default:0;column:gender" json:"gender"`
|
||||
LastLoginIP string `gorm:"size:20;column:last_login_ip" json:"lastLoginIp"`
|
||||
|
||||
Reference in New Issue
Block a user