token.go 712 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. package token
  2. import (
  3. "errors"
  4. "fmt"
  5. "mtp2_if/rediscli"
  6. )
  7. var TokenMap map[int]string
  8. func Init() {
  9. TokenMap = make(map[int]string)
  10. TokenMap[250000000001] = "testtoken"
  11. }
  12. func CheckToken(accountid int, token string) (bool, error) {
  13. realToken, ok := TokenMap[accountid]
  14. if ok {
  15. if realToken != token {
  16. return false, errors.New("token is invalid")
  17. }
  18. } else {
  19. key := fmt.Sprintf("monitor:online_loginid::%d", accountid)
  20. field := "Token"
  21. realToken, err := rediscli.GetRedisClient().HGet(key, field).Result()
  22. if err != nil {
  23. return false, err
  24. }
  25. TokenMap[accountid] = realToken
  26. if realToken != token {
  27. return false, errors.New("token is invalid")
  28. }
  29. }
  30. return true, nil
  31. }