global.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package wx
  2. import (
  3. "git.qianqiusoft.com/qianqiusoft/light-apiengine/config"
  4. "git.qianqiusoft.com/qianqiusoft/light-apiengine/entitys"
  5. "sync"
  6. "time"
  7. "github.com/silenceper/wechat"
  8. "github.com/silenceper/wechat/cache"
  9. "github.com/silenceper/wechat/oauth"
  10. )
  11. var wc *wechat.Wechat
  12. var wxoauth *oauth.Oauth
  13. var wxConfig struct {
  14. WxPayCallback string // 完整微信支付回调url,
  15. AppId string // 公众号appid
  16. AppSecret string // 公众号appsecret
  17. Token string // 设置token
  18. EncodingAseKey string // ase key
  19. PayMchId string // 商户号id
  20. PayKey string // 商户支付设置的key
  21. RedirectUrl string // 微信Oauth完整跳转uri
  22. Scope string // 微信Oauth的scope
  23. ResponseType string // 微信oauth的response type
  24. State string // 微信oauth的state
  25. }
  26. func init() {
  27. c := getLocalCache()
  28. wxConfig.WxPayCallback = config.AppConfig.GetKey("wxpay_callback") // 完整微信回调url,
  29. wxConfig.AppId = config.AppConfig.GetKey("appid") // 公众号appid
  30. wxConfig.AppSecret = config.AppConfig.GetKey("appsecret") // 公众号appsecret
  31. wxConfig.Token = config.AppConfig.GetKey("token")
  32. wxConfig.EncodingAseKey = config.AppConfig.GetKey("encoding_aes_key")
  33. wxConfig.PayMchId = config.AppConfig.GetKey("mch_id") // 商户号id
  34. wxConfig.PayKey = config.AppConfig.GetKey("pay_key") // 商户支付设置的key
  35. wxConfig.RedirectUrl = config.AppConfig.GetKey("redirect_url")
  36. wxConfig.Scope = config.AppConfig.GetKey("scope")
  37. wxConfig.ResponseType = config.AppConfig.GetKey("response_type")
  38. wxConfig.State = config.AppConfig.GetKey("state")
  39. config := &wechat.Config{wxConfig.AppId, wxConfig.AppSecret, wxConfig.Token, wxConfig.EncodingAseKey, wxConfig.PayMchId, wxConfig.WxPayCallback, wxConfig.PayKey, c}
  40. wc = wechat.NewWechat(config)
  41. wxoauth = wc.GetOauth()
  42. }
  43. /*********************local cache*************************/
  44. type LocalCache struct {
  45. cache.Cache
  46. m map[string]interface{}
  47. mutex sync.Mutex
  48. }
  49. func getLocalCache()*LocalCache{
  50. lc := &LocalCache{}
  51. lc.m = make(map[string]interface{})
  52. return lc
  53. }
  54. func (lc *LocalCache)Get(key string) interface{}{
  55. lc.mutex.Lock()
  56. defer lc.mutex.Unlock()
  57. if o, e := lc.m[key]; !e{
  58. return nil
  59. }else{
  60. timing := lc.m[key + "timing"].(time.Time)
  61. if time.Now().Sub(timing) > 90 * time.Minute{
  62. // 过期返回nil
  63. return nil
  64. }
  65. return o
  66. }
  67. }
  68. func (lc *LocalCache)Set(key string, val interface{}, timeout time.Duration) error{
  69. lc.mutex.Lock()
  70. defer lc.mutex.Unlock()
  71. lc.m[key] = val
  72. lc.m[key + "timing"] = time.Now()
  73. return nil
  74. }
  75. func (lc *LocalCache)IsExist(key string) bool{
  76. lc.mutex.Lock()
  77. defer lc.mutex.Unlock()
  78. _, e := lc.m[key]
  79. return e
  80. }
  81. func (lc *LocalCache)Delete(key string) error {
  82. lc.mutex.Lock()
  83. defer lc.mutex.Unlock()
  84. if _, e := lc.m[key]; e {
  85. delete(lc.m, key)
  86. }
  87. return nil
  88. }
  89. /*********************************************************/
  90. // 支付参数
  91. type PayParams struct{
  92. // 请求上下文
  93. Ctx *entitys.CtrlContext
  94. // 价格(以分为单位)
  95. TotalFee int
  96. // 商品描述,默认body
  97. Body string
  98. // 额外信息,默认位user_id
  99. Attach string
  100. // 交易单号(最长32位)
  101. OutTradeNo string
  102. // 用户openid,再jsapi支付下必填,其他的可不填
  103. OpenId string
  104. }
  105. // jsapy 支付配置,用于返回给网页设置api参数
  106. type JSAPIPayConfig struct {
  107. Timestamp string `json:"timeStamp"` // 此处用string,ios下用int是错误的
  108. NonceStr string `json:"nonceStr"`
  109. Package string `json:"package"`
  110. SignType string `json:"signType"`
  111. PaySign string `json:"paySign"`
  112. OutTradeNo string `json:"outTradeNo"`
  113. }
  114. // 微信oauth跳转信息,
  115. type WxOAuthConfig struct {
  116. AuthUrl string `json:"auth_url"`
  117. AppId string `json:"app_id"`
  118. // 给微信跳转的url
  119. RedirectUrl string `json:"redirect_url"`
  120. ResponseType string `json:"response_type"`
  121. Scope string `json:"scope"`
  122. State string `json:"state"`
  123. // 完整的微信Oauth的url
  124. OAuthRedirectUrl string `json:"oauth_redirect_url"`
  125. }