f
This commit is contained in:
@@ -73,6 +73,9 @@ func (l *BindMobileLogic) BindMobile(req *types.BindMobileReq) (resp *types.Bind
|
||||
if claims.UserType == model.UserTypeTemp {
|
||||
userTemp, err := l.svcCtx.UserTempModel.FindOne(l.ctx, claims.UserId)
|
||||
if err != nil {
|
||||
if errors.Is(err, model.ErrNotFound) {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.USER_TEMP_INVALID), "绑定手机号, 临时用户不存在: %d", claims.UserId)
|
||||
}
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "绑定手机号, 读取临时用户失败: %v", err)
|
||||
}
|
||||
userAuth, err := l.svcCtx.UserAuthModel.FindOneByUserIdAuthType(l.ctx, user.Id, userTemp.AuthType)
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"context"
|
||||
"bdrp-server/app/main/api/internal/svc"
|
||||
"bdrp-server/app/main/api/internal/types"
|
||||
"bdrp-server/app/main/model"
|
||||
"bdrp-server/common/ctxdata"
|
||||
"bdrp-server/common/xerr"
|
||||
"bdrp-server/pkg/lzkit/crypto"
|
||||
"context"
|
||||
|
||||
"github.com/jinzhu/copier"
|
||||
"github.com/pkg/errors"
|
||||
@@ -38,6 +38,15 @@ func (l *DetailLogic) Detail() (resp *types.UserInfoResp, err error) {
|
||||
userID := claims.UserId
|
||||
userType := claims.UserType
|
||||
if userType != model.UserTypeNormal {
|
||||
if userType == model.UserTypeTemp {
|
||||
_, err = l.svcCtx.UserTempModel.FindOne(l.ctx, userID)
|
||||
if err != nil {
|
||||
if errors.Is(err, model.ErrNotFound) {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.USER_TEMP_INVALID), "用户信息, 临时用户不存在, %v", err)
|
||||
}
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "用户信息, 查询临时用户失败, %v", err)
|
||||
}
|
||||
}
|
||||
return &types.UserInfoResp{
|
||||
UserInfo: types.User{
|
||||
Id: userID,
|
||||
|
||||
@@ -37,7 +37,10 @@ func (l *GetTokenLogic) GetToken() (resp *types.MobileCodeLoginResp, err error)
|
||||
if claims.UserType == model.UserTypeTemp {
|
||||
_, err := l.svcCtx.UserTempModel.FindOne(l.ctx, claims.UserId)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "用户信息, %v", err)
|
||||
if errors.Is(err, model.ErrNotFound) {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.USER_TEMP_INVALID), "用户信息, 临时用户不存在, %v", err)
|
||||
}
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "用户信息, 查询临时用户失败, %v", err)
|
||||
}
|
||||
} else {
|
||||
user, err := l.svcCtx.UserModel.FindOne(l.ctx, claims.UserId)
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"context"
|
||||
"bdrp-server/app/main/model"
|
||||
"bdrp-server/common/xerr"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
@@ -104,6 +104,8 @@ func (l *WxH5AuthLogic) WxH5Auth(req *types.WXH5AuthReq) (resp *types.WXH5AuthRe
|
||||
type AccessTokenResp struct {
|
||||
AccessToken string `json:"access_token"`
|
||||
Openid string `json:"openid"`
|
||||
ErrCode int `json:"errcode,omitempty"`
|
||||
ErrMsg string `json:"errmsg,omitempty"`
|
||||
}
|
||||
|
||||
// GetAccessToken 通过code获取access_token
|
||||
@@ -112,26 +114,46 @@ func (l *WxH5AuthLogic) GetAccessToken(code string) (*AccessTokenResp, error) {
|
||||
appSecret := l.svcCtx.Config.WechatH5.AppSecret
|
||||
|
||||
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)
|
||||
const (
|
||||
maxRetryTimes = 3
|
||||
httpTimeout = 5 * time.Second
|
||||
retryDelay = 100 * time.Millisecond
|
||||
)
|
||||
|
||||
resp, err := http.Get(url)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
client := &http.Client{Timeout: httpTimeout}
|
||||
var lastErr error
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
for attempt := 1; attempt <= maxRetryTimes; attempt++ {
|
||||
resp, err := client.Get(url)
|
||||
if err != nil {
|
||||
lastErr = err
|
||||
} else {
|
||||
body, readErr := io.ReadAll(resp.Body)
|
||||
resp.Body.Close()
|
||||
if readErr != nil {
|
||||
lastErr = readErr
|
||||
} else {
|
||||
var accessTokenResp AccessTokenResp
|
||||
if unmarshalErr := json.Unmarshal(body, &accessTokenResp); unmarshalErr != nil {
|
||||
lastErr = unmarshalErr
|
||||
} else {
|
||||
if accessTokenResp.ErrCode != 0 {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR),
|
||||
"微信接口返回错误: errcode=%d, errmsg=%s",
|
||||
accessTokenResp.ErrCode, accessTokenResp.ErrMsg)
|
||||
}
|
||||
if accessTokenResp.AccessToken == "" || accessTokenResp.Openid == "" {
|
||||
return nil, errors.New("微信接口返回数据不完整")
|
||||
}
|
||||
return &accessTokenResp, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if attempt < maxRetryTimes {
|
||||
time.Sleep(time.Duration(attempt) * retryDelay)
|
||||
}
|
||||
}
|
||||
|
||||
var accessTokenResp AccessTokenResp
|
||||
if err = json.Unmarshal(body, &accessTokenResp); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if accessTokenResp.AccessToken == "" || accessTokenResp.Openid == "" {
|
||||
return nil, errors.New("accessTokenResp.AccessToken为空")
|
||||
}
|
||||
|
||||
return &accessTokenResp, nil
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "请求微信access_token接口失败(重试%d次): %v", maxRetryTimes, lastErr)
|
||||
}
|
||||
|
||||
@@ -119,36 +119,52 @@ func (l *WxMiniAuthLogic) GetSessionKey(code string) (*SessionKeyResp, error) {
|
||||
|
||||
url := fmt.Sprintf("https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code",
|
||||
appID, appSecret, code)
|
||||
const (
|
||||
maxRetryTimes = 3
|
||||
httpTimeout = 5 * time.Second
|
||||
retryDelay = 100 * time.Millisecond
|
||||
)
|
||||
|
||||
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()
|
||||
client := &http.Client{Timeout: httpTimeout}
|
||||
var lastErr error
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "读取响应失败: %v", err)
|
||||
for attempt := 1; attempt <= maxRetryTimes; attempt++ {
|
||||
resp, err := client.Get(url)
|
||||
if err != nil {
|
||||
lastErr = err
|
||||
} else {
|
||||
body, readErr := io.ReadAll(resp.Body)
|
||||
resp.Body.Close()
|
||||
if readErr != nil {
|
||||
lastErr = readErr
|
||||
} else {
|
||||
var sessionKeyResp SessionKeyResp
|
||||
if unmarshalErr := json.Unmarshal(body, &sessionKeyResp); unmarshalErr != nil {
|
||||
lastErr = unmarshalErr
|
||||
} else {
|
||||
// 检查微信返回的错误码
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if attempt < maxRetryTimes {
|
||||
time.Sleep(time.Duration(attempt) * retryDelay)
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "请求微信session_key接口失败(重试%d次): %v", maxRetryTimes, lastErr)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user