From 323f14e1f5c44fad32f02eae95b4e86f19b48154 Mon Sep 17 00:00:00 2001 From: Mrx <18278715334@163.com> Date: Thu, 16 Apr 2026 16:56:04 +0800 Subject: [PATCH] pull --- .../internal/service/verificationService.go | 109 +++++++++--------- 1 file changed, 55 insertions(+), 54 deletions(-) diff --git a/app/main/api/internal/service/verificationService.go b/app/main/api/internal/service/verificationService.go index 8ba5e6e..4132d9e 100644 --- a/app/main/api/internal/service/verificationService.go +++ b/app/main/api/internal/service/verificationService.go @@ -1,26 +1,27 @@ package service import ( - "context" - "encoding/json" - "fmt" - "io" - "net/http" - "ycc-server/app/main/api/internal/config" - tianyuanapi "ycc-server/app/main/api/internal/service/tianyuanapi_sdk" + "context" + "encoding/json" + "fmt" + "io" + "net/http" + "ycc-server/app/main/api/internal/config" + tianyuanapi "ycc-server/app/main/api/internal/service/tianyuanapi_sdk" - "github.com/tidwall/gjson" + "github.com/tidwall/gjson" ) + type VerificationService struct { c config.Config - tianyuanapi *tianyuanapi.Client + tianyuanapi *tianyuanapi.Client apiRequestService *ApiRequestService } func NewVerificationService(c config.Config, tianyuanapi *tianyuanapi.Client, apiRequestService *ApiRequestService) *VerificationService { return &VerificationService{ c: c, - tianyuanapi: tianyuanapi, + tianyuanapi: tianyuanapi, apiRequestService: apiRequestService, } } @@ -156,52 +157,52 @@ func (r *VerificationService) ThreeFactorVerification(request ThreeFactorVerific // GetWechatH5OpenID 通过code获取微信H5 OpenID func (r *VerificationService) GetWechatH5OpenID(ctx context.Context, code string) (string, error) { - appID := r.c.WechatH5.AppID - appSecret := r.c.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) - resp, err := http.Get(url) - if err != nil { - return "", err - } - defer resp.Body.Close() - body, err := io.ReadAll(resp.Body) - if err != nil { - return "", err - } - var data struct { - Openid string `json:"openid"` - } - if err := json.Unmarshal(body, &data); err != nil { - return "", err - } - if data.Openid == "" { - return "", fmt.Errorf("openid为空") - } - return data.Openid, nil + appID := r.c.WechatH5.AppID + appSecret := r.c.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) + resp, err := http.Get(url) + if err != nil { + return "", err + } + defer resp.Body.Close() + body, err := io.ReadAll(resp.Body) + if err != nil { + return "", err + } + var data struct { + Openid string `json:"openid"` + } + if err := json.Unmarshal(body, &data); err != nil { + return "", err + } + if data.Openid == "" { + return "", fmt.Errorf("openid为空") + } + return data.Openid, nil } // GetWechatMiniOpenID 通过code获取微信小程序 OpenID func (r *VerificationService) GetWechatMiniOpenID(ctx context.Context, code string) (string, error) { - appID := r.c.WechatMini.AppID - appSecret := r.c.WechatMini.AppSecret - url := fmt.Sprintf("https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code", appID, appSecret, code) - resp, err := http.Get(url) - if err != nil { - return "", err - } - defer resp.Body.Close() - body, err := io.ReadAll(resp.Body) - if err != nil { - return "", err - } - var data struct { - Openid string `json:"openid"` - } - if err := json.Unmarshal(body, &data); err != nil { - return "", err - } - if data.Openid == "" { - return "", fmt.Errorf("openid为空") - } - return data.Openid, nil + appID := r.c.WechatMini.AppID + appSecret := r.c.WechatMini.AppSecret + url := fmt.Sprintf("https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code", appID, appSecret, code) + resp, err := http.Get(url) + if err != nil { + return "", err + } + defer resp.Body.Close() + body, err := io.ReadAll(resp.Body) + if err != nil { + return "", err + } + var data struct { + Openid string `json:"openid"` + } + if err := json.Unmarshal(body, &data); err != nil { + return "", err + } + if data.Openid == "" { + return "", fmt.Errorf("openid为空") + } + return data.Openid, nil }