mirror of
https://github.com/silenceper/wechat.git
synced 2026-02-13 09:12:27 +08:00
Compare commits
4 Commits
feature/re
...
6658f71702
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6658f71702 | ||
|
|
990ba6ede9 | ||
|
|
44b09c7c3b | ||
|
|
61bcd6b0e4 |
85
credential/work_js_ticket.go
Normal file
85
credential/work_js_ticket.go
Normal file
@@ -0,0 +1,85 @@
|
|||||||
|
package credential
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/silenceper/wechat/v2/cache"
|
||||||
|
"github.com/silenceper/wechat/v2/util"
|
||||||
|
)
|
||||||
|
|
||||||
|
// 获取ticket的url https://developer.work.weixin.qq.com/document/path/90506
|
||||||
|
const getQyWxTicketURL = "https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token=%s"
|
||||||
|
const getQyAppTicketURL = "https://qyapi.weixin.qq.com/cgi-bin/ticket/get?access_token=%s&type=agent_config"
|
||||||
|
|
||||||
|
// WorkJsTicket 默认获取js ticket方法
|
||||||
|
type WorkJsTicket struct {
|
||||||
|
appID string
|
||||||
|
agentID string
|
||||||
|
cacheKeyPrefix string
|
||||||
|
cache cache.Cache
|
||||||
|
//jsAPITicket 读写锁 同一个AppID一个
|
||||||
|
jsAPITicketLock *sync.Mutex
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewWorkJsTicket new
|
||||||
|
func NewWorkJsTicket(appID string, agentID string, cacheKeyPrefix string, cache cache.Cache) JsTicketHandle {
|
||||||
|
return &WorkJsTicket{
|
||||||
|
appID: appID,
|
||||||
|
agentID: agentID,
|
||||||
|
cache: cache,
|
||||||
|
cacheKeyPrefix: cacheKeyPrefix,
|
||||||
|
jsAPITicketLock: new(sync.Mutex),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetTicket 获取企业微信jsapi_ticket
|
||||||
|
func (js *WorkJsTicket) GetTicket(accessToken string) (ticketStr string, err error) {
|
||||||
|
//先从cache中取
|
||||||
|
jsAPITicketCacheKey := fmt.Sprintf("%s_jsapi_ticket_%s", js.cacheKeyPrefix, js.appID)
|
||||||
|
if val := js.cache.Get(jsAPITicketCacheKey); val != nil {
|
||||||
|
return val.(string), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
js.jsAPITicketLock.Lock()
|
||||||
|
defer js.jsAPITicketLock.Unlock()
|
||||||
|
|
||||||
|
// 双检,防止重复从微信服务器获取
|
||||||
|
if val := js.cache.Get(jsAPITicketCacheKey); val != nil {
|
||||||
|
return val.(string), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var ticket ResTicket
|
||||||
|
ticket, err = GetQyWxTicketFromServer(accessToken, js.agentID != "")
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
expires := ticket.ExpiresIn - 1500
|
||||||
|
err = js.cache.Set(jsAPITicketCacheKey, ticket.Ticket, time.Duration(expires)*time.Second)
|
||||||
|
ticketStr = ticket.Ticket
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetQyWxTicketFromServer 从企业微信服务器中获取ticket
|
||||||
|
func GetQyWxTicketFromServer(accessToken string, isApp bool) (ticket ResTicket, err error) {
|
||||||
|
var response []byte
|
||||||
|
url := fmt.Sprintf(getQyWxTicketURL, accessToken)
|
||||||
|
if isApp {
|
||||||
|
url = fmt.Sprintf(getQyAppTicketURL, accessToken)
|
||||||
|
}
|
||||||
|
response, err = util.HTTPGet(url)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
err = json.Unmarshal(response, &ticket)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if ticket.ErrCode != 0 {
|
||||||
|
err = fmt.Errorf("getTicket Error : errcode=%d , errmsg=%s", ticket.ErrCode, ticket.ErrMsg)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
93
work/js/js.go
Normal file
93
work/js/js.go
Normal file
@@ -0,0 +1,93 @@
|
|||||||
|
package js
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/silenceper/wechat/v2/credential"
|
||||||
|
"github.com/silenceper/wechat/v2/util"
|
||||||
|
"github.com/silenceper/wechat/v2/work/context"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Js struct
|
||||||
|
type Js struct {
|
||||||
|
*context.Context
|
||||||
|
credential.JsTicketHandle
|
||||||
|
}
|
||||||
|
|
||||||
|
// Config 返回给用户jssdk配置信息
|
||||||
|
type Config struct {
|
||||||
|
CorpID string `json:"corp_id"`
|
||||||
|
Agentid string `json:"agentid"`
|
||||||
|
Timestamp int64 `json:"timestamp"`
|
||||||
|
NonceStr string `json:"nonce_str"`
|
||||||
|
Signature string `json:"signature"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewJs init
|
||||||
|
func NewJs(context *context.Context) *Js {
|
||||||
|
js := new(Js)
|
||||||
|
js.Context = context
|
||||||
|
jsTicketHandle := credential.NewWorkJsTicket(context.CorpID, context.AgentID, credential.CacheKeyWorkPrefix, context.Cache)
|
||||||
|
js.SetJsTicketHandle(jsTicketHandle)
|
||||||
|
return js
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetJsTicketHandle 自定义js ticket取值方式
|
||||||
|
func (js *Js) SetJsTicketHandle(ticketHandle credential.JsTicketHandle) {
|
||||||
|
js.JsTicketHandle = ticketHandle
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetConfig 获取jssdk需要的配置参数
|
||||||
|
// uri 为当前网页地址
|
||||||
|
func (js *Js) GetConfig(uri string) (config *Config, err error) {
|
||||||
|
config = new(Config)
|
||||||
|
var accessToken string
|
||||||
|
accessToken, err = js.GetAccessToken()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var ticketStr string
|
||||||
|
ticketStr, err = js.GetTicket(accessToken)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
nonceStr := util.RandomStr(16)
|
||||||
|
timestamp := util.GetCurrTS()
|
||||||
|
str := fmt.Sprintf("jsapi_ticket=%s&noncestr=%s×tamp=%d&url=%s", ticketStr, nonceStr, timestamp, uri)
|
||||||
|
sigStr := util.Signature(str)
|
||||||
|
|
||||||
|
config.CorpID = js.CorpID
|
||||||
|
config.Agentid = js.AgentID
|
||||||
|
config.NonceStr = nonceStr
|
||||||
|
config.Timestamp = timestamp
|
||||||
|
config.Signature = sigStr
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetAgentConfig 获取jssdk需要的配置参数
|
||||||
|
// uri 为当前网页地址
|
||||||
|
func (js *Js) GetAgentConfig(uri string) (config *Config, err error) {
|
||||||
|
config = new(Config)
|
||||||
|
var accessToken string
|
||||||
|
accessToken, err = js.GetAccessToken()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var ticketStr string
|
||||||
|
ticketStr, err = js.GetTicket(accessToken)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
nonceStr := util.RandomStr(16)
|
||||||
|
timestamp := util.GetCurrTS()
|
||||||
|
str := fmt.Sprintf("jsapi_ticket=%s&noncestr=%s×tamp=%d&url=%s", ticketStr, nonceStr, timestamp, uri)
|
||||||
|
sigStr := util.Signature(str)
|
||||||
|
|
||||||
|
config.CorpID = js.CorpID
|
||||||
|
config.NonceStr = nonceStr
|
||||||
|
config.Timestamp = timestamp
|
||||||
|
config.Signature = sigStr
|
||||||
|
return
|
||||||
|
}
|
||||||
@@ -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
|
||||||
|
}
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import (
|
|||||||
"github.com/silenceper/wechat/v2/work/context"
|
"github.com/silenceper/wechat/v2/work/context"
|
||||||
"github.com/silenceper/wechat/v2/work/externalcontact"
|
"github.com/silenceper/wechat/v2/work/externalcontact"
|
||||||
"github.com/silenceper/wechat/v2/work/invoice"
|
"github.com/silenceper/wechat/v2/work/invoice"
|
||||||
|
"github.com/silenceper/wechat/v2/work/js"
|
||||||
"github.com/silenceper/wechat/v2/work/kf"
|
"github.com/silenceper/wechat/v2/work/kf"
|
||||||
"github.com/silenceper/wechat/v2/work/material"
|
"github.com/silenceper/wechat/v2/work/material"
|
||||||
"github.com/silenceper/wechat/v2/work/message"
|
"github.com/silenceper/wechat/v2/work/message"
|
||||||
@@ -91,3 +92,8 @@ func (wk *Work) GetInvoice() *invoice.Client {
|
|||||||
func (wk *Work) GetCheckin() *checkin.Client {
|
func (wk *Work) GetCheckin() *checkin.Client {
|
||||||
return checkin.NewClient(wk.ctx)
|
return checkin.NewClient(wk.ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetJs js-sdk配置
|
||||||
|
func (wk *Work) GetJs() *js.Js {
|
||||||
|
return js.NewJs(wk.ctx)
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user