mirror of
https://github.com/silenceper/wechat.git
synced 2026-02-08 14:42:26 +08:00
feat:企业微信客户端API JS-SDK wx.config 和 wx.agentConfig 方法权限签名 (#817)
* feat: enhance WorkAccessToken to include AgentID for improved token management - Added AgentID field to WorkAccessToken struct. - Updated NewWorkAccessToken function to accept AgentID as a parameter. - Modified access token cache key to incorporate AgentID, ensuring unique cache entries per agent. This change improves the handling of access tokens in a multi-agent environment. * refactor: enhance WorkAccessToken to improve cache key handling - Updated the AgentID field in WorkAccessToken struct to clarify its optional nature for distinguishing applications. - Modified the access token cache key construction to support both new and legacy formats based on the presence of AgentID. - Added comments for better understanding of the cache key logic and its compatibility with historical versions. This change improves the flexibility and clarity of access token management in multi-agent scenarios. * feat(work): add JsSdk method for JavaScript SDK integration - Introduced a new JsSdk method in the Work struct to facilitate the creation of a Js instance. - This addition enhances the functionality of the Work module by enabling JavaScript SDK support. This change improves the integration capabilities for developers working with the WeChat Work API. * fix gofmt
This commit is contained in:
73
work/jsapi/jsapi.go
Normal file
73
work/jsapi/jsapi.go
Normal file
@@ -0,0 +1,73 @@
|
||||
package jsapi
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
|
||||
"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
|
||||
jsTicket *credential.WorkJsTicket
|
||||
}
|
||||
|
||||
// NewJs init
|
||||
func NewJs(context *context.Context) *Js {
|
||||
js := new(Js)
|
||||
js.Context = context
|
||||
js.jsTicket = credential.NewWorkJsTicket(
|
||||
context.Config.CorpID,
|
||||
context.Config.AgentID,
|
||||
credential.CacheKeyWorkPrefix,
|
||||
context.Cache,
|
||||
)
|
||||
return js
|
||||
}
|
||||
|
||||
// Config 返回给用户使用的配置
|
||||
type Config struct {
|
||||
Timestamp int64 `json:"timestamp"`
|
||||
NonceStr string `json:"nonce_str"`
|
||||
Signature string `json:"signature"`
|
||||
}
|
||||
|
||||
// GetConfig 获取企业微信JS配置 https://developer.work.weixin.qq.com/document/path/90514
|
||||
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.jsTicket.GetTicket(accessToken, credential.TicketTypeCorpJs)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
config.NonceStr = util.RandomStr(16)
|
||||
config.Timestamp = util.GetCurrTS()
|
||||
config.Signature = util.Signature(ticketStr, config.NonceStr, strconv.FormatInt(config.Timestamp, 10), uri)
|
||||
return
|
||||
}
|
||||
|
||||
// GetAgentConfig 获取企业微信应用JS配置 https://developer.work.weixin.qq.com/document/path/94313
|
||||
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.jsTicket.GetTicket(accessToken, credential.TicketTypeAgentJs)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
config.NonceStr = util.RandomStr(16)
|
||||
config.Timestamp = util.GetCurrTS()
|
||||
config.Signature = util.Signature(ticketStr, config.NonceStr, strconv.FormatInt(config.Timestamp, 10), uri)
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user