dao.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. package dao
  2. import (
  3. "context"
  4. "encoding/json"
  5. "go-common/app/interface/bbq/wechat/internal/conf"
  6. "go-common/app/interface/bbq/wechat/internal/model"
  7. "go-common/library/cache/redis"
  8. "go-common/library/log"
  9. "io/ioutil"
  10. "net/http"
  11. )
  12. // Dao dao
  13. type Dao struct {
  14. c *conf.Config
  15. // cache *fanout.Fanout
  16. redis *redis.Pool
  17. }
  18. // type _cache interface {
  19. // cache: -nullcache=&model.InviteCode{DeviceID:""} -check_null_code=$==nil||$.DeviceID==""
  20. // InviteCode(c context.Context, deviceID string) (*model.InviteCode, error)
  21. // }
  22. // var 常量
  23. var (
  24. api string
  25. jsapi string
  26. appid string
  27. secret string
  28. )
  29. // New init mysql db
  30. func New(c *conf.Config) (dao *Dao) {
  31. dao = &Dao{
  32. c: c,
  33. // cache: fanout.New("cache", fanout.Worker(1), fanout.Buffer(1024)),
  34. redis: redis.NewPool(c.Redis),
  35. }
  36. api = c.URLs.Weixin
  37. jsapi = c.URLs.Jsapi
  38. appid = c.Weixin.AppID
  39. secret = c.Weixin.Secret
  40. return
  41. }
  42. // Close close the resource.
  43. func (d *Dao) Close() {
  44. d.redis.Close()
  45. }
  46. // Ping dao ping
  47. func (d *Dao) Ping(c context.Context) (err error) {
  48. conn := d.redis.Get(c)
  49. _, err = conn.Do("SET", "PING", "PONG")
  50. conn.Close()
  51. log.V(1).Infov(c, log.KV("event", "redis_ping"))
  52. return
  53. }
  54. // TokenGet Get Token
  55. func (d *Dao) TokenGet(c context.Context) (token string, err error) {
  56. req, err := http.NewRequest("GET", api, nil)
  57. if err != nil {
  58. log.Error("weixin request error (%v)", err)
  59. }
  60. q := req.URL.Query()
  61. q.Add("grant_type", "client_credential")
  62. q.Add("appid", appid)
  63. q.Add("secret", secret)
  64. req.URL.RawQuery = q.Encode()
  65. var resp *http.Response
  66. resp, err = http.DefaultClient.Do(req)
  67. if err != nil {
  68. log.Error("weixin response error (%v)", err)
  69. }
  70. body, err := ioutil.ReadAll(resp.Body)
  71. defer resp.Body.Close()
  72. res := new(model.WXToken)
  73. err = json.Unmarshal(body, &res)
  74. if err != nil {
  75. log.Error("weixin token parse error (%v)", err)
  76. }
  77. token, err = d.TicketGet(c, res.Token, res.Expires)
  78. return
  79. }
  80. // TicketGet Get Ticket
  81. func (d *Dao) TicketGet(c context.Context, access string, expires int) (token string, err error) {
  82. req, err := http.NewRequest("GET", jsapi, nil)
  83. if err != nil {
  84. log.Error("ticket request error (%v)", err)
  85. }
  86. q := req.URL.Query()
  87. q.Add("access_token", access)
  88. q.Add("type", "jsapi")
  89. req.URL.RawQuery = q.Encode()
  90. var resp *http.Response
  91. resp, err = http.DefaultClient.Do(req)
  92. if err != nil {
  93. log.Error("ticket response error (%v)", err)
  94. }
  95. body, err := ioutil.ReadAll(resp.Body)
  96. defer resp.Body.Close()
  97. res := new(model.WXTicket)
  98. err = json.Unmarshal(body, &res)
  99. if err != nil {
  100. log.Error("ticket parse error (%v)", err)
  101. }
  102. token = res.Ticket
  103. err = d.TokenUpdate(c, token, expires)
  104. return
  105. }
  106. // TokenUpdate redis设置
  107. func (d *Dao) TokenUpdate(c context.Context, token string, expires int) (err error) {
  108. conn := d.redis.Get(c)
  109. defer conn.Close()
  110. _, err = conn.Do("setex", appid, expires, token)
  111. if err != nil {
  112. log.Errorv(c, log.KV("event", "redis_set"), log.KV("key", appid), log.KV("value", token))
  113. }
  114. return
  115. }
  116. // TokenGetLast 获取最新token
  117. func (d *Dao) TokenGetLast(c context.Context) (token string, err error) {
  118. conn := d.redis.Get(c)
  119. defer conn.Close()
  120. var data []byte
  121. if data, err = redis.Bytes(conn.Do("get", appid)); err != nil {
  122. if err == redis.ErrNil {
  123. err = nil
  124. log.V(1).Infov(c, log.KV("event", "redis_get"), log.KV("key", appid), log.KV("result", "not_found"))
  125. } else {
  126. log.Errorv(c, log.KV("event", "redis_get"), log.KV("key", appid))
  127. }
  128. return
  129. }
  130. token = string(data)
  131. return
  132. }