mirror of
https://github.com/silenceper/wechat.git
synced 2026-02-04 12:52:27 +08:00
* fix: platform wx oauth and js config issue. fix silenceper/wechat#301. * fix: platform query auth code error not detected. * feat: migrate platform oauth and js api. * fix: func name
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) *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, 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
|
|
}
|