123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- package wechat
- import (
- "net/url"
- "time"
- "git.chuangxin1.com/cx/myth"
- )
- func keyMini(appid string) string {
- return "wechat:mini:client:" + appid
- }
- // NewMiniClient new mini client
- func NewMiniClient(appID, appSecret string) *MiniClient {
- key := keyMini(appID)
- if v, ok := memcache.Load(key); ok {
- return v.(*MiniClient)
- }
- c := &MiniClient{AppID: appID, AppSecret: appSecret}
- memcache.Store(key, c)
- return c
- }
- // GetMiniOpenID get openid by code
- func (mc *MiniClient) GetMiniOpenID(frm FormCode) (openid string, err error) {
- uri := BaseURL + "/sns/jscode2session?"
- args := url.Values{}
- args.Add("grant_type", "authorization_code")
- args.Add("appid", mc.AppID)
- args.Add("secret", mc.AppSecret)
- args.Add("js_code", frm.Code)
- uri += args.Encode()
- var jq *myth.JSONQuery
- if jq, err = getJSON(uri); err == nil {
- openid, err = jq.String(`openid`)
- }
- return
- }
- // getToken get token
- func (mc *MiniClient) getToken() (token string, err error) {
- now := time.Now().Unix()
- if mc.LastTokenTime > 0 {
- if now-mc.LastTokenTime < TokenExpires {
- token = mc.AccessToken
- return
- }
- }
- uri := BaseURL + "/cgi-bin/token?"
- args := url.Values{}
- args.Add("grant_type", "client_credential")
- args.Add("appid", mc.AppID)
- args.Add("secret", mc.AppSecret)
- uri += args.Encode()
- var jq *myth.JSONQuery
- if jq, err = getJSON(uri); err == nil {
- mc.LastTokenTime = now
- mc.AccessToken, err = jq.String(`access_token`)
- token = mc.AccessToken
- key := keyMini(mc.AppID)
- memcache.Store(key, mc)
- }
- return
- }
- // TemplateList /cgi-bin/wxopen/template/list
- func (mc *MiniClient) TemplateList(offset, count int) (body []byte, err error) {
- uri := BaseURL + "/cgi-bin/wxopen/template/list?"
- if mc.AccessToken, err = mc.getToken(); err != nil {
- return
- }
- args := url.Values{}
- args.Add("access_token", mc.AccessToken)
- uri += args.Encode()
- params := make(map[string]interface{})
- params["offset"] = offset
- params["count"] = count
- var msg myth.HTTPMessage
- msg, err = postJSON(uri, params)
- if err == nil {
- body = msg.Body
- }
- /*
- data, err := json.Marshal(params)
- if err != nil {
- return
- }
- body, err = postJSONBody(uri, data)
- // */
- return
- }
- // TemplateSend /cgi-bin/message/wxopen/template/send
- func (mc *MiniClient) TemplateSend(template MiniTemplateMessage) (jq *myth.JSONQuery, err error) {
- uri := BaseURL + "/cgi-bin/message/wxopen/template/send?"
- if mc.AccessToken, err = mc.getToken(); err != nil {
- return
- }
- args := url.Values{}
- args.Add("access_token", mc.AccessToken)
- uri += args.Encode()
- var msg myth.HTTPMessage
- msg, err = postJSON(uri, template)
- if err == nil {
- jq, err = msg.JSONQuery()
- }
- return
- }
- // UniformSend /cgi-bin/message/wxopen/template/uniform_send
- func (mc *MiniClient) UniformSend(template MiniUniformMessage) (jq *myth.JSONQuery, err error) {
- uri := BaseURL + "/cgi-bin/message/wxopen/template/uniform_send?"
- if mc.AccessToken, err = mc.getToken(); err != nil {
- return
- }
- args := url.Values{}
- args.Add("access_token", mc.AccessToken)
- uri += args.Encode()
- var msg myth.HTTPMessage
- msg, err = postJSON(uri, template)
- if err == nil {
- jq, err = msg.JSONQuery()
- }
- return
- }
|