|
@@ -2,8 +2,11 @@ package wechat
|
|
|
|
|
|
import (
|
|
import (
|
|
"encoding/json"
|
|
"encoding/json"
|
|
|
|
+ "fmt"
|
|
"net/url"
|
|
"net/url"
|
|
"time"
|
|
"time"
|
|
|
|
+
|
|
|
|
+ "git.chuangxin1.com/cx/util"
|
|
)
|
|
)
|
|
|
|
|
|
// Client wechat
|
|
// Client wechat
|
|
@@ -15,6 +18,9 @@ type Client struct {
|
|
|
|
|
|
AccessToken string
|
|
AccessToken string
|
|
LastTokenTime int64
|
|
LastTokenTime int64
|
|
|
|
+
|
|
|
|
+ Ticket string
|
|
|
|
+ LastTicketTime int64
|
|
}
|
|
}
|
|
|
|
|
|
// FormAuthorize get code
|
|
// FormAuthorize get code
|
|
@@ -136,6 +142,15 @@ type TemplateMessage struct {
|
|
Data TemplateData `json:"data"`
|
|
Data TemplateData `json:"data"`
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+// SignPackage sign package
|
|
|
|
+type SignPackage struct {
|
|
|
|
+ AppID string `json:"appId"`
|
|
|
|
+ NonceStr string `json:"nonceStr"`
|
|
|
|
+ Timestamp int64 `json:"timestamp"`
|
|
|
|
+ Signature string `json:"signature"`
|
|
|
|
+ URL string `json:"url"`
|
|
|
|
+}
|
|
|
|
+
|
|
func key(appid string) string {
|
|
func key(appid string) string {
|
|
return "wechat:client:" + appid
|
|
return "wechat:client:" + appid
|
|
}
|
|
}
|
|
@@ -291,6 +306,68 @@ func (wc Client) GetMaterial(mtype string, offset, count int) (res Material, err
|
|
return
|
|
return
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+func (wc *Client) getTicket(now int64) (ticket string, err error) {
|
|
|
|
+ var res Response
|
|
|
|
+ if wc.LastTicketTime > 0 {
|
|
|
|
+ if now-wc.LastTokenTime < TokenExpires {
|
|
|
|
+ ticket = wc.Ticket
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ args := url.Values{}
|
|
|
|
+ args.Add("access_token", wc.AccessToken)
|
|
|
|
+ args.Add("type", "jsapi")
|
|
|
|
+
|
|
|
|
+ uri := BaseURL + "/cgi-bin/ticket/getticket?"
|
|
|
|
+ uri += args.Encode()
|
|
|
|
+ if res, err = getJSON(uri); err == nil {
|
|
|
|
+ fmt.Println(res)
|
|
|
|
+ wc.LastTicketTime = now
|
|
|
|
+ wc.Ticket = res.Ticket
|
|
|
|
+
|
|
|
|
+ key := key(wc.AppID)
|
|
|
|
+ cache.Store(key, wc)
|
|
|
|
+
|
|
|
|
+ ticket = res.Ticket
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// GetSignPackage JS 签名
|
|
|
|
+// uri 当前 URL
|
|
|
|
+// nonceStr 随机字符串
|
|
|
|
+func (wc Client) GetSignPackage(uri, nonceStr string) (sign SignPackage, err error) {
|
|
|
|
+ if wc.AccessToken, err = wc.getToken(); err != nil {
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ var (
|
|
|
|
+ s string
|
|
|
|
+ unix int64
|
|
|
|
+ )
|
|
|
|
+ unix = time.Now().Unix()
|
|
|
|
+ if wc.Ticket, err = wc.getTicket(unix); err != nil {
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //fmt.Println(wc.Ticket)
|
|
|
|
+ s = `jsapi_ticket=` + wc.Ticket
|
|
|
|
+ s += `&noncestr=` + nonceStr
|
|
|
|
+ s += `×tamp=` + fmt.Sprintf("%d", unix)
|
|
|
|
+ s += `&url=` + uri
|
|
|
|
+
|
|
|
|
+ fmt.Println(s)
|
|
|
|
+ sign.AppID = wc.AppID
|
|
|
|
+ sign.NonceStr = nonceStr
|
|
|
|
+ sign.Signature = util.SHA1(s)
|
|
|
|
+ sign.Timestamp = unix
|
|
|
|
+ sign.URL = uri
|
|
|
|
+
|
|
|
|
+ return
|
|
|
|
+}
|
|
|
|
+
|
|
// SendTemplateMessage send template message
|
|
// SendTemplateMessage send template message
|
|
// POST /cgi-bin/message/template/send?access_token=ACCESS_TOKEN
|
|
// POST /cgi-bin/message/template/send?access_token=ACCESS_TOKEN
|
|
func (wc Client) SendTemplateMessage(template TemplateMessage) (res Response, err error) {
|
|
func (wc Client) SendTemplateMessage(template TemplateMessage) (res Response, err error) {
|