token.go 886 B

1234567891011121314151617181920212223242526272829303132333435363738
  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. _getTokenSQL = "SELECT mid,appid,token,expires,type FROM user_token_%s where token = ? limit 1"
  13. )
  14. // Token get token by access_token
  15. func (d *Dao) Token(c context.Context, token []byte, ct time.Time) (res *model.Token, err error) {
  16. row := d.db.QueryRow(c, fmt.Sprintf(_getTokenSQL, formatSuffix(ct)), token)
  17. res = new(model.Token)
  18. var tokenByte []byte
  19. if err = row.Scan(&res.Mid, &res.AppID, &tokenByte, &res.Expires, &res.Type); 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.Token = hex.EncodeToString(tokenByte)
  29. return
  30. }
  31. func formatSuffix(t time.Time) string {
  32. return t.Format("200601")
  33. }