mirror of
https://github.com/silenceper/wechat.git
synced 2026-02-04 12:52:27 +08:00
feat: 公众号: 批量获取用户信息。 (#686)
* feat: 公众号: 批量获取用户信息。 * update: 公众号: 调整批量获取用户方法的输入参数。 * update: 公众号: 调整批量获取用户方法的输入参数。
This commit is contained in:
@@ -2,6 +2,7 @@ package user
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
|
||||||
@@ -10,9 +11,10 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
userInfoURL = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=%s&openid=%s&lang=zh_CN"
|
userInfoURL = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=%s&openid=%s&lang=zh_CN"
|
||||||
updateRemarkURL = "https://api.weixin.qq.com/cgi-bin/user/info/updateremark?access_token=%s"
|
userInfoBatchURL = "https://api.weixin.qq.com/cgi-bin/user/info/batchget"
|
||||||
userListURL = "https://api.weixin.qq.com/cgi-bin/user/get"
|
updateRemarkURL = "https://api.weixin.qq.com/cgi-bin/user/info/updateremark?access_token=%s"
|
||||||
|
userListURL = "https://api.weixin.qq.com/cgi-bin/user/get"
|
||||||
)
|
)
|
||||||
|
|
||||||
// User 用户管理
|
// User 用户管理
|
||||||
@@ -30,7 +32,11 @@ func NewUser(context *context.Context) *User {
|
|||||||
// Info 用户基本信息
|
// Info 用户基本信息
|
||||||
type Info struct {
|
type Info struct {
|
||||||
util.CommonError
|
util.CommonError
|
||||||
|
userInfo
|
||||||
|
}
|
||||||
|
|
||||||
|
// 用户基本信息
|
||||||
|
type userInfo struct {
|
||||||
Subscribe int32 `json:"subscribe"`
|
Subscribe int32 `json:"subscribe"`
|
||||||
OpenID string `json:"openid"`
|
OpenID string `json:"openid"`
|
||||||
Nickname string `json:"nickname"`
|
Nickname string `json:"nickname"`
|
||||||
@@ -88,6 +94,48 @@ func (user *User) GetUserInfo(openID string) (userInfo *Info, err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// BatchGetUserInfoParams 批量获取用户基本信息参数
|
||||||
|
type BatchGetUserInfoParams struct {
|
||||||
|
UserList []BatchGetUserListItem `json:"user_list"` // 需要批量获取基本信息的用户列表
|
||||||
|
}
|
||||||
|
|
||||||
|
// BatchGetUserListItem 需要获取基本信息的用户
|
||||||
|
type BatchGetUserListItem struct {
|
||||||
|
OpenID string `json:"openid"` // 用户的标识,对当前公众号唯一
|
||||||
|
Lang string `json:"lang"` // 国家地区语言版本,zh_CN 简体,zh_TW 繁体,en 英语,默认为zh-CN
|
||||||
|
}
|
||||||
|
|
||||||
|
// InfoList 用户基本信息列表
|
||||||
|
type InfoList struct {
|
||||||
|
util.CommonError
|
||||||
|
UserInfoList []userInfo `json:"user_info_list"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// BatchGetUserInfo 批量获取用户基本信息
|
||||||
|
func (user *User) BatchGetUserInfo(params BatchGetUserInfoParams) (*InfoList, error) {
|
||||||
|
if len(params.UserList) > 100 {
|
||||||
|
return nil, errors.New("params length must be less than or equal to 100")
|
||||||
|
}
|
||||||
|
|
||||||
|
ak, err := user.GetAccessToken()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
uri := fmt.Sprintf("%s?access_token=%s", userInfoBatchURL, ak)
|
||||||
|
res, err := util.PostJSON(uri, params)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var data InfoList
|
||||||
|
err = util.DecodeWithError(res, &data, "BatchGetUserInfo")
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &data, nil
|
||||||
|
}
|
||||||
|
|
||||||
// UpdateRemark 设置用户备注名
|
// UpdateRemark 设置用户备注名
|
||||||
func (user *User) UpdateRemark(openID, remark string) (err error) {
|
func (user *User) UpdateRemark(openID, remark string) (err error) {
|
||||||
var accessToken string
|
var accessToken string
|
||||||
|
|||||||
Reference in New Issue
Block a user