global.go 4.2 KB

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