-
Notifications
You must be signed in to change notification settings - Fork 0
/
web.go
40 lines (33 loc) · 883 Bytes
/
web.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package wechat
import (
"errors"
"strings"
"github.com/sohaha/zlsgo/znet"
"github.com/sohaha/zlsgo/ztype"
)
type RouterOption struct {
Prefix string
JsapiTicketCallback func(*znet.Context, ztype.Map, error)
}
func (w *Engine) Router(r *znet.Engine, opt RouterOption) {
opt.Prefix = strings.TrimRight(opt.Prefix, "/")
r.GET(opt.Prefix+"/js_ticket", func(c *znet.Context) {
opt.JsapiTicketCallback(getJsapiTicket(w, c))
})
}
func getJsapiTicket(wx *Engine, c *znet.Context) (*znet.Context, ztype.Map, error) {
jsapiTicket, err := wx.GetJsapiTicket()
if err != nil {
return c, nil, errors.New(ErrorMsg(err))
}
url := c.Host(true)
jsSign, err := wx.GetJsSign(url)
if err != nil {
return c, nil, errors.New(ErrorMsg(err))
}
return c, map[string]interface{}{
"jsapiTicket": jsapiTicket,
"jsSign": jsSign,
"url": url,
}, nil
}