global.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. return o
  61. }
  62. }
  63. func (lc *LocalCache)Set(key string, val interface{}, timeout time.Duration) error{
  64. lc.mutex.Lock()
  65. defer lc.mutex.Unlock()
  66. lc.m[key] = val
  67. return nil
  68. }
  69. func (lc *LocalCache)IsExist(key string) bool{
  70. lc.mutex.Lock()
  71. defer lc.mutex.Unlock()
  72. _, e := lc.m[key]
  73. return e
  74. }
  75. func (lc *LocalCache)Delete(key string) error {
  76. lc.mutex.Lock()
  77. defer lc.mutex.Unlock()
  78. if _, e := lc.m[key]; e {
  79. delete(lc.m, key)
  80. }
  81. return nil
  82. }
  83. /*********************************************************/
  84. // 支付参数
  85. type PayParams struct{
  86. // 请求上下文
  87. Ctx *entitys.CtrlContext
  88. // 价格(以分为单位)
  89. TotalFee int
  90. // 商品描述,默认body
  91. Body string
  92. // 额外信息,默认位user_id
  93. Attach string
  94. // 交易单号(最长32位)
  95. OutTradeNo string
  96. // 用户openid,再jsapi支付下必填,其他的可不填
  97. OpenId string
  98. }
  99. // jsapy 支付配置,用于返回给网页设置api参数
  100. type JSAPIPayConfig struct {
  101. Timestamp string `json:"timeStamp"` // 此处用string,ios下用int是错误的
  102. NonceStr string `json:"nonceStr"`
  103. Package string `json:"package"`
  104. SignType string `json:"signType"`
  105. PaySign string `json:"paySign"`
  106. OutTradeNo string `json:"outTradeNo"`
  107. }
  108. // 微信oauth跳转信息,
  109. type WxOAuthConfig struct {
  110. AuthUrl string `json:"auth_url"`
  111. AppId string `json:"app_id"`
  112. // 给微信跳转的url
  113. RedirectUrl string `json:"redirect_url"`
  114. ResponseType string `json:"response_type"`
  115. Scope string `json:"scope"`
  116. State string `json:"state"`
  117. // 完整的微信Oauth的url
  118. OAuthRedirectUrl string `json:"oauth_redirect_url"`
  119. }