From f06581c5d70521017fed540052a16c0ab09e3288 Mon Sep 17 00:00:00 2001 From: jiangyong27 Date: Thu, 5 Dec 2024 11:28:49 +0800 Subject: [PATCH] noauth --- weixin/app_sdk.go | 2 +- weixin/base.go | 11 +++++++---- weixin/oa_sdk.go | 8 ++++++-- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/weixin/app_sdk.go b/weixin/app_sdk.go index bffc823..71f5610 100644 --- a/weixin/app_sdk.go +++ b/weixin/app_sdk.go @@ -14,5 +14,5 @@ func NewAppSdk(appid, secret string) *AppSdk { } func (o *AppSdk) GetUserInfoByCode(code string) (*UserInfo, error) { - return o.getUserInfoByCode(code) + return o.getUserInfoByCode(code, true) } diff --git a/weixin/base.go b/weixin/base.go index 376d125..f12cfde 100644 --- a/weixin/base.go +++ b/weixin/base.go @@ -69,7 +69,7 @@ func (o *BaseSdk) getAccessToken() (string, error) { return o.accessToken, nil } -func (o *BaseSdk) getUserInfoByCode(code string) (*UserInfo, error) { +func (o *BaseSdk) getUserInfoByCode(code string, auth bool) (*UserInfo, error) { url := fmt.Sprintf("%s?appid=%s&secret=%s&code=%s&grant_type=authorization_code", code2AccessTokenUrl, o.appid, o.secret, code) res, err := http.Get(url) @@ -94,9 +94,12 @@ func (o *BaseSdk) getUserInfoByCode(code string) (*UserInfo, error) { user.AccessToken = g.Get("access_token").String() user.Openid = g.Get("openid").String() - err = o.getUserInfo(&user) - if err != nil { - return nil, err + if auth { + err = o.getUserInfo(&user) + if err != nil { + return nil, err + } + } return &user, nil diff --git a/weixin/oa_sdk.go b/weixin/oa_sdk.go index 9f5dd48..dbbe0f1 100644 --- a/weixin/oa_sdk.go +++ b/weixin/oa_sdk.go @@ -57,8 +57,12 @@ func (o *OaSdk) GetQrCode(sceneStr string) (string, error) { return qrcodeUrl, nil } -func (o *BaseSdk) GetUserInfoByCode(code string) (*UserInfo, error) { - return o.getUserInfoByCode(code) +func (o *OaSdk) GetUserInfoByCode(code string) (*UserInfo, error) { + return o.getUserInfoByCode(code, true) +} + +func (o *OaSdk) GetUserInfoByCodeNoAuth(code string) (*UserInfo, error) { + return o.getUserInfoByCode(code, false) } func (o *OaSdk) GetUserInfoByOpenid(openid string) (*UserInfo, error) {