-
Notifications
You must be signed in to change notification settings - Fork 12
/
sign-jsapi.go
52 lines (45 loc) · 1.19 KB
/
sign-jsapi.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
41
42
43
44
45
46
47
48
49
50
51
package ce
import (
"github.com/rosbit/go-wx-api/auth"
"github.com/rosbit/go-wx-api/tools"
"fmt"
"net/http"
)
// POST ${commonEndpoints.SignJSAPI}
// s=<service-name-in-conf>&u=<url-calling-jsapi-in-urlencoding>
func SignJSAPI(w http.ResponseWriter, r *http.Request) {
service := r.FormValue("s")
if service == "" {
writeError(w, http.StatusBadRequest, "s(ervice) parameter expected")
return
}
wxParams, ok := wxParamsCache[service]
if !ok {
writeError(w, http.StatusBadRequest, fmt.Sprintf("unknown service name %s", service))
return
}
url := r.FormValue("u")
if url == "" {
writeError(w, http.StatusBadRequest, "u(rl) parameter expected")
return
}
accessToken, err := wxauth.NewAccessTokenWithParams(wxParams).Get()
if err != nil {
writeError(w, http.StatusInternalServerError, err.Error())
return
}
nonce, timestamp, signature, err := wxtools.SignJSAPI(accessToken, url)
if err != nil {
writeError(w, http.StatusInternalServerError, err.Error())
return
}
writeJson(w, http.StatusOK, map[string]interface{}{
"code": http.StatusOK,
"msg": "OK",
"params": map[string]interface{}{
"nonce": nonce,
"timestamp": timestamp,
"signature": signature,
},
})
}