mini.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. package wechat
  2. import (
  3. "encoding/json"
  4. "errors"
  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}
  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. /*
  50. now := time.Now().Unix()
  51. if mc.LastTokenTime > 0 {
  52. if now-mc.LastTokenTime < TokenExpires {
  53. token = mc.AccessToken
  54. return
  55. }
  56. }
  57. // */
  58. key := keyMiniToken(mc.AppID)
  59. var ct ClientToken
  60. s := ``
  61. now := time.Now().Unix()
  62. s, err = cache.Get(key)
  63. if err == nil {
  64. err = json.Unmarshal([]byte(s), &ct)
  65. if err != nil {
  66. return
  67. }
  68. if mc.LastTokenTime > 0 {
  69. if now-mc.LastTokenTime < TokenExpires {
  70. token = mc.AccessToken
  71. return
  72. }
  73. }
  74. }
  75. token, err = mc.UpdateToken()
  76. return
  77. }
  78. func (mc *MiniClient) UpdateToken() (token string, err error) {
  79. now := time.Now().Unix()
  80. uri := BaseURL + "/cgi-bin/token?"
  81. args := url.Values{}
  82. args.Add("grant_type", "client_credential")
  83. args.Add("appid", mc.AppID)
  84. args.Add("secret", mc.AppSecret)
  85. uri += args.Encode()
  86. var jq *myth.JSONQuery
  87. jq, err = getJSON(uri)
  88. if err != nil {
  89. return
  90. }
  91. key := keyMiniToken(mc.AppID)
  92. mc.LastTokenTime = now
  93. mc.AccessToken, err = jq.String(`access_token`)
  94. token = mc.AccessToken
  95. k := keyMini(mc.AppID)
  96. memcache.Store(k, mc)
  97. ct := ClientToken{AppID: mc.AppID, AccessToken: mc.AccessToken, LastTokenTime: now}
  98. bs, _ := json.Marshal(ct)
  99. cache.Set(key, string(bs), 0)
  100. return
  101. }
  102. // TemplateList /cgi-bin/wxopen/template/list
  103. func (mc *MiniClient) TemplateList(offset, count int) (body []byte, err error) {
  104. uri := BaseURL + "/cgi-bin/wxopen/template/list?"
  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. params := make(map[string]interface{})
  112. params["offset"] = offset
  113. params["count"] = count
  114. var msg myth.HTTPMessage
  115. msg, err = postJSON(uri, params)
  116. if err == nil {
  117. body = msg.Body
  118. }
  119. /*
  120. data, err := json.Marshal(params)
  121. if err != nil {
  122. return
  123. }
  124. body, err = postJSONBody(uri, data)
  125. // */
  126. return
  127. }
  128. // MiniPhoneInfo 手机信息
  129. type MiniPhoneInfo struct {
  130. ResponseMsg
  131. PhoneInfo struct {
  132. PhoneNumber string `json:"phoneNumber"`
  133. PurePhoneNumber string `json:"purePhoneNumber"`
  134. CountryCode string `json:"countryCode"`
  135. Watermark struct {
  136. Timestamp int64 `json:"timestamp"`
  137. AppID string `json:"appid"`
  138. } `json:"watermark"`
  139. } `json:"phone_info"`
  140. }
  141. // GetPhoneInfo 获取手机信息 /wxa/business/getuserphonenumber
  142. func (mc *MiniClient) GetPhoneInfo(frm FormCode) (res MiniPhoneInfo, err error) {
  143. uri := BaseURL + "/wxa/business/getuserphonenumber?"
  144. if mc.AccessToken, err = mc.getToken(); err != nil {
  145. return
  146. }
  147. args := url.Values{}
  148. args.Add("access_token", mc.AccessToken)
  149. uri += args.Encode()
  150. var msg myth.HTTPMessage
  151. msg, err = postJSON(uri, frm)
  152. if err != nil {
  153. return
  154. }
  155. err = msg.JSON(&res)
  156. if err != nil {
  157. return
  158. }
  159. if res.ErrCode != 0 {
  160. err = errors.New(res.ErrMsg)
  161. }
  162. return
  163. }
  164. // TemplateSend /cgi-bin/message/wxopen/template/send
  165. func (mc *MiniClient) TemplateSend(template MiniTemplateMessage) (jq *myth.JSONQuery, err error) {
  166. uri := BaseURL + "/cgi-bin/message/wxopen/template/send?"
  167. if mc.AccessToken, err = mc.getToken(); err != nil {
  168. return
  169. }
  170. args := url.Values{}
  171. args.Add("access_token", mc.AccessToken)
  172. uri += args.Encode()
  173. var msg myth.HTTPMessage
  174. msg, err = postJSON(uri, template)
  175. if err == nil {
  176. jq, err = msg.JSONQuery()
  177. }
  178. return
  179. }
  180. // UniformSend /cgi-bin/message/wxopen/template/uniform_send
  181. func (mc *MiniClient) UniformSend(template MiniUniformMessage) (jq *myth.JSONQuery, err error) {
  182. uri := BaseURL + "/cgi-bin/message/wxopen/template/uniform_send?"
  183. if mc.AccessToken, err = mc.getToken(); err != nil {
  184. return
  185. }
  186. args := url.Values{}
  187. args.Add("access_token", mc.AccessToken)
  188. uri += args.Encode()
  189. var msg myth.HTTPMessage
  190. msg, err = postJSON(uri, template)
  191. if err == nil {
  192. jq, err = msg.JSONQuery()
  193. }
  194. return
  195. }