2025-01-10 00:09:25 +08:00
|
|
|
|
package user
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"fmt"
|
|
|
|
|
"io"
|
|
|
|
|
"net/http"
|
2025-06-09 12:34:52 +08:00
|
|
|
|
"qnc-server/app/main/model"
|
2025-04-11 13:10:17 +08:00
|
|
|
|
jwtx "qnc-server/common/jwt"
|
|
|
|
|
"qnc-server/common/xerr"
|
2025-01-10 00:09:25 +08:00
|
|
|
|
"time"
|
|
|
|
|
|
2025-04-11 13:10:17 +08:00
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
|
|
|
|
|
2025-06-09 12:34:52 +08:00
|
|
|
|
"qnc-server/app/main/api/internal/svc"
|
|
|
|
|
"qnc-server/app/main/api/internal/types"
|
2025-01-10 00:09:25 +08:00
|
|
|
|
|
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type WxH5AuthLogic struct {
|
|
|
|
|
logx.Logger
|
|
|
|
|
ctx context.Context
|
|
|
|
|
svcCtx *svc.ServiceContext
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewWxH5AuthLogic(ctx context.Context, svcCtx *svc.ServiceContext) *WxH5AuthLogic {
|
|
|
|
|
return &WxH5AuthLogic{
|
|
|
|
|
Logger: logx.WithContext(ctx),
|
|
|
|
|
ctx: ctx,
|
|
|
|
|
svcCtx: svcCtx,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (l *WxH5AuthLogic) WxH5Auth(req *types.WXH5AuthReq) (resp *types.WXH5AuthResp, err error) {
|
|
|
|
|
// Step 1: 使用code获取access_token
|
2025-05-11 11:48:10 +08:00
|
|
|
|
accessTokenResp, err := l.GetAccessToken(req.Code)
|
2025-01-10 00:09:25 +08:00
|
|
|
|
if err != nil {
|
2025-03-07 03:48:59 +08:00
|
|
|
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "获取access_token失败: %v", err)
|
2025-01-10 00:09:25 +08:00
|
|
|
|
}
|
2025-04-26 15:10:01 +08:00
|
|
|
|
if accessTokenResp.AccessToken == "" || accessTokenResp.Openid == "" {
|
2025-05-11 03:01:34 +08:00
|
|
|
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "获取access_token为空: %v", accessTokenResp)
|
2025-04-26 15:10:01 +08:00
|
|
|
|
}
|
2025-01-10 00:09:25 +08:00
|
|
|
|
// Step 2: 查找用户授权信息
|
2025-01-21 20:46:38 +08:00
|
|
|
|
userAuth, findErr := l.svcCtx.UserAuthModel.FindOneByAuthTypeAuthKey(l.ctx, model.UserAuthTypeWxh5, accessTokenResp.Openid)
|
2025-01-10 00:09:25 +08:00
|
|
|
|
if findErr != nil && !errors.Is(findErr, model.ErrNotFound) {
|
2025-01-21 16:55:08 +08:00
|
|
|
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "查询用户授权失败,findErr: %v", findErr)
|
2025-01-10 00:09:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Step 3: 查找或创建用户
|
|
|
|
|
var user *model.User
|
|
|
|
|
if userAuth != nil {
|
|
|
|
|
// 授权信息存在,查找用户
|
|
|
|
|
userModel, findUserErr := l.svcCtx.UserModel.FindOne(l.ctx, userAuth.UserId)
|
|
|
|
|
if findUserErr != nil {
|
2025-01-21 16:55:08 +08:00
|
|
|
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "查询用户失败,userId: %v", findUserErr)
|
2025-01-10 00:09:25 +08:00
|
|
|
|
}
|
|
|
|
|
user = userModel
|
|
|
|
|
} else {
|
|
|
|
|
// 授权信息不存在,创建新用户
|
|
|
|
|
user = &model.User{}
|
|
|
|
|
if transErr := l.svcCtx.UserModel.Trans(l.ctx, func(context context.Context, session sqlx.Session) error {
|
|
|
|
|
// 插入数据库
|
|
|
|
|
insertResult, insertErr := l.svcCtx.UserModel.Insert(l.ctx, session, user)
|
|
|
|
|
if insertErr != nil {
|
|
|
|
|
return errors.Wrapf(insertErr, "创建新用户失败,openid: %s", accessTokenResp.Openid)
|
|
|
|
|
}
|
2025-06-09 12:34:52 +08:00
|
|
|
|
// 获取插入后生成的 main.Id
|
2025-01-10 00:09:25 +08:00
|
|
|
|
lastInsertId, lastInsertIdErr := insertResult.LastInsertId()
|
|
|
|
|
if lastInsertIdErr != nil {
|
|
|
|
|
return errors.Wrapf(lastInsertIdErr, "获取新用户ID失败,openid: %s", accessTokenResp.Openid)
|
|
|
|
|
}
|
|
|
|
|
user.Id = lastInsertId
|
|
|
|
|
// 创建用户授权信息
|
|
|
|
|
userAuth = &model.UserAuth{
|
|
|
|
|
UserId: user.Id,
|
|
|
|
|
AuthKey: accessTokenResp.Openid,
|
2025-01-21 16:55:08 +08:00
|
|
|
|
AuthType: model.UserAuthTypeWxh5, // 微信小程序
|
2025-01-10 00:09:25 +08:00
|
|
|
|
}
|
|
|
|
|
if _, insertUserAuthErr := l.svcCtx.UserAuthModel.Insert(l.ctx, session, userAuth); insertUserAuthErr != nil {
|
|
|
|
|
return errors.Wrapf(insertUserAuthErr, "创建用户授权失败,openid: %s", accessTokenResp.Openid)
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}); transErr != nil {
|
2025-01-21 16:55:08 +08:00
|
|
|
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "创建新用户事务失败: %v", transErr)
|
2025-01-10 00:09:25 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Step 4: 生成JWT Token
|
|
|
|
|
token, genErr := jwtx.GenerateJwtToken(user.Id, l.svcCtx.Config.JwtAuth.AccessSecret, l.svcCtx.Config.JwtAuth.AccessExpire)
|
|
|
|
|
if genErr != nil {
|
2025-01-21 16:55:08 +08:00
|
|
|
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "生成JWT token失败: %v", genErr)
|
2025-01-10 00:09:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
now := time.Now().Unix()
|
|
|
|
|
return &types.WXH5AuthResp{
|
|
|
|
|
AccessToken: token,
|
|
|
|
|
AccessExpire: now + l.svcCtx.Config.JwtAuth.AccessExpire,
|
|
|
|
|
RefreshAfter: now + l.svcCtx.Config.JwtAuth.RefreshAfter,
|
|
|
|
|
}, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type AccessTokenResp struct {
|
|
|
|
|
AccessToken string `json:"access_token"`
|
|
|
|
|
Openid string `json:"openid"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GetAccessToken 通过code获取access_token
|
2025-05-11 11:48:10 +08:00
|
|
|
|
func (l *WxH5AuthLogic) GetAccessToken(code string) (*AccessTokenResp, error) {
|
|
|
|
|
appID := l.svcCtx.Config.WechatH5.AppID
|
|
|
|
|
appSecret := l.svcCtx.Config.WechatH5.AppSecret
|
2025-01-10 00:09:25 +08:00
|
|
|
|
|
|
|
|
|
url := fmt.Sprintf("https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code", appID, appSecret, code)
|
|
|
|
|
|
|
|
|
|
resp, err := http.Get(url)
|
|
|
|
|
if err != nil {
|
2025-01-21 20:00:18 +08:00
|
|
|
|
return nil, err
|
2025-01-10 00:09:25 +08:00
|
|
|
|
}
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
|
|
|
if err != nil {
|
2025-01-21 20:00:18 +08:00
|
|
|
|
return nil, err
|
2025-01-10 00:09:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var accessTokenResp AccessTokenResp
|
2025-01-21 20:00:18 +08:00
|
|
|
|
if err = json.Unmarshal(body, &accessTokenResp); err != nil {
|
|
|
|
|
return nil, err
|
2025-01-10 00:09:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-01-21 20:00:18 +08:00
|
|
|
|
//if accessTokenResp.AccessToken == "" {
|
|
|
|
|
// return nil, errors.New("accessTokenResp.AccessToken为空")
|
|
|
|
|
//}
|
2025-01-10 00:09:25 +08:00
|
|
|
|
|
|
|
|
|
return &accessTokenResp, nil
|
|
|
|
|
}
|