Browse Source

save access token to cache

ls 3 years ago
parent
commit
4dbfb7200d
5 changed files with 71 additions and 86 deletions
  1. 6 0
      wechat/client.go
  2. 0 30
      wechat/pay.go
  3. 1 0
      wechat/pay_v3.go
  4. 33 56
      wechat/structure.go
  5. 31 0
      wechat/structure_v3.go

+ 6 - 0
wechat/client.go

@@ -14,6 +14,10 @@ func key(appid string) string {
 	return "wechat:client:" + appid
 }
 
+func keyToken(appid string) string {
+	return "wechat:client:token:" + appid
+}
+
 // NewClient new client
 func NewClient(appID, appSecret, token, encodingAESKey string) *Client {
 	key := key(appID)
@@ -53,6 +57,8 @@ func (wc *Client) getToken() (token string, err error) {
 		token = wc.AccessToken
 		key := key(wc.AppID)
 		cache.Store(key, wc)
+		key = keyToken(wc.AppID)
+		cache.Store(key, accessToken)
 	}
 	return
 }

+ 0 - 30
wechat/pay.go

@@ -47,36 +47,6 @@ type PayOrderQuery struct {
 	Sign          string   `xml:"sign"`
 }
 
-// FormPayNotify notify
-type FormPayNotify struct {
-	XMLName  xml.Name `xml:"xml" json:"_,omitempty"`
-	AppID    string   `form:"appid" xml:"appid"`
-	Attach   string   `form:"attach" xml:"attach"`
-	BankType string   `form:"bank_type" xml:"bank_type"`
-	CashFee  int      `form:"cash_fee" xml:"cash_fee"`
-	FeeType  string   `form:"fee_type" xml:"fee_type"`
-
-	MchID       string `form:"mch_id" xml:"mch_id"`
-	IsSubscribe string `form:"is_subscribe" xml:"is_subscribe"`
-	NonceStr    string `form:"nonce_str" xml:"nonce_str"`
-	OpenID      string `form:"openid" xml:"openid"`
-	OutTradeNo  string `form:"out_trade_no" xml:"out_trade_no"`
-
-	ResultCode string `form:"result_code" xml:"result_code"`
-	ReturnMsg  string `form:"return_msg" xml:"return_msg"`
-	ReturnCode string `form:"return_code" xml:"return_code"`
-	ErrCodeDes string `form:"err_code_des" xml:"err_code_des"`
-	ErrCode    string `form:"err_code" xml:"err_code"`
-
-	Sign     string `form:"sign" xml:"sign"`
-	TimeEnd  string `form:"time_end" xml:"time_end"`
-	TotalFee int    `form:"total_fee" xml:"total_fee"`
-
-	TradeType     string `form:"trade_type" xml:"trade_type"`
-	TransactionID string `form:"transaction_id" xml:"transaction_id"`
-	ContractID    string `form:"contract_id" xml:"contract_id"`
-}
-
 // PayReply pay reply
 type PayReply struct {
 	XMLName        xml.Name `xml:"xml" json:"_,omitempty"`

+ 1 - 0
wechat/pay_v3.go

@@ -0,0 +1 @@
+package wechat

+ 33 - 56
wechat/structure.go

@@ -13,8 +13,9 @@ type Client struct {
 
 	//ReadLock *sync.Mutex
 
-	AccessToken   string
-	LastTokenTime int64
+	AccessToken    string
+	LastTokenTime  int64
+	TokenFromCache bool
 
 	Ticket         string
 	LastTicketTime int64
@@ -71,42 +72,6 @@ type FormURLState struct {
 	State       string `form:"state" json:"state" xml:"state"`
 }
 
-/*
-// Response response
-type Response struct {
-	ErrCode int    `json:"errcode"`
-	ErrMsg  string `json:"errmsg"`
-
-	// token
-	AccessToken string `json:"access_token"`
-	ExpiresIn   int    `json:"expires_in"`
-
-	// openid
-	RefreshToken string `json:"refresh_token"`
-	OpenID       string `json:"openid"`
-	Scope        string `json:"scope"`
-
-	// user info
-	NickName   string `json:"nickname"`
-	Sex        string `json:"sex"`
-	Province   string `json:"province"`
-	City       string `json:"city"`
-	Country    string `json:"country"`
-	HeadImgURL string `json:"headimgurl"`
-	Privilege  string `json:"privilege"`
-	UnionID    string `json:"unionid"`
-
-	// template message
-	MsgID int64 `json:"msgid"`
-
-	// jsapi_ticket
-	Ticket string `json:"ticket"`
-
-	// mini get openid by code
-	SessionKey string `json:"session_key"`
-}
-// */
-
 // Message message
 type Message struct {
 	XMLName      xml.Name `xml:"xml"`
@@ -226,24 +191,6 @@ type ReplyMessage struct {
 	Articles     *articleItems `xml:"Articles,omitempty" json:"Articles,omitempty"`
 }
 
-/*
-<xml>
-  <ToUserName><![CDATA[toUser]]></ToUserName>
-  <FromUserName><![CDATA[fromUser]]></FromUserName>
-  <CreateTime>12345678</CreateTime>
-  <MsgType><![CDATA[news]]></MsgType>
-  <ArticleCount>1</ArticleCount>
-  <Articles>
-    <item>
-      <Title><![CDATA[title1]]></Title>
-      <Description><![CDATA[description1]]></Description>
-      <PicUrl><![CDATA[picurl]]></PicUrl>
-      <Url><![CDATA[url]]></Url>
-    </item>
-  </Articles>
-</xml>
-// */
-
 // EventTemplateReply event reply
 type EventTemplateReply struct {
 	XMLName      xml.Name `xml:"xml"`
@@ -428,3 +375,33 @@ type MiniUniformMessage struct {
 	WeApp  *WeappTemplateMessage `json:"weapp_template_msg,omitempty"`
 	MP     *MpTemplateMessage    `json:"mp_template_msg,omitempty"`
 }
+
+// FormPayNotify notify
+type FormPayNotify struct {
+	XMLName  xml.Name `xml:"xml" json:"_,omitempty"`
+	AppID    string   `form:"appid" xml:"appid"`
+	Attach   string   `form:"attach" xml:"attach"`
+	BankType string   `form:"bank_type" xml:"bank_type"`
+	CashFee  int      `form:"cash_fee" xml:"cash_fee"`
+	FeeType  string   `form:"fee_type" xml:"fee_type"`
+
+	MchID       string `form:"mch_id" xml:"mch_id"`
+	IsSubscribe string `form:"is_subscribe" xml:"is_subscribe"`
+	NonceStr    string `form:"nonce_str" xml:"nonce_str"`
+	OpenID      string `form:"openid" xml:"openid"`
+	OutTradeNo  string `form:"out_trade_no" xml:"out_trade_no"`
+
+	ResultCode string `form:"result_code" xml:"result_code"`
+	ReturnMsg  string `form:"return_msg" xml:"return_msg"`
+	ReturnCode string `form:"return_code" xml:"return_code"`
+	ErrCodeDes string `form:"err_code_des" xml:"err_code_des"`
+	ErrCode    string `form:"err_code" xml:"err_code"`
+
+	Sign     string `form:"sign" xml:"sign"`
+	TimeEnd  string `form:"time_end" xml:"time_end"`
+	TotalFee int    `form:"total_fee" xml:"total_fee"`
+
+	TradeType     string `form:"trade_type" xml:"trade_type"`
+	TransactionID string `form:"transaction_id" xml:"transaction_id"`
+	ContractID    string `form:"contract_id" xml:"contract_id"`
+}

+ 31 - 0
wechat/structure_v3.go

@@ -0,0 +1,31 @@
+package wechat
+
+/* ******************************************
+	HTTP Status code
+
+	200 ok
+	204 处理成功,无返回 Body
+	400 协议或者参数非法, PARAM_ERROR
+	401 签名验证失败, SIGN_ERROR
+	403 权限异常, NO_AUTH
+	404 请求的资源不存在, ORDER_NOT_EXIST
+	429 请求超过频率限制, RATELIMIT_EXCEEDED
+	500 系统错误, SYSTEM_ERROR
+	502 服务下线,暂时不可用, SERVICE_UNAVAILABLE
+	503 服务不可用,过载保护, SERVICE_UNAVAILABLE
+// ****************************************** */
+
+// WepayResponseDetail detail
+type WepayResponseDetail struct {
+	Field    string `json:"field"`
+	Value    string `json:"value"`
+	Issue    string `json:"issue"`
+	Location string `json:"location"`
+}
+
+// WepayResponseMessageV3 v3 返回消息头
+type WepayResponseMessageV3 struct {
+	Code    string              `json:"code"`
+	Message string              `json:"message"`
+	Detail  WepayResponseDetail `json:"detail"`
+}