互动
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
1.5 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. package client
  2. import (
  3. "fmt"
  4. "hudongzhuanjia/controllers"
  5. "hudongzhuanjia/libs/wx"
  6. "hudongzhuanjia/utils"
  7. "hudongzhuanjia/utils/define"
  8. "time"
  9. )
  10. type WxCtl struct {
  11. controllers.AuthorCtl
  12. }
  13. const SignUrl = "jsapi_ticket=%s&noncestr=%s&timestamp=%d&url=%s"
  14. type TicketCache struct {
  15. Url string `json:"url"`
  16. Token string `json:"token"`
  17. TokenExpired int64 `json:"ticket_expired"`
  18. Ticket string `json:"ticket"`
  19. TicketExpired int64 `json:"ticket_expired"`
  20. }
  21. var cache TicketCache
  22. // 检测token是否过期
  23. // 检测ticket是否过期
  24. func (t *WxCtl) Ticket() {
  25. url := t.MustGet("url")
  26. nonceStr := utils.RandomStr(32)
  27. timestamp := time.Now().Unix()
  28. if cache.TokenExpired <= time.Now().Unix() {
  29. token, err := wx.GetTicketToken()
  30. t.CheckErr(err)
  31. if token.ErrCode != 0 {
  32. t.ERROR(token.ErrMsg, token.ErrCode)
  33. }
  34. cache.Token = token.AccessToken
  35. cache.TokenExpired = token.ExpiresIn + 7200
  36. }
  37. if cache.TicketExpired <= time.Now().Unix() {
  38. ticket, err := wx.GetTicket(cache.Token)
  39. t.CheckErr(err)
  40. if ticket.ErrCode != 0 {
  41. t.ERROR(ticket.ErrMsg, ticket.ErrCode)
  42. }
  43. cache.TicketExpired = ticket.ExpiresIn + 7200
  44. cache.Ticket = ticket.Ticket
  45. }
  46. query := fmt.Sprintf(SignUrl, cache.Ticket, nonceStr, timestamp, url)
  47. signature := utils.Sha1(query)
  48. t.JSON(map[string]interface{}{
  49. "signature": signature,
  50. "noncestr": nonceStr,
  51. "timestamp": timestamp,
  52. "ticket": cache.Ticket,
  53. "appid": define.AppId,
  54. })
  55. }