From 6e67c0d63bcb074d2d228d043e0daeb454e9b226 Mon Sep 17 00:00:00 2001 From: guzeng Date: Tue, 26 Apr 2022 16:43:19 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=8E=B7=E5=8F=96=E5=85=AC?= =?UTF-8?q?=E4=BC=97=E5=8F=B7openid=E5=8F=8A=E7=94=A8=E6=88=B7=E4=BF=A1?= =?UTF-8?q?=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- openid.go | 32 +++++++++++++++++++++++++++++++- openid_test.go | 14 ++++++++++++++ url.go | 6 ++++++ user.go | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ user_test.go | 13 +++++++++++++ 5 files changed, 114 insertions(+), 1 deletion(-) create mode 100644 openid_test.go create mode 100644 user.go create mode 100644 user_test.go diff --git a/openid.go b/openid.go index 46579a4..e1f7478 100644 --- a/openid.go +++ b/openid.go @@ -23,6 +23,16 @@ type MiniAppOpenidData struct { Errmsg string `json:"errmsg"` } +type MpOpenIdData struct { + AccessToken string `json:"access_token"` + ExpiresIn int `json:"expires_in"` + RefreshToken string `json:"refresh_token"` + Openid string `json:"openid"` + Scope string `json:"scope"` + Errcode int `json:"errcode"` + Errmsg string `json:"errmsg"` +} + /** * 从微信api取openid */ @@ -43,7 +53,7 @@ func GetAppOpenid(appid, secret, code string) (AppOpenIdData, error) { } /** - * 从微信api取openid + * 从微信api取小程序openid */ func GetMiniAppOpenid(appid, secret, code string) (MiniAppOpenidData, error) { @@ -61,3 +71,23 @@ func GetMiniAppOpenid(appid, secret, code string) (MiniAppOpenidData, error) { return data, err } + +/** + * 从微信api取公众号openid + */ +func GetMpOpenid(appid, secret, code string) (MpOpenIdData, error) { + + url := fmt.Sprintf(GET_MP_OPENID_API, appid, secret, code) + + data_byte, err := SendHttp("GET", url, nil) + + var data MpOpenIdData + + if err != nil { + return data, err + } + + err = json.Unmarshal(data_byte, &data) + + return data, err +} diff --git a/openid_test.go b/openid_test.go new file mode 100644 index 0000000..2ec5238 --- /dev/null +++ b/openid_test.go @@ -0,0 +1,14 @@ +package wechat + +import ( + "testing" +) + +func Test_GetMpOpenid(t *testing.T) { + appid := "wx470b16375ed3c1ae" + appsecret := "cd567382c953c478626f4b7446c3508e" + code := "053gTKkl2FAV394okUkl2j4EvB0gTKkZ" + ret, err := GetMpOpenid(appid, appsecret, code) + t.Log(ret) + t.Log(err) +} diff --git a/url.go b/url.go index 7aa046f..c2bcb94 100644 --- a/url.go +++ b/url.go @@ -15,6 +15,9 @@ const MINIAPP_UNIFORM_MESSAGE_API string = "https://api.weixin.qq.com/cgi-bin/me //小程序获取openid const GET_MINIAPP_OPENID_API string = "https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code" +//公众号获取openid +const GET_MP_OPENID_API string = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code" + //小程序发送订阅消息 const MINIAPP_SUBSCRIBE_MESSAGE_SEND_API string = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=%s" @@ -25,3 +28,6 @@ const GET_MINIAPP_QRCODE string = "https://api.weixin.qq.com/wxa/getwxacodeunlim //公众号授权 const OFFICAL_ACCOUNT_AUTHORIZATION_URL string = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=%s&redirect_uri=%s&response_type=code&scope=snsapi_userinfo&state=%s#wechat_redirect" + +//公众号获取用户信息 +const MP_USERINFO_API string = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=%s&openid=%s&lang=%s" diff --git a/user.go b/user.go new file mode 100644 index 0000000..52f25f7 --- /dev/null +++ b/user.go @@ -0,0 +1,50 @@ +package wechat + +import ( + "errors" + "fmt" +) + +type MpUserInfo struct { + Subscribe int `json:"subscribe"` + Openid string `json:"openid"` + Language string `json:"language"` + SubscribeTime int `json:"subscribe_time"` + Unionid string `json:"unionid"` + Remark string `json:"remark"` + Groupid int `json:"groupid"` + TagidList interface{} `json:"tagid_list"` + SubscribeScene string `json:"subscribe_scene"` + QrScene int `json:"qr_scene"` + QrSceneStr string `json:"qr_scene_str"` + Errcode int `json:"errcode"` + Errmsg string `json:"errmsg"` +} + +/** + * 公众号获取用户基本信息(UnionID机制) + */ +func GetMpUserInfo(access_token, openid string, lang ...string) (MpUserInfo, error) { + + var data MpUserInfo + + var language string = "zh_CN" + if len(lang) > 0 { + language = lang[0] + } + + url := fmt.Sprintf(MP_USERINFO_API, access_token, openid, language) + + data_byte, err := SendHttp("GET", url, nil) + if err != nil { + return data, err + } + + err = json.Unmarshal(data_byte, &data) + + if data.Errcode != 0 { + return data, errors.New(data.Errmsg) + } + + return data, err +} diff --git a/user_test.go b/user_test.go new file mode 100644 index 0000000..8bca80f --- /dev/null +++ b/user_test.go @@ -0,0 +1,13 @@ +package wechat + +import ( + "testing" +) + +func Test_GetMpUserInfo(t *testing.T) { + access_token := "56_V9X_7MOCa4iAxPTycxgtjftFrt_68u_T64EqSBmU-qTCF1wEG9vsfxv-4UbnEsFoTHOdwTGPkt0TCGw8fB6g9CbvipiiddE0iStdHdD_F4fnYbMqQqJ8cP9CmvM73KQJphXExnUk8Li4bmASBAGaAFAJIO" + openid := "ocUSI5LIiU6mDPt8lFEaP9Y8mIcQ" + ret, err := GetMpUserInfo(access_token, openid) + t.Log(ret) + t.Log(err) +}