Tokey.go 2.7 KB

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