Tokey.go 1.5 KB

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