mini.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package wechat
  2. import (
  3. "net/url"
  4. "time"
  5. "git.chuangxin1.com/cx/myth"
  6. )
  7. func keyMini(appid string) string {
  8. return "wechat:mini:client:" + appid
  9. }
  10. // NewMiniClient new mini client
  11. func NewMiniClient(appID, appSecret string) *MiniClient {
  12. key := keyMini(appID)
  13. if v, ok := cache.Load(key); ok {
  14. return v.(*MiniClient)
  15. }
  16. c := &MiniClient{AppID: appID, AppSecret: appSecret}
  17. cache.Store(key, c)
  18. return c
  19. }
  20. // GetMiniOpenID get openid by code
  21. func (mc *MiniClient) GetMiniOpenID(frm FormCode) (openid string, err error) {
  22. uri := BaseURL + "/sns/jscode2session?"
  23. args := url.Values{}
  24. args.Add("grant_type", "authorization_code")
  25. args.Add("appid", mc.AppID)
  26. args.Add("secret", mc.AppSecret)
  27. args.Add("js_code", frm.Code)
  28. uri += args.Encode()
  29. var jq *myth.JSONQuery
  30. if jq, err = getJSON(uri); err == nil {
  31. openid, err = jq.String(`openid`)
  32. }
  33. return
  34. }
  35. // getToken get token
  36. func (mc *MiniClient) getToken() (token string, err error) {
  37. now := time.Now().Unix()
  38. if mc.LastTokenTime > 0 {
  39. if now-mc.LastTokenTime < TokenExpires {
  40. token = mc.AccessToken
  41. return
  42. }
  43. }
  44. uri := BaseURL + "/cgi-bin/token?"
  45. args := url.Values{}
  46. args.Add("grant_type", "client_credential")
  47. args.Add("appid", mc.AppID)
  48. args.Add("secret", mc.AppSecret)
  49. uri += args.Encode()
  50. var jq *myth.JSONQuery
  51. if jq, err = getJSON(uri); err == nil {
  52. mc.LastTokenTime = now
  53. mc.AccessToken, err = jq.String(`access_token`)
  54. token = mc.AccessToken
  55. key := keyMini(mc.AppID)
  56. cache.Store(key, mc)
  57. }
  58. return
  59. }
  60. // TemplateList /cgi-bin/wxopen/template/list
  61. func (mc *MiniClient) TemplateList(offset, count int) (body []byte, err error) {
  62. uri := BaseURL + "/cgi-bin/wxopen/template/list?"
  63. if mc.AccessToken, err = mc.getToken(); err != nil {
  64. return
  65. }
  66. args := url.Values{}
  67. args.Add("access_token", mc.AccessToken)
  68. uri += args.Encode()
  69. params := make(map[string]interface{})
  70. params["offset"] = offset
  71. params["count"] = count
  72. var msg myth.HTTPMessage
  73. msg, err = postJSON(uri, params)
  74. if err == nil {
  75. body = msg.Body
  76. }
  77. /*
  78. data, err := json.Marshal(params)
  79. if err != nil {
  80. return
  81. }
  82. body, err = postJSONBody(uri, data)
  83. // */
  84. return
  85. }
  86. // TemplateSend /cgi-bin/message/wxopen/template/send
  87. func (mc *MiniClient) TemplateSend(template MiniTemplateMessage) (jq *myth.JSONQuery, err error) {
  88. uri := BaseURL + "/cgi-bin/message/wxopen/template/send?"
  89. if mc.AccessToken, err = mc.getToken(); err != nil {
  90. return
  91. }
  92. args := url.Values{}
  93. args.Add("access_token", mc.AccessToken)
  94. uri += args.Encode()
  95. var msg myth.HTTPMessage
  96. msg, err = postJSON(uri, template)
  97. if err == nil {
  98. jq, err = msg.JSONQuery()
  99. }
  100. return
  101. }
  102. // UniformSend /cgi-bin/message/wxopen/template/uniform_send
  103. func (mc *MiniClient) UniformSend(template MiniUniformMessage) (jq *myth.JSONQuery, err error) {
  104. uri := BaseURL + "/cgi-bin/message/wxopen/template/uniform_send?"
  105. if mc.AccessToken, err = mc.getToken(); err != nil {
  106. return
  107. }
  108. args := url.Values{}
  109. args.Add("access_token", mc.AccessToken)
  110. uri += args.Encode()
  111. var msg myth.HTTPMessage
  112. msg, err = postJSON(uri, template)
  113. if err == nil {
  114. jq, err = msg.JSONQuery()
  115. }
  116. return
  117. }