Browse Source

增加获取公众号openid及用户信息

master v0.2.0
guzeng 2 years ago
parent
commit
6e67c0d63b
5 changed files with 114 additions and 1 deletions
  1. +31
    -1
      openid.go
  2. +14
    -0
      openid_test.go
  3. +6
    -0
      url.go
  4. +50
    -0
      user.go
  5. +13
    -0
      user_test.go

+ 31
- 1
openid.go View File

@ -23,6 +23,16 @@ type MiniAppOpenidData struct {
Errmsg string `json:"errmsg"` 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 * 从微信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) { func GetMiniAppOpenid(appid, secret, code string) (MiniAppOpenidData, error) {
@ -61,3 +71,23 @@ func GetMiniAppOpenid(appid, secret, code string) (MiniAppOpenidData, error) {
return data, err 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
}

+ 14
- 0
openid_test.go View File

@ -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)
}

+ 6
- 0
url.go View File

@ -15,6 +15,9 @@ const MINIAPP_UNIFORM_MESSAGE_API string = "https://api.weixin.qq.com/cgi-bin/me
//小程序获取openid //小程序获取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" 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" 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 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"

+ 50
- 0
user.go View File

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

+ 13
- 0
user_test.go View File

@ -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)
}

Loading…
Cancel
Save