You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
wechatoffice/sns.userinfo.go

44 lines
2.2 KiB

2 years ago
package wechatoffice
import (
2 years ago
"context"
2 years ago
"encoding/json"
"fmt"
"go.dtapp.net/gorequest"
"net/http"
)
type SnsUserinfoResponse struct {
Openid string `json:"openid"` // 用户的唯一标识
Nickname string `json:"nickname"` // 用户昵称
Sex int `json:"sex"` // 用户的性别值为1时是男性值为2时是女性值为0时是未知
Province string `json:"province"` // 用户个人资料填写的省份
City string `json:"city"` // 普通用户个人资料填写的城市
Country string `json:"country"` // 国家如中国为CN
Headimgurl string `json:"headimgurl"` // 用户头像最后一个数值代表正方形头像大小有0、46、64、96、132数值可选0代表640*640正方形头像用户没有头像时该项为空。若用户更换头像原有头像URL将失效。
Privilege []string `json:"privilege"` // 用户特权信息json 数组如微信沃卡用户为chinaunicom
Unionid string `json:"unionid,omitempty"` // 只有在用户将公众号绑定到微信开放平台帐号后,才会出现该字段。
}
type SnsUserinfoResult struct {
Result SnsUserinfoResponse // 结果
Body []byte // 内容
Http gorequest.Response // 请求
Err error // 错误
}
func newSnsUserinfoResult(result SnsUserinfoResponse, body []byte, http gorequest.Response, err error) *SnsUserinfoResult {
return &SnsUserinfoResult{Result: result, Body: body, Http: http, Err: err}
}
// SnsUserinfo 拉取用户信息(需scope为 snsapi_userinfo)
// https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_webpage_authorization.html#0
2 years ago
func (c *Client) SnsUserinfo(ctx context.Context, accessToken, openid string) *SnsUserinfoResult {
2 years ago
// 请求
2 years ago
request, err := c.request(ctx, fmt.Sprintf(apiUrl+"/sns/userinfo?access_token=%s&openid=%s&lang=zh_CN", accessToken, openid), map[string]interface{}{}, http.MethodGet)
2 years ago
// 定义
var response SnsUserinfoResponse
err = json.Unmarshal(request.ResponseBody, &response)
return newSnsUserinfoResult(response, request.ResponseBody, request, err)
}