package wx import ( "git.qianqiusoft.com/qianqiusoft/light-apiengine/config" "git.qianqiusoft.com/qianqiusoft/light-apiengine/entitys" "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 := 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]interface{} mutex sync.Mutex } func getLocalCache()*LocalCache{ lc := &LocalCache{} lc.m = make(map[string]interface{}) return lc } func (lc *LocalCache)Get(key string) interface{}{ lc.mutex.Lock() defer lc.mutex.Unlock() if o, e := lc.m[key]; !e{ return nil }else{ timing := lc.m[key + "timing"].(time.Time) if time.Now().Sub(timing) > 90 * time.Minute{ // 过期返回nil return nil } return o } } func (lc *LocalCache)Set(key string, val interface{}, timeout time.Duration) error{ lc.mutex.Lock() defer lc.mutex.Unlock() lc.m[key] = val lc.m[key + "timing"] = time.Now() return nil } func (lc *LocalCache)IsExist(key string) bool{ lc.mutex.Lock() defer lc.mutex.Unlock() _, e := lc.m[key] return e } func (lc *LocalCache)Delete(key string) error { lc.mutex.Lock() defer lc.mutex.Unlock() if _, e := lc.m[key]; e { delete(lc.m, key) } return nil } /*********************************************************/ // 支付参数 type PayParams struct{ // 请求上下文 Ctx *entitys.CtrlContext // 价格(以分为单位) TotalFee int // 商品描述,默认body Body string // 额外信息,默认位user_id Attach string // 交易单号(最长32位) OutTradeNo string // 用户openid,再jsapi支付下必填,其他的可不填 OpenId string } // jsapy 支付配置,用于返回给网页设置api参数 type JSAPIPayConfig struct { Timestamp string `json:"timeStamp"` // 此处用string,ios下用int是错误的 NonceStr string `json:"nonceStr"` Package string `json:"package"` SignType string `json:"signType"` 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"` }