From e4d42589181c03fc87f11b330c61fb5f907f3ece Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E6=96=B9=E6=88=90?= Date: Sun, 19 Oct 2025 00:23:44 +0800 Subject: [PATCH] =?UTF-8?q?refactor(wechat):=20=E9=87=8D=E6=9E=84=E5=BE=AE?= =?UTF-8?q?=E4=BF=A1=E4=BA=8C=E7=BB=B4=E7=A0=81=E7=94=9F=E6=88=90=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E4=B8=8A=E4=B8=8B=E6=96=87=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 移除全局token缓存逻辑,统一使用core.Context接口 修改GenerateQRCode方法签名,增加上下文参数 更新相关调用链以适配新的上下文处理方式 --- internal/api/wechat/generate_qrcode.go | 2 +- internal/pkg/wechat/qrcode.go | 34 +++++--------------------- logs/mini-chat-access.log | 1 - 3 files changed, 7 insertions(+), 30 deletions(-) diff --git a/internal/api/wechat/generate_qrcode.go b/internal/api/wechat/generate_qrcode.go index fdc6fd3..f783aeb 100644 --- a/internal/api/wechat/generate_qrcode.go +++ b/internal/api/wechat/generate_qrcode.go @@ -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( diff --git a/internal/pkg/wechat/qrcode.go b/internal/pkg/wechat/qrcode.go index bc5f506..56ad3e2 100644 --- a/internal/pkg/wechat/qrcode.go +++ b/internal/pkg/wechat/qrcode.go @@ -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, } diff --git a/logs/mini-chat-access.log b/logs/mini-chat-access.log index 9c25d46..e69de29 100644 --- a/logs/mini-chat-access.log +++ b/logs/mini-chat-access.log @@ -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"}