jsapi.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package jsapi
  2. import (
  3. "crypto/sha1"
  4. "encoding/hex"
  5. "encoding/json"
  6. "fmt"
  7. "io/ioutil"
  8. "net/http"
  9. neturl "net/url"
  10. "strconv"
  11. "time"
  12. "dashoo.cn/opms_libary/plugin/dingtalk/base"
  13. "dashoo.cn/opms_libary/plugin/dingtalk/context"
  14. )
  15. //Jsapi 包装
  16. type Jsapi struct {
  17. base.Base
  18. }
  19. //NewJsapi init
  20. func NewJsapi(context *context.Context) *Jsapi {
  21. jsapi := new(Jsapi)
  22. jsapi.Context = context
  23. return jsapi
  24. }
  25. func (s *Jsapi) Sign(jsTicket, nonceStr, url string, timeStamp int64) (string, error) {
  26. url, err := neturl.QueryUnescape(url)
  27. if err != nil {
  28. return "", fmt.Errorf("decode url 异常 %s", err.Error())
  29. }
  30. plain := "jsapi_ticket=" + jsTicket + "&noncestr=" + nonceStr + "&timestamp=" + strconv.FormatInt(timeStamp, 10) + "&url=" + url
  31. sum := sha1.Sum([]byte(plain))
  32. return hex.EncodeToString(sum[:]), nil
  33. }
  34. func (s *Jsapi) GetJsapiTicket(accessToken string) (string, error) {
  35. cacheKey := fmt.Sprintf("jsapi_ticket_%s", s.Context.AppKey)
  36. val, err := s.Context.Cache.Get(cacheKey)
  37. if err != nil {
  38. return "", fmt.Errorf("获取 jsapi_ticket 缓存异常 %s", err.Error())
  39. }
  40. if val != nil {
  41. return val.(string), nil
  42. }
  43. resp, err := s.GetTicketFromServer(accessToken)
  44. if err != nil {
  45. return "", err
  46. }
  47. err = s.Context.Cache.Set(cacheKey, resp.Ticket,
  48. time.Second*time.Duration(resp.ExpiresIn-1500))
  49. if err != nil {
  50. return "", fmt.Errorf("设置 jsapi_ticket 缓存异常 %s", err.Error())
  51. }
  52. return resp.Ticket, nil
  53. }
  54. type JsapiTicketResp struct {
  55. Errcode int `json:"errcode"`
  56. Ticket string `json:"ticket"`
  57. Errmsg string `json:"errmsg"`
  58. ExpiresIn int `json:"expires_in"`
  59. }
  60. //GetTicketFromServer 强制从服务器获取 ticket
  61. func (s *Jsapi) GetTicketFromServer(accessToken string) (JsapiTicketResp, error) {
  62. resp := JsapiTicketResp{}
  63. url := fmt.Sprintf("https://oapi.dingtalk.com/get_jsapi_ticket?access_token=%s", accessToken)
  64. signResp, err := http.Get(url)
  65. if err != nil {
  66. return resp, fmt.Errorf("调用钉钉 get_jsapi_ticket http 请求异常 :%s", err.Error())
  67. }
  68. defer signResp.Body.Close()
  69. b, err := ioutil.ReadAll(signResp.Body)
  70. if err != nil {
  71. return resp, fmt.Errorf("调用钉钉 get_jsapi_ticket 读取返回异常 :%s", err.Error())
  72. }
  73. fmt.Println(string(b))
  74. err = json.Unmarshal(b, &resp)
  75. if err != nil {
  76. return resp, fmt.Errorf("调用钉钉 get_jsapi_ticket 解析 json 异常 %s:%s", string(b), err.Error())
  77. }
  78. if resp.Errcode != 0 {
  79. return resp, fmt.Errorf("调用钉钉 get_jsapi_ticket 返回异常 %s:%s", string(b), err.Error())
  80. }
  81. return resp, nil
  82. }
  83. // type DingTokenResp struct {
  84. // Errcode int `json:"errcode"`
  85. // AccessToken string `json:"access_token"`
  86. // Errmsg string `json:"errmsg"`
  87. // ExpiresIn int `json:"expires_in"`
  88. // }
  89. // func AccessToken(){
  90. // url := fmt.Sprintf(
  91. // "https://oapi.dingtalk.com/gettoken?appkey=%s&appsecret=%s",
  92. // dingtalk.Client.Context.AppKey,
  93. // dingtalk.Client.Context.AppSecret)
  94. // signResp, err := http.Get(url)
  95. // if err != nil {
  96. // return fmt.Errorf("调用钉钉 gettoken http 请求异常 :%s", err.Error())
  97. // }
  98. // defer signResp.Body.Close()
  99. // b, err := ioutil.ReadAll(signResp.Body)
  100. // if err != nil {
  101. // return fmt.Errorf("调用钉钉 gettoken 读取返回异常 :%s", err.Error())
  102. // }
  103. // fmt.Println(string(b))
  104. // tokenResp := DingTokenResp{}
  105. // err = json.Unmarshal(b, &tokenResp)
  106. // if err != nil {
  107. // return fmt.Errorf("调用钉钉 gettoken 解析 json 异常 %s:%s", string(b), err.Error())
  108. // }
  109. // if tokenResp.Errcode != 0 {
  110. // return fmt.Errorf("调用钉钉 gettoken 返回异常 %s:%s", string(b), err.Error())
  111. // }
  112. // }