1
0
mirror of https://github.com/silenceper/wechat.git synced 2026-02-04 12:52:27 +08:00
Files
wechat/officialaccount/js/js.go
mahongran 010e49c35c fix: work js-api signature (#818)
* 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

* refactor(jsapi): simplify signature generation in GetConfig and GetAgentConfig methods

- Replaced the use of strconv to format the signature string with fmt.Sprintf for improved readability.
- Updated the signature generation logic in both GetConfig and GetAgentConfig methods to use a single formatted string.

This change enhances code clarity and maintains functionality in the signature generation process.

* fix gofmt

* fix gofmt

* fix gofmt

* fix gofmt

* refactor(js): correct initialization of Config in GetConfig method

- Removed redundant initialization of the Config variable and ensured it is instantiated correctly before use.
- This change improves code clarity and maintains the intended functionality of the GetConfig method.
2025-01-14 17:24:42 +08:00

63 lines
1.5 KiB
Go

package js
import (
"fmt"
"github.com/silenceper/wechat/v2/credential"
"github.com/silenceper/wechat/v2/officialaccount/context"
"github.com/silenceper/wechat/v2/util"
)
// Js struct
type Js struct {
*context.Context
credential.JsTicketHandle
}
// Config 返回给用户jssdk配置信息
type Config struct {
AppID string `json:"app_id"`
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.NewDefaultJsTicket(context.AppID, credential.CacheKeyOfficialAccountPrefix, 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) {
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&timestamp=%d&url=%s", ticketStr, nonceStr, timestamp, uri)
sigStr := util.Signature(str)
config = new(Config)
config.AppID = js.AppID
config.NonceStr = nonceStr
config.Timestamp = timestamp
config.Signature = sigStr
return
}