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"}