Tokey.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package Account
  2. import (
  3. "Cold_Api/conf"
  4. "fmt"
  5. "github.com/astaxie/beego/cache"
  6. _ "github.com/astaxie/beego/cache/redis"
  7. "github.com/beego/beego/v2/core/logs"
  8. uuid "github.com/satori/go.uuid"
  9. "log"
  10. "strconv"
  11. "strings"
  12. "time"
  13. )
  14. var redisCache_Tokey cache.Cache
  15. var Admin_r *Admin
  16. func init() {
  17. //注册模型
  18. //orm.RegisterModel(new(Tokey))
  19. config := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`,
  20. "redis_User_Tokey", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password)
  21. fmt.Println(config)
  22. var err error
  23. redisCache_Tokey, err = cache.NewCache("redis", config)
  24. if err != nil || redisCache_Tokey == nil {
  25. errMsg := "failed to init redis"
  26. logs.Error(errMsg, err)
  27. panic(errMsg)
  28. }
  29. }
  30. // ---------------- Redis -------------------
  31. // Redis_Set(m.T_sn,m) // Redis 更新缓存
  32. func Add_Tokey_Set(Uuid string) string {
  33. var Tokey string
  34. for true {
  35. Tokey = uuid.NewV4().String()
  36. if !redisCache_Tokey.IsExist(Tokey) {
  37. break
  38. }
  39. logs.Info("申请 TOKEY 重复!重新生成。", Tokey)
  40. }
  41. err := redisCache_Tokey.Put(Tokey, Uuid+"|0", 2*time.Hour)
  42. if err != nil {
  43. logs.Error("Add_Tokey_Set", err)
  44. }
  45. return Tokey
  46. }
  47. // if r,is :=Redis_Get(T_sn);is{
  48. // return r,nil
  49. // }
  50. func Redis_Tokey_Get(Tokey string) (string, bool) {
  51. if len(Tokey) < 10 {
  52. return "", false
  53. }
  54. if redisCache_Tokey.IsExist(Tokey) {
  55. //println("找到key:",key)
  56. v := redisCache_Tokey.Get(Tokey)
  57. value := string(v.([]byte)) //这里的转换很重要,Get返回的是interface
  58. err := redisCache_Tokey.Put(Tokey, value, 2*time.Hour) // 重新计次
  59. if err != nil {
  60. logs.Error("Redis_Tokey_Get", err)
  61. }
  62. return value, true
  63. }
  64. //println("没有 找到key:",key)
  65. return "", false
  66. }
  67. func Redis_Tokey_T_pid_Set(Tokey string, T_pid int) bool {
  68. if len(Tokey) < 10 {
  69. return false
  70. }
  71. if redisCache_Tokey.IsExist(Tokey) {
  72. //println("找到key:",key)
  73. v := redisCache_Tokey.Get(Tokey)
  74. value := string(v.([]byte)) //这里的转换很重要,Get返回的是interface
  75. T_uuid := strings.Split(value, "|")[0]
  76. err := redisCache_Tokey.Put(Tokey, T_uuid+"|"+strconv.Itoa(T_pid), 2*time.Hour) // 重新计次
  77. if err != nil {
  78. logs.Error("Redis_Tokey_T_pid_Set", err)
  79. }
  80. return true
  81. }
  82. return false
  83. }
  84. func Redis_Tokey_T_pid_Get(Tokey string) (int, bool) {
  85. if len(Tokey) < 10 {
  86. return 0, false
  87. }
  88. if redisCache_Tokey.IsExist(Tokey) {
  89. //println("找到key:",key)
  90. v := redisCache_Tokey.Get(Tokey)
  91. value := string(v.([]byte)) //这里的转换很重要,Get返回的是interface
  92. T_pid, _ := strconv.Atoi(strings.Split(value, "|")[1])
  93. return T_pid, true
  94. }
  95. return 0, false
  96. }
  97. // 登录验证
  98. func Verification(GetCookie string, GetString string) (bool, Admin) {
  99. // 自适应 参数
  100. //log.Println("登录验证Verification", GetCookie, GetString)
  101. User_tokey := GetCookie
  102. if len(User_tokey) == 0 {
  103. User_tokey = GetString
  104. }
  105. if len(User_tokey) == 0 {
  106. return false, Admin{}
  107. }
  108. tokey, is := Redis_Tokey_Get(User_tokey)
  109. T_uuid := strings.Split(tokey, "|")[0]
  110. if !is {
  111. return false, Admin{}
  112. }
  113. var err error
  114. admin_r, err := Read_Admin_ByUuid(T_uuid)
  115. if err != nil {
  116. return false, Admin{}
  117. }
  118. Admin_r = &admin_r
  119. log.Println("登录 Admin_name 为:", admin_r.T_name)
  120. return true, admin_r
  121. }