refactor(wechat): 重构微信二维码生成接口上下文处理
移除全局token缓存逻辑,统一使用core.Context接口 修改GenerateQRCode方法签名,增加上下文参数 更新相关调用链以适配新的上下文处理方式
This commit is contained in:
parent
2e86f8ae42
commit
e4d4258918
@ -46,7 +46,7 @@ func (h *handler) GenerateQRCode() core.HandlerFunc {
|
||||
}
|
||||
|
||||
// 调用微信小程序二维码生成函数
|
||||
imageData, err := wechat.GenerateQRCode(req.AppID, req.AppSecret, req.Path)
|
||||
imageData, err := wechat.GenerateQRCode(ctx, req.AppID, req.AppSecret, req.Path)
|
||||
if err != nil {
|
||||
h.logger.Error(fmt.Sprintf("生成微信小程序二维码失败: %s", err.Error()))
|
||||
ctx.AbortWithError(core.Error(
|
||||
|
||||
@ -76,7 +76,7 @@ func (e *QRCodeError) Error() string {
|
||||
}
|
||||
|
||||
// GetAccessToken 获取微信 access_token
|
||||
func GetAccessToken(ctx core.StdContext, config *WechatConfig) (string, error) {
|
||||
func GetAccessToken(ctx core.Context, config *WechatConfig) (string, error) {
|
||||
if config == nil {
|
||||
return "", fmt.Errorf("微信配置不能为空")
|
||||
}
|
||||
@ -88,30 +88,11 @@ func GetAccessToken(ctx core.StdContext, config *WechatConfig) (string, error) {
|
||||
if config.AppSecret == "" {
|
||||
return "", fmt.Errorf("AppSecret 不能为空")
|
||||
}
|
||||
|
||||
// 检查缓存
|
||||
globalTokenCache.mutex.RLock()
|
||||
if globalTokenCache.Token != "" && time.Now().Before(globalTokenCache.ExpiresAt) {
|
||||
token := globalTokenCache.Token
|
||||
globalTokenCache.mutex.RUnlock()
|
||||
return token, nil
|
||||
}
|
||||
globalTokenCache.mutex.RUnlock()
|
||||
|
||||
// 缓存过期或不存在,重新获取
|
||||
globalTokenCache.mutex.Lock()
|
||||
defer globalTokenCache.mutex.Unlock()
|
||||
|
||||
// 双重检查,防止并发情况下重复请求
|
||||
if globalTokenCache.Token != "" && time.Now().Before(globalTokenCache.ExpiresAt) {
|
||||
return globalTokenCache.Token, nil
|
||||
}
|
||||
|
||||
// 构建请求URL
|
||||
url := "https://api.weixin.qq.com/cgi-bin/token"
|
||||
|
||||
// 发送HTTP请求
|
||||
client := httpclient.GetHttpClientWithContext(ctx)
|
||||
client := httpclient.GetHttpClientWithContext(ctx.RequestContext())
|
||||
resp, err := client.R().
|
||||
SetQueryParams(map[string]string{
|
||||
"grant_type": "client_credential",
|
||||
@ -119,7 +100,6 @@ func GetAccessToken(ctx core.StdContext, config *WechatConfig) (string, error) {
|
||||
"secret": config.AppSecret,
|
||||
}).
|
||||
Get(url)
|
||||
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("获取access_token失败: %v", err)
|
||||
}
|
||||
@ -152,7 +132,7 @@ func GetAccessToken(ctx core.StdContext, config *WechatConfig) (string, error) {
|
||||
// config: 微信配置(AppID 和 AppSecret)
|
||||
// request: 请求参数
|
||||
// 返回: 成功时返回图片二进制数据,失败时返回错误
|
||||
func GetQRCodeWithConfig(ctx core.StdContext, config *WechatConfig, request *QRCodeRequest) (*QRCodeResponse, error) {
|
||||
func GetQRCodeWithConfig(ctx core.Context, config *WechatConfig, request *QRCodeRequest) (*QRCodeResponse, error) {
|
||||
// 自动获取 access_token
|
||||
accessToken, err := GetAccessToken(ctx, config)
|
||||
if err != nil {
|
||||
@ -166,7 +146,7 @@ func GetQRCodeWithConfig(ctx core.StdContext, config *WechatConfig, request *QRC
|
||||
// accessToken: 接口调用凭证
|
||||
// request: 请求参数
|
||||
// 返回: 成功时返回图片二进制数据,失败时返回错误
|
||||
func GetQRCode(ctx core.StdContext, accessToken string, request *QRCodeRequest) (*QRCodeResponse, error) {
|
||||
func GetQRCode(ctx core.Context, accessToken string, request *QRCodeRequest) (*QRCodeResponse, error) {
|
||||
if accessToken == "" {
|
||||
return nil, fmt.Errorf("access_token 不能为空")
|
||||
}
|
||||
@ -201,7 +181,7 @@ func GetQRCode(ctx core.StdContext, accessToken string, request *QRCodeRequest)
|
||||
return nil, fmt.Errorf("序列化请求参数失败: %v", err)
|
||||
}
|
||||
|
||||
client := httpclient.GetHttpClientWithContext(ctx)
|
||||
client := httpclient.GetHttpClientWithContext(ctx.RequestContext())
|
||||
resp, err := client.R().
|
||||
SetHeader("Content-Type", "application/json").
|
||||
SetBody(requestBody).
|
||||
@ -240,14 +220,12 @@ func isJSONResponse(data []byte) bool {
|
||||
}
|
||||
|
||||
// GenerateQRCode 最简化的小程序码生成接口
|
||||
func GenerateQRCode(appID, appSecret, path string) ([]byte, error) {
|
||||
ctx := core.StdContext{}
|
||||
func GenerateQRCode(ctx core.Context, appID, appSecret, path string) ([]byte, error) {
|
||||
|
||||
config := &WechatConfig{
|
||||
AppID: appID,
|
||||
AppSecret: appSecret,
|
||||
}
|
||||
|
||||
request := &QRCodeRequest{
|
||||
Path: path,
|
||||
}
|
||||
|
||||
@ -1 +0,0 @@
|
||||
{"level":"fatal","time":"2025-10-18 19:32:41","caller":"logger/logger.go:333","msg":"http server startup err","domain":"mini-chat[fat]","error":"listen tcp :9991: bind: address already in use"}
|
||||
Loading…
x
Reference in New Issue
Block a user