mini.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. package wechat
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/url"
  6. "time"
  7. "git.chuangxin1.com/cx/myth"
  8. "git.chuangxin1.com/cx/myth/cache"
  9. )
  10. func keyMini(appid string) string {
  11. return "wechat:mini:client:" + appid
  12. }
  13. func keyMiniToken(appid string) string {
  14. return "wechat:mini:client:token:" + appid
  15. }
  16. // NewMiniClient new mini client
  17. func NewMiniClient(appID, appSecret string) *MiniClient {
  18. key := keyMini(appID)
  19. if v, ok := memcache.Load(key); ok {
  20. return v.(*MiniClient)
  21. }
  22. c := &MiniClient{AppID: appID, AppSecret: appSecret, UseCacheToken: true}
  23. memcache.Store(key, c)
  24. return c
  25. }
  26. // TokenKey token key in cache
  27. func (mc *MiniClient) TokenKey() string {
  28. return keyMiniToken(mc.AppID)
  29. }
  30. // GetMiniOpenID get openid by code
  31. func (mc *MiniClient) GetMiniOpenID(frm FormCode) (s MiniSession, err error) {
  32. uri := BaseURL + "/sns/jscode2session?"
  33. args := url.Values{}
  34. args.Add("grant_type", "authorization_code")
  35. args.Add("appid", mc.AppID)
  36. args.Add("secret", mc.AppSecret)
  37. args.Add("js_code", frm.Code)
  38. uri += args.Encode()
  39. var jq *myth.JSONQuery
  40. if jq, err = getJSON(uri); err == nil {
  41. s.OpenID, err = jq.String(`openid`)
  42. s.SessionKey, _ = jq.String(`session_key`)
  43. s.UnionID, _ = jq.String(`unionid`)
  44. }
  45. return
  46. }
  47. // getToken get token
  48. func (mc *MiniClient) getToken() (token string, err error) {
  49. if mc.UseCacheToken {
  50. key := keyMiniToken(mc.AppID)
  51. var ct ClientToken
  52. s := ``
  53. //now := time.Now().Unix()
  54. s, err = cache.Get(key)
  55. if err != nil {
  56. return
  57. }
  58. err = json.Unmarshal([]byte(s), &ct)
  59. if err != nil {
  60. return
  61. }
  62. token = ct.AccessToken
  63. return
  64. }
  65. now := time.Now().Unix()
  66. if mc.LastTokenTime > 0 {
  67. if now-mc.LastTokenTime < TokenExpires {
  68. token = mc.AccessToken
  69. return
  70. }
  71. }
  72. token, err = mc.UpdateToken()
  73. return
  74. }
  75. func (mc *MiniClient) UpdateToken() (token string, err error) {
  76. now := time.Now().Unix()
  77. uri := BaseURL + "/cgi-bin/token?"
  78. args := url.Values{}
  79. args.Add("grant_type", "client_credential")
  80. args.Add("appid", mc.AppID)
  81. args.Add("secret", mc.AppSecret)
  82. uri += args.Encode()
  83. var jq *myth.JSONQuery
  84. jq, err = getJSON(uri)
  85. if err != nil {
  86. return
  87. }
  88. key := keyMiniToken(mc.AppID)
  89. mc.LastTokenTime = now
  90. mc.AccessToken, err = jq.String(`access_token`)
  91. token = mc.AccessToken
  92. k := keyMini(mc.AppID)
  93. memcache.Store(k, mc)
  94. ct := ClientToken{AppID: mc.AppID, AccessToken: mc.AccessToken, LastTokenTime: now}
  95. bs, _ := json.Marshal(ct)
  96. cache.Set(key, string(bs), 0)
  97. return
  98. }
  99. // TemplateList /cgi-bin/wxopen/template/list
  100. func (mc *MiniClient) TemplateList(offset, count int) (body []byte, err error) {
  101. uri := BaseURL + "/cgi-bin/wxopen/template/list?"
  102. if mc.AccessToken, err = mc.getToken(); err != nil {
  103. return
  104. }
  105. args := url.Values{}
  106. args.Add("access_token", mc.AccessToken)
  107. uri += args.Encode()
  108. params := make(map[string]interface{})
  109. params["offset"] = offset
  110. params["count"] = count
  111. var msg myth.HTTPMessage
  112. msg, err = postJSON(uri, params)
  113. if err == nil {
  114. body = msg.Body
  115. }
  116. /*
  117. data, err := json.Marshal(params)
  118. if err != nil {
  119. return
  120. }
  121. body, err = postJSONBody(uri, data)
  122. // */
  123. return
  124. }
  125. // MiniPhoneInfo 手机信息
  126. type MiniPhoneInfo struct {
  127. ResponseMsg
  128. PhoneInfo struct {
  129. PhoneNumber string `json:"phoneNumber"`
  130. PurePhoneNumber string `json:"purePhoneNumber"`
  131. CountryCode string `json:"countryCode"`
  132. Watermark struct {
  133. Timestamp int64 `json:"timestamp"`
  134. AppID string `json:"appid"`
  135. } `json:"watermark"`
  136. } `json:"phone_info"`
  137. }
  138. // GetPhoneInfo 获取手机信息 /wxa/business/getuserphonenumber
  139. func (mc *MiniClient) GetPhoneInfo(frm FormCode) (res MiniPhoneInfo, err error) {
  140. uri := BaseURL + "/wxa/business/getuserphonenumber?"
  141. if mc.AccessToken, err = mc.getToken(); err != nil {
  142. return
  143. }
  144. args := url.Values{}
  145. args.Add("access_token", mc.AccessToken)
  146. uri += args.Encode()
  147. var msg myth.HTTPMessage
  148. msg, err = postJSON(uri, frm)
  149. if err != nil {
  150. return
  151. }
  152. err = msg.JSON(&res)
  153. if err != nil {
  154. return
  155. }
  156. if res.ErrCode != 0 {
  157. err = fmt.Errorf("%s uri: %s", res.ErrMsg, uri)
  158. }
  159. return
  160. }
  161. // TemplateSend /cgi-bin/message/wxopen/template/send
  162. func (mc *MiniClient) TemplateSend(template MiniTemplateMessage) (jq *myth.JSONQuery, err error) {
  163. uri := BaseURL + "/cgi-bin/message/wxopen/template/send?"
  164. if mc.AccessToken, err = mc.getToken(); err != nil {
  165. return
  166. }
  167. args := url.Values{}
  168. args.Add("access_token", mc.AccessToken)
  169. uri += args.Encode()
  170. var msg myth.HTTPMessage
  171. msg, err = postJSON(uri, template)
  172. if err == nil {
  173. jq, err = msg.JSONQuery()
  174. }
  175. return
  176. }
  177. // UniformSend /cgi-bin/message/wxopen/template/uniform_send
  178. func (mc *MiniClient) UniformSend(template MiniUniformMessage) (jq *myth.JSONQuery, err error) {
  179. uri := BaseURL + "/cgi-bin/message/wxopen/template/uniform_send?"
  180. if mc.AccessToken, err = mc.getToken(); err != nil {
  181. return
  182. }
  183. args := url.Values{}
  184. args.Add("access_token", mc.AccessToken)
  185. uri += args.Encode()
  186. var msg myth.HTTPMessage
  187. msg, err = postJSON(uri, template)
  188. if err == nil {
  189. jq, err = msg.JSONQuery()
  190. }
  191. return
  192. }