Tokey.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package Account
  2. import (
  3. "ColdVerify_server/conf"
  4. "ColdVerify_server/logs"
  5. "fmt"
  6. "github.com/astaxie/beego/cache"
  7. _ "github.com/astaxie/beego/cache/redis"
  8. uuid "github.com/satori/go.uuid"
  9. "log"
  10. "time"
  11. )
  12. var redisCache_Tokey cache.Cache
  13. func init() {
  14. //注册模型
  15. //orm.RegisterModel(new(Tokey))
  16. config := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`,
  17. "redis_Tokey", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password)
  18. logs.Println(config)
  19. var err error
  20. redisCache_Tokey, err = cache.NewCache("redis", config)
  21. if err != nil || redisCache_Tokey == nil {
  22. errMsg := "failed to init redis"
  23. logs.Println(errMsg, err)
  24. }
  25. }
  26. // ---------------- Redis -------------------
  27. // Redis_Set(m.T_sn,m) // Redis 更新缓存
  28. func Redis_Tokey_Set(key string, r string) (err error) {
  29. err = redisCache_Tokey.Put(key, r, 24*7*time.Hour)
  30. if err != nil {
  31. logs.Println("set key:", key, ",value:", r, err)
  32. }
  33. return
  34. }
  35. // if r,is :=Redis_Get(T_sn);is{
  36. // return r,nil
  37. // }
  38. func Redis_Tokey_Get(key string) (r string, is bool) {
  39. if redisCache_Tokey.IsExist(key) {
  40. //println("找到key:",key)
  41. v := redisCache_Tokey.Get(key)
  42. value := string(v.([]byte))
  43. return value, true
  44. }
  45. //println("没有 找到key:",key)
  46. return "", false
  47. }
  48. func Redis_DelK(key string) (err error) {
  49. err = redisCache_Tokey.Delete(key)
  50. return
  51. }
  52. // ---------------- 特殊方法 -------------------
  53. // 验证 TOKEY
  54. func Read_Tokey(User_tokey string) (string, bool) {
  55. return Redis_Tokey_Get(User_tokey)
  56. }
  57. // 添加 Tokey
  58. func Add_Tokey(User_uuid string) (Tokey string) {
  59. for true {
  60. Tokey = uuid.NewV4().String()
  61. _, is := Redis_Tokey_Get(Tokey)
  62. if !is {
  63. break
  64. }
  65. fmt.Print("申请 TOKEY 重复!重新生成。", Tokey)
  66. }
  67. Redis_Tokey_Set(Tokey, User_uuid)
  68. return Tokey
  69. }
  70. // 登录验证
  71. func Verification(GetCookie string, GetString string) (User, bool) {
  72. // 自适应 参数
  73. User_tokey := GetCookie
  74. if len(User_tokey) == 0 {
  75. User_tokey = GetString
  76. }
  77. if len(User_tokey) == 0 {
  78. return User{}, false
  79. }
  80. // 判断 tokey 是否存在
  81. tokey, is := Read_Tokey(User_tokey)
  82. if !is {
  83. return User{}, false
  84. }
  85. err, user_r := Read_User_ByT_uuid(tokey)
  86. if err != nil {
  87. return User{}, false
  88. }
  89. log.Println("登录 User_name 为:", user_r.T_name)
  90. return user_r, true
  91. }
  92. // 登录验证
  93. func Verification_Admin(GetCookie string, GetString string) (Admin, bool) {
  94. // 自适应 参数
  95. User_tokey := GetCookie
  96. if len(User_tokey) == 0 {
  97. User_tokey = GetString
  98. }
  99. if len(User_tokey) == 0 {
  100. return Admin{}, false
  101. }
  102. // 判断 tokey 是否存在
  103. tokey, is := Read_Tokey(User_tokey)
  104. if !is {
  105. return Admin{}, false
  106. }
  107. err, user_r := Read_Admin_ByT_uuid(tokey)
  108. if err != nil {
  109. return Admin{}, false
  110. }
  111. log.Println("登录 Admin_name 为:", user_r.T_name)
  112. return user_r, true
  113. }