|
|
@@ -1,7 +1,19 @@
|
|
|
package jsapi
|
|
|
|
|
|
-import "dashoo.cn/opms_libary/plugin/dingtalk/base"
|
|
|
-import "dashoo.cn/opms_libary/plugin/dingtalk/context"
|
|
|
+import (
|
|
|
+ "crypto/sha1"
|
|
|
+ "encoding/hex"
|
|
|
+ "encoding/json"
|
|
|
+ "fmt"
|
|
|
+ "io/ioutil"
|
|
|
+ "net/http"
|
|
|
+ neturl "net/url"
|
|
|
+ "strconv"
|
|
|
+ "time"
|
|
|
+
|
|
|
+ "dashoo.cn/opms_libary/plugin/dingtalk/base"
|
|
|
+ "dashoo.cn/opms_libary/plugin/dingtalk/context"
|
|
|
+)
|
|
|
|
|
|
//Jsapi 包装
|
|
|
type Jsapi struct {
|
|
|
@@ -15,7 +27,103 @@ func NewJsapi(context *context.Context) *Jsapi {
|
|
|
return jsapi
|
|
|
}
|
|
|
|
|
|
-func (s *Jsapi) Sign(jsticket, nonceStr, url string, timeStamp int64) (result string, err error) {
|
|
|
+func (s *Jsapi) Sign(jsTicket, nonceStr, url string, timeStamp int64) (string, error) {
|
|
|
+ url, err := neturl.QueryUnescape(url)
|
|
|
+ if err != nil {
|
|
|
+ return "", fmt.Errorf("decode url 异常 %s", err.Error())
|
|
|
+ }
|
|
|
+
|
|
|
+ plain := "jsapi_ticket=" + jsTicket + "&noncestr=" + nonceStr + "×tamp=" + strconv.FormatInt(timeStamp, 10)+ "&url=" + url
|
|
|
+ sum := sha1.Sum([]byte(plain))
|
|
|
+ return hex.EncodeToString(sum[:]), nil
|
|
|
+}
|
|
|
+
|
|
|
+func (s *Jsapi) GetJsapiTicket(accessToken string) (string, error) {
|
|
|
+ cacheKey := fmt.Sprintf("jsapi_ticket_%s", s.Context.AppKey)
|
|
|
+ val, err := s.Context.Cache.Get(cacheKey)
|
|
|
+ if err != nil {
|
|
|
+ return "", fmt.Errorf("获取 jsapi_ticket 缓存异常 %s", err.Error())
|
|
|
+ }
|
|
|
+ if val != nil {
|
|
|
+ return val.(string), nil
|
|
|
+ }
|
|
|
|
|
|
- return
|
|
|
+ resp, err := s.GetTicketFromServer(accessToken)
|
|
|
+ if err != nil {
|
|
|
+ return "", err
|
|
|
+ }
|
|
|
+
|
|
|
+ err = s.Context.Cache.Set(cacheKey, resp.Ticket,
|
|
|
+ time.Second*time.Duration(resp.ExpiresIn-1500))
|
|
|
+ if err != nil {
|
|
|
+ return "", fmt.Errorf("设置 jsapi_ticket 缓存异常 %s", err.Error())
|
|
|
+ }
|
|
|
+ return resp.Ticket, nil
|
|
|
+}
|
|
|
+
|
|
|
+type JsapiTicketResp struct {
|
|
|
+ Errcode int `json:"errcode"`
|
|
|
+ Ticket string `json:"ticket"`
|
|
|
+ Errmsg string `json:"errmsg"`
|
|
|
+ ExpiresIn int `json:"expires_in"`
|
|
|
}
|
|
|
+
|
|
|
+//GetTicketFromServer 强制从服务器获取 ticket
|
|
|
+func (s *Jsapi) GetTicketFromServer(accessToken string) (JsapiTicketResp, error) {
|
|
|
+ resp := JsapiTicketResp{}
|
|
|
+ url := fmt.Sprintf("https://oapi.dingtalk.com/get_jsapi_ticket?access_token=%s", accessToken)
|
|
|
+ signResp, err := http.Get(url)
|
|
|
+ if err != nil {
|
|
|
+ return resp, fmt.Errorf("调用钉钉 get_jsapi_ticket http 请求异常 :%s", err.Error())
|
|
|
+ }
|
|
|
+ defer signResp.Body.Close()
|
|
|
+
|
|
|
+ b, err := ioutil.ReadAll(signResp.Body)
|
|
|
+ if err != nil {
|
|
|
+ return resp, fmt.Errorf("调用钉钉 get_jsapi_ticket 读取返回异常 :%s", err.Error())
|
|
|
+ }
|
|
|
+ fmt.Println(string(b))
|
|
|
+
|
|
|
+ err = json.Unmarshal(b, &resp)
|
|
|
+ if err != nil {
|
|
|
+ return resp, fmt.Errorf("调用钉钉 get_jsapi_ticket 解析 json 异常 %s:%s", string(b), err.Error())
|
|
|
+ }
|
|
|
+ if resp.Errcode != 0 {
|
|
|
+ return resp, fmt.Errorf("调用钉钉 get_jsapi_ticket 返回异常 %s:%s", string(b), err.Error())
|
|
|
+ }
|
|
|
+ return resp, nil
|
|
|
+}
|
|
|
+
|
|
|
+// type DingTokenResp struct {
|
|
|
+// Errcode int `json:"errcode"`
|
|
|
+// AccessToken string `json:"access_token"`
|
|
|
+// Errmsg string `json:"errmsg"`
|
|
|
+// ExpiresIn int `json:"expires_in"`
|
|
|
+// }
|
|
|
+
|
|
|
+// func AccessToken(){
|
|
|
+// url := fmt.Sprintf(
|
|
|
+// "https://oapi.dingtalk.com/gettoken?appkey=%s&appsecret=%s",
|
|
|
+// dingtalk.Client.Context.AppKey,
|
|
|
+// dingtalk.Client.Context.AppSecret)
|
|
|
+// signResp, err := http.Get(url)
|
|
|
+// if err != nil {
|
|
|
+// return fmt.Errorf("调用钉钉 gettoken http 请求异常 :%s", err.Error())
|
|
|
+// }
|
|
|
+// defer signResp.Body.Close()
|
|
|
+
|
|
|
+// b, err := ioutil.ReadAll(signResp.Body)
|
|
|
+// if err != nil {
|
|
|
+// return fmt.Errorf("调用钉钉 gettoken 读取返回异常 :%s", err.Error())
|
|
|
+// }
|
|
|
+// fmt.Println(string(b))
|
|
|
+
|
|
|
+// tokenResp := DingTokenResp{}
|
|
|
+// err = json.Unmarshal(b, &tokenResp)
|
|
|
+// if err != nil {
|
|
|
+// return fmt.Errorf("调用钉钉 gettoken 解析 json 异常 %s:%s", string(b), err.Error())
|
|
|
+// }
|
|
|
+// if tokenResp.Errcode != 0 {
|
|
|
+// return fmt.Errorf("调用钉钉 gettoken 返回异常 %s:%s", string(b), err.Error())
|
|
|
+// }
|
|
|
+// }
|