Tokey.go 2.4 KB

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