|
|
@@ -3,63 +3,94 @@ package wx
|
|
|
import (
|
|
|
"git.qianqiusoft.com/qianqiusoft/light-apiengine/config"
|
|
|
"git.qianqiusoft.com/qianqiusoft/light-apiengine/entitys"
|
|
|
- "strconv"
|
|
|
+ "sync"
|
|
|
"time"
|
|
|
|
|
|
"github.com/silenceper/wechat"
|
|
|
"github.com/silenceper/wechat/cache"
|
|
|
+ "github.com/silenceper/wechat/oauth"
|
|
|
)
|
|
|
|
|
|
var wc *wechat.Wechat
|
|
|
+var wxoauth *oauth.Oauth
|
|
|
+
|
|
|
+var wxConfig struct {
|
|
|
+ WxPayCallback string // 完整微信支付回调url,
|
|
|
+ AppId string // 公众号appid
|
|
|
+ AppSecret string // 公众号appsecret
|
|
|
+ Token string // 设置token
|
|
|
+ EncodingAseKey string // ase key
|
|
|
+ PayMchId string // 商户号id
|
|
|
+ PayKey string // 商户支付设置的key
|
|
|
+ RedirectUrl string // 微信Oauth完整跳转uri
|
|
|
+ Scope string // 微信Oauth的scope
|
|
|
+ ResponseType string // 微信oauth的response type
|
|
|
+ State string // 微信oauth的state
|
|
|
+}
|
|
|
|
|
|
func init() {
|
|
|
- c := &LocalCache{}
|
|
|
- wxpayCallback := config.AppConfig.GetKey("wxpay_callback") // 完整微信回调url,
|
|
|
- appId := config.AppConfig.GetKey("app_id") // 公众号appid
|
|
|
- appSecret := config.AppConfig.GetKey("app_secret") // 公众号app secret
|
|
|
- token := config.AppConfig.GetKey("token")
|
|
|
- encodingAseKey := config.AppConfig.GetKey("encoding_aes_key")
|
|
|
- payMchId := config.AppConfig.GetKey("mch_id") // 商户号id
|
|
|
- payKey := config.AppConfig.GetKey("pay_key") // 商户支付设置的key
|
|
|
-
|
|
|
- config := &wechat.Config{appId, appSecret, token, encodingAseKey, payMchId, wxpayCallback, payKey, c}
|
|
|
+ c := getLocalCache()
|
|
|
+ wxConfig.WxPayCallback = config.AppConfig.GetKey("wxpay_callback") // 完整微信回调url,
|
|
|
+ wxConfig.AppId = config.AppConfig.GetKey("appid") // 公众号appid
|
|
|
+ wxConfig.AppSecret = config.AppConfig.GetKey("appsecret") // 公众号appsecret
|
|
|
+ wxConfig.Token = config.AppConfig.GetKey("token")
|
|
|
+ wxConfig.EncodingAseKey = config.AppConfig.GetKey("encoding_aes_key")
|
|
|
+ wxConfig.PayMchId = config.AppConfig.GetKey("mch_id") // 商户号id
|
|
|
+ wxConfig.PayKey = config.AppConfig.GetKey("pay_key") // 商户支付设置的key
|
|
|
+ wxConfig.RedirectUrl = config.AppConfig.GetKey("redirect_url")
|
|
|
+ wxConfig.Scope = config.AppConfig.GetKey("scope")
|
|
|
+ wxConfig.ResponseType = config.AppConfig.GetKey("response_type")
|
|
|
+ wxConfig.State = config.AppConfig.GetKey("state")
|
|
|
+
|
|
|
+ config := &wechat.Config{wxConfig.AppId, wxConfig.AppSecret, wxConfig.Token, wxConfig.EncodingAseKey, wxConfig.PayMchId, wxConfig.WxPayCallback, wxConfig.PayKey, c}
|
|
|
wc = wechat.NewWechat(config)
|
|
|
+ wxoauth = wc.GetOauth()
|
|
|
}
|
|
|
|
|
|
/*********************local cache*************************/
|
|
|
type LocalCache struct {
|
|
|
cache.Cache
|
|
|
- m map[string]string
|
|
|
+ m map[string]interface{}
|
|
|
+ mutex sync.Mutex
|
|
|
}
|
|
|
|
|
|
-func (this *LocalCache) Get(key string) interface{} {
|
|
|
- if this.m == nil {
|
|
|
- return nil
|
|
|
- }
|
|
|
-
|
|
|
- tokenTime := this.m["timeout"]
|
|
|
- if tokenTime == "" {
|
|
|
- return nil
|
|
|
- }
|
|
|
+func getLocalCache()*LocalCache{
|
|
|
+ lc := &LocalCache{}
|
|
|
+ lc.m = make(map[string]interface{})
|
|
|
|
|
|
- tInt64, _ := strconv.ParseInt(tokenTime, 10, 64)
|
|
|
+ return lc
|
|
|
+}
|
|
|
+func (lc *LocalCache)Get(key string) interface{}{
|
|
|
+ lc.mutex.Lock()
|
|
|
+ defer lc.mutex.Unlock()
|
|
|
|
|
|
- if time.Now().After(time.Unix(tInt64, 0)) {
|
|
|
+ if o, e := lc.m[key]; !e{
|
|
|
return nil
|
|
|
+ }else{
|
|
|
+ return o
|
|
|
}
|
|
|
-
|
|
|
- return this.m[key]
|
|
|
}
|
|
|
+func (lc *LocalCache)Set(key string, val interface{}, timeout time.Duration) error{
|
|
|
+ lc.mutex.Lock()
|
|
|
+ defer lc.mutex.Unlock()
|
|
|
|
|
|
-func (this *LocalCache) Set(key string, val interface{}, timeout time.Duration) error {
|
|
|
- if this.m == nil {
|
|
|
- this.m = make(map[string]string)
|
|
|
- }
|
|
|
+ lc.m[key] = val
|
|
|
+ return nil
|
|
|
+}
|
|
|
+func (lc *LocalCache)IsExist(key string) bool{
|
|
|
+ lc.mutex.Lock()
|
|
|
+ defer lc.mutex.Unlock()
|
|
|
|
|
|
- t := time.Now().Add(timeout).Unix()
|
|
|
+ _, e := lc.m[key]
|
|
|
+ return e
|
|
|
+}
|
|
|
+func (lc *LocalCache)Delete(key string) error {
|
|
|
+ lc.mutex.Lock()
|
|
|
+ defer lc.mutex.Unlock()
|
|
|
|
|
|
- this.m["timeout"] = strconv.FormatInt(t, 10)
|
|
|
- this.m[key] = val.(string)
|
|
|
+ if _, e := lc.m[key]; e {
|
|
|
+ delete(lc.m, key)
|
|
|
+ }
|
|
|
return nil
|
|
|
}
|
|
|
/*********************************************************/
|
|
|
@@ -89,3 +120,16 @@ type PayParams struct{
|
|
|
PaySign string `json:"paySign"`
|
|
|
OutTradeNo string `json:"outTradeNo"`
|
|
|
}
|
|
|
+
|
|
|
+// 微信oauth跳转信息,
|
|
|
+type WxOAuthConfig struct {
|
|
|
+ AuthUrl string `json:"auth_url"`
|
|
|
+ AppId string `json:"app_id"`
|
|
|
+ // 给微信跳转的url
|
|
|
+ RedirectUrl string `json:"redirect_url"`
|
|
|
+ ResponseType string `json:"response_type"`
|
|
|
+ Scope string `json:"scope"`
|
|
|
+ State string `json:"state"`
|
|
|
+ // 完整的微信Oauth的url
|
|
|
+ OAuthRedirectUrl string `json:"oauth_redirect_url"`
|
|
|
+}
|