|
@@ -3,12 +3,12 @@ package wx
|
|
|
import (
|
|
import (
|
|
|
"git.qianqiusoft.com/qianqiusoft/light-apiengine/config"
|
|
"git.qianqiusoft.com/qianqiusoft/light-apiengine/config"
|
|
|
"git.qianqiusoft.com/qianqiusoft/light-apiengine/entitys"
|
|
"git.qianqiusoft.com/qianqiusoft/light-apiengine/entitys"
|
|
|
- "strconv"
|
|
|
|
|
|
|
+ "sync"
|
|
|
"time"
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/silenceper/wechat"
|
|
"github.com/silenceper/wechat"
|
|
|
- "github.com/silenceper/wechat/oauth"
|
|
|
|
|
"github.com/silenceper/wechat/cache"
|
|
"github.com/silenceper/wechat/cache"
|
|
|
|
|
+ "github.com/silenceper/wechat/oauth"
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
var wc *wechat.Wechat
|
|
var wc *wechat.Wechat
|
|
@@ -29,7 +29,7 @@ var wxConfig struct {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
func init() {
|
|
|
- c := &LocalCache{}
|
|
|
|
|
|
|
+ c := getLocalCache()
|
|
|
wxConfig.WxPayCallback = config.AppConfig.GetKey("wxpay_callback") // 完整微信回调url,
|
|
wxConfig.WxPayCallback = config.AppConfig.GetKey("wxpay_callback") // 完整微信回调url,
|
|
|
wxConfig.AppId = config.AppConfig.GetKey("appid") // 公众号appid
|
|
wxConfig.AppId = config.AppConfig.GetKey("appid") // 公众号appid
|
|
|
wxConfig.AppSecret = config.AppConfig.GetKey("appsecret") // 公众号appsecret
|
|
wxConfig.AppSecret = config.AppConfig.GetKey("appsecret") // 公众号appsecret
|
|
@@ -50,37 +50,47 @@ func init() {
|
|
|
/*********************local cache*************************/
|
|
/*********************local cache*************************/
|
|
|
type LocalCache struct {
|
|
type LocalCache struct {
|
|
|
cache.Cache
|
|
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
|
|
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
|
|
return nil
|
|
|
}
|
|
}
|
|
|
/*********************************************************/
|
|
/*********************************************************/
|