global.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package wx
  2. import (
  3. "git.qianqiusoft.com/qianqiusoft/light-apiengine/config"
  4. "git.qianqiusoft.com/qianqiusoft/light-apiengine/entitys"
  5. "strconv"
  6. "time"
  7. "github.com/silenceper/wechat"
  8. "github.com/silenceper/wechat/oauth"
  9. "github.com/silenceper/wechat/cache"
  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 := &LocalCache{}
  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]string
  47. }
  48. func (this *LocalCache) Get(key string) interface{} {
  49. if this.m == nil {
  50. return nil
  51. }
  52. tokenTime := this.m["timeout"]
  53. if tokenTime == "" {
  54. return nil
  55. }
  56. tInt64, _ := strconv.ParseInt(tokenTime, 10, 64)
  57. if time.Now().After(time.Unix(tInt64, 0)) {
  58. return nil
  59. }
  60. return this.m[key]
  61. }
  62. func (this *LocalCache) Set(key string, val interface{}, timeout time.Duration) error {
  63. if this.m == nil {
  64. this.m = make(map[string]string)
  65. }
  66. t := time.Now().Add(timeout).Unix()
  67. this.m["timeout"] = strconv.FormatInt(t, 10)
  68. this.m[key] = val.(string)
  69. return nil
  70. }
  71. /*********************************************************/
  72. // 支付参数
  73. type PayParams struct{
  74. // 请求上下文
  75. Ctx *entitys.CtrlContext
  76. // 价格(以分为单位)
  77. TotalFee int
  78. // 商品描述,默认body
  79. Body string
  80. // 额外信息,默认位user_id
  81. Attach string
  82. // 交易单号(最长32位)
  83. OutTradeNo string
  84. // 用户openid,再jsapi支付下必填,其他的可不填
  85. OpenId string
  86. }
  87. // jsapy 支付配置,用于返回给网页设置api参数
  88. type JSAPIPayConfig struct {
  89. Timestamp string `json:"timeStamp"` // 此处用string,ios下用int是错误的
  90. NonceStr string `json:"nonceStr"`
  91. Package string `json:"package"`
  92. SignType string `json:"signType"`
  93. PaySign string `json:"paySign"`
  94. OutTradeNo string `json:"outTradeNo"`
  95. }
  96. // 微信oauth跳转信息,
  97. type WxOAuthConfig struct {
  98. AuthUrl string `json:"auth_url"`
  99. AppId string `json:"app_id"`
  100. // 给微信跳转的url
  101. RedirectUrl string `json:"redirect_url"`
  102. ResponseType string `json:"response_type"`
  103. Scope string `json:"scope"`
  104. State string `json:"state"`
  105. // 完整的微信Oauth的url
  106. OAuthRedirectUrl string `json:"oauth_redirect_url"`
  107. }