mirror of
https://github.com/silenceper/wechat.git
synced 2026-02-13 01:02:27 +08:00
Compare commits
6 Commits
feature/re
...
e61b441be5
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e61b441be5 | ||
|
|
a15ccac41e | ||
|
|
b6762f11ef | ||
|
|
3fbe8634d9 | ||
|
|
990ba6ede9 | ||
|
|
44b09c7c3b |
@@ -101,10 +101,11 @@ func (ak *DefaultAccessToken) GetAccessTokenContext(ctx context.Context) (access
|
|||||||
// 不强制更新access_token,可用于不同环境不同服务而不需要分布式锁以及公用缓存,避免access_token争抢
|
// 不强制更新access_token,可用于不同环境不同服务而不需要分布式锁以及公用缓存,避免access_token争抢
|
||||||
// https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/mp-access-token/getStableAccessToken.html
|
// https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/mp-access-token/getStableAccessToken.html
|
||||||
type StableAccessToken struct {
|
type StableAccessToken struct {
|
||||||
appID string
|
appID string
|
||||||
appSecret string
|
appSecret string
|
||||||
cacheKeyPrefix string
|
cacheKeyPrefix string
|
||||||
cache cache.Cache
|
cache cache.Cache
|
||||||
|
accessTokenLock *sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewStableAccessToken new StableAccessToken
|
// NewStableAccessToken new StableAccessToken
|
||||||
@@ -113,10 +114,11 @@ func NewStableAccessToken(appID, appSecret, cacheKeyPrefix string, cache cache.C
|
|||||||
panic("cache is need")
|
panic("cache is need")
|
||||||
}
|
}
|
||||||
return &StableAccessToken{
|
return &StableAccessToken{
|
||||||
appID: appID,
|
appID: appID,
|
||||||
appSecret: appSecret,
|
appSecret: appSecret,
|
||||||
cache: cache,
|
cache: cache,
|
||||||
cacheKeyPrefix: cacheKeyPrefix,
|
cacheKeyPrefix: cacheKeyPrefix,
|
||||||
|
accessTokenLock: new(sync.Mutex),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -130,7 +132,20 @@ func (ak *StableAccessToken) GetAccessTokenContext(ctx context.Context) (accessT
|
|||||||
// 先从cache中取
|
// 先从cache中取
|
||||||
accessTokenCacheKey := fmt.Sprintf("%s_stable_access_token_%s", ak.cacheKeyPrefix, ak.appID)
|
accessTokenCacheKey := fmt.Sprintf("%s_stable_access_token_%s", ak.cacheKeyPrefix, ak.appID)
|
||||||
if val := ak.cache.Get(accessTokenCacheKey); val != nil {
|
if val := ak.cache.Get(accessTokenCacheKey); val != nil {
|
||||||
return val.(string), nil
|
if accessToken = val.(string); accessToken != "" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 加上lock,是为了防止在并发获取token时,cache刚好失效,导致从微信服务器上获取到不同token
|
||||||
|
ak.accessTokenLock.Lock()
|
||||||
|
defer ak.accessTokenLock.Unlock()
|
||||||
|
|
||||||
|
// 双检,防止重复从微信服务器获取
|
||||||
|
if val := ak.cache.Get(accessTokenCacheKey); val != nil {
|
||||||
|
if accessToken = val.(string); accessToken != "" {
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// cache失效,从微信服务器获取
|
// cache失效,从微信服务器获取
|
||||||
|
|||||||
@@ -14,4 +14,5 @@ type Config struct {
|
|||||||
Token string `json:"token"` // token
|
Token string `json:"token"` // token
|
||||||
EncodingAESKey string `json:"encoding_aes_key"` // EncodingAESKey
|
EncodingAESKey string `json:"encoding_aes_key"` // EncodingAESKey
|
||||||
Cache cache.Cache
|
Cache cache.Cache
|
||||||
|
UseStableAK bool // use the stable access_token
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,7 +34,13 @@ type MiniProgram struct {
|
|||||||
|
|
||||||
// NewMiniProgram 实例化小程序 API
|
// NewMiniProgram 实例化小程序 API
|
||||||
func NewMiniProgram(cfg *config.Config) *MiniProgram {
|
func NewMiniProgram(cfg *config.Config) *MiniProgram {
|
||||||
defaultAkHandle := credential.NewDefaultAccessToken(cfg.AppID, cfg.AppSecret, credential.CacheKeyMiniProgramPrefix, cfg.Cache)
|
var defaultAkHandle credential.AccessTokenContextHandle
|
||||||
|
const cacheKeyPrefix = credential.CacheKeyMiniProgramPrefix
|
||||||
|
if cfg.UseStableAK {
|
||||||
|
defaultAkHandle = credential.NewStableAccessToken(cfg.AppID, cfg.AppSecret, cacheKeyPrefix, cfg.Cache)
|
||||||
|
} else {
|
||||||
|
defaultAkHandle = credential.NewDefaultAccessToken(cfg.AppID, cfg.AppSecret, cacheKeyPrefix, cfg.Cache)
|
||||||
|
}
|
||||||
ctx := &context.Context{
|
ctx := &context.Context{
|
||||||
Config: cfg,
|
Config: cfg,
|
||||||
AccessTokenHandle: defaultAkHandle,
|
AccessTokenHandle: defaultAkHandle,
|
||||||
|
|||||||
@@ -11,4 +11,5 @@ type Config struct {
|
|||||||
Token string `json:"token"` // token
|
Token string `json:"token"` // token
|
||||||
EncodingAESKey string `json:"encoding_aes_key"` // EncodingAESKey
|
EncodingAESKey string `json:"encoding_aes_key"` // EncodingAESKey
|
||||||
Cache cache.Cache
|
Cache cache.Cache
|
||||||
|
UseStableAK bool // use the stable access_token
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -49,7 +49,13 @@ type OfficialAccount struct {
|
|||||||
|
|
||||||
// NewOfficialAccount 实例化公众号API
|
// NewOfficialAccount 实例化公众号API
|
||||||
func NewOfficialAccount(cfg *config.Config) *OfficialAccount {
|
func NewOfficialAccount(cfg *config.Config) *OfficialAccount {
|
||||||
defaultAkHandle := credential.NewDefaultAccessToken(cfg.AppID, cfg.AppSecret, credential.CacheKeyOfficialAccountPrefix, cfg.Cache)
|
var defaultAkHandle credential.AccessTokenContextHandle
|
||||||
|
const cacheKeyPrefix = credential.CacheKeyOfficialAccountPrefix
|
||||||
|
if cfg.UseStableAK {
|
||||||
|
defaultAkHandle = credential.NewStableAccessToken(cfg.AppID, cfg.AppSecret, cacheKeyPrefix, cfg.Cache)
|
||||||
|
} else {
|
||||||
|
defaultAkHandle = credential.NewDefaultAccessToken(cfg.AppID, cfg.AppSecret, cacheKeyPrefix, cfg.Cache)
|
||||||
|
}
|
||||||
ctx := &context.Context{
|
ctx := &context.Context{
|
||||||
Config: cfg,
|
Config: cfg,
|
||||||
AccessTokenHandle: defaultAkHandle,
|
AccessTokenHandle: defaultAkHandle,
|
||||||
|
|||||||
@@ -18,20 +18,23 @@ const (
|
|||||||
|
|
||||||
// ReceptionistOptions 添加接待人员请求参数
|
// ReceptionistOptions 添加接待人员请求参数
|
||||||
type ReceptionistOptions struct {
|
type ReceptionistOptions struct {
|
||||||
OpenKFID string `json:"open_kfid"` // 客服帐号ID
|
OpenKFID string `json:"open_kfid"` // 客服帐号ID
|
||||||
UserIDList []string `json:"userid_list"` // 接待人员userid列表。第三方应用填密文userid,即open_userid 可填充个数:1 ~ 100。超过100个需分批调用。
|
UserIDList []string `json:"userid_list"` // 接待人员userid列表。第三方应用填密文userid,即open_userid 可填充个数:1 ~ 100。超过100个需分批调用。
|
||||||
|
DepartmentIDList []int `json:"department_id_list"` // 接待人员部门id列表 可填充个数:0 ~ 100。超过100个需分批调用。
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReceptionistSchema 添加接待人员响应内容
|
// ReceptionistSchema 添加接待人员响应内容
|
||||||
type ReceptionistSchema struct {
|
type ReceptionistSchema struct {
|
||||||
util.CommonError
|
util.CommonError
|
||||||
ResultList []struct {
|
ResultList []struct {
|
||||||
UserID string `json:"userid"`
|
UserID string `json:"userid"`
|
||||||
|
DepartmentID int `json:"department_id"`
|
||||||
util.CommonError
|
util.CommonError
|
||||||
} `json:"result_list"`
|
} `json:"result_list"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReceptionistAdd 添加接待人员
|
// ReceptionistAdd 添加接待人员
|
||||||
|
// @see https://developer.work.weixin.qq.com/document/path/94646
|
||||||
func (r *Client) ReceptionistAdd(options ReceptionistOptions) (info ReceptionistSchema, err error) {
|
func (r *Client) ReceptionistAdd(options ReceptionistOptions) (info ReceptionistSchema, err error) {
|
||||||
var (
|
var (
|
||||||
accessToken string
|
accessToken string
|
||||||
@@ -49,10 +52,11 @@ func (r *Client) ReceptionistAdd(options ReceptionistOptions) (info Receptionist
|
|||||||
if info.ErrCode != 0 {
|
if info.ErrCode != 0 {
|
||||||
return info, NewSDKErr(info.ErrCode, info.ErrMsg)
|
return info, NewSDKErr(info.ErrCode, info.ErrMsg)
|
||||||
}
|
}
|
||||||
return info, nil
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReceptionistDel 删除接待人员
|
// ReceptionistDel 删除接待人员
|
||||||
|
// @see https://developer.work.weixin.qq.com/document/path/94647
|
||||||
func (r *Client) ReceptionistDel(options ReceptionistOptions) (info ReceptionistSchema, err error) {
|
func (r *Client) ReceptionistDel(options ReceptionistOptions) (info ReceptionistSchema, err error) {
|
||||||
var (
|
var (
|
||||||
accessToken string
|
accessToken string
|
||||||
@@ -72,19 +76,22 @@ func (r *Client) ReceptionistDel(options ReceptionistOptions) (info Receptionist
|
|||||||
if info.ErrCode != 0 {
|
if info.ErrCode != 0 {
|
||||||
return info, NewSDKErr(info.ErrCode, info.ErrMsg)
|
return info, NewSDKErr(info.ErrCode, info.ErrMsg)
|
||||||
}
|
}
|
||||||
return info, nil
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReceptionistListSchema 获取接待人员列表响应内容
|
// ReceptionistListSchema 获取接待人员列表响应内容
|
||||||
type ReceptionistListSchema struct {
|
type ReceptionistListSchema struct {
|
||||||
util.CommonError
|
util.CommonError
|
||||||
ReceptionistList []struct {
|
ReceptionistList []struct {
|
||||||
UserID string `json:"userid"` // 接待人员的userid。第三方应用获取到的为密文userid,即open_userid
|
UserID string `json:"userid"` // 接待人员的userid。第三方应用获取到的为密文userid,即open_userid
|
||||||
Status int `json:"status"` // 接待人员的接待状态。0:接待中,1:停止接待。第三方应用需具有“管理帐号、分配会话和收发消息”权限才可获取
|
Status int `json:"status"` // 接待人员的接待状态。0:接待中,1:停止接待。第三方应用需具有“管理帐号、分配会话和收发消息”权限才可获取
|
||||||
|
DepartmentID int `json:"department_id"` // 接待人员部门的id
|
||||||
|
StopType int `json:"stop_type"` // 接待人员的接待状态为「停止接待」的子类型。0:停止接待,1:暂时挂起
|
||||||
} `json:"servicer_list"`
|
} `json:"servicer_list"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReceptionistList 获取接待人员列表
|
// ReceptionistList 获取接待人员列表
|
||||||
|
// @see https://developer.work.weixin.qq.com/document/path/94645
|
||||||
func (r *Client) ReceptionistList(kfID string) (info ReceptionistListSchema, err error) {
|
func (r *Client) ReceptionistList(kfID string) (info ReceptionistListSchema, err error) {
|
||||||
var (
|
var (
|
||||||
accessToken string
|
accessToken string
|
||||||
@@ -104,5 +111,5 @@ func (r *Client) ReceptionistList(kfID string) (info ReceptionistListSchema, err
|
|||||||
if info.ErrCode != 0 {
|
if info.ErrCode != 0 {
|
||||||
return info, NewSDKErr(info.ErrCode, info.ErrMsg)
|
return info, NewSDKErr(info.ErrCode, info.ErrMsg)
|
||||||
}
|
}
|
||||||
return info, nil
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,6 +14,8 @@ const (
|
|||||||
uploadTempFile = "https://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token=%s&type=%s"
|
uploadTempFile = "https://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token=%s&type=%s"
|
||||||
// uploadAttachment 上传附件资源
|
// uploadAttachment 上传附件资源
|
||||||
uploadAttachment = "https://qyapi.weixin.qq.com/cgi-bin/media/upload_attachment?access_token=%s&media_type=%s&attachment_type=%d"
|
uploadAttachment = "https://qyapi.weixin.qq.com/cgi-bin/media/upload_attachment?access_token=%s&media_type=%s&attachment_type=%d"
|
||||||
|
// getTempFile 获取临时素材
|
||||||
|
getTempFile = "https://qyapi.weixin.qq.com/cgi-bin/media/get?access_token=%s&media_id=%s"
|
||||||
)
|
)
|
||||||
|
|
||||||
// UploadImgResponse 上传图片响应
|
// UploadImgResponse 上传图片响应
|
||||||
@@ -148,3 +150,21 @@ func (r *Client) UploadAttachmentFromReader(filename, mediaType string, reader i
|
|||||||
err = util.DecodeWithError(response, result, "UploadAttachment")
|
err = util.DecodeWithError(response, result, "UploadAttachment")
|
||||||
return result, err
|
return result, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetTempFile 获取临时素材
|
||||||
|
// @see https://developer.work.weixin.qq.com/document/path/90254
|
||||||
|
func (r *Client) GetTempFile(mediaID string) ([]byte, error) {
|
||||||
|
var (
|
||||||
|
accessToken string
|
||||||
|
err error
|
||||||
|
)
|
||||||
|
if accessToken, err = r.GetAccessToken(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
url := fmt.Sprintf(getTempFile, accessToken, mediaID)
|
||||||
|
response, err := util.HTTPGet(url)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return response, nil
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user