mirror of
https://github.com/silenceper/wechat.git
synced 2026-02-10 15:52:26 +08:00
39
user/user.go
39
user/user.go
@@ -2,6 +2,7 @@ package user
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/silenceper/wechat/context"
|
"github.com/silenceper/wechat/context"
|
||||||
@@ -11,6 +12,7 @@ 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"
|
updateRemarkURL = "https://api.weixin.qq.com/cgi-bin/user/info/updateremark?access_token=%s"
|
||||||
|
batchGetUserInfoURL = "https://api.weixin.qq.com/cgi-bin/user/info/batchget?access_token=%s"
|
||||||
)
|
)
|
||||||
|
|
||||||
//User 用户管理
|
//User 用户管理
|
||||||
@@ -45,6 +47,13 @@ type Info struct {
|
|||||||
TagidList []int32 `json:"tagid_list"`
|
TagidList []int32 `json:"tagid_list"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// BatchUserQuery 待查询的用户列表
|
||||||
|
type BatchUserQuery struct {
|
||||||
|
OpenID string `json:"openid"`
|
||||||
|
Lang string `json:"lang"`
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
//GetUserInfo 获取用户基本信息
|
//GetUserInfo 获取用户基本信息
|
||||||
func (user *User) GetUserInfo(openID string) (userInfo *Info, err error) {
|
func (user *User) GetUserInfo(openID string) (userInfo *Info, err error) {
|
||||||
var accessToken string
|
var accessToken string
|
||||||
@@ -88,3 +97,33 @@ func (user *User) UpdateRemark(openID, remark string) (err error) {
|
|||||||
|
|
||||||
return util.DecodeWithCommonError(response, "UpdateRemark")
|
return util.DecodeWithCommonError(response, "UpdateRemark")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// BatchGetUser 批量获取用户基本信息
|
||||||
|
func (user *User) BatchGetUser(batchUserQuery ... *BatchUserQuery)([]*Info, error){
|
||||||
|
if len(batchUserQuery)>100{
|
||||||
|
return nil, errors.New("最多支持一次拉取100条")
|
||||||
|
}
|
||||||
|
var accessToken string
|
||||||
|
accessToken, err := user.GetAccessToken()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
requestMap := make(map[string]interface{})
|
||||||
|
requestMap["user_list"] = batchUserQuery
|
||||||
|
uri := fmt.Sprintf(batchGetUserInfoURL, accessToken)
|
||||||
|
response, err := util.PostJSON(uri, requestMap)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
// batchUserQueryResponse 批量查询返回值
|
||||||
|
type batchUserQueryResponse struct {
|
||||||
|
List []*Info `json:"user_info_list"`
|
||||||
|
}
|
||||||
|
userList := &batchUserQueryResponse{}
|
||||||
|
err = json.Unmarshal(response, userList)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return userList.List, nil
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user