mirror of
https://github.com/silenceper/wechat.git
synced 2026-02-04 12:52:27 +08:00
* [feature] Format the code and improve Mini Program authorization to obtain openid(miniprogram/auth/auth.go Code2Session) * [feature] CheckEncryptedData (https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/user-info/auth.checkEncryptedData.html) * upgrade json error * upgrade json error Co-authored-by: houseme <houseme@outlook.com>
58 lines
1.4 KiB
Go
58 lines
1.4 KiB
Go
package js
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/silenceper/wechat/v2/credential"
|
|
"github.com/silenceper/wechat/v2/officialaccount/context"
|
|
officialJs "github.com/silenceper/wechat/v2/officialaccount/js"
|
|
"github.com/silenceper/wechat/v2/util"
|
|
)
|
|
|
|
// Js wx jssdk
|
|
type Js struct {
|
|
*context.Context
|
|
credential.JsTicketHandle
|
|
}
|
|
|
|
// NewJs init
|
|
func NewJs(context *context.Context, appID string) *Js {
|
|
js := new(Js)
|
|
js.Context = context
|
|
jsTicketHandle := credential.NewDefaultJsTicket(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, appid string) (config *officialJs.Config, err error) {
|
|
config = new(officialJs.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.AppID = appid
|
|
config.NonceStr = nonceStr
|
|
config.Timestamp = timestamp
|
|
config.Signature = sigStr
|
|
return
|
|
}
|