feat: rbac迁移完成,并已部署至dev服务器
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"sundynix-micro-go/app/auth/api/internal/svc"
|
||||
"sundynix-micro-go/app/auth/api/internal/types"
|
||||
"sundynix-micro-go/common/utils/captcha"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type GetCaptchaLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewGetCaptchaLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetCaptchaLogic {
|
||||
return &GetCaptchaLogic{Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx}
|
||||
}
|
||||
|
||||
func (l *GetCaptchaLogic) GetCaptcha() (resp *types.CaptchaResp, err error) {
|
||||
id, b64s, err := captcha.Generate()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("生成验证码失败")
|
||||
}
|
||||
return &types.CaptchaResp{
|
||||
CaptchaId: id,
|
||||
CaptchaImg: b64s,
|
||||
}, nil
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.10.1
|
||||
|
||||
package auth
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
|
||||
"sundynix-micro-go/app/auth/api/internal/svc"
|
||||
"sundynix-micro-go/app/auth/api/internal/types"
|
||||
sysPb "sundynix-micro-go/app/system/rpc/system"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type LoginByPhoneLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewLoginByPhoneLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginByPhoneLogic {
|
||||
return &LoginByPhoneLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *LoginByPhoneLogic) LoginByPhone(req *types.LoginByPhoneReq) (resp *types.LoginResp, err error) {
|
||||
// 1. 调用微信接口获取手机号
|
||||
// TODO: 从配置中获取access_token
|
||||
accessToken := "" // 需要通过微信API获取
|
||||
apiURL := "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=" + accessToken
|
||||
|
||||
data := map[string]interface{}{"code": req.Code}
|
||||
jsonData, _ := json.Marshal(data)
|
||||
|
||||
httpResp, err := http.Post(apiURL, "application/json", nil)
|
||||
if err != nil {
|
||||
l.Errorf("获取手机号失败: %v", err)
|
||||
return nil, fmt.Errorf("获取手机号失败")
|
||||
}
|
||||
defer httpResp.Body.Close()
|
||||
|
||||
body, _ := io.ReadAll(httpResp.Body)
|
||||
var dataMap map[string]interface{}
|
||||
_ = json.Unmarshal(body, &dataMap)
|
||||
_ = jsonData
|
||||
_ = url.Values{}
|
||||
|
||||
// 2. 通过 user-rpc 查询用户
|
||||
userResp, err := l.svcCtx.SystemRpc.GetUserByOpenId(l.ctx, &sysPb.GetUserByOpenIdReq{
|
||||
OpenId: req.OpenId,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("用户不存在")
|
||||
}
|
||||
|
||||
// 3. 如果需要更新手机号
|
||||
phoneInfo, ok := dataMap["phone_info"].(map[string]interface{})
|
||||
if ok {
|
||||
phoneNumber, _ := phoneInfo["phoneNumber"].(string)
|
||||
if phoneNumber != "" && userResp.User.Phone == "" {
|
||||
_, _ = l.svcCtx.SystemRpc.UpdateUser(l.ctx, &sysPb.UpdateUserReq{
|
||||
Id: userResp.User.Id,
|
||||
Phone: phoneNumber,
|
||||
})
|
||||
userResp.User.Phone = phoneNumber
|
||||
}
|
||||
}
|
||||
|
||||
// 4. 生成Token(复用统一的 generateToken 函数)
|
||||
token, err := generateToken(l.svcCtx.Config.Auth.AccessSecret, l.svcCtx.Config.Auth.AccessExpire, userResp.User)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("生成Token失败")
|
||||
}
|
||||
|
||||
return &types.LoginResp{
|
||||
Token: token,
|
||||
UserInfo: userResp.User,
|
||||
}, nil
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"sundynix-micro-go/app/auth/api/internal/svc"
|
||||
"sundynix-micro-go/app/auth/api/internal/types"
|
||||
sysPb "sundynix-micro-go/app/system/rpc/system"
|
||||
"sundynix-micro-go/common/utils/captcha"
|
||||
jwtUtil "sundynix-micro-go/common/utils/jwt"
|
||||
|
||||
jwtv5 "github.com/golang-jwt/jwt/v5"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type LoginLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
r *http.Request
|
||||
}
|
||||
|
||||
func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
|
||||
return &LoginLogic{Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx}
|
||||
}
|
||||
|
||||
// SetRequest 注入 http.Request 以便从请求头取 clientId
|
||||
func (l *LoginLogic) SetRequest(r *http.Request) {
|
||||
l.r = r
|
||||
}
|
||||
|
||||
func (l *LoginLogic) Login(req *types.LoginReq) (resp *types.LoginResp, err error) {
|
||||
// 1. 校验验证码
|
||||
if !captcha.Verify(req.CaptchaId, req.Captcha) {
|
||||
return nil, fmt.Errorf("验证码错误")
|
||||
}
|
||||
|
||||
// 2. 从请求头获取 clientId
|
||||
clientId := ""
|
||||
if l.r != nil {
|
||||
clientId = l.r.Header.Get("X-Client-Id")
|
||||
}
|
||||
if clientId == "" {
|
||||
return nil, fmt.Errorf("缺少客户端标识")
|
||||
}
|
||||
|
||||
// 3. 查询 Client 信息,获取 token 过期时间
|
||||
clientResp, err := l.svcCtx.SystemRpc.GetClientById(l.ctx, &sysPb.GetClientByIdReq{ClientId: clientId})
|
||||
if err != nil {
|
||||
l.Errorf("[Login] ❌ 查询客户端失败 | clientId=%s | err=%v", clientId, err)
|
||||
return nil, fmt.Errorf("无效的客户端标识,请确认 X-Client-Id 请求头是否正确")
|
||||
}
|
||||
activeTimeout := clientResp.Client.ActiveTimeout
|
||||
if activeTimeout <= 0 {
|
||||
activeTimeout = 7200
|
||||
}
|
||||
|
||||
// 4. 调用 system-rpc 验证账号密码
|
||||
rpcResp, err := l.svcCtx.SystemRpc.LoginByAccount(l.ctx, &sysPb.LoginByAccountReq{
|
||||
Account: req.Account,
|
||||
Password: req.Password,
|
||||
})
|
||||
if err != nil {
|
||||
l.Errorf("[Login] ❌ 账号验证失败 | account=%s | err=%v", req.Account, err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 5. 签发 JWT Token
|
||||
token, err := generateToken(l.svcCtx.Config.Auth.AccessSecret, activeTimeout, rpcResp.User)
|
||||
if err != nil {
|
||||
l.Errorf("[Login] ❌ 生成 Token 失败 | userId=%s | err=%v", rpcResp.User.Id, err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
l.Infof("[Login] ✅ 登录成功 | account=%s | userId=%s | clientId=%s | tokenExpire=%ds",
|
||||
req.Account, rpcResp.User.Id, clientId, activeTimeout)
|
||||
|
||||
return &types.LoginResp{
|
||||
Token: token,
|
||||
UserInfo: map[string]interface{}{
|
||||
"id": rpcResp.User.Id,
|
||||
"name": rpcResp.User.Name,
|
||||
"account": rpcResp.User.Account,
|
||||
"nickName": rpcResp.User.NickName,
|
||||
"phone": rpcResp.User.Phone,
|
||||
"avatarId": rpcResp.User.AvatarId,
|
||||
"gender": rpcResp.User.Gender,
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
func generateToken(accessSecret string, activeTimeout int64, userInfo *sysPb.UserInfo) (string, error) {
|
||||
j := jwtUtil.NewJWT(accessSecret)
|
||||
claims := jwtUtil.CustomClaims{
|
||||
BaseClaims: jwtUtil.BaseClaims{
|
||||
ID: userInfo.Id,
|
||||
Account: userInfo.Account,
|
||||
},
|
||||
BufferTime: 3600,
|
||||
RegisteredClaims: jwtv5.RegisteredClaims{
|
||||
Audience: jwtv5.ClaimStrings{"sundynix"},
|
||||
NotBefore: jwtv5.NewNumericDate(time.Now().Add(-1000)),
|
||||
ExpiresAt: jwtv5.NewNumericDate(time.Now().Add(time.Duration(activeTimeout) * time.Second)),
|
||||
Issuer: "sundynix",
|
||||
},
|
||||
}
|
||||
token, err := j.CreateToken(claims)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("生成Token失败: %w", err)
|
||||
}
|
||||
return token, nil
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.10.1
|
||||
|
||||
package auth
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
|
||||
"sundynix-micro-go/app/auth/api/internal/svc"
|
||||
"sundynix-micro-go/app/auth/api/internal/types"
|
||||
sysPb "sundynix-micro-go/app/system/rpc/system"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type MiniLoginLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
// 微信小程序登录
|
||||
func NewMiniLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *MiniLoginLogic {
|
||||
return &MiniLoginLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
// WxCode2SessionResp 微信code2session响应
|
||||
type WxCode2SessionResp struct {
|
||||
Openid string `json:"openid"`
|
||||
SessionKey string `json:"session_key"`
|
||||
Unionid string `json:"unionid"`
|
||||
Errcode int `json:"errcode"`
|
||||
Errmsg string `json:"errmsg"`
|
||||
}
|
||||
|
||||
func (l *MiniLoginLogic) MiniLogin(req *types.MiniLoginReq) (resp *types.LoginResp, err error) {
|
||||
// 1. 调用微信接口获取openid和session_key
|
||||
// TODO: 从配置中获取AppId和AppSecret,当前先用固定值
|
||||
appID := "wxb463820bf36dd5d6"
|
||||
appSecret := "731784a74c76c6d31fa00bb847af2c7d"
|
||||
|
||||
params := url.Values{}
|
||||
params.Set("appid", appID)
|
||||
params.Set("secret", appSecret)
|
||||
params.Set("js_code", req.Code)
|
||||
params.Set("grant_type", "authorization_code")
|
||||
fullURL := "https://api.weixin.qq.com/sns/jscode2session?" + params.Encode()
|
||||
|
||||
httpResp, err := http.Get(fullURL)
|
||||
if err != nil {
|
||||
l.Errorf("微信登录接口请求失败: %v", err)
|
||||
return nil, fmt.Errorf("微信登录接口请求失败")
|
||||
}
|
||||
defer httpResp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(httpResp.Body)
|
||||
if err != nil {
|
||||
l.Errorf("读取微信接口响应失败: %v", err)
|
||||
return nil, fmt.Errorf("读取微信登录接口响应失败")
|
||||
}
|
||||
|
||||
var wxResp WxCode2SessionResp
|
||||
if err = json.Unmarshal(body, &wxResp); err != nil {
|
||||
l.Errorf("解析微信接口响应失败: %v", err)
|
||||
return nil, fmt.Errorf("解析微信登录接口响应失败")
|
||||
}
|
||||
|
||||
if wxResp.Errcode != 0 {
|
||||
l.Errorf("微信接口返回错误: errcode=%d, errmsg=%s", wxResp.Errcode, wxResp.Errmsg)
|
||||
return nil, fmt.Errorf("微信登录失败: %s", wxResp.Errmsg)
|
||||
}
|
||||
|
||||
if wxResp.Openid == "" {
|
||||
return nil, fmt.Errorf("openid为空")
|
||||
}
|
||||
|
||||
// 2. 通过 user-rpc 创建或获取用户
|
||||
createResp, err := l.svcCtx.SystemRpc.CreateUser(l.ctx, &sysPb.CreateUserReq{
|
||||
Name: "",
|
||||
OpenId: wxResp.Openid,
|
||||
SessionKey: wxResp.SessionKey,
|
||||
ClientId: req.ClientId,
|
||||
})
|
||||
if err != nil {
|
||||
l.Errorf("创建用户失败: %v", err)
|
||||
return nil, fmt.Errorf("登录失败")
|
||||
}
|
||||
|
||||
// 3. 生成JWT Token(复用统一的 generateToken 函数)
|
||||
token, err := generateToken(l.svcCtx.Config.Auth.AccessSecret, l.svcCtx.Config.Auth.AccessExpire, createResp.User)
|
||||
if err != nil {
|
||||
l.Errorf("生成Token失败: %v", err)
|
||||
return nil, fmt.Errorf("登录失败")
|
||||
}
|
||||
|
||||
return &types.LoginResp{
|
||||
Token: token,
|
||||
UserInfo: createResp.User,
|
||||
}, nil
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.10.1
|
||||
|
||||
package user
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"sundynix-micro-go/app/auth/api/internal/svc"
|
||||
"sundynix-micro-go/app/auth/api/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type ChangePasswordLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
// 修改密码
|
||||
func NewChangePasswordLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ChangePasswordLogic {
|
||||
return &ChangePasswordLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *ChangePasswordLogic) ChangePassword(req *types.ChangePasswordReq) error {
|
||||
// todo: add your logic here and delete this line
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.10.1
|
||||
|
||||
package user
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"sundynix-micro-go/app/auth/api/internal/svc"
|
||||
"sundynix-micro-go/app/auth/api/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type GetLocationLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
// 获取位置信息
|
||||
func NewGetLocationLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetLocationLogic {
|
||||
return &GetLocationLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *GetLocationLogic) GetLocation(req *types.LocationReq) error {
|
||||
// todo: add your logic here and delete this line
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"sort"
|
||||
|
||||
"sundynix-micro-go/app/auth/api/internal/svc"
|
||||
sysPb "sundynix-micro-go/app/system/rpc/system"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type GetUserInfoLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewGetUserInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetUserInfoLogic {
|
||||
return &GetUserInfoLogic{Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx}
|
||||
}
|
||||
|
||||
func (l *GetUserInfoLogic) GetUserInfo() (resp interface{}, err error) {
|
||||
// 1. 从 JWT claims 中获取 userId
|
||||
userId := fmt.Sprintf("%v", l.ctx.Value("userId"))
|
||||
if userId == "" || userId == "<nil>" {
|
||||
return nil, fmt.Errorf("用户未登录")
|
||||
}
|
||||
|
||||
// 2. 调用 system-rpc 获取用户基础信息
|
||||
userResp, err := l.svcCtx.SystemRpc.GetUserById(l.ctx, &sysPb.GetUserByIdReq{Id: userId})
|
||||
if err != nil {
|
||||
l.Errorf("[GetUserInfo] ❌ 获取用户信息失败 | userId=%s | err=%v", userId, err)
|
||||
return nil, fmt.Errorf("获取用户信息失败")
|
||||
}
|
||||
|
||||
// 3. 获取用户角色列表
|
||||
rolesResp, err := l.svcCtx.SystemRpc.GetRolesByUserId(l.ctx, &sysPb.GetRolesByUserIdReq{UserId: userId})
|
||||
if err != nil {
|
||||
l.Errorf("[GetUserInfo] ⚠️ 获取用户角色失败(已降级返回空角色) | userId=%s | err=%v", userId, err)
|
||||
rolesResp = &sysPb.GetRolesByUserIdResp{Roles: []*sysPb.RoleInfo{}}
|
||||
}
|
||||
|
||||
// 4. 根据角色获取菜单权限(合并所有角色的菜单)
|
||||
menuMap := make(map[string]interface{})
|
||||
var allMenus []interface{}
|
||||
var roleCodes []string
|
||||
|
||||
for _, role := range rolesResp.Roles {
|
||||
roleCodes = append(roleCodes, role.Code)
|
||||
|
||||
menusResp, err := l.svcCtx.SystemRpc.GetMenusByRoleId(l.ctx, &sysPb.GetMenusByRoleIdReq{RoleId: role.Id})
|
||||
if err != nil {
|
||||
l.Errorf("获取角色[%s]菜单失败: %v", role.Name, err)
|
||||
continue
|
||||
}
|
||||
for _, m := range menusResp.Menus {
|
||||
if _, exists := menuMap[m.Id]; !exists {
|
||||
menuMap[m.Id] = true
|
||||
allMenus = append(allMenus, convertMenuInfo(m))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 5. 对顶层菜单进行重新排序,防止不同角色的合并打乱顺序
|
||||
sort.Slice(allMenus, func(i, j int) bool {
|
||||
m1 := allMenus[i].(map[string]interface{})
|
||||
m2 := allMenus[j].(map[string]interface{})
|
||||
s1 := m1["sort"].(int32)
|
||||
s2 := m2["sort"].(int32)
|
||||
return s1 < s2
|
||||
})
|
||||
|
||||
// 5. 组装完整的用户信息返回
|
||||
u := userResp.User
|
||||
return map[string]interface{}{
|
||||
"id": u.Id,
|
||||
"name": u.Name,
|
||||
"account": u.Account,
|
||||
"nickName": u.NickName,
|
||||
"phone": u.Phone,
|
||||
"avatarId": u.AvatarId,
|
||||
"gender": u.Gender,
|
||||
"roles": roleCodes,
|
||||
"menus": allMenus,
|
||||
"createdAt": u.CreatedAt,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// convertMenuInfo 将proto MenuInfo转为map
|
||||
func convertMenuInfo(m *sysPb.MenuInfo) map[string]interface{} {
|
||||
menu := map[string]interface{}{
|
||||
"id": m.Id,
|
||||
"parentId": m.ParentId,
|
||||
"category": m.Category,
|
||||
"name": m.Name,
|
||||
"title": m.Title,
|
||||
"code": m.Code,
|
||||
"path": m.Path,
|
||||
"permission": m.Permission,
|
||||
"locale": m.Locale,
|
||||
"icon": m.Icon,
|
||||
"sort": m.Sort,
|
||||
}
|
||||
if len(m.Children) > 0 {
|
||||
var children []map[string]interface{}
|
||||
for _, child := range m.Children {
|
||||
children = append(children, convertMenuInfo(child))
|
||||
}
|
||||
menu["children"] = children
|
||||
}
|
||||
return menu
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.10.1
|
||||
|
||||
package user
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"sundynix-micro-go/app/auth/api/internal/svc"
|
||||
"sundynix-micro-go/app/auth/api/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type GetWeatherLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
// 获取天气信息
|
||||
func NewGetWeatherLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetWeatherLogic {
|
||||
return &GetWeatherLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *GetWeatherLogic) GetWeather(req *types.WeatherReq) error {
|
||||
// todo: add your logic here and delete this line
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.10.1
|
||||
|
||||
package user
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"sundynix-micro-go/app/auth/api/internal/svc"
|
||||
"sundynix-micro-go/app/auth/api/internal/types"
|
||||
sysPb "sundynix-micro-go/app/system/rpc/system"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type UpdateUserLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewUpdateUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateUserLogic {
|
||||
return &UpdateUserLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *UpdateUserLogic) UpdateUser(req *types.UpdateUserReq) error {
|
||||
userId := fmt.Sprintf("%v", l.ctx.Value("userId"))
|
||||
if userId == "" || userId == "<nil>" {
|
||||
return fmt.Errorf("用户未登录")
|
||||
}
|
||||
|
||||
_, err := l.svcCtx.SystemRpc.UpdateUser(l.ctx, &sysPb.UpdateUserReq{
|
||||
Id: userId,
|
||||
Name: req.Name,
|
||||
Account: req.Account,
|
||||
Phone: req.Phone,
|
||||
AvatarId: req.AvatarId,
|
||||
NickName: req.NickName,
|
||||
})
|
||||
if err != nil {
|
||||
l.Errorf("更新用户失败: %v", err)
|
||||
return fmt.Errorf("更新用户失败")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user