Tokey.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package Account
  2. import (
  3. "Yunlot/conf"
  4. "Yunlot/logs"
  5. "fmt"
  6. "github.com/astaxie/beego/cache"
  7. _ "github.com/astaxie/beego/cache/redis"
  8. uuid "github.com/satori/go.uuid"
  9. "time"
  10. )
  11. var redis_Tokey cache.Cache
  12. func init() {
  13. config := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`,
  14. "redis_Tokey", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password)
  15. var err error
  16. redis_Tokey, err = cache.NewCache("redis", config)
  17. if err != nil || redis_Tokey == nil {
  18. logs.Println(config)
  19. panic(any(err))
  20. }
  21. }
  22. // ---------------- Redis -------------------
  23. // Redis_Set(m.T_sn,m) // Redis 更新缓存
  24. func Redis_Tokey_Set(key string, r string) (err error) {
  25. err = redis_Tokey.Put(key, r, 24*time.Hour)
  26. if err != nil {
  27. logs.Println("set key:", key, ",value:", r, err)
  28. }
  29. return
  30. }
  31. // if r,is :=Redis_Get(T_sn);is{
  32. // return r,nil
  33. // }
  34. func Redis_Tokey_Get(key string) (r string, is bool) {
  35. if redis_Tokey.IsExist(key) {
  36. //println("找到key:",key)
  37. v := redis_Tokey.Get(key)
  38. value := string(v.([]byte))
  39. Redis_Tokey_Set(key, value)
  40. return value, true
  41. }
  42. //println("没有 找到key:",key)
  43. return "", false
  44. }
  45. func Redis_DelK(key string) (err error) {
  46. err = redis_Tokey.Delete(key)
  47. return
  48. }
  49. // ---------------- 方法 -------------------
  50. // 验证 TOKEY
  51. func Read_Tokey(User_tokey string) (string, bool) {
  52. return Redis_Tokey_Get(User_tokey)
  53. }
  54. // 添加 Tokey
  55. func Add_Tokey(User_uuid string) (Tokey string) {
  56. for true {
  57. Tokey = uuid.NewV4().String()
  58. _, is := Redis_Tokey_Get(Tokey)
  59. if !is {
  60. break
  61. }
  62. fmt.Print("申请 TOKEY 重复!重新生成。", Tokey)
  63. }
  64. Redis_Tokey_Set(Tokey, User_uuid)
  65. return Tokey
  66. }