| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- package jsapi
- 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 {
- base.Base
- }
- //NewJsapi init
- func NewJsapi(context *context.Context) *Jsapi {
- jsapi := new(Jsapi)
- jsapi.Context = context
- return jsapi
- }
- 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
- }
- 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())
- // }
- // }
|