init: init refactor
This commit is contained in:
@@ -0,0 +1,100 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.10.1
|
||||
|
||||
package auth
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"time"
|
||||
|
||||
"sundynix-micro-go/app/user/api/internal/svc"
|
||||
"sundynix-micro-go/app/user/api/internal/types"
|
||||
"sundynix-micro-go/app/user/rpc/user"
|
||||
jwtUtil "sundynix-micro-go/common/utils/jwt"
|
||||
|
||||
jwtv5 "github.com/golang-jwt/jwt/v5"
|
||||
"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.UserRpc.GetUserByOpenId(l.ctx, &user.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.UserRpc.UpdateUser(l.ctx, &user.UpdateUserReq{
|
||||
Id: userResp.User.Id,
|
||||
Phone: phoneNumber,
|
||||
})
|
||||
userResp.User.Phone = phoneNumber
|
||||
}
|
||||
}
|
||||
|
||||
// 4. 生成Token
|
||||
j := jwtUtil.NewJWT(l.svcCtx.Config.Auth.AccessSecret)
|
||||
claims := jwtUtil.CustomClaims{
|
||||
BaseClaims: jwtUtil.BaseClaims{
|
||||
ID: userResp.User.Id,
|
||||
Account: userResp.User.Account,
|
||||
},
|
||||
BufferTime: 3600,
|
||||
RegisteredClaims: jwtv5.RegisteredClaims{
|
||||
ExpiresAt: jwtv5.NewNumericDate(time.Now().Add(time.Duration(l.svcCtx.Config.Auth.AccessExpire) * time.Second)),
|
||||
Issuer: "sundynix",
|
||||
},
|
||||
}
|
||||
token, _ := j.CreateToken(claims)
|
||||
|
||||
return &types.LoginResp{
|
||||
Token: token,
|
||||
UserInfo: userResp.User,
|
||||
}, nil
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.10.1
|
||||
|
||||
package auth
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"sundynix-micro-go/app/user/api/internal/svc"
|
||||
"sundynix-micro-go/app/user/api/internal/types"
|
||||
"sundynix-micro-go/app/user/rpc/user"
|
||||
jwtUtil "sundynix-micro-go/common/utils/jwt"
|
||||
|
||||
jwtv5 "github.com/golang-jwt/jwt/v5"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
)
|
||||
|
||||
type LoginLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
|
||||
return &LoginLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *LoginLogic) Login(req *types.LoginReq) (resp *types.LoginResp, err error) {
|
||||
// 通过 user-rpc 查询用户 (account登录暂用GetUserByOpenId的方式,后续需补充account查询RPC)
|
||||
// 这里简化处理:直接在api层查询并验证密码
|
||||
// TODO: 后续应该在user-rpc中增加LoginByAccount方法
|
||||
|
||||
_ = req
|
||||
_ = l
|
||||
|
||||
return nil, status.Error(codes.Unimplemented, "账号密码登录功能开发中")
|
||||
}
|
||||
|
||||
// generateToken 生成JWT Token的辅助方法
|
||||
func generateToken(config svc.ServiceContext, userInfo *user.UserInfo) (string, error) {
|
||||
j := jwtUtil.NewJWT(config.Config.Auth.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(config.Config.Auth.AccessExpire) * time.Second)),
|
||||
Issuer: "sundynix",
|
||||
},
|
||||
}
|
||||
token, err := j.CreateToken(claims)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("生成Token失败: %w", err)
|
||||
}
|
||||
return token, nil
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.10.1
|
||||
|
||||
package auth
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"time"
|
||||
|
||||
"sundynix-micro-go/app/user/api/internal/svc"
|
||||
"sundynix-micro-go/app/user/api/internal/types"
|
||||
"sundynix-micro-go/app/user/rpc/user"
|
||||
jwtUtil "sundynix-micro-go/common/utils/jwt"
|
||||
|
||||
jwtv5 "github.com/golang-jwt/jwt/v5"
|
||||
"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.UserRpc.CreateUser(l.ctx, &user.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
|
||||
j := jwtUtil.NewJWT(l.svcCtx.Config.Auth.AccessSecret)
|
||||
claims := jwtUtil.CustomClaims{
|
||||
BaseClaims: jwtUtil.BaseClaims{
|
||||
ID: createResp.User.Id,
|
||||
Account: createResp.User.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(l.svcCtx.Config.Auth.AccessExpire) * time.Second)),
|
||||
Issuer: "sundynix",
|
||||
},
|
||||
}
|
||||
token, err := j.CreateToken(claims)
|
||||
if err != nil {
|
||||
l.Errorf("生成Token失败: %v", err)
|
||||
return nil, fmt.Errorf("登录失败")
|
||||
}
|
||||
|
||||
return &types.LoginResp{
|
||||
Token: token,
|
||||
UserInfo: createResp.User,
|
||||
}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user