1
0
mirror of https://github.com/silenceper/wechat.git synced 2026-02-05 13:12:26 +08:00

Compare commits

..

4 Commits

Author SHA1 Message Date
silenceper
42b0966049 Revert "feat: 小程序/公众号增加 openApi 管理。 (#685)"
This reverts commit 31f8e24994.
2023-06-05 15:12:46 +08:00
sam
31f8e24994 feat: 小程序/公众号增加 openApi 管理。 (#685)
* feat: 小程序/公众号增加 openApi 管理。

* update: 修改字段名大小写。

* update: 修改字段名大小写。

* style: import 换行。

* update: openApi 管理放到内部包中。
2023-06-04 21:57:15 +08:00
sam
ae1b056515 feat: 公众号: 批量获取用户信息。 (#686)
* feat: 公众号: 批量获取用户信息。

* update: 公众号: 调整批量获取用户方法的输入参数。

* update: 公众号: 调整批量获取用户方法的输入参数。
2023-05-31 21:55:21 +08:00
jingyuexing
8bae546b77 Improve developer experience (#681)
* feat: 添加query 以及query单元测试
feat: 添加模板字符串解析以及模板字符串单元测试

* improve: 序列化请求参数,使得参数更易读

* delete: delete useless module

* format: format code

* docs: add function comment

* docs: comment method

* fix: fixed type convert error

* feat: support any type

* feat: support other type

* format: format code

* test: check logger

* format: format code

* test: udpate testing case

* del: remove useless code

* del: remove useless module

* test: update testing case

*  feat: support for unsigned integers

*  feat: template string support any type
2023-05-31 17:26:43 +08:00
6 changed files with 180 additions and 14 deletions

View File

@@ -2,6 +2,7 @@ package user
import (
"encoding/json"
"errors"
"fmt"
"net/url"
@@ -10,9 +11,10 @@ import (
)
const (
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"
userListURL = "https://api.weixin.qq.com/cgi-bin/user/get"
userInfoURL = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=%s&openid=%s&lang=zh_CN"
userInfoBatchURL = "https://api.weixin.qq.com/cgi-bin/user/info/batchget"
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 用户管理
@@ -30,7 +32,11 @@ func NewUser(context *context.Context) *User {
// Info 用户基本信息
type Info struct {
util.CommonError
userInfo
}
// 用户基本信息
type userInfo struct {
Subscribe int32 `json:"subscribe"`
OpenID string `json:"openid"`
Nickname string `json:"nickname"`
@@ -88,6 +94,48 @@ func (user *User) GetUserInfo(openID string) (userInfo *Info, err error) {
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 设置用户备注名
func (user *User) UpdateRemark(openID, remark string) (err error) {
var accessToken string

24
util/query.go Normal file
View File

@@ -0,0 +1,24 @@
package util
import (
"fmt"
"strings"
)
// Query 将Map序列化为Query参数
func Query(params map[string]interface{}) string {
finalString := make([]string, 0)
for key, value := range params {
valueString := ""
switch v := value.(type) {
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
valueString = fmt.Sprintf("%d", v)
case bool:
valueString = fmt.Sprintf("%v", v)
default:
valueString = fmt.Sprintf("%s", v)
}
finalString = append(finalString, strings.Join([]string{key, valueString}, "="))
}
return strings.Join(finalString, "&")
}

19
util/query_test.go Normal file
View File

@@ -0,0 +1,19 @@
package util
import (
"testing"
)
// TestQuery query method test case
func TestQuery(t *testing.T) {
result := Query(map[string]interface{}{
"age": 12,
"name": "Alan",
"cat": "Peter",
})
if result == "" {
// 由于hash是乱序 所以没法很好的预测输出的字符串
// 将会输出符合Query规则的字符串 "age=12&name=Alan&cat=Peter"
t.Error("NOT PASS")
}
}

24
util/template.go Normal file
View File

@@ -0,0 +1,24 @@
package util
import (
"fmt"
"strings"
)
// Template 对字符串中的和map的key相同的字符串进行模板替换 仅支持 形如: {name}
func Template(source string, data map[string]interface{}) string {
sourceCopy := &source
for k, val := range data {
valStr := ""
switch v := val.(type) {
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
valStr = fmt.Sprintf("%d", v)
case bool:
valStr = fmt.Sprintf("%v", v)
default:
valStr = fmt.Sprintf("%s", v)
}
*sourceCopy = strings.Replace(*sourceCopy, strings.Join([]string{"{", k, "}"}, ""), valStr, 1)
}
return *sourceCopy
}

20
util/template_test.go Normal file
View File

@@ -0,0 +1,20 @@
package util
import (
"testing"
)
// TestTemplate testing case about Template method
func TestTemplate(t *testing.T) {
result := Template("{name}={age};{with}={another};any={any};boolean={boolean}", map[string]interface{}{
"name": "Helan",
"age": "33",
"with": "Pep",
"another": "C",
"any": 33,
"boolean": false,
})
if result != "Helan=33;Pep=C;any=33;boolean=false" {
t.Error("NOT PSS testing")
}
}

View File

@@ -1,22 +1,22 @@
package addresslist
import (
"fmt"
"strings"
"github.com/silenceper/wechat/v2/util"
)
const (
// userSimpleListURL 获取部门成员
userSimpleListURL = "https://qyapi.weixin.qq.com/cgi-bin/user/simplelist?access_token=%s&department_id=%d"
userSimpleListURL = "https://qyapi.weixin.qq.com/cgi-bin/user/simplelist"
// userGetURL 读取成员
userGetURL = "https://qyapi.weixin.qq.com/cgi-bin/user/get?access_token=%s&userid=%s"
userGetURL = "https://qyapi.weixin.qq.com/cgi-bin/user/get"
// userListIDURL 获取成员ID列表
userListIDURL = "https://qyapi.weixin.qq.com/cgi-bin/user/list_id?access_token=%s"
userListIDURL = "https://qyapi.weixin.qq.com/cgi-bin/user/list_id"
// convertToOpenIDURL userID转openID
convertToOpenIDURL = "https://qyapi.weixin.qq.com/cgi-bin/user/convert_to_openid?access_token=%s"
convertToOpenIDURL = "https://qyapi.weixin.qq.com/cgi-bin/user/convert_to_openid"
// convertToUserIDURL openID转userID
convertToUserIDURL = "https://qyapi.weixin.qq.com/cgi-bin/user/convert_to_userid?access_token=%s"
convertToUserIDURL = "https://qyapi.weixin.qq.com/cgi-bin/user/convert_to_userid"
)
type (
@@ -45,7 +45,13 @@ func (r *Client) UserSimpleList(departmentID int) ([]*UserList, error) {
return nil, err
}
var response []byte
if response, err = util.HTTPGet(fmt.Sprintf(userSimpleListURL, accessToken, departmentID)); err != nil {
if response, err = util.HTTPGet(strings.Join([]string{
userSimpleListURL,
util.Query(map[string]interface{}{
"access_token": accessToken,
"department_id": departmentID,
}),
}, "?")); err != nil {
return nil, err
}
result := &UserSimpleListResponse{}
@@ -129,7 +135,15 @@ func (r *Client) UserGet(UserID string) (*UserGetResponse, error) {
return nil, err
}
var response []byte
if response, err = util.HTTPGet(fmt.Sprintf(userGetURL, accessToken, UserID)); err != nil {
if response, err = util.HTTPGet(
strings.Join([]string{
userGetURL,
util.Query(map[string]interface{}{
"access_token": accessToken,
"department_id": UserID,
}),
}, "?")); err != nil {
return nil, err
}
result := &UserGetResponse{}
@@ -170,7 +184,12 @@ func (r *Client) UserListID(req *UserListIDRequest) (*UserListIDResponse, error)
return nil, err
}
var response []byte
if response, err = util.PostJSON(fmt.Sprintf(userListIDURL, accessToken), req); err != nil {
if response, err = util.PostJSON(strings.Join([]string{
userListIDURL,
util.Query(map[string]interface{}{
"access_token": accessToken,
}),
}, "?"), req); err != nil {
return nil, err
}
result := &UserListIDResponse{}
@@ -204,7 +223,13 @@ func (r *Client) ConvertToOpenID(userID string) (string, error) {
return "", err
}
var response []byte
if response, err = util.PostJSON(fmt.Sprintf(convertToOpenIDURL, accessToken), &convertToOpenIDRequest{
if response, err = util.PostJSON(strings.Join([]string{
convertToOpenIDURL,
util.Query(map[string]interface{}{
"access_token": accessToken,
}),
}, "?"), &convertToOpenIDRequest{
UserID: userID,
}); err != nil {
return "", err
@@ -240,7 +265,13 @@ func (r *Client) ConvertToUserID(openID string) (string, error) {
return "", err
}
var response []byte
if response, err = util.PostJSON(fmt.Sprintf(convertToUserIDURL, accessToken), &convertToUserIDRequest{
if response, err = util.PostJSON(strings.Join([]string{
convertToUserIDURL,
util.Query(map[string]interface{}{
"access_token": accessToken,
}),
}, "?"), &convertToUserIDRequest{
OpenID: openID,
}); err != nil {
return "", err