global.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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/cache"
  9. )
  10. var wc *wechat.Wechat
  11. func init() {
  12. c := &LocalCache{}
  13. wxpayCallback := config.AppConfig.GetKey("wxpay_callback") // 完整微信回调url,
  14. appId := config.AppConfig.GetKey("app_id") // 公众号appid
  15. appSecret := config.AppConfig.GetKey("app_secret") // 公众号app secret
  16. token := config.AppConfig.GetKey("token")
  17. encodingAseKey := config.AppConfig.GetKey("encoding_aes_key")
  18. payMchId := config.AppConfig.GetKey("mch_id") // 商户号id
  19. payKey := config.AppConfig.GetKey("pay_key") // 商户支付设置的key
  20. config := &wechat.Config{appId, appSecret, token, encodingAseKey, payMchId, wxpayCallback, payKey, c}
  21. wc = wechat.NewWechat(config)
  22. }
  23. /*********************local cache*************************/
  24. type LocalCache struct {
  25. cache.Cache
  26. m map[string]string
  27. }
  28. func (this *LocalCache) Get(key string) interface{} {
  29. if this.m == nil {
  30. return nil
  31. }
  32. tokenTime := this.m["timeout"]
  33. if tokenTime == "" {
  34. return nil
  35. }
  36. tInt64, _ := strconv.ParseInt(tokenTime, 10, 64)
  37. if time.Now().After(time.Unix(tInt64, 0)) {
  38. return nil
  39. }
  40. return this.m[key]
  41. }
  42. func (this *LocalCache) Set(key string, val interface{}, timeout time.Duration) error {
  43. if this.m == nil {
  44. this.m = make(map[string]string)
  45. }
  46. t := time.Now().Add(timeout).Unix()
  47. this.m["timeout"] = strconv.FormatInt(t, 10)
  48. this.m[key] = val.(string)
  49. return nil
  50. }
  51. /*********************************************************/
  52. // 支付参数
  53. type PayParams struct{
  54. // 请求上下文
  55. Ctx *entitys.CtrlContext
  56. // 价格(以分为单位)
  57. TotalFee int
  58. // 商品描述,默认body
  59. Body string
  60. // 额外信息,默认位user_id
  61. Attach string
  62. // 交易单号(最长36位)
  63. OutTradeNo string
  64. // 用户openid,再jsapi支付下必填,其他的可不填
  65. OpenId string
  66. }
  67. // jsapy 支付配置,用于返回给网页设置api参数
  68. type JSAPIPayConfig struct {
  69. Timestamp string `json:"timeStamp"` // 此处用string,ios下用int是错误的
  70. NonceStr string `json:"nonceStr"`
  71. Package string `json:"package"`
  72. SignType string `json:"signType"`
  73. PaySign string `json:"paySign"`
  74. OutTradeNo string `json:"outTradeNo"`
  75. }