refresh.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package dao
  2. import (
  3. "context"
  4. "encoding/hex"
  5. "fmt"
  6. "time"
  7. "go-common/app/service/main/passport-auth/model"
  8. xsql "go-common/library/database/sql"
  9. "go-common/library/log"
  10. )
  11. const (
  12. _getRefreshSQL = "SELECT mid,appid,refresh,token,expires FROM user_refresh_%s WHERE refresh = ? limit 1"
  13. )
  14. // Refresh get token by access_token
  15. func (d *Dao) Refresh(c context.Context, rk []byte, ct time.Time) (res *model.Refresh, err error) {
  16. row := d.db.QueryRow(c, fmt.Sprintf(_getRefreshSQL, formatRefreshSuffix(ct)), rk)
  17. res = new(model.Refresh)
  18. var refresh, token []byte
  19. if err = row.Scan(&res.Mid, &res.AppID, &refresh, &token, &res.Expires); err != nil {
  20. if err == xsql.ErrNoRows {
  21. res = nil
  22. err = nil
  23. } else {
  24. log.Error("row.Scan() error(%v)", err)
  25. }
  26. return
  27. }
  28. res.Refresh = hex.EncodeToString(refresh)
  29. res.Token = hex.EncodeToString(token)
  30. return
  31. }
  32. func formatRefreshSuffix(t time.Time) string {
  33. return formatByDate(t.Year(), int(t.Month()))
  34. }
  35. func formatByDate(year, month int) string {
  36. if month%2 == 1 {
  37. return fmt.Sprintf("%4d%02d", year, month)
  38. }
  39. return fmt.Sprintf("%4d%02d", year, month-1)
  40. }