2025-11-27 13:09:54 +08:00
|
|
|
|
package user
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
2025-12-10 13:10:39 +08:00
|
|
|
|
"context"
|
|
|
|
|
|
"encoding/json"
|
|
|
|
|
|
"fmt"
|
|
|
|
|
|
"io"
|
|
|
|
|
|
"net/http"
|
|
|
|
|
|
"time"
|
2025-11-27 13:09:54 +08:00
|
|
|
|
|
2025-12-10 13:10:39 +08:00
|
|
|
|
"ycc-server/app/main/api/internal/svc"
|
|
|
|
|
|
"ycc-server/app/main/api/internal/types"
|
|
|
|
|
|
"ycc-server/app/main/model"
|
|
|
|
|
|
"ycc-server/common/xerr"
|
2025-11-27 13:09:54 +08:00
|
|
|
|
|
2025-12-10 13:10:39 +08:00
|
|
|
|
"github.com/google/uuid"
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
2025-11-27 13:09:54 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type WxMiniAuthLogic struct {
|
|
|
|
|
|
logx.Logger
|
|
|
|
|
|
ctx context.Context
|
|
|
|
|
|
svcCtx *svc.ServiceContext
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func NewWxMiniAuthLogic(ctx context.Context, svcCtx *svc.ServiceContext) *WxMiniAuthLogic {
|
|
|
|
|
|
return &WxMiniAuthLogic{
|
|
|
|
|
|
Logger: logx.WithContext(ctx),
|
|
|
|
|
|
ctx: ctx,
|
|
|
|
|
|
svcCtx: svcCtx,
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (l *WxMiniAuthLogic) WxMiniAuth(req *types.WXMiniAuthReq) (resp *types.WXMiniAuthResp, err error) {
|
|
|
|
|
|
// 1. 获取session_key和openid
|
|
|
|
|
|
sessionKeyResp, err := l.GetSessionKey(req.Code)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "获取session_key失败: %v", err)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 2. 查找用户授权信息
|
|
|
|
|
|
userAuth, err := l.svcCtx.UserAuthModel.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)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 3. 处理用户信息
|
2025-12-10 13:10:39 +08:00
|
|
|
|
var userID string
|
|
|
|
|
|
if userAuth != nil {
|
|
|
|
|
|
// 已存在用户,直接登录
|
|
|
|
|
|
userID = userAuth.UserId
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// 新用户创建为临时用户(没有mobile)
|
|
|
|
|
|
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
|
|
|
|
|
|
}
|
2025-11-27 13:09:54 +08:00
|
|
|
|
|
2025-12-10 13:10:39 +08:00
|
|
|
|
// 4. 生成JWT Token(动态计算userType)
|
|
|
|
|
|
token, err := l.svcCtx.UserService.GeneralUserToken(l.ctx, userID)
|
2025-11-27 13:09:54 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "生成JWT Token失败: %v", err)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 5. 返回登录结果
|
|
|
|
|
|
now := time.Now().Unix()
|
|
|
|
|
|
return &types.WXMiniAuthResp{
|
|
|
|
|
|
AccessToken: token,
|
|
|
|
|
|
AccessExpire: now + l.svcCtx.Config.JwtAuth.AccessExpire,
|
|
|
|
|
|
RefreshAfter: now + l.svcCtx.Config.JwtAuth.RefreshAfter,
|
|
|
|
|
|
}, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// SessionKeyResp 小程序登录返回结构
|
|
|
|
|
|
type SessionKeyResp struct {
|
|
|
|
|
|
Openid string `json:"openid"`
|
|
|
|
|
|
SessionKey string `json:"session_key"`
|
|
|
|
|
|
Unionid string `json:"unionid,omitempty"`
|
|
|
|
|
|
ErrCode int `json:"errcode,omitempty"`
|
|
|
|
|
|
ErrMsg string `json:"errmsg,omitempty"`
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// GetSessionKey 通过code获取小程序的session_key和openid
|
|
|
|
|
|
func (l *WxMiniAuthLogic) GetSessionKey(code string) (*SessionKeyResp, error) {
|
|
|
|
|
|
var appID string
|
|
|
|
|
|
var appSecret string
|
|
|
|
|
|
|
|
|
|
|
|
appID = l.svcCtx.Config.WechatMini.AppID
|
|
|
|
|
|
appSecret = l.svcCtx.Config.WechatMini.AppSecret
|
|
|
|
|
|
|
|
|
|
|
|
url := fmt.Sprintf("https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code",
|
|
|
|
|
|
appID, appSecret, code)
|
|
|
|
|
|
|
|
|
|
|
|
resp, err := http.Get(url)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "获取session_key失败: %v", err)
|
|
|
|
|
|
}
|
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "读取响应失败: %v", err)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var sessionKeyResp SessionKeyResp
|
|
|
|
|
|
if err = json.Unmarshal(body, &sessionKeyResp); err != nil {
|
|
|
|
|
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "解析响应失败: %v", err)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 检查微信返回的错误码
|
|
|
|
|
|
if sessionKeyResp.ErrCode != 0 {
|
|
|
|
|
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR),
|
|
|
|
|
|
"微信接口返回错误: errcode=%d, errmsg=%s",
|
|
|
|
|
|
sessionKeyResp.ErrCode, sessionKeyResp.ErrMsg)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 验证必要字段
|
|
|
|
|
|
if sessionKeyResp.Openid == "" || sessionKeyResp.SessionKey == "" {
|
|
|
|
|
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR),
|
|
|
|
|
|
"微信接口返回数据不完整: openid=%s, session_key=%s",
|
|
|
|
|
|
sessionKeyResp.Openid, sessionKeyResp.SessionKey)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return &sessionKeyResp, nil
|
|
|
|
|
|
}
|