1
0
mirror of https://github.com/silenceper/wechat.git synced 2026-03-01 00:35:26 +08:00

Compare commits

..

8 Commits

Author SHA1 Message Date
markwang
b12825f83b feat:企业微信-电子发票 (#695)
Co-authored-by: markwang <www.wang61@qq.com>
2023-06-21 17:31:08 +08:00
okhowang
86cbd8c0b2 feat: 微信回调消息增加菜单ID字段 (#694)
close #693
2023-06-16 18:07:15 +08:00
markwang
46c3722308 feat:企业微信-通讯录管理-互联企业 (#688) 2023-06-12 13:34:56 +08:00
houseme
7d11af713b feat: improve workflows config and upgrade action version (#690) 2023-06-11 12:08:38 +08:00
silenceper
fafb1784da Update go.yml: 指定golangci-lint 版本为v1.52.2 (#689) 2023-06-09 20:35:49 +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
17 changed files with 818 additions and 28 deletions

View File

@@ -10,17 +10,21 @@ jobs:
golangci:
strategy:
matrix:
go-version: [1.16.x,1.17.x,1.18.x]
go-version: [ '1.16','1.17','1.18','1.19','1.20' ]
name: golangci-lint
runs-on: ubuntu-latest
steps:
- uses: actions/setup-go@v3
- uses: actions/checkout@v3
- name: Setup Golang ${{ matrix.go-version }}
uses: actions/setup-go@v4
with:
go-version: ${{ matrix.go-version }}
- name: Checkout
uses: actions/checkout@v3
- name: golangci-lint
uses: golangci/golangci-lint-action@v3.2.0
uses: golangci/golangci-lint-action@v3
with:
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
version: latest
version: v1.52.2
build:
name: Test
runs-on: ubuntu-latest
@@ -28,7 +32,7 @@ jobs:
redis:
image: redis
ports:
- 6379:6379
- 6379:6379
options: --entrypoint redis-server
memcached:
image: memcached
@@ -38,14 +42,14 @@ jobs:
# strategy set
strategy:
matrix:
go: ["1.16", "1.17", "1.18"]
go: [ '1.16','1.17','1.18','1.19','1.20' ]
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- name: Set up Go 1.x
uses: actions/setup-go@v2
uses: actions/setup-go@v4
with:
go-version: ${{ matrix.go }}
id: go
- name: Test
run: go test -v -race ./...
run: go test -v -race ./...

View File

@@ -11,17 +11,17 @@ jobs:
steps:
-
name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v3
with:
fetch-depth: 0
-
name: Set up Go
uses: actions/setup-go@v2
uses: actions/setup-go@v4
with:
go-version: 1.15
go-version: 1.16
-
name: Run GoReleaser
uses: goreleaser/goreleaser-action@v2
uses: goreleaser/goreleaser-action@v4
with:
version: latest
args: release --rm-dist

36
domain/openapi/mgnt.go Normal file
View File

@@ -0,0 +1,36 @@
package openapi
import "github.com/silenceper/wechat/v2/util"
// GetAPIQuotaParams 查询API调用额度参数
type GetAPIQuotaParams struct {
CgiPath string `json:"cgi_path"` // api的请求地址例如"/cgi-bin/message/custom/send";不要前缀“https://api.weixin.qq.com” ,也不要漏了"/",否则都会76003的报错
}
// APIQuota API调用额度
type APIQuota struct {
util.CommonError
Quota struct {
DailyLimit int64 `json:"daily_limit"` // 当天该账号可调用该接口的次数
Used int64 `json:"used"` // 当天已经调用的次数
Remain int64 `json:"remain"` // 当天剩余调用次数
} `json:"quota"` // 详情
}
// GetRidInfoParams 查询rid信息参数
type GetRidInfoParams struct {
Rid string `json:"rid"` // 调用接口报错返回的rid
}
// RidInfo rid信息
type RidInfo struct {
util.CommonError
Request struct {
InvokeTime int64 `json:"invoke_time"` // 发起请求的时间戳
CostInMs int64 `json:"cost_in_ms"` // 请求毫秒级耗时
RequestURL string `json:"request_url"` // 请求的URL参数
RequestBody string `json:"request_body"` // post请求的请求参数
ResponseBody string `json:"response_body"` // 接口请求返回参数
ClientIP string `json:"client_ip"` // 接口请求的客户端ip
} `json:"request"` // 该rid对应的请求详情
}

127
internal/openapi/mgnt.go Normal file
View File

@@ -0,0 +1,127 @@
package openapi
import (
"errors"
"fmt"
"github.com/silenceper/wechat/v2/domain/openapi"
mpContext "github.com/silenceper/wechat/v2/miniprogram/context"
ocContext "github.com/silenceper/wechat/v2/officialaccount/context"
"github.com/silenceper/wechat/v2/util"
)
const (
clearQuotaURL = "https://api.weixin.qq.com/cgi-bin/clear_quota" // 重置API调用次数
getAPIQuotaURL = "https://api.weixin.qq.com/cgi-bin/openapi/quota/get" // 查询API调用额度
getRidInfoURL = "https://api.weixin.qq.com/cgi-bin/openapi/rid/get" // 查询rid信息
clearQuotaByAppSecretURL = "https://api.weixin.qq.com/cgi-bin/clear_quota/v2" // 使用AppSecret重置 API 调用次数
)
// OpenAPI openApi管理
type OpenAPI struct {
ctx interface{}
}
// NewOpenAPI 实例化
func NewOpenAPI(ctx interface{}) *OpenAPI {
return &OpenAPI{ctx: ctx}
}
// ClearQuota 重置API调用次数
// https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/openApi-mgnt/clearQuota.html
func (o *OpenAPI) ClearQuota() error {
appID, _, err := o.getAppIDAndSecret()
if err != nil {
return err
}
var payload = struct {
AppID string `json:"appid"`
}{
AppID: appID,
}
res, err := o.doPostRequest(clearQuotaURL, payload)
if err != nil {
return err
}
return util.DecodeWithCommonError(res, "ClearQuota")
}
// GetAPIQuota 查询API调用额度
// https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/openApi-mgnt/getApiQuota.html
func (o *OpenAPI) GetAPIQuota(params openapi.GetAPIQuotaParams) (quota openapi.APIQuota, err error) {
res, err := o.doPostRequest(getAPIQuotaURL, params)
if err != nil {
return
}
err = util.DecodeWithError(res, &quota, "GetAPIQuota")
return
}
// GetRidInfo 查询rid信息
// https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/openApi-mgnt/getRidInfo.html
func (o *OpenAPI) GetRidInfo(params openapi.GetRidInfoParams) (r openapi.RidInfo, err error) {
res, err := o.doPostRequest(getRidInfoURL, params)
if err != nil {
return
}
err = util.DecodeWithError(res, &r, "GetRidInfo")
return
}
// ClearQuotaByAppSecret 使用AppSecret重置 API 调用次数
// https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/openApi-mgnt/clearQuotaByAppSecret.html
func (o *OpenAPI) ClearQuotaByAppSecret() error {
id, secret, err := o.getAppIDAndSecret()
if err != nil {
return err
}
uri := fmt.Sprintf("%s?appid=%s&appsecret=%s", clearQuotaByAppSecretURL, id, secret)
res, err := util.HTTPPost(uri, "")
if err != nil {
return err
}
return util.DecodeWithCommonError(res, "ClearQuotaByAppSecret")
}
// 获取 AppID 和 AppSecret
func (o *OpenAPI) getAppIDAndSecret() (string, string, error) {
switch o.ctx.(type) {
case *mpContext.Context:
c := o.ctx.(*mpContext.Context)
return c.AppID, c.AppSecret, nil
case *ocContext.Context:
c := o.ctx.(*ocContext.Context)
return c.AppID, c.AppSecret, nil
default:
return "", "", errors.New("invalid context type")
}
}
// 获取 AccessToken
func (o *OpenAPI) getAccessToken() (string, error) {
switch o.ctx.(type) {
case *mpContext.Context:
return o.ctx.(*mpContext.Context).GetAccessToken()
case *ocContext.Context:
return o.ctx.(*ocContext.Context).GetAccessToken()
default:
return "", errors.New("invalid context type")
}
}
// 创建 POST 请求
func (o *OpenAPI) doPostRequest(uri string, payload interface{}) ([]byte, error) {
ak, err := o.getAccessToken()
if err != nil {
return nil, err
}
uri = fmt.Sprintf("%s?access_token=%s", uri, ak)
return util.PostJSON(uri, payload)
}

View File

@@ -2,6 +2,7 @@ package miniprogram
import (
"github.com/silenceper/wechat/v2/credential"
"github.com/silenceper/wechat/v2/internal/openapi"
"github.com/silenceper/wechat/v2/miniprogram/analysis"
"github.com/silenceper/wechat/v2/miniprogram/auth"
"github.com/silenceper/wechat/v2/miniprogram/business"
@@ -126,3 +127,8 @@ func (miniProgram *MiniProgram) GetShortLink() *shortlink.ShortLink {
func (miniProgram *MiniProgram) GetSURLScheme() *urlscheme.URLScheme {
return urlscheme.NewURLScheme(miniProgram.ctx)
}
// GetOpenAPI openApi管理接口
func (miniProgram *MiniProgram) GetOpenAPI() *openapi.OpenAPI {
return openapi.NewOpenAPI(miniProgram.ctx)
}

View File

@@ -123,6 +123,7 @@ type MixMessage struct {
Title string `xml:"Title"`
Description string `xml:"Description"`
URL string `xml:"Url"`
BizMsgMenuID int64 `xml:"bizmsgmenuid"`
// 事件相关
Event EventType `xml:"Event" json:"Event"`

View File

@@ -4,6 +4,7 @@ import (
stdcontext "context"
"net/http"
"github.com/silenceper/wechat/v2/internal/openapi"
"github.com/silenceper/wechat/v2/officialaccount/draft"
"github.com/silenceper/wechat/v2/officialaccount/freepublish"
"github.com/silenceper/wechat/v2/officialaccount/ocr"
@@ -212,3 +213,8 @@ func (officialAccount *OfficialAccount) GetSubscribe() *message.Subscribe {
func (officialAccount *OfficialAccount) GetCustomerServiceManager() *customerservice.Manager {
return customerservice.NewCustomerServiceManager(officialAccount.ctx)
}
// GetOpenAPI openApi管理接口
func (officialAccount *OfficialAccount) GetOpenAPI() *openapi.OpenAPI {
return openapi.NewOpenAPI(officialAccount.ctx)
}

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

@@ -0,0 +1,230 @@
package addresslist
import (
"fmt"
"github.com/silenceper/wechat/v2/util"
)
const (
// getPermListURL 获取应用的可见范围
getPermListURL = "https://qyapi.weixin.qq.com/cgi-bin/linkedcorp/agent/get_perm_list?access_token=%s"
// getLinkedCorpUserURL 获取互联企业成员详细信息
getLinkedCorpUserURL = "https://qyapi.weixin.qq.com/cgi-bin/linkedcorp/user/get?access_token=%s"
// linkedCorpSimpleListURL 获取互联企业部门成员
linkedCorpSimpleListURL = "https://qyapi.weixin.qq.com/cgi-bin/linkedcorp/user/simplelist?access_token=%s"
// linkedCorpUserListURL 获取互联企业部门成员详情
linkedCorpUserListURL = "https://qyapi.weixin.qq.com/cgi-bin/linkedcorp/user/list?access_token=%s"
// linkedCorpDepartmentListURL 获取互联企业部门列表
linkedCorpDepartmentListURL = "https://qyapi.weixin.qq.com/cgi-bin/linkedcorp/department/list?access_token=%s"
)
// GetPermListResponse 获取应用的可见范围响应
type GetPermListResponse struct {
util.CommonError
UserIDs []string `json:"userids"`
DepartmentIDs []string `json:"department_ids"`
}
// GetPermList 获取应用的可见范围
// see https://developer.work.weixin.qq.com/document/path/93172
func (r *Client) GetPermList() (*GetPermListResponse, error) {
var (
accessToken string
err error
)
if accessToken, err = r.GetAccessToken(); err != nil {
return nil, err
}
var response []byte
if response, err = util.HTTPPost(fmt.Sprintf(getPermListURL, accessToken), ""); err != nil {
return nil, err
}
result := &GetPermListResponse{}
if err = util.DecodeWithError(response, result, "GetPermList"); err != nil {
return nil, err
}
return result, nil
}
// GetLinkedCorpUserRequest 获取互联企业成员详细信息请求
type GetLinkedCorpUserRequest struct {
UserID string `json:"userid"`
}
// GetLinkedCorpUserResponse 获取互联企业成员详细信息响应
type GetLinkedCorpUserResponse struct {
util.CommonError
UserInfo LinkedCorpUserInfo `json:"user_info"`
}
// LinkedCorpUserInfo 互联企业成员详细信息
type LinkedCorpUserInfo struct {
UserID string `json:"userid"`
Name string `json:"name"`
Department []string `json:"department"`
Mobile string `json:"mobile"`
Telephone string `json:"telephone"`
Email string `json:"email"`
Position string `json:"position"`
CorpID string `json:"corpid"`
Extattr Extattr `json:"extattr"`
}
// Extattr 互联企业成员详细信息扩展属性
type Extattr struct {
Attrs []ExtattrItem `json:"attrs"`
}
// ExtattrItem 互联企业成员详细信息扩展属性条目
type ExtattrItem struct {
Name string `json:"name"`
Value string `json:"value,omitempty"`
Type int `json:"type"`
Text ExtattrItemText `json:"text,omitempty"`
Web ExtattrItemWeb `json:"web,omitempty"`
}
// ExtattrItemText 互联企业成员详细信息自定义属性(文本)
type ExtattrItemText struct {
Value string `json:"value"`
}
// ExtattrItemWeb 互联企业成员详细信息自定义属性(网页)
type ExtattrItemWeb struct {
URL string `json:"url"`
Title string `json:"title"`
}
// GetLinkedCorpUser 获取互联企业成员详细信息
// see https://developer.work.weixin.qq.com/document/path/93171
func (r *Client) GetLinkedCorpUser(req *GetLinkedCorpUserRequest) (*GetLinkedCorpUserResponse, error) {
var (
accessToken string
err error
)
if accessToken, err = r.GetAccessToken(); err != nil {
return nil, err
}
var response []byte
if response, err = util.PostJSON(fmt.Sprintf(getLinkedCorpUserURL, accessToken), req); err != nil {
return nil, err
}
result := &GetLinkedCorpUserResponse{}
if err = util.DecodeWithError(response, result, "GetLinkedCorpUser"); err != nil {
return nil, err
}
return result, nil
}
// LinkedCorpSimpleListRequest 获取互联企业部门成员请求
type LinkedCorpSimpleListRequest struct {
DepartmentID string `json:"department_id"`
}
// LinkedCorpSimpleListResponse 获取互联企业部门成员响应
type LinkedCorpSimpleListResponse struct {
util.CommonError
Userlist []LinkedCorpUser `json:"userlist"`
}
// LinkedCorpUser 企业部门成员
type LinkedCorpUser struct {
UserID string `json:"userid"`
Name string `json:"name"`
Department []string `json:"department"`
CorpID string `json:"corpid"`
}
// LinkedCorpSimpleList 获取互联企业部门成员
// see https://developer.work.weixin.qq.com/document/path/93168
func (r *Client) LinkedCorpSimpleList(req *LinkedCorpSimpleListRequest) (*LinkedCorpSimpleListResponse, error) {
var (
accessToken string
err error
)
if accessToken, err = r.GetAccessToken(); err != nil {
return nil, err
}
var response []byte
if response, err = util.PostJSON(fmt.Sprintf(linkedCorpSimpleListURL, accessToken), req); err != nil {
return nil, err
}
result := &LinkedCorpSimpleListResponse{}
if err = util.DecodeWithError(response, result, "LinkedCorpSimpleList"); err != nil {
return nil, err
}
return result, nil
}
// LinkedCorpUserListRequest 获取互联企业部门成员详情请求
type LinkedCorpUserListRequest struct {
DepartmentID string `json:"department_id"`
}
// LinkedCorpUserListResponse 获取互联企业部门成员详情响应
type LinkedCorpUserListResponse struct {
util.CommonError
UserList []LinkedCorpUserInfo `json:"userlist"`
}
// LinkedCorpUserList 获取互联企业部门成员详情
// see https://developer.work.weixin.qq.com/document/path/93169
func (r *Client) LinkedCorpUserList(req *LinkedCorpUserListRequest) (*LinkedCorpUserListResponse, error) {
var (
accessToken string
err error
)
if accessToken, err = r.GetAccessToken(); err != nil {
return nil, err
}
var response []byte
if response, err = util.PostJSON(fmt.Sprintf(linkedCorpUserListURL, accessToken), req); err != nil {
return nil, err
}
result := &LinkedCorpUserListResponse{}
if err = util.DecodeWithError(response, result, "LinkedCorpUserList"); err != nil {
return nil, err
}
return result, nil
}
// LinkedCorpDepartmentListRequest 获取互联企业部门列表请求
type LinkedCorpDepartmentListRequest struct {
DepartmentID string `json:"department_id"`
}
// LinkedCorpDepartmentListResponse 获取互联企业部门列表响应
type LinkedCorpDepartmentListResponse struct {
util.CommonError
DepartmentList []LinkedCorpDepartment `json:"department_list"`
}
// LinkedCorpDepartment 互联企业部门
type LinkedCorpDepartment struct {
DepartmentID string `json:"department_id"`
DepartmentName string `json:"department_name"`
ParentID string `json:"parentid"`
Order int `json:"order"`
}
// LinkedCorpDepartmentList 获取互联企业部门列表
// see https://developer.work.weixin.qq.com/document/path/93170
func (r *Client) LinkedCorpDepartmentList(req *LinkedCorpDepartmentListRequest) (*LinkedCorpDepartmentListResponse, error) {
var (
accessToken string
err error
)
if accessToken, err = r.GetAccessToken(); err != nil {
return nil, err
}
var response []byte
if response, err = util.PostJSON(fmt.Sprintf(linkedCorpDepartmentListURL, accessToken), req); err != nil {
return nil, err
}
result := &LinkedCorpDepartmentListResponse{}
if err = util.DecodeWithError(response, result, "LinkedCorpDepartmentList"); err != nil {
return nil, err
}
return result, nil
}

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

17
work/invoice/client.go Normal file
View File

@@ -0,0 +1,17 @@
package invoice
import (
"github.com/silenceper/wechat/v2/work/context"
)
// Client 电子发票接口实例
type Client struct {
*context.Context
}
// NewClient 初始化实例
func NewClient(ctx *context.Context) *Client {
return &Client{
ctx,
}
}

191
work/invoice/invoice.go Normal file
View File

@@ -0,0 +1,191 @@
package invoice
import (
"fmt"
"github.com/silenceper/wechat/v2/util"
)
const (
// getInvoiceInfoURL 查询电子发票
getInvoiceInfoURL = "https://qyapi.weixin.qq.com/cgi-bin/card/invoice/reimburse/getinvoiceinfo?access_token=%s"
// updateInvoiceStatusURL 更新发票状态
updateInvoiceStatusURL = "https://qyapi.weixin.qq.com/cgi-bin/card/invoice/reimburse/updateinvoicestatus?access_token=%s"
// updateStatusBatchURL 批量更新发票状态
updateStatusBatchURL = "https://qyapi.weixin.qq.com/cgi-bin/card/invoice/reimburse/updatestatusbatch?access_token=%s"
// getInvoiceInfoBatchURL 批量查询电子发票
getInvoiceInfoBatchURL = "https://qyapi.weixin.qq.com/cgi-bin/card/invoice/reimburse/getinvoiceinfobatch?access_token=%s"
)
// GetInvoiceInfoRequest 查询电子发票请求
type GetInvoiceInfoRequest struct {
CardID string `json:"card_id"`
EncryptCode string `json:"encrypt_code"`
}
// GetInvoiceInfoResponse 查询电子发票响应
type GetInvoiceInfoResponse struct {
util.CommonError
CardID string `json:"card_id"`
BeginTime int64 `json:"begin_time"`
EndTime int64 `json:"end_time"`
OpenID string `json:"openid"`
Type string `json:"type"`
Payee string `json:"payee"`
Detail string `json:"detail"`
UserInfo UserInfo `json:"user_info"`
}
// UserInfo 发票的用户信息
type UserInfo struct {
Fee int64 `json:"fee"`
Title string `json:"title"`
BillingTime int64 `json:"billing_time"`
BillingNo string `json:"billing_no"`
BillingCode string `json:"billing_code"`
Info []Info `json:"info"`
FeeWithoutTax int64 `json:"fee_without_tax"`
Tax int64 `json:"tax"`
Detail string `json:"detail"`
PdfURL string `json:"pdf_url"`
TripPdfURL string `json:"trip_pdf_url"`
ReimburseStatus string `json:"reimburse_status"`
CheckCode string `json:"check_code"`
BuyerNumber string `json:"buyer_number"`
BuyerAddressAndPhone string `json:"buyer_address_and_phone"`
BuyerBankAccount string `json:"buyer_bank_account"`
SellerNumber string `json:"seller_number"`
SellerAddressAndPhone string `json:"seller_address_and_phone"`
SellerBankAccount string `json:"seller_bank_account"`
Remarks string `json:"remarks"`
Cashier string `json:"cashier"`
Maker string `json:"maker"`
}
// Info 商品信息结构
type Info struct {
Name string `json:"name"`
Num int64 `json:"num"`
Unit string `json:"unit"`
Fee int64 `json:"fee"`
Price int64 `json:"price"`
}
// GetInvoiceInfo 查询电子发票
// see https://developer.work.weixin.qq.com/document/path/90284
func (r *Client) GetInvoiceInfo(req *GetInvoiceInfoRequest) (*GetInvoiceInfoResponse, error) {
var (
accessToken string
err error
)
if accessToken, err = r.GetAccessToken(); err != nil {
return nil, err
}
var response []byte
if response, err = util.PostJSON(fmt.Sprintf(getInvoiceInfoURL, accessToken), req); err != nil {
return nil, err
}
result := &GetInvoiceInfoResponse{}
if err = util.DecodeWithError(response, result, "GetInvoiceInfo"); err != nil {
return nil, err
}
return result, nil
}
// UpdateInvoiceStatusRequest 更新发票状态请求
type UpdateInvoiceStatusRequest struct {
CardID string `json:"card_id"`
EncryptCode string `json:"encrypt_code"`
ReimburseStatus string `json:"reimburse_status"`
}
// UpdateInvoiceStatus 更新发票状态
// see https://developer.work.weixin.qq.com/document/path/90285
func (r *Client) UpdateInvoiceStatus(req *UpdateInvoiceStatusRequest) error {
var (
accessToken string
err error
)
if accessToken, err = r.GetAccessToken(); err != nil {
return err
}
var response []byte
if response, err = util.PostJSON(fmt.Sprintf(updateInvoiceStatusURL, accessToken), req); err != nil {
return err
}
return util.DecodeWithCommonError(response, "UpdateInvoiceStatus")
}
// UpdateStatusBatchRequest 批量更新发票状态
type UpdateStatusBatchRequest struct {
OpenID string `json:"openid"`
ReimburseStatus string `json:"reimburse_status"`
InvoiceList []Invoice `json:"invoice_list"`
}
// Invoice 发票卡券
type Invoice struct {
CardID string `json:"card_id"`
EncryptCode string `json:"encrypt_code"`
}
// UpdateStatusBatch 批量更新发票状态
// see https://developer.work.weixin.qq.com/document/path/90286
func (r *Client) UpdateStatusBatch(req *UpdateStatusBatchRequest) error {
var (
accessToken string
err error
)
if accessToken, err = r.GetAccessToken(); err != nil {
return err
}
var response []byte
if response, err = util.PostJSON(fmt.Sprintf(updateStatusBatchURL, accessToken), req); err != nil {
return err
}
return util.DecodeWithCommonError(response, "UpdateStatusBatch")
}
// GetInvoiceInfoBatchRequest 批量查询电子发票请求
type GetInvoiceInfoBatchRequest struct {
ItemList []Invoice `json:"item_list"`
}
// GetInvoiceInfoBatchResponse 批量查询电子发票响应
type GetInvoiceInfoBatchResponse struct {
util.CommonError
ItemList []Item `json:"item_list"`
}
// Item 电子发票的结构化信息
type Item struct {
CardID string `json:"card_id"`
BeginTime int64 `json:"begin_time"`
EndTime int64 `json:"end_time"`
OpenID string `json:"openid"`
Type string `json:"type"`
Payee string `json:"payee"`
Detail string `json:"detail"`
UserInfo UserInfo `json:"user_info"`
}
// GetInvoiceInfoBatch 批量查询电子发票
// see https://developer.work.weixin.qq.com/document/path/90287
func (r *Client) GetInvoiceInfoBatch(req *GetInvoiceInfoBatchRequest) (*GetInvoiceInfoBatchResponse, error) {
var (
accessToken string
err error
)
if accessToken, err = r.GetAccessToken(); err != nil {
return nil, err
}
var response []byte
if response, err = util.PostJSON(fmt.Sprintf(getInvoiceInfoBatchURL, accessToken), req); err != nil {
return nil, err
}
result := &GetInvoiceInfoBatchResponse{}
if err = util.DecodeWithError(response, result, "GetInvoiceInfoBatch"); err != nil {
return nil, err
}
return result, nil
}

View File

@@ -7,6 +7,7 @@ import (
"github.com/silenceper/wechat/v2/work/config"
"github.com/silenceper/wechat/v2/work/context"
"github.com/silenceper/wechat/v2/work/externalcontact"
"github.com/silenceper/wechat/v2/work/invoice"
"github.com/silenceper/wechat/v2/work/kf"
"github.com/silenceper/wechat/v2/work/material"
"github.com/silenceper/wechat/v2/work/message"
@@ -79,3 +80,8 @@ func (wk *Work) GetMessage() *message.Client {
func (wk *Work) GetAppChat() *appchat.Client {
return appchat.NewClient(wk.ctx)
}
// GetInvoice get invoice
func (wk *Work) GetInvoice() *invoice.Client {
return invoice.NewClient(wk.ctx)
}