f
This commit is contained in:
@@ -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