mirror of
https://github.com/silenceper/wechat.git
synced 2026-02-04 21:02:25 +08:00
Compare commits
36 Commits
v2.1.5-rc.
...
v2.1.6-rc.
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
45ad2ab8ca | ||
|
|
5d0e32e2ea | ||
|
|
822cfaa6c8 | ||
|
|
acaab64fe7 | ||
|
|
b12825f83b | ||
|
|
86cbd8c0b2 | ||
|
|
46c3722308 | ||
|
|
7d11af713b | ||
|
|
fafb1784da | ||
|
|
31f8e24994 | ||
|
|
ae1b056515 | ||
|
|
8bae546b77 | ||
|
|
9df943df69 | ||
|
|
0a37184c2f | ||
|
|
b4f243ab13 | ||
|
|
d92cd35533 | ||
|
|
58621cd79d | ||
|
|
8821a3856d | ||
|
|
4094adc5b4 | ||
|
|
cb0928a03c | ||
|
|
d6371c7289 | ||
|
|
07b7dc40fc | ||
|
|
01784c2a4a | ||
|
|
8bc1474777 | ||
|
|
ca0b74e082 | ||
|
|
fbda048f62 | ||
|
|
494082ff4f | ||
|
|
9c0189340b | ||
|
|
d39615f2fa | ||
|
|
429219b53f | ||
|
|
6e80b71cb2 | ||
|
|
ab354c4d03 | ||
|
|
d1d034eb95 | ||
|
|
1a9dbc493b | ||
|
|
04559ed4bb | ||
|
|
abd583df01 |
24
.github/workflows/go.yml
vendored
24
.github/workflows/go.yml
vendored
@@ -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 ./...
|
||||
8
.github/workflows/release.yml
vendored
8
.github/workflows/release.yml
vendored
@@ -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
|
||||
|
||||
46
cache/cache.go
vendored
46
cache/cache.go
vendored
@@ -1,6 +1,9 @@
|
||||
package cache
|
||||
|
||||
import "time"
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Cache interface
|
||||
type Cache interface {
|
||||
@@ -9,3 +12,44 @@ type Cache interface {
|
||||
IsExist(key string) bool
|
||||
Delete(key string) error
|
||||
}
|
||||
|
||||
// ContextCache interface
|
||||
type ContextCache interface {
|
||||
Cache
|
||||
GetContext(ctx context.Context, key string) interface{}
|
||||
SetContext(ctx context.Context, key string, val interface{}, timeout time.Duration) error
|
||||
IsExistContext(ctx context.Context, key string) bool
|
||||
DeleteContext(ctx context.Context, key string) error
|
||||
}
|
||||
|
||||
// GetContext get value from cache
|
||||
func GetContext(ctx context.Context, cache Cache, key string) interface{} {
|
||||
if cache, ok := cache.(ContextCache); ok {
|
||||
return cache.GetContext(ctx, key)
|
||||
}
|
||||
return cache.Get(key)
|
||||
}
|
||||
|
||||
// SetContext set value to cache
|
||||
func SetContext(ctx context.Context, cache Cache, key string, val interface{}, timeout time.Duration) error {
|
||||
if cache, ok := cache.(ContextCache); ok {
|
||||
return cache.SetContext(ctx, key, val, timeout)
|
||||
}
|
||||
return cache.Set(key, val, timeout)
|
||||
}
|
||||
|
||||
// IsExistContext check value exists in cache.
|
||||
func IsExistContext(ctx context.Context, cache Cache, key string) bool {
|
||||
if cache, ok := cache.(ContextCache); ok {
|
||||
return cache.IsExistContext(ctx, key)
|
||||
}
|
||||
return cache.IsExist(key)
|
||||
}
|
||||
|
||||
// DeleteContext delete value in cache.
|
||||
func DeleteContext(ctx context.Context, cache Cache, key string) error {
|
||||
if cache, ok := cache.(ContextCache); ok {
|
||||
return cache.DeleteContext(ctx, key)
|
||||
}
|
||||
return cache.Delete(key)
|
||||
}
|
||||
|
||||
28
cache/redis.go
vendored
28
cache/redis.go
vendored
@@ -47,7 +47,12 @@ func (r *Redis) SetRedisCtx(ctx context.Context) {
|
||||
|
||||
// Get 获取一个值
|
||||
func (r *Redis) Get(key string) interface{} {
|
||||
result, err := r.conn.Do(r.ctx, "GET", key).Result()
|
||||
return r.GetContext(r.ctx, key)
|
||||
}
|
||||
|
||||
// GetContext 获取一个值
|
||||
func (r *Redis) GetContext(ctx context.Context, key string) interface{} {
|
||||
result, err := r.conn.Do(ctx, "GET", key).Result()
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
@@ -56,17 +61,32 @@ func (r *Redis) Get(key string) interface{} {
|
||||
|
||||
// Set 设置一个值
|
||||
func (r *Redis) Set(key string, val interface{}, timeout time.Duration) error {
|
||||
return r.conn.SetEX(r.ctx, key, val, timeout).Err()
|
||||
return r.SetContext(r.ctx, key, val, timeout)
|
||||
}
|
||||
|
||||
// SetContext 设置一个值
|
||||
func (r *Redis) SetContext(ctx context.Context, key string, val interface{}, timeout time.Duration) error {
|
||||
return r.conn.SetEX(ctx, key, val, timeout).Err()
|
||||
}
|
||||
|
||||
// IsExist 判断key是否存在
|
||||
func (r *Redis) IsExist(key string) bool {
|
||||
result, _ := r.conn.Exists(r.ctx, key).Result()
|
||||
return r.IsExistContext(r.ctx, key)
|
||||
}
|
||||
|
||||
// IsExistContext 判断key是否存在
|
||||
func (r *Redis) IsExistContext(ctx context.Context, key string) bool {
|
||||
result, _ := r.conn.Exists(ctx, key).Result()
|
||||
|
||||
return result > 0
|
||||
}
|
||||
|
||||
// Delete 删除
|
||||
func (r *Redis) Delete(key string) error {
|
||||
return r.conn.Del(r.ctx, key).Err()
|
||||
return r.DeleteContext(r.ctx, key)
|
||||
}
|
||||
|
||||
// DeleteContext 删除
|
||||
func (r *Redis) DeleteContext(ctx context.Context, key string) error {
|
||||
return r.conn.Del(ctx, key).Err()
|
||||
}
|
||||
|
||||
10
cache/redis_test.go
vendored
10
cache/redis_test.go
vendored
@@ -4,17 +4,23 @@ import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/alicebob/miniredis/v2"
|
||||
)
|
||||
|
||||
func TestRedis(t *testing.T) {
|
||||
server, err := miniredis.Run()
|
||||
if err != nil {
|
||||
t.Error("miniredis.Run Error", err)
|
||||
}
|
||||
t.Cleanup(server.Close)
|
||||
var (
|
||||
timeoutDuration = time.Second
|
||||
ctx = context.Background()
|
||||
opts = &RedisOpts{
|
||||
Host: "127.0.0.1:6379",
|
||||
Host: server.Addr(),
|
||||
}
|
||||
redis = NewRedis(ctx, opts)
|
||||
err error
|
||||
val = "silenceper"
|
||||
key = "username"
|
||||
)
|
||||
|
||||
@@ -12,9 +12,11 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
// AccessTokenURL 获取access_token的接口
|
||||
// accessTokenURL 获取access_token的接口
|
||||
accessTokenURL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s"
|
||||
// AccessTokenURL 企业微信获取access_token的接口
|
||||
// stableAccessTokenURL 获取稳定版access_token的接口
|
||||
stableAccessTokenURL = "https://api.weixin.qq.com/cgi-bin/stable_token"
|
||||
// workAccessTokenURL 企业微信获取access_token的接口
|
||||
workAccessTokenURL = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=%s&corpsecret=%s"
|
||||
// CacheKeyOfficialAccountPrefix 微信公众号cache key前缀
|
||||
CacheKeyOfficialAccountPrefix = "gowechat_officialaccount_"
|
||||
@@ -79,17 +81,87 @@ func (ak *DefaultAccessToken) GetAccessTokenContext(ctx context.Context) (access
|
||||
|
||||
// cache失效,从微信服务器获取
|
||||
var resAccessToken ResAccessToken
|
||||
resAccessToken, err = GetTokenFromServerContext(ctx, fmt.Sprintf(accessTokenURL, ak.appID, ak.appSecret))
|
||||
if resAccessToken, err = GetTokenFromServerContext(ctx, fmt.Sprintf(accessTokenURL, ak.appID, ak.appSecret)); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if err = ak.cache.Set(accessTokenCacheKey, resAccessToken.AccessToken, time.Duration(resAccessToken.ExpiresIn-1500)*time.Second); err != nil {
|
||||
return
|
||||
}
|
||||
accessToken = resAccessToken.AccessToken
|
||||
return
|
||||
}
|
||||
|
||||
// StableAccessToken 获取稳定版接口调用凭据(与getAccessToken获取的调用凭证完全隔离,互不影响)
|
||||
// 不强制更新access_token,可用于不同环境不同服务而不需要分布式锁以及公用缓存,避免access_token争抢
|
||||
// https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/mp-access-token/getStableAccessToken.html
|
||||
type StableAccessToken struct {
|
||||
appID string
|
||||
appSecret string
|
||||
cacheKeyPrefix string
|
||||
cache cache.Cache
|
||||
}
|
||||
|
||||
// NewStableAccessToken new StableAccessToken
|
||||
func NewStableAccessToken(appID, appSecret, cacheKeyPrefix string, cache cache.Cache) AccessTokenContextHandle {
|
||||
if cache == nil {
|
||||
panic("cache is need")
|
||||
}
|
||||
return &StableAccessToken{
|
||||
appID: appID,
|
||||
appSecret: appSecret,
|
||||
cache: cache,
|
||||
cacheKeyPrefix: cacheKeyPrefix,
|
||||
}
|
||||
}
|
||||
|
||||
// GetAccessToken 获取access_token,先从cache中获取,没有则从服务端获取
|
||||
func (ak *StableAccessToken) GetAccessToken() (accessToken string, err error) {
|
||||
return ak.GetAccessTokenContext(context.Background())
|
||||
}
|
||||
|
||||
// GetAccessTokenContext 获取access_token,先从cache中获取,没有则从服务端获取
|
||||
func (ak *StableAccessToken) GetAccessTokenContext(ctx context.Context) (accessToken string, err error) {
|
||||
// 先从cache中取
|
||||
accessTokenCacheKey := fmt.Sprintf("%s_stable_access_token_%s", ak.cacheKeyPrefix, ak.appID)
|
||||
if val := ak.cache.Get(accessTokenCacheKey); val != nil {
|
||||
return val.(string), nil
|
||||
}
|
||||
|
||||
// cache失效,从微信服务器获取
|
||||
var resAccessToken ResAccessToken
|
||||
resAccessToken, err = ak.GetAccessTokenDirectly(ctx, false)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
expires := resAccessToken.ExpiresIn - 1500
|
||||
err = ak.cache.Set(accessTokenCacheKey, resAccessToken.AccessToken, time.Duration(expires)*time.Second)
|
||||
expires := resAccessToken.ExpiresIn - 300
|
||||
_ = ak.cache.Set(accessTokenCacheKey, resAccessToken.AccessToken, time.Duration(expires)*time.Second)
|
||||
|
||||
accessToken = resAccessToken.AccessToken
|
||||
return
|
||||
}
|
||||
|
||||
// GetAccessTokenDirectly 从微信获取access_token
|
||||
func (ak *StableAccessToken) GetAccessTokenDirectly(ctx context.Context, forceRefresh bool) (resAccessToken ResAccessToken, err error) {
|
||||
b, err := util.PostJSONContext(ctx, stableAccessTokenURL, map[string]interface{}{
|
||||
"grant_type": "client_credential",
|
||||
"appid": ak.appID,
|
||||
"secret": ak.appSecret,
|
||||
"force_refresh": forceRefresh,
|
||||
})
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
accessToken = resAccessToken.AccessToken
|
||||
|
||||
if err = json.Unmarshal(b, &resAccessToken); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if resAccessToken.ErrCode != 0 {
|
||||
err = fmt.Errorf("get stable access_token error : errcode=%v , errormsg=%v", resAccessToken.ErrCode, resAccessToken.ErrMsg)
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
36
domain/openapi/mgnt.go
Normal file
36
domain/openapi/mgnt.go
Normal 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对应的请求详情
|
||||
}
|
||||
1
go.mod
1
go.mod
@@ -3,6 +3,7 @@ module github.com/silenceper/wechat/v2
|
||||
go 1.16
|
||||
|
||||
require (
|
||||
github.com/alicebob/miniredis/v2 v2.30.0
|
||||
github.com/bradfitz/gomemcache v0.0.0-20220106215444-fb4bf637b56d
|
||||
github.com/fatih/structs v1.1.0
|
||||
github.com/go-redis/redis/v8 v8.11.5
|
||||
|
||||
7
go.sum
7
go.sum
@@ -1,3 +1,7 @@
|
||||
github.com/alicebob/gopher-json v0.0.0-20200520072559-a9ecdc9d1d3a h1:HbKu58rmZpUGpz5+4FfNmIU+FmZg2P3Xaj2v2bfNWmk=
|
||||
github.com/alicebob/gopher-json v0.0.0-20200520072559-a9ecdc9d1d3a/go.mod h1:SGnFV6hVsYE877CKEZ6tDNTjaSXYUk6QqoIK6PrAtcc=
|
||||
github.com/alicebob/miniredis/v2 v2.30.0 h1:uA3uhDbCxfO9+DI/DuGeAMr9qI+noVWwGPNTFuKID5M=
|
||||
github.com/alicebob/miniredis/v2 v2.30.0/go.mod h1:84TWKZlxYkfgMucPBf5SOQBYJceZeQRFIaQgNMiCX6Q=
|
||||
github.com/bradfitz/gomemcache v0.0.0-20220106215444-fb4bf637b56d h1:pVrfxiGfwelyab6n21ZBkbkmbevaf+WvMIiR7sr97hw=
|
||||
github.com/bradfitz/gomemcache v0.0.0-20220106215444-fb4bf637b56d/go.mod h1:H0wQNHz2YrLsuXOZozoeDmnHXkNCRmMW0gwFWDfEZDA=
|
||||
github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE=
|
||||
@@ -71,6 +75,8 @@ github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JT
|
||||
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
|
||||
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
|
||||
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
|
||||
github.com/yuin/gopher-lua v0.0.0-20220504180219-658193537a64 h1:5mLPGnFdSsevFRFc9q3yYbBkB6tsm4aCwwQV/j1JQAQ=
|
||||
github.com/yuin/gopher-lua v0.0.0-20220504180219-658193537a64/go.mod h1:GBR0iDaNXjAgGg9zfCvksxSRnQx76gclCIb7kdAd1Pw=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
||||
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
||||
@@ -89,6 +95,7 @@ golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJ
|
||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190204203706-41f3e6584952/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20190904154756-749cb33beabd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
|
||||
127
internal/openapi/mgnt.go
Normal file
127
internal/openapi/mgnt.go
Normal 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, "a, "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)
|
||||
}
|
||||
@@ -40,8 +40,8 @@ type ResCode2Session struct {
|
||||
type RspCheckEncryptedData struct {
|
||||
util.CommonError
|
||||
|
||||
Vaild bool `json:"vaild"` // 是否是合法的数据
|
||||
CreateTime uint `json:"create_time"` // 加密数据生成的时间戳
|
||||
Vaild bool `json:"vaild"` // 是否是合法的数据
|
||||
CreateTime uint64 `json:"create_time"` // 加密数据生成的时间戳
|
||||
}
|
||||
|
||||
// Code2Session 登录凭证校验。
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -37,7 +37,7 @@ type ShortLinker struct {
|
||||
// resShortLinker 返回结构体
|
||||
type resShortLinker struct {
|
||||
// 通用错误
|
||||
*util.CommonError
|
||||
util.CommonError
|
||||
|
||||
// 返回的 shortLink
|
||||
Link string `json:"link"`
|
||||
|
||||
@@ -38,6 +38,7 @@ type CustomerMessage struct {
|
||||
Wxcard *MediaWxcard `json:"wxcard,omitempty"` // 可选
|
||||
Msgmenu *MediaMsgmenu `json:"msgmenu,omitempty"` // 可选
|
||||
Miniprogrampage *MediaMiniprogrampage `json:"miniprogrampage,omitempty"` // 可选
|
||||
Mpnewsarticle *MediaArticle `json:"mpnewsarticle,omitempty"` // 可选
|
||||
}
|
||||
|
||||
// NewCustomerTextMessage 文本消息结构体构造方法
|
||||
@@ -97,6 +98,11 @@ type MediaResource struct {
|
||||
MediaID string `json:"media_id"`
|
||||
}
|
||||
|
||||
// MediaArticle 消息使用的已发布文章id
|
||||
type MediaArticle struct {
|
||||
ArticleID string `json:"article_id"`
|
||||
}
|
||||
|
||||
// MediaVideo 视频消息包含的内容
|
||||
type MediaVideo struct {
|
||||
MediaID string `json:"media_id"`
|
||||
|
||||
@@ -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"`
|
||||
@@ -195,6 +196,14 @@ type MixMessage struct {
|
||||
LegalPersonaName string `xml:"legal_persona_name"`
|
||||
ComponentPhone string `xml:"component_phone"`
|
||||
} `xml:"info"`
|
||||
ResultInfo struct {
|
||||
APIName string `xml:"api_name"`
|
||||
ApplyTime string `xml:"apply_time"`
|
||||
AuditID string `xml:"audit_id"`
|
||||
AuditTime string `xml:"audit_time"`
|
||||
Reason string `xml:"reason"`
|
||||
Status string `xml:"status"`
|
||||
} `xml:"result_info"`
|
||||
|
||||
// 卡券相关
|
||||
CardID string `xml:"CardId"`
|
||||
@@ -213,6 +222,10 @@ type MixMessage struct {
|
||||
TraceID string `xml:"trace_id"`
|
||||
StatusCode int `xml:"status_code"`
|
||||
|
||||
//小程序名称审核结果事件推送
|
||||
Ret int32 `xml:"ret"` //审核结果 2:失败,3:成功
|
||||
NickName string `xml:"nickname"` //小程序昵称
|
||||
|
||||
// 设备相关
|
||||
device.MsgDevice
|
||||
|
||||
|
||||
@@ -29,11 +29,12 @@ func NewTemplate(context *context.Context) *Template {
|
||||
|
||||
// TemplateMessage 发送的模板消息内容
|
||||
type TemplateMessage struct {
|
||||
ToUser string `json:"touser"` // 必须, 接受者OpenID
|
||||
TemplateID string `json:"template_id"` // 必须, 模版ID
|
||||
URL string `json:"url,omitempty"` // 可选, 用户点击后跳转的URL, 该URL必须处于开发者在公众平台网站中设置的域中
|
||||
Color string `json:"color,omitempty"` // 可选, 整个消息的颜色, 可以不设置
|
||||
Data map[string]*TemplateDataItem `json:"data"` // 必须, 模板数据
|
||||
ToUser string `json:"touser"` // 必须, 接受者OpenID
|
||||
TemplateID string `json:"template_id"` // 必须, 模版ID
|
||||
URL string `json:"url,omitempty"` // 可选, 用户点击后跳转的URL, 该URL必须处于开发者在公众平台网站中设置的域中
|
||||
Color string `json:"color,omitempty"` // 可选, 整个消息的颜色, 可以不设置
|
||||
Data map[string]*TemplateDataItem `json:"data"` // 必须, 模板数据
|
||||
ClientMsgID string `json:"client_msg_id,omitempty"` // 可选, 防重入ID
|
||||
|
||||
MiniProgram struct {
|
||||
AppID string `json:"appid"` // 所需跳转到的小程序appid(该小程序appid必须与发模板消息的公众号是绑定关联关系)
|
||||
|
||||
@@ -64,6 +64,10 @@ type ResAccessToken struct {
|
||||
OpenID string `json:"openid"`
|
||||
Scope string `json:"scope"`
|
||||
|
||||
// IsSnapShotUser 是否为快照页模式虚拟账号,只有当用户是快照页模式虚拟账号时返回,值为1
|
||||
// 公众号文档 https://developers.weixin.qq.com/community/minihome/doc/000c2c34068880629ced91a2f56001
|
||||
IsSnapShotUser int `json:"is_snapshotuser"`
|
||||
|
||||
// UnionID 只有在用户将公众号绑定到微信开放平台帐号后,才会出现该字段。
|
||||
// 公众号文档 https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140842
|
||||
UnionID string `json:"unionid"`
|
||||
|
||||
@@ -154,134 +154,120 @@ func NewOCR(c *context.Context) *OCR {
|
||||
}
|
||||
|
||||
// IDCard 身份证OCR识别接口
|
||||
func (ocr *OCR) IDCard(path string) (ResIDCard ResIDCard, err error) {
|
||||
func (ocr *OCR) IDCard(path string) (resIDCard ResIDCard, err error) {
|
||||
accessToken, err := ocr.GetAccessToken()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
uri := fmt.Sprintf("%s?img_url=%s&access_token=%s", ocrIDCardURL, url.QueryEscape(path), accessToken)
|
||||
|
||||
response, err := util.HTTPPost(uri, "")
|
||||
response, err := util.HTTPPost(fmt.Sprintf("%s?img_url=%s&access_token=%s", ocrIDCardURL, url.QueryEscape(path), accessToken), "")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
err = util.DecodeWithError(response, &ResIDCard, "OCRIDCard")
|
||||
err = util.DecodeWithError(response, &resIDCard, "OCRIDCard")
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// BankCard 银行卡OCR识别接口
|
||||
func (ocr *OCR) BankCard(path string) (ResBankCard ResBankCard, err error) {
|
||||
func (ocr *OCR) BankCard(path string) (resBankCard ResBankCard, err error) {
|
||||
accessToken, err := ocr.GetAccessToken()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
uri := fmt.Sprintf("%s?img_url=%s&access_token=%s", ocrBankCardURL, url.QueryEscape(path), accessToken)
|
||||
|
||||
response, err := util.HTTPPost(uri, "")
|
||||
response, err := util.HTTPPost(fmt.Sprintf("%s?img_url=%s&access_token=%s", ocrBankCardURL, url.QueryEscape(path), accessToken), "")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
err = util.DecodeWithError(response, &ResBankCard, "OCRBankCard")
|
||||
err = util.DecodeWithError(response, &resBankCard, "OCRBankCard")
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// Driving 行驶证OCR识别接口
|
||||
func (ocr *OCR) Driving(path string) (ResDriving ResDriving, err error) {
|
||||
func (ocr *OCR) Driving(path string) (resDriving ResDriving, err error) {
|
||||
accessToken, err := ocr.GetAccessToken()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
uri := fmt.Sprintf("%s?img_url=%s&access_token=%s", ocrDrivingURL, url.QueryEscape(path), accessToken)
|
||||
|
||||
response, err := util.HTTPPost(uri, "")
|
||||
response, err := util.HTTPPost(fmt.Sprintf("%s?img_url=%s&access_token=%s", ocrDrivingURL, url.QueryEscape(path), accessToken), "")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
err = util.DecodeWithError(response, &ResDriving, "OCRDriving")
|
||||
err = util.DecodeWithError(response, &resDriving, "OCRDriving")
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// DrivingLicense 驾驶证OCR识别接口
|
||||
func (ocr *OCR) DrivingLicense(path string) (ResDrivingLicense ResDrivingLicense, err error) {
|
||||
func (ocr *OCR) DrivingLicense(path string) (resDrivingLicense ResDrivingLicense, err error) {
|
||||
accessToken, err := ocr.GetAccessToken()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
uri := fmt.Sprintf("%s?img_url=%s&access_token=%s", ocrDrivingLicenseURL, url.QueryEscape(path), accessToken)
|
||||
|
||||
response, err := util.HTTPPost(uri, "")
|
||||
response, err := util.HTTPPost(fmt.Sprintf("%s?img_url=%s&access_token=%s", ocrDrivingLicenseURL, url.QueryEscape(path), accessToken), "")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
err = util.DecodeWithError(response, &ResDrivingLicense, "OCRDrivingLicense")
|
||||
err = util.DecodeWithError(response, &resDrivingLicense, "OCRDrivingLicense")
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// BizLicense 营业执照OCR识别接口
|
||||
func (ocr *OCR) BizLicense(path string) (ResBizLicense ResBizLicense, err error) {
|
||||
func (ocr *OCR) BizLicense(path string) (resBizLicense ResBizLicense, err error) {
|
||||
accessToken, err := ocr.GetAccessToken()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
uri := fmt.Sprintf("%s?img_url=%s&access_token=%s", ocrBizLicenseURL, url.QueryEscape(path), accessToken)
|
||||
|
||||
response, err := util.HTTPPost(uri, "")
|
||||
response, err := util.HTTPPost(fmt.Sprintf("%s?img_url=%s&access_token=%s", ocrBizLicenseURL, url.QueryEscape(path), accessToken), "")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
err = util.DecodeWithError(response, &ResBizLicense, "OCRBizLicense")
|
||||
err = util.DecodeWithError(response, &resBizLicense, "OCRBizLicense")
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// Common 通用印刷体OCR识别接口
|
||||
func (ocr *OCR) Common(path string) (ResCommon ResCommon, err error) {
|
||||
func (ocr *OCR) Common(path string) (resCommon ResCommon, err error) {
|
||||
accessToken, err := ocr.GetAccessToken()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
uri := fmt.Sprintf("%s?img_url=%s&access_token=%s", ocrCommonURL, url.QueryEscape(path), accessToken)
|
||||
|
||||
response, err := util.HTTPPost(uri, "")
|
||||
response, err := util.HTTPPost(fmt.Sprintf("%s?img_url=%s&access_token=%s", ocrCommonURL, url.QueryEscape(path), accessToken), "")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
err = util.DecodeWithError(response, &ResCommon, "OCRCommon")
|
||||
err = util.DecodeWithError(response, &resCommon, "OCRCommon")
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// PlateNumber 车牌OCR识别接口
|
||||
func (ocr *OCR) PlateNumber(path string) (ResPlateNumber ResPlateNumber, err error) {
|
||||
func (ocr *OCR) PlateNumber(path string) (resPlateNumber ResPlateNumber, err error) {
|
||||
accessToken, err := ocr.GetAccessToken()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
uri := fmt.Sprintf("%s?img_url=%s&access_token=%s", ocrPlateNumberURL, url.QueryEscape(path), accessToken)
|
||||
|
||||
response, err := util.HTTPPost(uri, "")
|
||||
response, err := util.HTTPPost(fmt.Sprintf("%s?img_url=%s&access_token=%s", ocrPlateNumberURL, url.QueryEscape(path), accessToken), "")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
err = util.DecodeWithError(response, &ResPlateNumber, "OCRPlateNumber")
|
||||
err = util.DecodeWithError(response, &resPlateNumber, "OCRPlateNumber")
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -2,11 +2,13 @@
|
||||
package context
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"time"
|
||||
|
||||
"github.com/silenceper/wechat/v2/cache"
|
||||
"github.com/silenceper/wechat/v2/util"
|
||||
)
|
||||
|
||||
@@ -31,24 +33,29 @@ type ComponentAccessToken struct {
|
||||
ExpiresIn int64 `json:"expires_in"`
|
||||
}
|
||||
|
||||
// GetComponentAccessToken 获取 ComponentAccessToken
|
||||
func (ctx *Context) GetComponentAccessToken() (string, error) {
|
||||
// GetComponentAccessTokenContext 获取 ComponentAccessToken
|
||||
func (ctx *Context) GetComponentAccessTokenContext(stdCtx context.Context) (string, error) {
|
||||
accessTokenCacheKey := fmt.Sprintf("component_access_token_%s", ctx.AppID)
|
||||
val := ctx.Cache.Get(accessTokenCacheKey)
|
||||
val := cache.GetContext(stdCtx, ctx.Cache, accessTokenCacheKey)
|
||||
if val == nil {
|
||||
return "", fmt.Errorf("cann't get component access token")
|
||||
}
|
||||
return val.(string), nil
|
||||
}
|
||||
|
||||
// SetComponentAccessToken 通过component_verify_ticket 获取 ComponentAccessToken
|
||||
func (ctx *Context) SetComponentAccessToken(verifyTicket string) (*ComponentAccessToken, error) {
|
||||
// GetComponentAccessToken 获取 ComponentAccessToken
|
||||
func (ctx *Context) GetComponentAccessToken() (string, error) {
|
||||
return ctx.GetComponentAccessTokenContext(context.Background())
|
||||
}
|
||||
|
||||
// SetComponentAccessTokenContext 通过component_verify_ticket 获取 ComponentAccessToken
|
||||
func (ctx *Context) SetComponentAccessTokenContext(stdCtx context.Context, verifyTicket string) (*ComponentAccessToken, error) {
|
||||
body := map[string]string{
|
||||
"component_appid": ctx.AppID,
|
||||
"component_appsecret": ctx.AppSecret,
|
||||
"component_verify_ticket": verifyTicket,
|
||||
}
|
||||
respBody, err := util.PostJSON(componentAccessTokenURL, body)
|
||||
respBody, err := util.PostJSONContext(stdCtx, componentAccessTokenURL, body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -64,15 +71,20 @@ func (ctx *Context) SetComponentAccessToken(verifyTicket string) (*ComponentAcce
|
||||
|
||||
accessTokenCacheKey := fmt.Sprintf("component_access_token_%s", ctx.AppID)
|
||||
expires := at.ExpiresIn - 1500
|
||||
if err := ctx.Cache.Set(accessTokenCacheKey, at.AccessToken, time.Duration(expires)*time.Second); err != nil {
|
||||
if err := cache.SetContext(stdCtx, ctx.Cache, accessTokenCacheKey, at.AccessToken, time.Duration(expires)*time.Second); err != nil {
|
||||
return nil, nil
|
||||
}
|
||||
return at, nil
|
||||
}
|
||||
|
||||
// GetPreCode 获取预授权码
|
||||
func (ctx *Context) GetPreCode() (string, error) {
|
||||
cat, err := ctx.GetComponentAccessToken()
|
||||
// SetComponentAccessToken 通过component_verify_ticket 获取 ComponentAccessToken
|
||||
func (ctx *Context) SetComponentAccessToken(stdCtx context.Context, verifyTicket string) (*ComponentAccessToken, error) {
|
||||
return ctx.SetComponentAccessTokenContext(stdCtx, verifyTicket)
|
||||
}
|
||||
|
||||
// GetPreCodeContext 获取预授权码
|
||||
func (ctx *Context) GetPreCodeContext(stdCtx context.Context) (string, error) {
|
||||
cat, err := ctx.GetComponentAccessTokenContext(stdCtx)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
@@ -80,7 +92,7 @@ func (ctx *Context) GetPreCode() (string, error) {
|
||||
"component_appid": ctx.AppID,
|
||||
}
|
||||
uri := fmt.Sprintf(getPreCodeURL, cat)
|
||||
body, err := util.PostJSON(uri, req)
|
||||
body, err := util.PostJSONContext(stdCtx, uri, req)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
@@ -95,24 +107,39 @@ func (ctx *Context) GetPreCode() (string, error) {
|
||||
return ret.PreCode, nil
|
||||
}
|
||||
|
||||
// GetComponentLoginPage 获取第三方公众号授权链接(扫码授权)
|
||||
func (ctx *Context) GetComponentLoginPage(redirectURI string, authType int, bizAppID string) (string, error) {
|
||||
code, err := ctx.GetPreCode()
|
||||
// GetPreCode 获取预授权码
|
||||
func (ctx *Context) GetPreCode() (string, error) {
|
||||
return ctx.GetPreCodeContext(context.Background())
|
||||
}
|
||||
|
||||
// GetComponentLoginPageContext 获取第三方公众号授权链接(扫码授权)
|
||||
func (ctx *Context) GetComponentLoginPageContext(stdCtx context.Context, redirectURI string, authType int, bizAppID string) (string, error) {
|
||||
code, err := ctx.GetPreCodeContext(stdCtx)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return fmt.Sprintf(componentLoginURL, ctx.AppID, code, url.QueryEscape(redirectURI), authType, bizAppID), nil
|
||||
}
|
||||
|
||||
// GetBindComponentURL 获取第三方公众号授权链接(链接跳转,适用移动端)
|
||||
func (ctx *Context) GetBindComponentURL(redirectURI string, authType int, bizAppID string) (string, error) {
|
||||
code, err := ctx.GetPreCode()
|
||||
// GetComponentLoginPage 获取第三方公众号授权链接(扫码授权)
|
||||
func (ctx *Context) GetComponentLoginPage(redirectURI string, authType int, bizAppID string) (string, error) {
|
||||
return ctx.GetComponentLoginPageContext(context.Background(), redirectURI, authType, bizAppID)
|
||||
}
|
||||
|
||||
// GetBindComponentURLContext 获取第三方公众号授权链接(链接跳转,适用移动端)
|
||||
func (ctx *Context) GetBindComponentURLContext(stdCtx context.Context, redirectURI string, authType int, bizAppID string) (string, error) {
|
||||
code, err := ctx.GetPreCodeContext(stdCtx)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return fmt.Sprintf(bindComponentURL, authType, ctx.AppID, code, url.QueryEscape(redirectURI), bizAppID), nil
|
||||
}
|
||||
|
||||
// GetBindComponentURL 获取第三方公众号授权链接(链接跳转,适用移动端)
|
||||
func (ctx *Context) GetBindComponentURL(redirectURI string, authType int, bizAppID string) (string, error) {
|
||||
return ctx.GetBindComponentURLContext(context.Background(), redirectURI, authType, bizAppID)
|
||||
}
|
||||
|
||||
// ID 微信返回接口中各种类型字段
|
||||
type ID struct {
|
||||
ID int `json:"id"`
|
||||
@@ -137,9 +164,9 @@ type AuthrAccessToken struct {
|
||||
RefreshToken string `json:"authorizer_refresh_token"`
|
||||
}
|
||||
|
||||
// QueryAuthCode 使用授权码换取公众号或小程序的接口调用凭据和授权信息
|
||||
func (ctx *Context) QueryAuthCode(authCode string) (*AuthBaseInfo, error) {
|
||||
cat, err := ctx.GetComponentAccessToken()
|
||||
// QueryAuthCodeContext 使用授权码换取公众号或小程序的接口调用凭据和授权信息
|
||||
func (ctx *Context) QueryAuthCodeContext(stdCtx context.Context, authCode string) (*AuthBaseInfo, error) {
|
||||
cat, err := ctx.GetComponentAccessTokenContext(stdCtx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -149,7 +176,7 @@ func (ctx *Context) QueryAuthCode(authCode string) (*AuthBaseInfo, error) {
|
||||
"authorization_code": authCode,
|
||||
}
|
||||
uri := fmt.Sprintf(queryAuthURL, cat)
|
||||
body, err := util.PostJSON(uri, req)
|
||||
body, err := util.PostJSONContext(stdCtx, uri, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -169,9 +196,14 @@ func (ctx *Context) QueryAuthCode(authCode string) (*AuthBaseInfo, error) {
|
||||
return ret.Info, nil
|
||||
}
|
||||
|
||||
// RefreshAuthrToken 获取(刷新)授权公众号或小程序的接口调用凭据(令牌)
|
||||
func (ctx *Context) RefreshAuthrToken(appid, refreshToken string) (*AuthrAccessToken, error) {
|
||||
cat, err := ctx.GetComponentAccessToken()
|
||||
// QueryAuthCode 使用授权码换取公众号或小程序的接口调用凭据和授权信息
|
||||
func (ctx *Context) QueryAuthCode(authCode string) (*AuthBaseInfo, error) {
|
||||
return ctx.QueryAuthCodeContext(context.Background(), authCode)
|
||||
}
|
||||
|
||||
// RefreshAuthrTokenContext 获取(刷新)授权公众号或小程序的接口调用凭据(令牌)
|
||||
func (ctx *Context) RefreshAuthrTokenContext(stdCtx context.Context, appid, refreshToken string) (*AuthrAccessToken, error) {
|
||||
cat, err := ctx.GetComponentAccessTokenContext(stdCtx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -182,7 +214,7 @@ func (ctx *Context) RefreshAuthrToken(appid, refreshToken string) (*AuthrAccessT
|
||||
"authorizer_refresh_token": refreshToken,
|
||||
}
|
||||
uri := fmt.Sprintf(refreshTokenURL, cat)
|
||||
body, err := util.PostJSON(uri, req)
|
||||
body, err := util.PostJSONContext(stdCtx, uri, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -193,22 +225,32 @@ func (ctx *Context) RefreshAuthrToken(appid, refreshToken string) (*AuthrAccessT
|
||||
}
|
||||
|
||||
authrTokenKey := "authorizer_access_token_" + appid
|
||||
if err := ctx.Cache.Set(authrTokenKey, ret.AccessToken, time.Minute*80); err != nil {
|
||||
if err := cache.SetContext(stdCtx, ctx.Cache, authrTokenKey, ret.AccessToken, time.Second*time.Duration(ret.ExpiresIn-30)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
// GetAuthrAccessToken 获取授权方AccessToken
|
||||
func (ctx *Context) GetAuthrAccessToken(appid string) (string, error) {
|
||||
// RefreshAuthrToken 获取(刷新)授权公众号或小程序的接口调用凭据(令牌)
|
||||
func (ctx *Context) RefreshAuthrToken(appid, refreshToken string) (*AuthrAccessToken, error) {
|
||||
return ctx.RefreshAuthrTokenContext(context.Background(), appid, refreshToken)
|
||||
}
|
||||
|
||||
// GetAuthrAccessTokenContext 获取授权方AccessToken
|
||||
func (ctx *Context) GetAuthrAccessTokenContext(stdCtx context.Context, appid string) (string, error) {
|
||||
authrTokenKey := "authorizer_access_token_" + appid
|
||||
val := ctx.Cache.Get(authrTokenKey)
|
||||
val := cache.GetContext(stdCtx, ctx.Cache, authrTokenKey)
|
||||
if val == nil {
|
||||
return "", fmt.Errorf("cannot get authorizer %s access token", appid)
|
||||
}
|
||||
return val.(string), nil
|
||||
}
|
||||
|
||||
// GetAuthrAccessToken 获取授权方AccessToken
|
||||
func (ctx *Context) GetAuthrAccessToken(appid string) (string, error) {
|
||||
return ctx.GetAuthrAccessTokenContext(context.Background(), appid)
|
||||
}
|
||||
|
||||
// AuthorizerInfo 授权方详细信息
|
||||
type AuthorizerInfo struct {
|
||||
NickName string `json:"nick_name"`
|
||||
@@ -258,9 +300,9 @@ type CategoriesInfo struct {
|
||||
Second string `wx:"second"`
|
||||
}
|
||||
|
||||
// GetAuthrInfo 获取授权方的帐号基本信息
|
||||
func (ctx *Context) GetAuthrInfo(appid string) (*AuthorizerInfo, *AuthBaseInfo, error) {
|
||||
cat, err := ctx.GetComponentAccessToken()
|
||||
// GetAuthrInfoContext 获取授权方的帐号基本信息
|
||||
func (ctx *Context) GetAuthrInfoContext(stdCtx context.Context, appid string) (*AuthorizerInfo, *AuthBaseInfo, error) {
|
||||
cat, err := ctx.GetComponentAccessTokenContext(stdCtx)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
@@ -271,7 +313,7 @@ func (ctx *Context) GetAuthrInfo(appid string) (*AuthorizerInfo, *AuthBaseInfo,
|
||||
}
|
||||
|
||||
uri := fmt.Sprintf(getComponentInfoURL, cat)
|
||||
body, err := util.PostJSON(uri, req)
|
||||
body, err := util.PostJSONContext(stdCtx, uri, req)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
@@ -286,3 +328,8 @@ func (ctx *Context) GetAuthrInfo(appid string) (*AuthorizerInfo, *AuthBaseInfo,
|
||||
|
||||
return ret.AuthorizerInfo, ret.AuthorizationInfo, nil
|
||||
}
|
||||
|
||||
// GetAuthrInfo 获取授权方的帐号基本信息
|
||||
func (ctx *Context) GetAuthrInfo(appid string) (*AuthorizerInfo, *AuthBaseInfo, error) {
|
||||
return ctx.GetAuthrInfoContext(context.Background(), appid)
|
||||
}
|
||||
|
||||
@@ -36,7 +36,7 @@ type RegisterMiniProgramParam struct {
|
||||
func (component *Component) RegisterMiniProgram(param *RegisterMiniProgramParam) error {
|
||||
componentAK, err := component.GetComponentAccessToken()
|
||||
if err != nil {
|
||||
return nil
|
||||
return err
|
||||
}
|
||||
url := fmt.Sprintf(fastregisterweappURL+"?action=create&component_access_token=%s", componentAK)
|
||||
data, err := util.PostJSON(url, param)
|
||||
@@ -58,7 +58,7 @@ type GetRegistrationStatusParam struct {
|
||||
func (component *Component) GetRegistrationStatus(param *GetRegistrationStatusParam) error {
|
||||
componentAK, err := component.GetComponentAccessToken()
|
||||
if err != nil {
|
||||
return nil
|
||||
return err
|
||||
}
|
||||
url := fmt.Sprintf(fastregisterweappURL+"?action=search&component_access_token=%s", componentAK)
|
||||
data, err := util.PostJSON(url, param)
|
||||
|
||||
@@ -18,9 +18,6 @@ type OpenPlatform struct {
|
||||
|
||||
// NewOpenPlatform new openplatform
|
||||
func NewOpenPlatform(cfg *config.Config) *OpenPlatform {
|
||||
if cfg.Cache == nil {
|
||||
panic("cache 未设置")
|
||||
}
|
||||
ctx := &context.Context{
|
||||
Config: cfg,
|
||||
}
|
||||
|
||||
16
util/http.go
16
util/http.go
@@ -69,8 +69,8 @@ func HTTPPostContext(ctx context.Context, uri string, data []byte, header map[st
|
||||
return io.ReadAll(response.Body)
|
||||
}
|
||||
|
||||
// PostJSON post json 数据请求
|
||||
func PostJSON(uri string, obj interface{}) ([]byte, error) {
|
||||
// PostJSONContext post json 数据请求
|
||||
func PostJSONContext(ctx context.Context, uri string, obj interface{}) ([]byte, error) {
|
||||
jsonBuf := new(bytes.Buffer)
|
||||
enc := json.NewEncoder(jsonBuf)
|
||||
enc.SetEscapeHTML(false)
|
||||
@@ -78,7 +78,12 @@ func PostJSON(uri string, obj interface{}) ([]byte, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
response, err := http.Post(uri, "application/json;charset=utf-8", jsonBuf)
|
||||
req, err := http.NewRequestWithContext(ctx, "POST", uri, jsonBuf)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json;charset=utf-8")
|
||||
response, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -90,6 +95,11 @@ func PostJSON(uri string, obj interface{}) ([]byte, error) {
|
||||
return io.ReadAll(response.Body)
|
||||
}
|
||||
|
||||
// PostJSON post json 数据请求
|
||||
func PostJSON(uri string, obj interface{}) ([]byte, error) {
|
||||
return PostJSONContext(context.Background(), uri, obj)
|
||||
}
|
||||
|
||||
// PostJSONWithRespContentType post json数据请求,且返回数据类型
|
||||
func PostJSONWithRespContentType(uri string, obj interface{}) ([]byte, string, error) {
|
||||
jsonBuf := new(bytes.Buffer)
|
||||
|
||||
24
util/query.go
Normal file
24
util/query.go
Normal 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
19
util/query_test.go
Normal 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
24
util/template.go
Normal 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
20
util/template_test.go
Normal 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")
|
||||
}
|
||||
}
|
||||
@@ -68,10 +68,16 @@ func (wc *Wechat) GetPay(cfg *payConfig.Config) *pay.Pay {
|
||||
|
||||
// GetOpenPlatform 获取微信开放平台的实例
|
||||
func (wc *Wechat) GetOpenPlatform(cfg *openConfig.Config) *openplatform.OpenPlatform {
|
||||
if cfg.Cache == nil {
|
||||
cfg.Cache = wc.cache
|
||||
}
|
||||
return openplatform.NewOpenPlatform(cfg)
|
||||
}
|
||||
|
||||
// GetWork 获取企业微信的实例
|
||||
func (wc *Wechat) GetWork(cfg *workConfig.Config) *work.Work {
|
||||
if cfg.Cache == nil {
|
||||
cfg.Cache = wc.cache
|
||||
}
|
||||
return work.NewWork(cfg)
|
||||
}
|
||||
|
||||
@@ -7,8 +7,8 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
// DepartmentSimpleListURL 获取子部门ID列表
|
||||
DepartmentSimpleListURL = "https://qyapi.weixin.qq.com/cgi-bin/department/simplelist?access_token=%s&id=%d"
|
||||
// departmentSimpleListURL 获取子部门ID列表
|
||||
departmentSimpleListURL = "https://qyapi.weixin.qq.com/cgi-bin/department/simplelist?access_token=%s&id=%d"
|
||||
)
|
||||
|
||||
type (
|
||||
@@ -36,7 +36,7 @@ func (r *Client) DepartmentSimpleList(departmentID int) ([]*DepartmentID, error)
|
||||
return nil, err
|
||||
}
|
||||
var response []byte
|
||||
if response, err = util.HTTPGet(fmt.Sprintf(DepartmentSimpleListURL, accessToken, departmentID)); err != nil {
|
||||
if response, err = util.HTTPGet(fmt.Sprintf(departmentSimpleListURL, accessToken, departmentID)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result := &DepartmentSimpleListResponse{}
|
||||
|
||||
230
work/addresslist/linkedcorp.go
Normal file
230
work/addresslist/linkedcorp.go
Normal 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
|
||||
}
|
||||
242
work/addresslist/tag.go
Normal file
242
work/addresslist/tag.go
Normal file
@@ -0,0 +1,242 @@
|
||||
package addresslist
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/silenceper/wechat/v2/util"
|
||||
)
|
||||
|
||||
const (
|
||||
// createTagURL 创建标签
|
||||
createTagURL = "https://qyapi.weixin.qq.com/cgi-bin/tag/create?access_token=%s"
|
||||
// updateTagURL 更新标签名字
|
||||
updateTagURL = "https://qyapi.weixin.qq.com/cgi-bin/tag/update?access_token=%s"
|
||||
// deleteTagURL 删除标签
|
||||
deleteTagURL = "https://qyapi.weixin.qq.com/cgi-bin/tag/delete?access_token=%s&tagid=%d"
|
||||
// getTagURL 获取标签成员
|
||||
getTagURL = "https://qyapi.weixin.qq.com/cgi-bin/tag/get?access_token=%s&tagid=%d"
|
||||
// addTagUsersURL 增加标签成员
|
||||
addTagUsersURL = "https://qyapi.weixin.qq.com/cgi-bin/tag/addtagusers?access_token=%s"
|
||||
// delTagUsersURL 删除标签成员
|
||||
delTagUsersURL = "https://qyapi.weixin.qq.com/cgi-bin/tag/deltagusers?access_token=%s"
|
||||
// listTagURL 获取标签列表
|
||||
listTagURL = "https://qyapi.weixin.qq.com/cgi-bin/tag/list?access_token=%s"
|
||||
)
|
||||
|
||||
type (
|
||||
// CreateTagRequest 创建标签请求
|
||||
CreateTagRequest struct {
|
||||
TagName string `json:"tagname"`
|
||||
TagID int `json:"tagid,omitempty"`
|
||||
}
|
||||
// CreateTagResponse 创建标签响应
|
||||
CreateTagResponse struct {
|
||||
util.CommonError
|
||||
TagID int `json:"tagid"`
|
||||
}
|
||||
)
|
||||
|
||||
// CreateTag 创建标签
|
||||
// see https://developer.work.weixin.qq.com/document/path/90210
|
||||
func (r *Client) CreateTag(req *CreateTagRequest) (*CreateTagResponse, 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(createTagURL, accessToken), req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result := &CreateTagResponse{}
|
||||
if err = util.DecodeWithError(response, result, "CreateTag"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
type (
|
||||
// UpdateTagRequest 更新标签名字请求
|
||||
UpdateTagRequest struct {
|
||||
TagID int `json:"tagid"`
|
||||
TagName string `json:"tagname"`
|
||||
}
|
||||
)
|
||||
|
||||
// UpdateTag 更新标签名字
|
||||
// see https://developer.work.weixin.qq.com/document/path/90211
|
||||
func (r *Client) UpdateTag(req *UpdateTagRequest) 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(updateTagURL, accessToken), req); err != nil {
|
||||
return err
|
||||
}
|
||||
return util.DecodeWithCommonError(response, "UpdateTag")
|
||||
}
|
||||
|
||||
// DeleteTag 删除标签
|
||||
// @see https://developer.work.weixin.qq.com/document/path/90212
|
||||
func (r *Client) DeleteTag(tagID int) error {
|
||||
var (
|
||||
accessToken string
|
||||
err error
|
||||
)
|
||||
if accessToken, err = r.GetAccessToken(); err != nil {
|
||||
return err
|
||||
}
|
||||
var response []byte
|
||||
if response, err = util.HTTPGet(fmt.Sprintf(deleteTagURL, accessToken, tagID)); err != nil {
|
||||
return err
|
||||
}
|
||||
return util.DecodeWithCommonError(response, "DeleteTag")
|
||||
}
|
||||
|
||||
type (
|
||||
// GetTagResponse 获取标签成员响应
|
||||
GetTagResponse struct {
|
||||
util.CommonError
|
||||
TagName string `json:"tagname"`
|
||||
UserList []GetTagUserList `json:"userlist"`
|
||||
PartyList []int `json:"partylist"`
|
||||
}
|
||||
// GetTagUserList 标签中包含的成员列表
|
||||
GetTagUserList struct {
|
||||
UserID string `json:"userid"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
)
|
||||
|
||||
// GetTag 获取标签成员
|
||||
// @see https://developer.work.weixin.qq.com/document/path/90213
|
||||
func (r *Client) GetTag(tagID int) (*GetTagResponse, error) {
|
||||
var (
|
||||
accessToken string
|
||||
err error
|
||||
)
|
||||
if accessToken, err = r.GetAccessToken(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var response []byte
|
||||
if response, err = util.HTTPGet(fmt.Sprintf(getTagURL, accessToken, tagID)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result := &GetTagResponse{}
|
||||
if err = util.DecodeWithError(response, result, "GetTag"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
type (
|
||||
// AddTagUsersRequest 增加标签成员请求
|
||||
AddTagUsersRequest struct {
|
||||
TagID int `json:"tagid"`
|
||||
UserList []string `json:"userlist"`
|
||||
PartyList []int `json:"partylist"`
|
||||
}
|
||||
// AddTagUsersResponse 增加标签成员响应
|
||||
AddTagUsersResponse struct {
|
||||
util.CommonError
|
||||
InvalidList string `json:"invalidlist"`
|
||||
InvalidParty []int `json:"invalidparty"`
|
||||
}
|
||||
)
|
||||
|
||||
// AddTagUsers 增加标签成员
|
||||
// see https://developer.work.weixin.qq.com/document/path/90214
|
||||
func (r *Client) AddTagUsers(req *AddTagUsersRequest) (*AddTagUsersResponse, 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(addTagUsersURL, accessToken), req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result := &AddTagUsersResponse{}
|
||||
if err = util.DecodeWithError(response, result, "AddTagUsers"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
type (
|
||||
// DelTagUsersRequest 删除标签成员请求
|
||||
DelTagUsersRequest struct {
|
||||
TagID int `json:"tagid"`
|
||||
UserList []string `json:"userlist"`
|
||||
PartyList []int `json:"partylist"`
|
||||
}
|
||||
// DelTagUsersResponse 删除标签成员响应
|
||||
DelTagUsersResponse struct {
|
||||
util.CommonError
|
||||
InvalidList string `json:"invalidlist"`
|
||||
InvalidParty []int `json:"invalidparty"`
|
||||
}
|
||||
)
|
||||
|
||||
// DelTagUsers 删除标签成员
|
||||
// see https://developer.work.weixin.qq.com/document/path/90215
|
||||
func (r *Client) DelTagUsers(req *DelTagUsersRequest) (*DelTagUsersResponse, 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(delTagUsersURL, accessToken), req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result := &DelTagUsersResponse{}
|
||||
if err = util.DecodeWithError(response, result, "DelTagUsers"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
type (
|
||||
// ListTagResponse 获取标签列表响应
|
||||
ListTagResponse struct {
|
||||
util.CommonError
|
||||
TagList []Tag `json:"taglist"`
|
||||
}
|
||||
// Tag 标签
|
||||
Tag struct {
|
||||
TagID int `json:"tagid"`
|
||||
TagName string `json:"tagname"`
|
||||
}
|
||||
)
|
||||
|
||||
// ListTag 获取标签列表
|
||||
// @see https://developer.work.weixin.qq.com/document/path/90216
|
||||
func (r *Client) ListTag() (*ListTagResponse, error) {
|
||||
var (
|
||||
accessToken string
|
||||
err error
|
||||
)
|
||||
if accessToken, err = r.GetAccessToken(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var response []byte
|
||||
if response, err = util.HTTPGet(fmt.Sprintf(listTagURL, accessToken)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result := &ListTagResponse{}
|
||||
if err = util.DecodeWithError(response, result, "ListTag"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
@@ -1,18 +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"
|
||||
// UserGetURL 读取成员
|
||||
UserGetURL = "https://qyapi.weixin.qq.com/cgi-bin/user/get?access_token=%s&userid=%s"
|
||||
// UserListIDURL 获取成员ID列表
|
||||
UserListIDURL = "https://qyapi.weixin.qq.com/cgi-bin/user/list_id?access_token=%s"
|
||||
// userSimpleListURL 获取部门成员
|
||||
userSimpleListURL = "https://qyapi.weixin.qq.com/cgi-bin/user/simplelist"
|
||||
// userGetURL 读取成员
|
||||
userGetURL = "https://qyapi.weixin.qq.com/cgi-bin/user/get"
|
||||
// userListIDURL 获取成员ID列表
|
||||
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"
|
||||
// convertToUserIDURL openID转userID
|
||||
convertToUserIDURL = "https://qyapi.weixin.qq.com/cgi-bin/user/convert_to_userid"
|
||||
)
|
||||
|
||||
type (
|
||||
@@ -41,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{}
|
||||
@@ -125,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{}
|
||||
@@ -166,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{}
|
||||
@@ -175,3 +198,87 @@ func (r *Client) UserListID(req *UserListIDRequest) (*UserListIDResponse, error)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
type (
|
||||
// convertToOpenIDRequest userID转openID请求
|
||||
convertToOpenIDRequest struct {
|
||||
UserID string `json:"userid"`
|
||||
}
|
||||
|
||||
// convertToOpenIDResponse userID转openID响应
|
||||
convertToOpenIDResponse struct {
|
||||
util.CommonError
|
||||
OpenID string `json:"openid"`
|
||||
}
|
||||
)
|
||||
|
||||
// ConvertToOpenID userID转openID
|
||||
// see https://developer.work.weixin.qq.com/document/path/90202
|
||||
func (r *Client) ConvertToOpenID(userID string) (string, error) {
|
||||
var (
|
||||
accessToken string
|
||||
err error
|
||||
)
|
||||
if accessToken, err = r.GetAccessToken(); err != nil {
|
||||
return "", err
|
||||
}
|
||||
var response []byte
|
||||
|
||||
if response, err = util.PostJSON(strings.Join([]string{
|
||||
convertToOpenIDURL,
|
||||
util.Query(map[string]interface{}{
|
||||
"access_token": accessToken,
|
||||
}),
|
||||
}, "?"), &convertToOpenIDRequest{
|
||||
UserID: userID,
|
||||
}); err != nil {
|
||||
return "", err
|
||||
}
|
||||
result := &convertToOpenIDResponse{}
|
||||
if err = util.DecodeWithError(response, result, "ConvertToOpenID"); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return result.OpenID, nil
|
||||
}
|
||||
|
||||
type (
|
||||
// convertToUserIDRequest openID转userID请求
|
||||
convertToUserIDRequest struct {
|
||||
OpenID string `json:"openid"`
|
||||
}
|
||||
|
||||
// convertToUserIDResponse openID转userID响应
|
||||
convertToUserIDResponse struct {
|
||||
util.CommonError
|
||||
UserID string `json:"userid"`
|
||||
}
|
||||
)
|
||||
|
||||
// ConvertToUserID openID转userID
|
||||
// see https://developer.work.weixin.qq.com/document/path/90202
|
||||
func (r *Client) ConvertToUserID(openID string) (string, error) {
|
||||
var (
|
||||
accessToken string
|
||||
err error
|
||||
)
|
||||
if accessToken, err = r.GetAccessToken(); err != nil {
|
||||
return "", err
|
||||
}
|
||||
var response []byte
|
||||
|
||||
if response, err = util.PostJSON(strings.Join([]string{
|
||||
convertToUserIDURL,
|
||||
util.Query(map[string]interface{}{
|
||||
"access_token": accessToken,
|
||||
}),
|
||||
}, "?"), &convertToUserIDRequest{
|
||||
OpenID: openID,
|
||||
}); err != nil {
|
||||
return "", err
|
||||
}
|
||||
result := &convertToUserIDResponse{}
|
||||
if err = util.DecodeWithError(response, result, "ConvertToUserID"); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return result.UserID, nil
|
||||
}
|
||||
|
||||
115
work/appchat/appchat.go
Normal file
115
work/appchat/appchat.go
Normal file
@@ -0,0 +1,115 @@
|
||||
package appchat
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/silenceper/wechat/v2/util"
|
||||
)
|
||||
|
||||
const (
|
||||
// 应用推送消息接口地址
|
||||
sendURL = "https://qyapi.weixin.qq.com/cgi-bin/appchat/send?access_token=%s"
|
||||
)
|
||||
|
||||
type (
|
||||
// SendRequestCommon 发送应用推送消息请求公共参数
|
||||
SendRequestCommon struct {
|
||||
// 群聊id
|
||||
ChatID string `json:"chatid"`
|
||||
// 消息类型
|
||||
MsgType string `json:"msgtype"`
|
||||
// 表示是否是保密消息,0表示否,1表示是,默认0
|
||||
Safe int `json:"safe"`
|
||||
}
|
||||
|
||||
// SendResponse 发送应用消息响应参数
|
||||
SendResponse struct {
|
||||
util.CommonError
|
||||
}
|
||||
|
||||
// SendTextRequest 发送文本消息的请求
|
||||
SendTextRequest struct {
|
||||
*SendRequestCommon
|
||||
Text TextField `json:"text"`
|
||||
}
|
||||
// TextField 文本消息参数
|
||||
TextField struct {
|
||||
// 消息内容,最长不超过2048个字节
|
||||
Content string `json:"content"`
|
||||
}
|
||||
|
||||
// SendImageRequest 发送图片消息的请求
|
||||
SendImageRequest struct {
|
||||
*SendRequestCommon
|
||||
Image ImageField `json:"image"`
|
||||
}
|
||||
// ImageField 图片消息参数
|
||||
ImageField struct {
|
||||
// 图片媒体文件id,可以调用上传临时素材接口获取
|
||||
MediaID string `json:"media_id"`
|
||||
}
|
||||
|
||||
// SendVoiceRequest 发送语音消息的请求
|
||||
SendVoiceRequest struct {
|
||||
*SendRequestCommon
|
||||
Voice VoiceField `json:"voice"`
|
||||
}
|
||||
// VoiceField 语音消息参数
|
||||
VoiceField struct {
|
||||
// 语音文件id,可以调用上传临时素材接口获取
|
||||
MediaID string `json:"media_id"`
|
||||
}
|
||||
)
|
||||
|
||||
// Send 发送应用消息
|
||||
// @desc 实现企业微信发送应用消息接口:https://developer.work.weixin.qq.com/document/path/90248
|
||||
func (r *Client) Send(apiName string, request interface{}) (*SendResponse, error) {
|
||||
// 获取accessToken
|
||||
accessToken, err := r.GetAccessToken()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// 请求参数转 JSON 格式
|
||||
jsonData, err := json.Marshal(request)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// 发起http请求
|
||||
response, err := util.HTTPPost(fmt.Sprintf(sendURL, accessToken), string(jsonData))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// 按照结构体解析返回值
|
||||
result := &SendResponse{}
|
||||
if err = util.DecodeWithError(response, result, apiName); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// 返回数据
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// SendText 发送文本消息
|
||||
func (r *Client) SendText(request SendTextRequest) (*SendResponse, error) {
|
||||
// 发送文本消息MsgType参数固定为:text
|
||||
request.MsgType = "text"
|
||||
return r.Send("MessageSendText", request)
|
||||
}
|
||||
|
||||
// SendImage 发送图片消息
|
||||
func (r *Client) SendImage(request SendImageRequest) (*SendResponse, error) {
|
||||
// 发送图片消息MsgType参数固定为:image
|
||||
request.MsgType = "image"
|
||||
return r.Send("MessageSendImage", request)
|
||||
}
|
||||
|
||||
// SendVoice 发送语音消息
|
||||
func (r *Client) SendVoice(request SendVoiceRequest) (*SendResponse, error) {
|
||||
// 发送语音消息MsgType参数固定为:voice
|
||||
request.MsgType = "voice"
|
||||
return r.Send("MessageSendVoice", request)
|
||||
}
|
||||
|
||||
// 以上实现了部分常用消息推送:SendText 发送文本消息、SendImage 发送图片消息、SendVoice 发送语音消息,
|
||||
// 如需扩展其他消息类型,建议按照以上格式,扩展对应消息类型的参数即可
|
||||
// 也可以直接使用Send方法,按照企业微信消息推送的接口文档传对应消息类型的参数来使用
|
||||
16
work/appchat/client.go
Normal file
16
work/appchat/client.go
Normal file
@@ -0,0 +1,16 @@
|
||||
// Package appchat 应用发送消息到群聊会话,企业微信接口:https://developer.work.weixin.qq.com/document/path/90248
|
||||
package appchat
|
||||
|
||||
import (
|
||||
"github.com/silenceper/wechat/v2/work/context"
|
||||
)
|
||||
|
||||
// Client 接口实例
|
||||
type Client struct {
|
||||
*context.Context
|
||||
}
|
||||
|
||||
// NewClient 初始化实例
|
||||
func NewClient(ctx *context.Context) *Client {
|
||||
return &Client{ctx}
|
||||
}
|
||||
45
work/externalcontact/callback.go
Normal file
45
work/externalcontact/callback.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package externalcontact
|
||||
|
||||
import (
|
||||
"encoding/xml"
|
||||
|
||||
"github.com/silenceper/wechat/v2/util"
|
||||
)
|
||||
|
||||
// 原始回调消息内容
|
||||
type callbackOriginMessage struct {
|
||||
ToUserName string // 企业微信的CorpID,当为第三方套件回调事件时,CorpID的内容为suiteid
|
||||
AgentID string // 接收的应用id,可在应用的设置页面获取
|
||||
Encrypt string // 消息结构体加密后的字符串
|
||||
}
|
||||
|
||||
// EventCallbackMessage 微信客户联系回调消息
|
||||
// https://developer.work.weixin.qq.com/document/path/92130
|
||||
type EventCallbackMessage struct {
|
||||
ToUserName string `json:"to_user_name"`
|
||||
FromUserName string `json:"from_user_name"`
|
||||
CreateTime int64 `json:"create_time"`
|
||||
MsgType string `json:"msg_type"`
|
||||
Event string `json:"event"`
|
||||
ChangeType string `json:"change_type"`
|
||||
UserID string `json:"user_id"`
|
||||
ExternalUserID string `json:"external_user_id"`
|
||||
State string `json:"state"`
|
||||
WelcomeCode string `json:"welcome_code"`
|
||||
}
|
||||
|
||||
// GetCallbackMessage 获取联系客户回调事件中的消息内容
|
||||
func (r *Client) GetCallbackMessage(encryptedMsg []byte) (msg EventCallbackMessage, err error) {
|
||||
var origin callbackOriginMessage
|
||||
if err = xml.Unmarshal(encryptedMsg, &origin); err != nil {
|
||||
return
|
||||
}
|
||||
_, bData, err := util.DecryptMsg(r.CorpID, origin.Encrypt, r.EncodingAESKey)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if err = xml.Unmarshal(bData, &msg); err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -7,16 +7,16 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
// AddContactWayURL 配置客户联系「联系我」方式
|
||||
AddContactWayURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/add_contact_way?access_token=%s"
|
||||
// GetContactWayURL 获取企业已配置的「联系我」方式
|
||||
GetContactWayURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/get_contact_way?access_token=%s"
|
||||
// UpdateContactWayURL 更新企业已配置的「联系我」方式
|
||||
UpdateContactWayURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/update_contact_way?access_token=%s"
|
||||
// ListContactWayURL 获取企业已配置的「联系我」列表
|
||||
ListContactWayURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/list_contact_way?access_token=%s"
|
||||
// DelContactWayURL 删除企业已配置的「联系我」方式
|
||||
DelContactWayURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/del_contact_way?access_token=%s"
|
||||
// addContactWayURL 配置客户联系「联系我」方式
|
||||
addContactWayURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/add_contact_way?access_token=%s"
|
||||
// getContactWayURL 获取企业已配置的「联系我」方式
|
||||
getContactWayURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/get_contact_way?access_token=%s"
|
||||
// updateContactWayURL 更新企业已配置的「联系我」方式
|
||||
updateContactWayURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/update_contact_way?access_token=%s"
|
||||
// listContactWayURL 获取企业已配置的「联系我」列表
|
||||
listContactWayURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/list_contact_way?access_token=%s"
|
||||
// delContactWayURL 删除企业已配置的「联系我」方式
|
||||
delContactWayURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/del_contact_way?access_token=%s"
|
||||
)
|
||||
|
||||
type (
|
||||
@@ -98,7 +98,7 @@ func (r *Client) AddContactWay(req *AddContactWayRequest) (*AddContactWayRespons
|
||||
return nil, err
|
||||
}
|
||||
var response []byte
|
||||
if response, err = util.PostJSON(fmt.Sprintf(AddContactWayURL, accessToken), req); err != nil {
|
||||
if response, err = util.PostJSON(fmt.Sprintf(addContactWayURL, accessToken), req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result := &AddContactWayResponse{}
|
||||
@@ -149,7 +149,7 @@ func (r *Client) GetContactWay(req *GetContactWayRequest) (*GetContactWayRespons
|
||||
return nil, err
|
||||
}
|
||||
var response []byte
|
||||
if response, err = util.PostJSON(fmt.Sprintf(GetContactWayURL, accessToken), req); err != nil {
|
||||
if response, err = util.PostJSON(fmt.Sprintf(getContactWayURL, accessToken), req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result := &GetContactWayResponse{}
|
||||
@@ -191,7 +191,7 @@ func (r *Client) UpdateContactWay(req *UpdateContactWayRequest) (*UpdateContactW
|
||||
return nil, err
|
||||
}
|
||||
var response []byte
|
||||
if response, err = util.PostJSON(fmt.Sprintf(UpdateContactWayURL, accessToken), req); err != nil {
|
||||
if response, err = util.PostJSON(fmt.Sprintf(updateContactWayURL, accessToken), req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result := &UpdateContactWayResponse{}
|
||||
@@ -232,7 +232,7 @@ func (r *Client) ListContactWay(req *ListContactWayRequest) (*ListContactWayResp
|
||||
return nil, err
|
||||
}
|
||||
var response []byte
|
||||
if response, err = util.PostJSON(fmt.Sprintf(ListContactWayURL, accessToken), req); err != nil {
|
||||
if response, err = util.PostJSON(fmt.Sprintf(listContactWayURL, accessToken), req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result := &ListContactWayResponse{}
|
||||
@@ -264,7 +264,7 @@ func (r *Client) DelContactWay(req *DelContactWayRequest) (*DelContactWayRespons
|
||||
return nil, err
|
||||
}
|
||||
var response []byte
|
||||
if response, err = util.PostJSON(fmt.Sprintf(DelContactWayURL, accessToken), req); err != nil {
|
||||
if response, err = util.PostJSON(fmt.Sprintf(delContactWayURL, accessToken), req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result := &DelContactWayResponse{}
|
||||
|
||||
@@ -8,14 +8,26 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
// FetchExternalContactUserListURL 获取客户列表
|
||||
FetchExternalContactUserListURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/list"
|
||||
// FetchExternalContactUserDetailURL 获取客户详情
|
||||
FetchExternalContactUserDetailURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/get"
|
||||
// FetchBatchExternalContactUserDetailURL 批量获取客户详情
|
||||
FetchBatchExternalContactUserDetailURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/batch/get_by_user"
|
||||
// UpdateUserRemarkURL 更新客户备注信息
|
||||
UpdateUserRemarkURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/remark"
|
||||
// fetchExternalContactUserListURL 获取客户列表
|
||||
fetchExternalContactUserListURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/list"
|
||||
// fetchExternalContactUserDetailURL 获取客户详情
|
||||
fetchExternalContactUserDetailURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/get"
|
||||
// fetchBatchExternalContactUserDetailURL 批量获取客户详情
|
||||
fetchBatchExternalContactUserDetailURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/batch/get_by_user"
|
||||
// updateUserRemarkURL 更新客户备注信息
|
||||
updateUserRemarkURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/remark"
|
||||
// listCustomerStrategyURL 获取规则组列表
|
||||
listCustomerStrategyURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/customer_strategy/list?access_token=%s"
|
||||
// getCustomerStrategyURL 获取规则组详情
|
||||
getCustomerStrategyURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/customer_strategy/get?access_token=%s"
|
||||
// getRangeCustomerStrategyURL 获取规则组管理范围
|
||||
getRangeCustomerStrategyURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/customer_strategy/get_range?access_token=%s"
|
||||
// createCustomerStrategyURL 创建新的规则组
|
||||
createCustomerStrategyURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/customer_strategy/create?access_token=%s"
|
||||
// editCustomerStrategyURL 编辑规则组及其管理范围
|
||||
editCustomerStrategyURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/customer_strategy/edit?access_token=%s"
|
||||
// delCustomerStrategyURL 删除规则组
|
||||
delCustomerStrategyURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/customer_strategy/del?access_token=%s"
|
||||
)
|
||||
|
||||
// ExternalUserListResponse 外部联系人列表响应
|
||||
@@ -32,7 +44,7 @@ func (r *Client) GetExternalUserList(userID string) ([]string, error) {
|
||||
return nil, err
|
||||
}
|
||||
var response []byte
|
||||
response, err = util.HTTPGet(fmt.Sprintf("%s?access_token=%v&userid=%v", FetchExternalContactUserListURL, accessToken, userID))
|
||||
response, err = util.HTTPGet(fmt.Sprintf("%s?access_token=%v&userid=%v", fetchExternalContactUserListURL, accessToken, userID))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -71,7 +83,7 @@ type FollowUser struct {
|
||||
UserID string `json:"userid"`
|
||||
Remark string `json:"remark"`
|
||||
Description string `json:"description"`
|
||||
CreateTime string `json:"create_time"`
|
||||
CreateTime int64 `json:"createtime"`
|
||||
Tags []Tag `json:"tags"`
|
||||
RemarkCorpName string `json:"remark_corp_name"`
|
||||
RemarkMobiles []string `json:"remark_mobiles"`
|
||||
@@ -103,7 +115,11 @@ func (r *Client) GetExternalUserDetail(externalUserID string, nextCursor ...stri
|
||||
return nil, err
|
||||
}
|
||||
var response []byte
|
||||
response, err = util.HTTPGet(fmt.Sprintf("%s?access_token=%v&external_userid=%v&cursor=%v", FetchExternalContactUserDetailURL, accessToken, externalUserID, nextCursor))
|
||||
var cursor string
|
||||
if len(nextCursor) > 0 {
|
||||
cursor = nextCursor[0]
|
||||
}
|
||||
response, err = util.HTTPGet(fmt.Sprintf("%s?access_token=%v&external_userid=%v&cursor=%v", fetchExternalContactUserDetailURL, accessToken, externalUserID, cursor))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -119,6 +135,7 @@ func (r *Client) GetExternalUserDetail(externalUserID string, nextCursor ...stri
|
||||
type BatchGetExternalUserDetailsRequest struct {
|
||||
UserIDList []string `json:"userid_list"`
|
||||
Cursor string `json:"cursor"`
|
||||
Limit int `json:"limit,omitempty"`
|
||||
}
|
||||
|
||||
// ExternalUserDetailListResponse 批量获取外部联系人详情响应
|
||||
@@ -152,7 +169,7 @@ type FollowInfo struct {
|
||||
UserID string `json:"userid"`
|
||||
Remark string `json:"remark"`
|
||||
Description string `json:"description"`
|
||||
CreateTime int `json:"create_time"`
|
||||
CreateTime int64 `json:"createtime"`
|
||||
TagID []string `json:"tag_id"`
|
||||
RemarkCorpName string `json:"remark_corp_name"`
|
||||
RemarkMobiles []string `json:"remark_mobiles"`
|
||||
@@ -173,7 +190,7 @@ func (r *Client) BatchGetExternalUserDetails(request BatchGetExternalUserDetails
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
response, err = util.HTTPPost(fmt.Sprintf("%s?access_token=%v", FetchBatchExternalContactUserDetailURL, accessToken), string(jsonData))
|
||||
response, err = util.HTTPPost(fmt.Sprintf("%s?access_token=%v", fetchBatchExternalContactUserDetailURL, accessToken), string(jsonData))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -208,9 +225,242 @@ func (r *Client) UpdateUserRemark(request UpdateUserRemarkRequest) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
response, err = util.HTTPPost(fmt.Sprintf("%s?access_token=%v", UpdateUserRemarkURL, accessToken), string(jsonData))
|
||||
response, err = util.HTTPPost(fmt.Sprintf("%s?access_token=%v", updateUserRemarkURL, accessToken), string(jsonData))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return util.DecodeWithCommonError(response, "UpdateUserRemark")
|
||||
}
|
||||
|
||||
// ListCustomerStrategyRequest 获取规则组列表请求
|
||||
type ListCustomerStrategyRequest struct {
|
||||
Cursor string `json:"cursor"`
|
||||
Limit int `json:"limit"`
|
||||
}
|
||||
|
||||
// ListCustomerStrategyResponse 获取规则组列表响应
|
||||
type ListCustomerStrategyResponse struct {
|
||||
util.CommonError
|
||||
Strategy []StrategyID `json:"strategy"`
|
||||
NextCursor string `json:"next_cursor"`
|
||||
}
|
||||
|
||||
// StrategyID 规则组ID
|
||||
type StrategyID struct {
|
||||
StrategyID int `json:"strategy_id"`
|
||||
}
|
||||
|
||||
// ListCustomerStrategy 获取规则组列表
|
||||
// @see https://developer.work.weixin.qq.com/document/path/94883#%E8%8E%B7%E5%8F%96%E8%A7%84%E5%88%99%E7%BB%84%E5%88%97%E8%A1%A8
|
||||
func (r *Client) ListCustomerStrategy(req *ListCustomerStrategyRequest) (*ListCustomerStrategyResponse, 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(listCustomerStrategyURL, accessToken), req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result := &ListCustomerStrategyResponse{}
|
||||
if err = util.DecodeWithError(response, result, "ListCustomerStrategy"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// GetCustomerStrategyRequest 获取规则组详情请求
|
||||
type GetCustomerStrategyRequest struct {
|
||||
StrategyID int `json:"strategy_id"`
|
||||
}
|
||||
|
||||
// GetCustomerStrategyResponse 获取规则组详情响应
|
||||
type GetCustomerStrategyResponse struct {
|
||||
util.CommonError
|
||||
Strategy Strategy `json:"strategy"`
|
||||
}
|
||||
|
||||
// Strategy 规则组
|
||||
type Strategy struct {
|
||||
StrategyID int `json:"strategy_id"`
|
||||
ParentID int `json:"parent_id"`
|
||||
StrategyName string `json:"strategy_name"`
|
||||
CreateTime int64 `json:"create_time"`
|
||||
AdminList []string `json:"admin_list"`
|
||||
Privilege Privilege `json:"privilege"`
|
||||
}
|
||||
|
||||
// Privilege 权限
|
||||
type Privilege struct {
|
||||
ViewCustomerList bool `json:"view_customer_list"`
|
||||
ViewCustomerData bool `json:"view_customer_data"`
|
||||
ViewRoomList bool `json:"view_room_list"`
|
||||
ContactMe bool `json:"contact_me"`
|
||||
JoinRoom bool `json:"join_room"`
|
||||
ShareCustomer bool `json:"share_customer"`
|
||||
OperResignCustomer bool `json:"oper_resign_customer"`
|
||||
OperResignGroup bool `json:"oper_resign_group"`
|
||||
SendCustomerMsg bool `json:"send_customer_msg"`
|
||||
EditWelcomeMsg bool `json:"edit_welcome_msg"`
|
||||
ViewBehaviorData bool `json:"view_behavior_data"`
|
||||
ViewRoomData bool `json:"view_room_data"`
|
||||
SendGroupMsg bool `json:"send_group_msg"`
|
||||
RoomDeduplication bool `json:"room_deduplication"`
|
||||
RapidReply bool `json:"rapid_reply"`
|
||||
OnjobCustomerTransfer bool `json:"onjob_customer_transfer"`
|
||||
EditAntiSpamRule bool `json:"edit_anti_spam_rule"`
|
||||
ExportCustomerList bool `json:"export_customer_list"`
|
||||
ExportCustomerData bool `json:"export_customer_data"`
|
||||
ExportCustomerGroupList bool `json:"export_customer_group_list"`
|
||||
ManageCustomerTag bool `json:"manage_customer_tag"`
|
||||
}
|
||||
|
||||
// GetCustomerStrategy 获取规则组详情
|
||||
// @see https://developer.work.weixin.qq.com/document/path/94883#%E8%8E%B7%E5%8F%96%E8%A7%84%E5%88%99%E7%BB%84%E8%AF%A6%E6%83%85
|
||||
func (r *Client) GetCustomerStrategy(req *GetCustomerStrategyRequest) (*GetCustomerStrategyResponse, 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(getCustomerStrategyURL, accessToken), req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result := &GetCustomerStrategyResponse{}
|
||||
if err = util.DecodeWithError(response, result, "GetCustomerStrategy"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// GetRangeCustomerStrategyRequest 获取规则组管理范围请求
|
||||
type GetRangeCustomerStrategyRequest struct {
|
||||
StrategyID int `json:"strategy_id"`
|
||||
Cursor string `json:"cursor"`
|
||||
Limit int `json:"limit"`
|
||||
}
|
||||
|
||||
// GetRangeCustomerStrategyResponse 获取规则组管理范围响应
|
||||
type GetRangeCustomerStrategyResponse struct {
|
||||
util.CommonError
|
||||
Range []Range `json:"range"`
|
||||
NextCursor string `json:"next_cursor"`
|
||||
}
|
||||
|
||||
// Range 管理范围节点
|
||||
type Range struct {
|
||||
Type int `json:"type"`
|
||||
UserID string `json:"userid,omitempty"`
|
||||
PartyID int `json:"partyid,omitempty"`
|
||||
}
|
||||
|
||||
// GetRangeCustomerStrategy 获取规则组管理范围
|
||||
// @see https://developer.work.weixin.qq.com/document/path/94883#%E8%8E%B7%E5%8F%96%E8%A7%84%E5%88%99%E7%BB%84%E7%AE%A1%E7%90%86%E8%8C%83%E5%9B%B4
|
||||
func (r *Client) GetRangeCustomerStrategy(req *GetRangeCustomerStrategyRequest) (*GetRangeCustomerStrategyResponse, 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(getRangeCustomerStrategyURL, accessToken), req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result := &GetRangeCustomerStrategyResponse{}
|
||||
if err = util.DecodeWithError(response, result, "GetRangeCustomerStrategy"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// CreateCustomerStrategyRequest 创建新的规则组请求
|
||||
type CreateCustomerStrategyRequest struct {
|
||||
ParentID int `json:"parent_id"`
|
||||
StrategyName string `json:"strategy_name"`
|
||||
AdminList []string `json:"admin_list"`
|
||||
Privilege Privilege `json:"privilege"`
|
||||
Range []Range `json:"range"`
|
||||
}
|
||||
|
||||
// CreateCustomerStrategyResponse 创建新的规则组响应
|
||||
type CreateCustomerStrategyResponse struct {
|
||||
util.CommonError
|
||||
StrategyID int `json:"strategy_id"`
|
||||
}
|
||||
|
||||
// CreateCustomerStrategy 创建新的规则组
|
||||
// @see https://developer.work.weixin.qq.com/document/path/94883#%E5%88%9B%E5%BB%BA%E6%96%B0%E7%9A%84%E8%A7%84%E5%88%99%E7%BB%84
|
||||
func (r *Client) CreateCustomerStrategy(req *CreateCustomerStrategyRequest) (*CreateCustomerStrategyResponse, 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(createCustomerStrategyURL, accessToken), req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result := &CreateCustomerStrategyResponse{}
|
||||
if err = util.DecodeWithError(response, result, "CreateCustomerStrategy"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// EditCustomerStrategyRequest 编辑规则组及其管理范围请求
|
||||
type EditCustomerStrategyRequest struct {
|
||||
StrategyID int `json:"strategy_id"`
|
||||
StrategyName string `json:"strategy_name"`
|
||||
AdminList []string `json:"admin_list"`
|
||||
Privilege Privilege `json:"privilege"`
|
||||
RangeAdd []Range `json:"range_add"`
|
||||
RangeDel []Range `json:"range_del"`
|
||||
}
|
||||
|
||||
// EditCustomerStrategy 编辑规则组及其管理范围
|
||||
// see https://developer.work.weixin.qq.com/document/path/94883#%E7%BC%96%E8%BE%91%E8%A7%84%E5%88%99%E7%BB%84%E5%8F%8A%E5%85%B6%E7%AE%A1%E7%90%86%E8%8C%83%E5%9B%B4
|
||||
func (r *Client) EditCustomerStrategy(req *EditCustomerStrategyRequest) 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(editCustomerStrategyURL, accessToken), req); err != nil {
|
||||
return err
|
||||
}
|
||||
return util.DecodeWithCommonError(response, "EditCustomerStrategy")
|
||||
}
|
||||
|
||||
// DelCustomerStrategyRequest 删除规则组请求
|
||||
type DelCustomerStrategyRequest struct {
|
||||
StrategyID int `json:"strategy_id"`
|
||||
}
|
||||
|
||||
// DelCustomerStrategy 删除规则组
|
||||
// see https://developer.work.weixin.qq.com/document/path/94883#%E5%88%A0%E9%99%A4%E8%A7%84%E5%88%99%E7%BB%84
|
||||
func (r *Client) DelCustomerStrategy(req *DelCustomerStrategyRequest) 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(delCustomerStrategyURL, accessToken), req); err != nil {
|
||||
return err
|
||||
}
|
||||
return util.DecodeWithCommonError(response, "DelCustomerStrategy")
|
||||
}
|
||||
|
||||
@@ -7,8 +7,8 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
// FetchFollowUserListURL 获取配置了客户联系功能的成员列表
|
||||
FetchFollowUserListURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/get_follow_user_list"
|
||||
// fetchFollowUserListURL 获取配置了客户联系功能的成员列表
|
||||
fetchFollowUserListURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/get_follow_user_list"
|
||||
)
|
||||
|
||||
// followerUserResponse 客户联系功能的成员列表响应
|
||||
@@ -25,7 +25,7 @@ func (r *Client) GetFollowUserList() ([]string, error) {
|
||||
return nil, err
|
||||
}
|
||||
var response []byte
|
||||
response, err = util.HTTPGet(fmt.Sprintf("%s?access_token=%s", FetchFollowUserListURL, accessToken))
|
||||
response, err = util.HTTPGet(fmt.Sprintf("%s?access_token=%s", fetchFollowUserListURL, accessToken))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -6,8 +6,8 @@ import (
|
||||
"github.com/silenceper/wechat/v2/util"
|
||||
)
|
||||
|
||||
// OpengIDToChatIDURL 客户群opengid转换URL
|
||||
const OpengIDToChatIDURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/opengid_to_chatid"
|
||||
// opengIDToChatIDURL 客户群opengid转换URL
|
||||
const opengIDToChatIDURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/opengid_to_chatid"
|
||||
|
||||
type (
|
||||
//GroupChatListRequest 获取客户群列表的请求参数
|
||||
@@ -39,7 +39,7 @@ func (r *Client) GetGroupChatList(req *GroupChatListRequest) (*GroupChatListResp
|
||||
return nil, err
|
||||
}
|
||||
var response []byte
|
||||
response, err = util.PostJSON(fmt.Sprintf("%s/list?access_token=%s", GroupChatURL, accessToken), req)
|
||||
response, err = util.PostJSON(fmt.Sprintf("%s/list?access_token=%s", groupChatURL, accessToken), req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -80,7 +80,7 @@ type (
|
||||
ChatID string `json:"chat_id"` //客户群ID
|
||||
Name string `json:"name"` //群名
|
||||
Owner string `json:"owner"` //群主ID
|
||||
CreateTime int `json:"create_time"` //群的创建时间
|
||||
CreateTime int64 `json:"create_time"` //群的创建时间
|
||||
Notice string `json:"notice"` //群公告
|
||||
MemberList []GroupChatMember `json:"member_list"` //群成员列表
|
||||
AdminList []GroupChatAdmin `json:"admin_list"` //群管理员列表
|
||||
@@ -100,7 +100,7 @@ func (r *Client) GetGroupChatDetail(req *GroupChatDetailRequest) (*GroupChatDeta
|
||||
return nil, err
|
||||
}
|
||||
var response []byte
|
||||
response, err = util.PostJSON(fmt.Sprintf("%s/get?access_token=%s", GroupChatURL, accessToken), req)
|
||||
response, err = util.PostJSON(fmt.Sprintf("%s/get?access_token=%s", groupChatURL, accessToken), req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -131,7 +131,7 @@ func (r *Client) OpengIDToChatID(req *OpengIDToChatIDRequest) (*OpengIDToChatIDR
|
||||
return nil, err
|
||||
}
|
||||
var response []byte
|
||||
response, err = util.PostJSON(fmt.Sprintf("%s?access_token=%s", OpengIDToChatIDURL, accessToken), req)
|
||||
response, err = util.PostJSON(fmt.Sprintf("%s?access_token=%s", opengIDToChatIDURL, accessToken), req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -6,8 +6,8 @@ import (
|
||||
"github.com/silenceper/wechat/v2/util"
|
||||
)
|
||||
|
||||
// GroupChatURL 客户群
|
||||
const GroupChatURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/groupchat"
|
||||
// groupChatURL 客户群
|
||||
const groupChatURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/groupchat"
|
||||
|
||||
type (
|
||||
// AddJoinWayRequest 添加群配置请求参数
|
||||
@@ -39,7 +39,7 @@ func (r *Client) AddJoinWay(req *AddJoinWayRequest) (*AddJoinWayResponse, error)
|
||||
if accessToken, err = r.GetAccessToken(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
response, err = util.PostJSON(fmt.Sprintf("%s/add_join_way?access_token=%s", GroupChatURL, accessToken), req)
|
||||
response, err = util.PostJSON(fmt.Sprintf("%s/add_join_way?access_token=%s", groupChatURL, accessToken), req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -86,7 +86,7 @@ func (r *Client) GetJoinWay(req *JoinWayConfigRequest) (*GetJoinWayResponse, err
|
||||
if accessToken, err = r.GetAccessToken(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
response, err = util.PostJSON(fmt.Sprintf("%s/get_join_way?access_token=%s", GroupChatURL, accessToken), req)
|
||||
response, err = util.PostJSON(fmt.Sprintf("%s/get_join_way?access_token=%s", groupChatURL, accessToken), req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -120,7 +120,7 @@ func (r *Client) UpdateJoinWay(req *UpdateJoinWayRequest) error {
|
||||
if accessToken, err = r.GetAccessToken(); err != nil {
|
||||
return err
|
||||
}
|
||||
response, err = util.PostJSON(fmt.Sprintf("%s/update_join_way?access_token=%s", GroupChatURL, accessToken), req)
|
||||
response, err = util.PostJSON(fmt.Sprintf("%s/update_join_way?access_token=%s", groupChatURL, accessToken), req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -138,7 +138,7 @@ func (r *Client) DelJoinWay(req *JoinWayConfigRequest) error {
|
||||
if accessToken, err = r.GetAccessToken(); err != nil {
|
||||
return err
|
||||
}
|
||||
response, err = util.PostJSON(fmt.Sprintf("%s/del_join_way?access_token=%s", GroupChatURL, accessToken), req)
|
||||
response, err = util.PostJSON(fmt.Sprintf("%s/del_join_way?access_token=%s", groupChatURL, accessToken), req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -7,24 +7,24 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
// AddMsgTemplateURL 创建企业群发
|
||||
AddMsgTemplateURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/add_msg_template?access_token=%s"
|
||||
// GetGroupMsgListV2URL 获取群发记录列表
|
||||
GetGroupMsgListV2URL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/get_groupmsg_list_v2?access_token=%s"
|
||||
// GetGroupMsgTaskURL 获取群发成员发送任务列表
|
||||
GetGroupMsgTaskURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/get_groupmsg_task?access_token=%s"
|
||||
// GetGroupMsgSendResultURL 获取企业群发成员执行结果
|
||||
GetGroupMsgSendResultURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/get_groupmsg_send_result?access_token=%s"
|
||||
// SendWelcomeMsgURL 发送新客户欢迎语
|
||||
SendWelcomeMsgURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/send_welcome_msg?access_token=%s"
|
||||
// AddGroupWelcomeTemplateURL 添加入群欢迎语素材
|
||||
AddGroupWelcomeTemplateURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/group_welcome_template/add?access_token=%s"
|
||||
// EditGroupWelcomeTemplateURL 编辑入群欢迎语素材
|
||||
EditGroupWelcomeTemplateURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/group_welcome_template/edit?access_token=%s"
|
||||
// GetGroupWelcomeTemplateURL 获取入群欢迎语素材
|
||||
GetGroupWelcomeTemplateURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/group_welcome_template/get?access_token=%s"
|
||||
// DelGroupWelcomeTemplateURL 删除入群欢迎语素材
|
||||
DelGroupWelcomeTemplateURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/group_welcome_template/del?access_token=%s"
|
||||
// addMsgTemplateURL 创建企业群发
|
||||
addMsgTemplateURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/add_msg_template?access_token=%s"
|
||||
// getGroupMsgListV2URL 获取群发记录列表
|
||||
getGroupMsgListV2URL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/get_groupmsg_list_v2?access_token=%s"
|
||||
// getGroupMsgTaskURL 获取群发成员发送任务列表
|
||||
getGroupMsgTaskURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/get_groupmsg_task?access_token=%s"
|
||||
// getGroupMsgSendResultURL 获取企业群发成员执行结果
|
||||
getGroupMsgSendResultURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/get_groupmsg_send_result?access_token=%s"
|
||||
// sendWelcomeMsgURL 发送新客户欢迎语
|
||||
sendWelcomeMsgURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/send_welcome_msg?access_token=%s"
|
||||
// addGroupWelcomeTemplateURL 添加入群欢迎语素材
|
||||
addGroupWelcomeTemplateURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/group_welcome_template/add?access_token=%s"
|
||||
// editGroupWelcomeTemplateURL 编辑入群欢迎语素材
|
||||
editGroupWelcomeTemplateURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/group_welcome_template/edit?access_token=%s"
|
||||
// getGroupWelcomeTemplateURL 获取入群欢迎语素材
|
||||
getGroupWelcomeTemplateURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/group_welcome_template/get?access_token=%s"
|
||||
// delGroupWelcomeTemplateURL 删除入群欢迎语素材
|
||||
delGroupWelcomeTemplateURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/group_welcome_template/del?access_token=%s"
|
||||
)
|
||||
|
||||
// AddMsgTemplateRequest 创建企业群发请求
|
||||
@@ -98,7 +98,7 @@ func (r *Client) AddMsgTemplate(req *AddMsgTemplateRequest) (*AddMsgTemplateResp
|
||||
return nil, err
|
||||
}
|
||||
var response []byte
|
||||
if response, err = util.PostJSON(fmt.Sprintf(AddMsgTemplateURL, accessToken), req); err != nil {
|
||||
if response, err = util.PostJSON(fmt.Sprintf(addMsgTemplateURL, accessToken), req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result := &AddMsgTemplateResponse{}
|
||||
@@ -111,8 +111,8 @@ func (r *Client) AddMsgTemplate(req *AddMsgTemplateRequest) (*AddMsgTemplateResp
|
||||
// GetGroupMsgListV2Request 获取群发记录列表请求
|
||||
type GetGroupMsgListV2Request struct {
|
||||
ChatType string `json:"chat_type"`
|
||||
StartTime int `json:"start_time"`
|
||||
EndTime int `json:"end_time"`
|
||||
StartTime int64 `json:"start_time"`
|
||||
EndTime int64 `json:"end_time"`
|
||||
Creator string `json:"creator,omitempty"`
|
||||
FilterType int `json:"filter_type"`
|
||||
Limit int `json:"limit"`
|
||||
@@ -130,7 +130,7 @@ type GetGroupMsgListV2Response struct {
|
||||
type GroupMsg struct {
|
||||
MsgID string `json:"msgid"`
|
||||
Creator string `json:"creator"`
|
||||
CreateTime int `json:"create_time"`
|
||||
CreateTime int64 `json:"create_time"`
|
||||
CreateType int `json:"create_type"`
|
||||
Text MsgText `json:"text"`
|
||||
Attachments []*Attachment `json:"attachments"`
|
||||
@@ -147,7 +147,7 @@ func (r *Client) GetGroupMsgListV2(req *GetGroupMsgListV2Request) (*GetGroupMsgL
|
||||
return nil, err
|
||||
}
|
||||
var response []byte
|
||||
if response, err = util.PostJSON(fmt.Sprintf(GetGroupMsgListV2URL, accessToken), req); err != nil {
|
||||
if response, err = util.PostJSON(fmt.Sprintf(getGroupMsgListV2URL, accessToken), req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result := &GetGroupMsgListV2Response{}
|
||||
@@ -189,7 +189,7 @@ func (r *Client) GetGroupMsgTask(req *GetGroupMsgTaskRequest) (*GetGroupMsgTaskR
|
||||
return nil, err
|
||||
}
|
||||
var response []byte
|
||||
if response, err = util.PostJSON(fmt.Sprintf(GetGroupMsgTaskURL, accessToken), req); err != nil {
|
||||
if response, err = util.PostJSON(fmt.Sprintf(getGroupMsgTaskURL, accessToken), req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result := &GetGroupMsgTaskResponse{}
|
||||
@@ -234,7 +234,7 @@ func (r *Client) GetGroupMsgSendResult(req *GetGroupMsgSendResultRequest) (*GetG
|
||||
return nil, err
|
||||
}
|
||||
var response []byte
|
||||
if response, err = util.PostJSON(fmt.Sprintf(GetGroupMsgSendResultURL, accessToken), req); err != nil {
|
||||
if response, err = util.PostJSON(fmt.Sprintf(getGroupMsgSendResultURL, accessToken), req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result := &GetGroupMsgSendResultResponse{}
|
||||
@@ -267,7 +267,7 @@ func (r *Client) SendWelcomeMsg(req *SendWelcomeMsgRequest) error {
|
||||
return err
|
||||
}
|
||||
var response []byte
|
||||
if response, err = util.PostJSON(fmt.Sprintf(SendWelcomeMsgURL, accessToken), req); err != nil {
|
||||
if response, err = util.PostJSON(fmt.Sprintf(sendWelcomeMsgURL, accessToken), req); err != nil {
|
||||
return err
|
||||
}
|
||||
result := &SendWelcomeMsgResponse{}
|
||||
@@ -306,7 +306,7 @@ func (r *Client) AddGroupWelcomeTemplate(req *AddGroupWelcomeTemplateRequest) (*
|
||||
return nil, err
|
||||
}
|
||||
var response []byte
|
||||
if response, err = util.PostJSON(fmt.Sprintf(AddGroupWelcomeTemplateURL, accessToken), req); err != nil {
|
||||
if response, err = util.PostJSON(fmt.Sprintf(addGroupWelcomeTemplateURL, accessToken), req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result := &AddGroupWelcomeTemplateResponse{}
|
||||
@@ -344,7 +344,7 @@ func (r *Client) EditGroupWelcomeTemplate(req *EditGroupWelcomeTemplateRequest)
|
||||
return err
|
||||
}
|
||||
var response []byte
|
||||
if response, err = util.PostJSON(fmt.Sprintf(EditGroupWelcomeTemplateURL, accessToken), req); err != nil {
|
||||
if response, err = util.PostJSON(fmt.Sprintf(editGroupWelcomeTemplateURL, accessToken), req); err != nil {
|
||||
return err
|
||||
}
|
||||
result := &EditGroupWelcomeTemplateResponse{}
|
||||
@@ -381,7 +381,7 @@ func (r *Client) GetGroupWelcomeTemplate(req *GetGroupWelcomeTemplateRequest) (*
|
||||
return nil, err
|
||||
}
|
||||
var response []byte
|
||||
if response, err = util.PostJSON(fmt.Sprintf(GetGroupWelcomeTemplateURL, accessToken), req); err != nil {
|
||||
if response, err = util.PostJSON(fmt.Sprintf(getGroupWelcomeTemplateURL, accessToken), req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result := &GetGroupWelcomeTemplateResponse{}
|
||||
@@ -413,7 +413,7 @@ func (r *Client) DelGroupWelcomeTemplate(req *DelGroupWelcomeTemplateRequest) er
|
||||
return err
|
||||
}
|
||||
var response []byte
|
||||
if response, err = util.PostJSON(fmt.Sprintf(DelGroupWelcomeTemplateURL, accessToken), req); err != nil {
|
||||
if response, err = util.PostJSON(fmt.Sprintf(delGroupWelcomeTemplateURL, accessToken), req); err != nil {
|
||||
return err
|
||||
}
|
||||
result := &DelGroupWelcomeTemplateResponse{}
|
||||
|
||||
@@ -8,12 +8,12 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
// GetUserBehaviorDataURL 获取「联系客户统计」数据
|
||||
GetUserBehaviorDataURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/get_user_behavior_data"
|
||||
// GetGroupChatStatURL 获取「群聊数据统计」数据 按群主聚合的方式
|
||||
GetGroupChatStatURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/groupchat/statistic"
|
||||
// GetGroupChatStatByDayURL 获取「群聊数据统计」数据 按自然日聚合的方式
|
||||
GetGroupChatStatByDayURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/groupchat/statistic_group_by_day"
|
||||
// getUserBehaviorDataURL 获取「联系客户统计」数据
|
||||
getUserBehaviorDataURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/get_user_behavior_data"
|
||||
// getGroupChatStatURL 获取「群聊数据统计」数据 按群主聚合的方式
|
||||
getGroupChatStatURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/groupchat/statistic"
|
||||
// getGroupChatStatByDayURL 获取「群聊数据统计」数据 按自然日聚合的方式
|
||||
getGroupChatStatByDayURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/groupchat/statistic_group_by_day"
|
||||
)
|
||||
|
||||
type (
|
||||
@@ -54,7 +54,7 @@ func (r *Client) GetUserBehaviorData(req *GetUserBehaviorRequest) ([]BehaviorDat
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
response, err = util.HTTPPost(fmt.Sprintf("%s?access_token=%v", GetUserBehaviorDataURL, accessToken), string(jsonData))
|
||||
response, err = util.HTTPPost(fmt.Sprintf("%s?access_token=%v", getUserBehaviorDataURL, accessToken), string(jsonData))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -120,7 +120,7 @@ func (r *Client) GetGroupChatStat(req *GetGroupChatStatRequest) (*GetGroupChatSt
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
response, err = util.HTTPPost(fmt.Sprintf("%s?access_token=%v", GetGroupChatStatURL, accessToken), string(jsonData))
|
||||
response, err = util.HTTPPost(fmt.Sprintf("%s?access_token=%v", getGroupChatStatURL, accessToken), string(jsonData))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -163,7 +163,7 @@ func (r *Client) GetGroupChatStatByDay(req *GetGroupChatStatByDayRequest) ([]Get
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
response, err = util.HTTPPost(fmt.Sprintf("%s?access_token=%v", GetGroupChatStatByDayURL, accessToken), string(jsonData))
|
||||
response, err = util.HTTPPost(fmt.Sprintf("%s?access_token=%v", getGroupChatStatByDayURL, accessToken), string(jsonData))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -8,16 +8,24 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
// GetCropTagURL 获取标签列表
|
||||
GetCropTagURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/get_corp_tag_list"
|
||||
// AddCropTagURL 添加标签
|
||||
AddCropTagURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/add_corp_tag"
|
||||
// EditCropTagURL 修改标签
|
||||
EditCropTagURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/edit_corp_tag"
|
||||
// DelCropTagURL 删除标签
|
||||
DelCropTagURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/del_corp_tag"
|
||||
// MarkCropTagURL 为客户打上、删除标签
|
||||
MarkCropTagURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/mark_tag"
|
||||
// getCropTagURL 获取标签列表
|
||||
getCropTagURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/get_corp_tag_list"
|
||||
// addCropTagURL 添加标签
|
||||
addCropTagURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/add_corp_tag"
|
||||
// editCropTagURL 修改标签
|
||||
editCropTagURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/edit_corp_tag"
|
||||
// delCropTagURL 删除标签
|
||||
delCropTagURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/del_corp_tag"
|
||||
// markCropTagURL 为客户打上、删除标签
|
||||
markCropTagURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/mark_tag"
|
||||
// getStrategyTagListURL 获取指定规则组下的企业客户标签
|
||||
getStrategyTagListURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/get_strategy_tag_list?access_token=%s"
|
||||
// addStrategyTagURL 为指定规则组创建企业客户标签
|
||||
addStrategyTagURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/add_strategy_tag?access_token=%s"
|
||||
// editStrategyTagURL 编辑指定规则组下的企业客户标签
|
||||
editStrategyTagURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/edit_strategy_tag?access_token=%s"
|
||||
// delStrategyTagURL 删除指定规则组下的企业客户标签
|
||||
delStrategyTagURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/del_strategy_tag?access_token=%s"
|
||||
)
|
||||
|
||||
// GetCropTagRequest 获取企业标签请求
|
||||
@@ -36,7 +44,7 @@ type GetCropTagListResponse struct {
|
||||
type TagGroup struct {
|
||||
GroupID string `json:"group_id"`
|
||||
GroupName string `json:"group_name"`
|
||||
CreateTime int `json:"create_time"`
|
||||
CreateTime int64 `json:"create_time"`
|
||||
GroupOrder int `json:"group_order"`
|
||||
Deleted bool `json:"deleted"`
|
||||
Tag []TagGroupTagItem `json:"tag"`
|
||||
@@ -46,7 +54,7 @@ type TagGroup struct {
|
||||
type TagGroupTagItem struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
CreateTime int `json:"create_time"`
|
||||
CreateTime int64 `json:"create_time"`
|
||||
Order int `json:"order"`
|
||||
Deleted bool `json:"deleted"`
|
||||
}
|
||||
@@ -63,7 +71,7 @@ func (r *Client) GetCropTagList(req GetCropTagRequest) ([]TagGroup, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
response, err = util.HTTPPost(fmt.Sprintf("%s?access_token=%v", GetCropTagURL, accessToken), string(jsonData))
|
||||
response, err = util.HTTPPost(fmt.Sprintf("%s?access_token=%v", getCropTagURL, accessToken), string(jsonData))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -109,7 +117,7 @@ func (r *Client) AddCropTag(req AddCropTagRequest) (*TagGroup, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
response, err = util.HTTPPost(fmt.Sprintf("%s?access_token=%v", AddCropTagURL, accessToken), string(jsonData))
|
||||
response, err = util.HTTPPost(fmt.Sprintf("%s?access_token=%v", addCropTagURL, accessToken), string(jsonData))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -141,7 +149,7 @@ func (r *Client) EditCropTag(req EditCropTagRequest) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
response, err = util.HTTPPost(fmt.Sprintf("%s?access_token=%v", EditCropTagURL, accessToken), string(jsonData))
|
||||
response, err = util.HTTPPost(fmt.Sprintf("%s?access_token=%v", editCropTagURL, accessToken), string(jsonData))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -167,7 +175,7 @@ func (r *Client) DeleteCropTag(req DeleteCropTagRequest) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
response, err = util.HTTPPost(fmt.Sprintf("%s?access_token=%v", DelCropTagURL, accessToken), string(jsonData))
|
||||
response, err = util.HTTPPost(fmt.Sprintf("%s?access_token=%v", delCropTagURL, accessToken), string(jsonData))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -195,9 +203,167 @@ func (r *Client) MarkTag(request MarkTagRequest) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
response, err = util.HTTPPost(fmt.Sprintf("%s?access_token=%v", MarkCropTagURL, accessToken), string(jsonData))
|
||||
response, err = util.HTTPPost(fmt.Sprintf("%s?access_token=%v", markCropTagURL, accessToken), string(jsonData))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return util.DecodeWithCommonError(response, "MarkTag")
|
||||
}
|
||||
|
||||
// GetStrategyTagListRequest 获取指定规则组下的企业客户标签请求
|
||||
type GetStrategyTagListRequest struct {
|
||||
StrategyID int `json:"strategy_id"`
|
||||
TagID []string `json:"tag_id"`
|
||||
GroupID []string `json:"group_id"`
|
||||
}
|
||||
|
||||
// GetStrategyTagListResponse 获取指定规则组下的企业客户标签响应
|
||||
type GetStrategyTagListResponse struct {
|
||||
util.CommonError
|
||||
TagGroup []StrategyTagGroup `json:"tag_group"`
|
||||
}
|
||||
|
||||
// StrategyTagGroup 规则组下的企业标签组
|
||||
type StrategyTagGroup struct {
|
||||
GroupID string `json:"group_id"`
|
||||
GroupName string `json:"group_name"`
|
||||
CreateTime int64 `json:"create_time"`
|
||||
Order int `json:"order"`
|
||||
StrategyID int `json:"strategy_id"`
|
||||
Tag []StrategyTag `json:"tag"`
|
||||
}
|
||||
|
||||
// StrategyTag 规则组下的企业标签
|
||||
type StrategyTag struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
CreateTime int64 `json:"create_time"`
|
||||
Order int `json:"order"`
|
||||
}
|
||||
|
||||
// GetStrategyTagList 获取指定规则组下的企业客户标签
|
||||
// @see https://developer.work.weixin.qq.com/document/path/94882#%E8%8E%B7%E5%8F%96%E6%8C%87%E5%AE%9A%E8%A7%84%E5%88%99%E7%BB%84%E4%B8%8B%E7%9A%84%E4%BC%81%E4%B8%9A%E5%AE%A2%E6%88%B7%E6%A0%87%E7%AD%BE
|
||||
func (r *Client) GetStrategyTagList(req *GetStrategyTagListRequest) (*GetStrategyTagListResponse, 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(getStrategyTagListURL, accessToken), req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result := &GetStrategyTagListResponse{}
|
||||
if err = util.DecodeWithError(response, result, "GetStrategyTagList"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// AddStrategyTagRequest 为指定规则组创建企业客户标签请求
|
||||
type AddStrategyTagRequest struct {
|
||||
StrategyID int `json:"strategy_id"`
|
||||
GroupID string `json:"group_id"`
|
||||
GroupName string `json:"group_name"`
|
||||
Order int `json:"order"`
|
||||
Tag []AddStrategyTagRequestItem `json:"tag"`
|
||||
}
|
||||
|
||||
// AddStrategyTagRequestItem 为指定规则组创建企业客户标签请求条目
|
||||
type AddStrategyTagRequestItem struct {
|
||||
Name string `json:"name"`
|
||||
Order int `json:"order"`
|
||||
}
|
||||
|
||||
// AddStrategyTagResponse 为指定规则组创建企业客户标签响应
|
||||
type AddStrategyTagResponse struct {
|
||||
util.CommonError
|
||||
TagGroup AddStrategyTagResponseTagGroup `json:"tag_group"`
|
||||
}
|
||||
|
||||
// AddStrategyTagResponseTagGroup 为指定规则组创建企业客户标签响应标签组
|
||||
type AddStrategyTagResponseTagGroup struct {
|
||||
GroupID string `json:"group_id"`
|
||||
GroupName string `json:"group_name"`
|
||||
CreateTime int64 `json:"create_time"`
|
||||
Order int `json:"order"`
|
||||
Tag []AddStrategyTagResponseItem `json:"tag"`
|
||||
}
|
||||
|
||||
// AddStrategyTagResponseItem 标签组内的标签列表
|
||||
type AddStrategyTagResponseItem struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
CreateTime int64 `json:"create_time"`
|
||||
Order int `json:"order"`
|
||||
}
|
||||
|
||||
// AddStrategyTag 为指定规则组创建企业客户标签
|
||||
// @see https://developer.work.weixin.qq.com/document/path/94882#%E4%B8%BA%E6%8C%87%E5%AE%9A%E8%A7%84%E5%88%99%E7%BB%84%E5%88%9B%E5%BB%BA%E4%BC%81%E4%B8%9A%E5%AE%A2%E6%88%B7%E6%A0%87%E7%AD%BE
|
||||
func (r *Client) AddStrategyTag(req *AddStrategyTagRequest) (*AddStrategyTagResponse, 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(addStrategyTagURL, accessToken), req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result := &AddStrategyTagResponse{}
|
||||
if err = util.DecodeWithError(response, result, "AddStrategyTag"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// EditStrategyTagRequest 编辑指定规则组下的企业客户标签请求
|
||||
type EditStrategyTagRequest struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Order int `json:"order"`
|
||||
}
|
||||
|
||||
// EditStrategyTag 编辑指定规则组下的企业客户标签
|
||||
// see https://developer.work.weixin.qq.com/document/path/94882#%E7%BC%96%E8%BE%91%E6%8C%87%E5%AE%9A%E8%A7%84%E5%88%99%E7%BB%84%E4%B8%8B%E7%9A%84%E4%BC%81%E4%B8%9A%E5%AE%A2%E6%88%B7%E6%A0%87%E7%AD%BE
|
||||
func (r *Client) EditStrategyTag(req *EditStrategyTagRequest) 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(editStrategyTagURL, accessToken), req); err != nil {
|
||||
return err
|
||||
}
|
||||
return util.DecodeWithCommonError(response, "EditStrategyTag")
|
||||
}
|
||||
|
||||
// DelStrategyTagRequest 删除指定规则组下的企业客户标签请求
|
||||
type DelStrategyTagRequest struct {
|
||||
TagID []string `json:"tag_id"`
|
||||
GroupID []string `json:"group_id"`
|
||||
}
|
||||
|
||||
// DelStrategyTag 删除指定规则组下的企业客户标签
|
||||
// see https://developer.work.weixin.qq.com/document/path/94882#%E5%88%A0%E9%99%A4%E6%8C%87%E5%AE%9A%E8%A7%84%E5%88%99%E7%BB%84%E4%B8%8B%E7%9A%84%E4%BC%81%E4%B8%9A%E5%AE%A2%E6%88%B7%E6%A0%87%E7%AD%BE
|
||||
func (r *Client) DelStrategyTag(req *DelStrategyTagRequest) 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(delStrategyTagURL, accessToken), req); err != nil {
|
||||
return err
|
||||
}
|
||||
return util.DecodeWithCommonError(response, "DelStrategyTag")
|
||||
}
|
||||
|
||||
17
work/invoice/client.go
Normal file
17
work/invoice/client.go
Normal 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
191
work/invoice/invoice.go
Normal 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
|
||||
}
|
||||
@@ -52,11 +52,12 @@ type callbackOriginMessage struct {
|
||||
|
||||
// CallbackMessage 微信客服回调消息
|
||||
type CallbackMessage struct {
|
||||
ToUserName string `json:"to_user_name"` // 微信客服组件ID
|
||||
CreateTime int `json:"create_time"` // 消息创建时间,unix时间戳
|
||||
MsgType string `json:"msgtype"` // 消息的类型,此时固定为 event
|
||||
Event string `json:"event"` // 事件的类型,此时固定为 kf_msg_or_event
|
||||
Token string `json:"token"` // 调用拉取消息接口时,需要传此token,用于校验请求的合法性
|
||||
ToUserName string `json:"to_user_name" xml:"ToUserName"` // 微信客服组件ID
|
||||
CreateTime int64 `json:"create_time" xml:"CreateTime"` // 消息创建时间,unix时间戳
|
||||
MsgType string `json:"msgtype" xml:"MsgType"` // 消息的类型,此时固定为 event
|
||||
Event string `json:"event" xml:"Event"` // 事件的类型,此时固定为 kf_msg_or_event
|
||||
Token string `json:"token" xml:"Token"` // 调用拉取消息接口时,需要传此token,用于校验请求的合法性
|
||||
OpenKfID string `json:"open_kfid" xml:"OpenKfId"` // 有新消息的客服帐号。可通过sync_msg接口指定open_kfid获取此客服帐号的消息
|
||||
}
|
||||
|
||||
// GetCallbackMessage 获取回调事件中的消息内容
|
||||
|
||||
@@ -16,9 +16,11 @@ const (
|
||||
|
||||
// SyncMsgOptions 获取消息查询参数
|
||||
type SyncMsgOptions struct {
|
||||
Cursor string `json:"cursor"` // 上一次调用时返回的next_cursor,第一次拉取可以不填, 不多于64字节
|
||||
Token string `json:"token"` // 回调事件返回的token字段,10分钟内有效;可不填,如果不填接口有严格的频率限制, 不多于128字节
|
||||
Limit uint `json:"limit"` // 期望请求的数据量,默认值和最大值都为1000, 注意:可能会出现返回条数少于limit的情况,需结合返回的has_more字段判断是否继续请求。
|
||||
Cursor string `json:"cursor"` // 上一次调用时返回的next_cursor,第一次拉取可以不填, 不多于64字节
|
||||
Token string `json:"token"` // 回调事件返回的token字段,10分钟内有效;可不填,如果不填接口有严格的频率限制, 不多于128字节
|
||||
Limit uint `json:"limit"` // 期望请求的数据量,默认值和最大值都为1000, 注意:可能会出现返回条数少于limit的情况,需结合返回的has_more字段判断是否继续请求。
|
||||
VoiceFormat uint `json:"voice_format,omitempty"` // 语音消息类型,0-Amr 1-Silk,默认0。可通过该参数控制返回的语音格式,开发者可按需选择自己程序支持的一种格式
|
||||
OpenKfID string `json:"open_kfid,omitempty"` // 指定拉取某个客服帐号的消息,否则默认返回有权限的客服帐号的消息。当客服帐号较多,建议按open_kfid来拉取以获取更好的性能。
|
||||
}
|
||||
|
||||
// SyncMsgSchema 获取消息查询响应内容
|
||||
|
||||
@@ -7,8 +7,10 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
// UploadImgURL 上传图片
|
||||
UploadImgURL = "https://qyapi.weixin.qq.com/cgi-bin/media/uploadimg?access_token=%s"
|
||||
// uploadImgURL 上传图片
|
||||
uploadImgURL = "https://qyapi.weixin.qq.com/cgi-bin/media/uploadimg?access_token=%s"
|
||||
// uploadTempFile 上传临时素材
|
||||
uploadTempFile = "https://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token=%s&type=%s"
|
||||
)
|
||||
|
||||
// UploadImgResponse 上传图片响应
|
||||
@@ -17,6 +19,14 @@ type UploadImgResponse struct {
|
||||
URL string `json:"url"`
|
||||
}
|
||||
|
||||
// UploadTempFileResponse 上传临时素材响应
|
||||
type UploadTempFileResponse struct {
|
||||
util.CommonError
|
||||
MediaID string `json:"media_id"`
|
||||
CreateAt string `json:"created_at"`
|
||||
Type string `json:"type"`
|
||||
}
|
||||
|
||||
// UploadImg 上传图片
|
||||
// @see https://developer.work.weixin.qq.com/document/path/90256
|
||||
func (r *Client) UploadImg(filename string) (*UploadImgResponse, error) {
|
||||
@@ -28,7 +38,7 @@ func (r *Client) UploadImg(filename string) (*UploadImgResponse, error) {
|
||||
return nil, err
|
||||
}
|
||||
var response []byte
|
||||
if response, err = util.PostFile("media", filename, fmt.Sprintf(UploadImgURL, accessToken)); err != nil {
|
||||
if response, err = util.PostFile("media", filename, fmt.Sprintf(uploadImgURL, accessToken)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result := &UploadImgResponse{}
|
||||
@@ -37,3 +47,25 @@ func (r *Client) UploadImg(filename string) (*UploadImgResponse, error) {
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// UploadTempFile 上传临时素材
|
||||
// @see https://developer.work.weixin.qq.com/document/path/90253
|
||||
// @mediaType 媒体文件类型,分别有图片(image)、语音(voice)、视频(video),普通文件(file)
|
||||
func (r *Client) UploadTempFile(filename string, mediaType string) (*UploadTempFileResponse, error) {
|
||||
var (
|
||||
accessToken string
|
||||
err error
|
||||
)
|
||||
if accessToken, err = r.GetAccessToken(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var response []byte
|
||||
if response, err = util.PostFile("media", filename, fmt.Sprintf(uploadTempFile, accessToken, mediaType)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result := &UploadTempFileResponse{}
|
||||
if err = util.DecodeWithError(response, result, "UploadTempFile"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
16
work/message/client.go
Normal file
16
work/message/client.go
Normal file
@@ -0,0 +1,16 @@
|
||||
// Package message 消息推送,实现企业微信消息推送相关接口:https://developer.work.weixin.qq.com/document/path/90235
|
||||
package message
|
||||
|
||||
import (
|
||||
"github.com/silenceper/wechat/v2/work/context"
|
||||
)
|
||||
|
||||
// Client 消息推送接口实例
|
||||
type Client struct {
|
||||
*context.Context
|
||||
}
|
||||
|
||||
// NewClient 初始化实例
|
||||
func NewClient(ctx *context.Context) *Client {
|
||||
return &Client{ctx}
|
||||
}
|
||||
132
work/message/message.go
Normal file
132
work/message/message.go
Normal file
@@ -0,0 +1,132 @@
|
||||
package message
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/silenceper/wechat/v2/util"
|
||||
)
|
||||
|
||||
const (
|
||||
// 发送应用消息的接口地址
|
||||
sendURL = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=%s"
|
||||
)
|
||||
|
||||
type (
|
||||
// SendRequestCommon 发送应用消息请求公共参数
|
||||
SendRequestCommon struct {
|
||||
// 指定接收消息的成员,成员ID列表(多个接收者用‘|’分隔,最多支持1000个)。 特殊情况:指定为"@all",则向该企业应用的全部成员发送
|
||||
ToUser string `json:"touser"`
|
||||
// 指定接收消息的部门,部门ID列表,多个接收者用‘|’分隔,最多支持100个。 当touser为"@all"时忽略本参数
|
||||
ToParty string `json:"toparty"`
|
||||
// 指定接收消息的标签,标签ID列表,多个接收者用‘|’分隔,最多支持100个。 当touser为"@all"时忽略本参数
|
||||
ToTag string `json:"totag"`
|
||||
// 消息类型,此时固定为:text
|
||||
MsgType string `json:"msgtype"`
|
||||
// 企业应用的id,整型。企业内部开发,可在应用的设置页面查看;第三方服务商,可通过接口 获取企业授权信息 获取该参数值
|
||||
AgentID string `json:"agentid"`
|
||||
// 表示是否是保密消息,0表示可对外分享,1表示不能分享且内容显示水印,默认为0
|
||||
Safe int `json:"safe"`
|
||||
// 表示是否开启id转译,0表示否,1表示是,默认0。仅第三方应用需要用到,企业自建应用可以忽略。
|
||||
EnableIDTrans int `json:"enable_id_trans"`
|
||||
// 表示是否开启重复消息检查,0表示否,1表示是,默认0
|
||||
EnableDuplicateCheck int `json:"enable_duplicate_check"`
|
||||
// 表示是否重复消息检查的时间间隔,默认1800s,最大不超过4小时
|
||||
DuplicateCheckInterval int `json:"duplicate_check_interval"`
|
||||
}
|
||||
// SendResponse 发送应用消息响应参数
|
||||
SendResponse struct {
|
||||
util.CommonError
|
||||
InvalidUser string `json:"invaliduser"` // 不合法的userid,不区分大小写,统一转为小写
|
||||
InvalidParty string `json:"invalidparty"` // 不合法的partyid
|
||||
InvalidTag string `json:"invalidtag"` // 不合法的标签id
|
||||
UnlicensedUser string `json:"unlicenseduser"` // 没有基础接口许可(包含已过期)的userid
|
||||
MsgID string `json:"msgid"` // 消息id
|
||||
ResponseCode string `json:"response_code"`
|
||||
}
|
||||
|
||||
// SendTextRequest 发送文本消息的请求
|
||||
SendTextRequest struct {
|
||||
*SendRequestCommon
|
||||
Text TextField `json:"text"`
|
||||
}
|
||||
// TextField 文本消息参数
|
||||
TextField struct {
|
||||
// 消息内容,最长不超过2048个字节,超过将截断(支持id转译)
|
||||
Content string `json:"content"`
|
||||
}
|
||||
|
||||
// SendImageRequest 发送图片消息的请求
|
||||
SendImageRequest struct {
|
||||
*SendRequestCommon
|
||||
Image ImageField `json:"image"`
|
||||
}
|
||||
// ImageField 图片消息参数
|
||||
ImageField struct {
|
||||
// 图片媒体文件id,可以调用上传临时素材接口获取
|
||||
MediaID string `json:"media_id"`
|
||||
}
|
||||
|
||||
// SendVoiceRequest 发送语音消息的请求
|
||||
SendVoiceRequest struct {
|
||||
*SendRequestCommon
|
||||
Voice VoiceField `json:"voice"`
|
||||
}
|
||||
// VoiceField 语音消息参数
|
||||
VoiceField struct {
|
||||
// 语音文件id,可以调用上传临时素材接口获取
|
||||
MediaID string `json:"media_id"`
|
||||
}
|
||||
)
|
||||
|
||||
// Send 发送应用消息
|
||||
// @desc 实现企业微信发送应用消息接口:https://developer.work.weixin.qq.com/document/path/90236
|
||||
func (r *Client) Send(apiName string, request interface{}) (*SendResponse, error) {
|
||||
// 获取accessToken
|
||||
accessToken, err := r.GetAccessToken()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// 请求参数转 JSON 格式
|
||||
jsonData, err := json.Marshal(request)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// 发起http请求
|
||||
response, err := util.HTTPPost(fmt.Sprintf(sendURL, accessToken), string(jsonData))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// 按照结构体解析返回值
|
||||
result := &SendResponse{}
|
||||
if err = util.DecodeWithError(response, result, apiName); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// 返回数据
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// SendText 发送文本消息
|
||||
func (r *Client) SendText(request SendTextRequest) (*SendResponse, error) {
|
||||
// 发送文本消息MsgType参数固定为:text
|
||||
request.MsgType = "text"
|
||||
return r.Send("MessageSendText", request)
|
||||
}
|
||||
|
||||
// SendImage 发送图片消息
|
||||
func (r *Client) SendImage(request SendImageRequest) (*SendResponse, error) {
|
||||
// 发送图片消息MsgType参数固定为:image
|
||||
request.MsgType = "image"
|
||||
return r.Send("MessageSendImage", request)
|
||||
}
|
||||
|
||||
// SendVoice 发送语音消息
|
||||
func (r *Client) SendVoice(request SendVoiceRequest) (*SendResponse, error) {
|
||||
// 发送语音消息MsgType参数固定为:voice
|
||||
request.MsgType = "voice"
|
||||
return r.Send("MessageSendVoice", request)
|
||||
}
|
||||
|
||||
// 以上实现了部分常用消息推送:SendText 发送文本消息、SendImage 发送图片消息、SendVoice 发送语音消息,
|
||||
// 如需扩展其他消息类型,建议按照以上格式,扩展对应消息类型的参数即可
|
||||
// 也可以直接使用Send方法,按照企业微信消息推送的接口文档传对应消息类型的参数来使用
|
||||
@@ -150,7 +150,7 @@ type TodoMessage struct {
|
||||
BaseMessage
|
||||
Todo struct {
|
||||
Title string `json:"title,omitempty"` // 代办的来源文本
|
||||
Content string `json:"content,omitempty"` // 代办的具体内容
|
||||
Content string `json:"content,omitempty"` // 代办的具体内容
|
||||
} `json:"todo,omitempty"`
|
||||
}
|
||||
|
||||
@@ -169,7 +169,7 @@ type CollectMessage struct {
|
||||
Collect struct {
|
||||
RoomName string `json:"room_name,omitempty"` // 填表消息所在的群名称。
|
||||
Creator string `json:"creator,omitempty"` // 创建者在群中的名字
|
||||
CreateTime string `json:"create_time,omitempty"` // 创建的时间
|
||||
CreateTime int64 `json:"create_time,omitempty"` // 创建的时间
|
||||
Details []CollectDetails `json:"details,omitempty"` // 表内容
|
||||
} `json:"collect,omitempty"`
|
||||
}
|
||||
@@ -266,10 +266,10 @@ type VoipDocShareMessage struct {
|
||||
type ExternalRedPacketMessage struct {
|
||||
BaseMessage
|
||||
RedPacket struct {
|
||||
Type int32 `json:"type,omitempty"` // 红包消息类型。1 普通红包、2 拼手气群红包。Uint32类型
|
||||
Wish int32 `json:"wish,omitempty"` // 红包祝福语。String类型
|
||||
TotalCnt int32 `json:"totalcnt,omitempty"` // 红包总个数。Uint32类型
|
||||
TotalAmount int32 `json:"totalamount,omitempty"` // 红包消息类型。1 普通红包、2 拼手气群红包。Uint32类型
|
||||
Type uint32 `json:"type,omitempty"` // 红包消息类型。1 普通红包、2 拼手气群红包。Uint32类型
|
||||
Wish string `json:"wish,omitempty"` // 红包祝福语。String类型
|
||||
TotalCnt uint32 `json:"totalcnt,omitempty"` // 红包总个数。Uint32类型
|
||||
TotalAmount uint32 `json:"totalamount,omitempty"` // 红包总金额。Uint32类型,单位为分。
|
||||
} `json:"redpacket,omitempty"`
|
||||
}
|
||||
|
||||
@@ -277,9 +277,9 @@ type ExternalRedPacketMessage struct {
|
||||
type SphFeedMessage struct {
|
||||
BaseMessage
|
||||
SphFeed struct {
|
||||
FeedType string `json:"feed_type,omitempty"` // 视频号消息类型
|
||||
SphName string `json:"sph_name,omitempty"` // 视频号账号名称
|
||||
FeedDesc uint64 `json:"feed_desc,omitempty"` // 视频号账号名称
|
||||
FeedType uint32 `json:"feed_type,omitempty"` // 视频号消息类型。2 图片、4 视频、9 直播。Uint32类型
|
||||
SphName string `json:"sph_name,omitempty"` // 视频号账号名称。String类型
|
||||
FeedDesc string `json:"feed_desc,omitempty"` // 视频号消息描述。String类型
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -23,6 +23,10 @@ var (
|
||||
oauthUserInfoURL = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token=%s&code=%s"
|
||||
// oauthQrContentTargetURL 构造独立窗口登录二维码
|
||||
oauthQrContentTargetURL = "https://open.work.weixin.qq.com/wwopen/sso/qrConnect?appid=%s&agentid=%s&redirect_uri=%s&state=%s"
|
||||
// getUserInfoURL 获取访问用户身份&获取用户登录身份
|
||||
getUserInfoURL = "https://qyapi.weixin.qq.com/cgi-bin/auth/getuserinfo?access_token=%s&code=%s"
|
||||
// getUserDetailURL 获取访问用户敏感信息
|
||||
getUserDetailURL = "https://qyapi.weixin.qq.com/cgi-bin/auth/getuserdetail?access_token=%s"
|
||||
)
|
||||
|
||||
// NewOauth new init oauth
|
||||
@@ -93,3 +97,73 @@ func (ctr *Oauth) UserFromCode(code string) (result ResUserInfo, err error) {
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// GetUserInfoResponse 获取访问用户身份&获取用户登录身份响应
|
||||
type GetUserInfoResponse struct {
|
||||
util.CommonError
|
||||
UserID string `json:"userid"`
|
||||
UserTicket string `json:"user_ticket"`
|
||||
OpenID string `json:"openid"`
|
||||
ExternalUserID string `json:"external_userid"`
|
||||
}
|
||||
|
||||
// GetUserInfo 获取访问用户身份&获取用户登录身份
|
||||
// @see https://developer.work.weixin.qq.com/document/path/90213 获取访问用户身份
|
||||
// @see https://developer.work.weixin.qq.com/document/path/98176 获取用户登录身份
|
||||
func (ctr *Oauth) GetUserInfo(code string) (*GetUserInfoResponse, error) {
|
||||
var (
|
||||
accessToken string
|
||||
err error
|
||||
)
|
||||
if accessToken, err = ctr.GetAccessToken(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var response []byte
|
||||
if response, err = util.HTTPGet(fmt.Sprintf(getUserInfoURL, accessToken, code)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result := &GetUserInfoResponse{}
|
||||
if err = util.DecodeWithError(response, result, "GetUserInfo"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// GetUserDetailRequest 获取访问用户敏感信息请求
|
||||
type GetUserDetailRequest struct {
|
||||
UserTicket string `json:"user_ticket"`
|
||||
}
|
||||
|
||||
// GetUserDetailResponse 获取访问用户敏感信息响应
|
||||
type GetUserDetailResponse struct {
|
||||
util.CommonError
|
||||
UserID string `json:"userid"`
|
||||
Gender string `json:"gender"`
|
||||
Avatar string `json:"avatar"`
|
||||
QrCode string `json:"qr_code"`
|
||||
Mobile string `json:"mobile"`
|
||||
Email string `json:"email"`
|
||||
BizMail string `json:"biz_mail"`
|
||||
Address string `json:"address"`
|
||||
}
|
||||
|
||||
// GetUserDetail 获取访问用户敏感信息
|
||||
// @see https://developer.work.weixin.qq.com/document/path/95833
|
||||
func (ctr *Oauth) GetUserDetail(req *GetUserDetailRequest) (*GetUserDetailResponse, error) {
|
||||
var (
|
||||
accessToken string
|
||||
err error
|
||||
)
|
||||
if accessToken, err = ctr.GetAccessToken(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var response []byte
|
||||
if response, err = util.PostJSON(fmt.Sprintf(getUserDetailURL, accessToken), req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result := &GetUserDetailResponse{}
|
||||
if err = util.DecodeWithError(response, result, "GetUserDetail"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
@@ -8,15 +8,15 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
// WebhookSendURL 机器人发送群组消息
|
||||
WebhookSendURL = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=%s"
|
||||
// webhookSendURL 机器人发送群组消息
|
||||
webhookSendURL = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=%s"
|
||||
)
|
||||
|
||||
// RobotBroadcast 群机器人消息发送
|
||||
// @see https://developer.work.weixin.qq.com/document/path/91770
|
||||
func (r *Client) RobotBroadcast(webhookKey string, options interface{}) (info util.CommonError, err error) {
|
||||
var data []byte
|
||||
if data, err = util.PostJSON(fmt.Sprintf(WebhookSendURL, webhookKey), options); err != nil {
|
||||
if data, err = util.PostJSON(fmt.Sprintf(webhookSendURL, webhookKey), options); err != nil {
|
||||
return
|
||||
}
|
||||
if err = json.Unmarshal(data, &info); err != nil {
|
||||
|
||||
18
work/work.go
18
work/work.go
@@ -3,11 +3,14 @@ package work
|
||||
import (
|
||||
"github.com/silenceper/wechat/v2/credential"
|
||||
"github.com/silenceper/wechat/v2/work/addresslist"
|
||||
"github.com/silenceper/wechat/v2/work/appchat"
|
||||
"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"
|
||||
"github.com/silenceper/wechat/v2/work/msgaudit"
|
||||
"github.com/silenceper/wechat/v2/work/oauth"
|
||||
"github.com/silenceper/wechat/v2/work/robot"
|
||||
@@ -67,3 +70,18 @@ func (wk *Work) GetMaterial() *material.Client {
|
||||
func (wk *Work) GetRobot() *robot.Client {
|
||||
return robot.NewClient(wk.ctx)
|
||||
}
|
||||
|
||||
// GetMessage 获取发送应用消息接口实例
|
||||
func (wk *Work) GetMessage() *message.Client {
|
||||
return message.NewClient(wk.ctx)
|
||||
}
|
||||
|
||||
// GetAppChat 获取应用发送消息到群聊会话接口实例
|
||||
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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user