Tokey.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package Admin
  2. import (
  3. "Cold_Api/conf"
  4. "fmt"
  5. "github.com/astaxie/beego/cache"
  6. _ "github.com/astaxie/beego/cache/redis"
  7. uuid "github.com/satori/go.uuid"
  8. "time"
  9. )
  10. //
  11. //type Tokey struct {
  12. // Id int `orm:"column(ID);size(11);auto;pk"`
  13. // Admin_uuid string `orm:"size(256);null"` //
  14. // User_tokey string `orm:"size(256);null"` //
  15. // User_num int `orm:"size(200);null"` // 操作次数
  16. // CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now 每次 model 保存时都会对时间自动更新
  17. // UpdateTime time.Time `orm:"column(update_time);type(timestamp);null;auto_now"` //auto_now_add 第一次保存时才设置时间
  18. //}
  19. //func (t *Tokey) TableName() string {
  20. // return "Tokey" // 数据库名称 // ************** 替换 FormulaList **************
  21. //}
  22. var redisCache_Tokey cache.Cache
  23. func init() {
  24. //注册模型
  25. //orm.RegisterModel(new(Tokey))
  26. config := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`,
  27. "redisCache_Tokey", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password)
  28. fmt.Println(config)
  29. var err error
  30. redisCache_Tokey, err = cache.NewCache("redis", config)
  31. if err != nil || redisCache_Tokey == nil {
  32. errMsg := "failed to init redis"
  33. fmt.Println(errMsg, err)
  34. }
  35. }
  36. // ---------------- Redis -------------------
  37. //Redis_Set(m.T_sn,m) // Redis 更新缓存
  38. func Add_Tokey_Set(Uuid string) string {
  39. var Tokey string
  40. for true {
  41. Tokey = uuid.NewV4().String()
  42. if !redisCache_Tokey.IsExist(Tokey) {
  43. break
  44. }
  45. fmt.Print("申请 TOKEY 重复!重新生成。", Tokey)
  46. }
  47. redisCache_Tokey.Put(Tokey, Uuid, 2*time.Hour)
  48. return Tokey
  49. }
  50. //if r,is :=Redis_Get(T_sn);is{
  51. //return r,nil
  52. //}
  53. func Redis_Tokey_Get(Tokey string) (string, bool) {
  54. if len(Tokey) < 10 {
  55. return "", false
  56. }
  57. if redisCache_Tokey.IsExist(Tokey) {
  58. //println("找到key:",key)
  59. v := redisCache_Tokey.Get(Tokey)
  60. value := string(v.([]byte)) //这里的转换很重要,Get返回的是interface
  61. redisCache_Tokey.Put(Tokey, value, 2*time.Hour) // 重新计次
  62. return value, true
  63. }
  64. //println("没有 找到key:",key)
  65. return "", false
  66. }
  67. //
  68. //// 验证 TOKEY
  69. //func Read_Verification_Tokey(User_tokey string) (bool, Tokey) {
  70. // r := Tokey{}
  71. // o := orm.NewOrm()
  72. // qs := o.QueryTable(new(Tokey))
  73. // err := qs.Filter("User_tokey",User_tokey).One(&r)
  74. //
  75. // if err != nil {
  76. // fmt.Println(err)
  77. // return false,r
  78. // }
  79. //
  80. // //println(time.Now().String(),time.Now().Unix())
  81. // //println(r.UpdateTime.String(),r.UpdateTime.Unix())
  82. // //
  83. // //println(r.Id,time.Now().Unix() - r.UpdateTime.Unix())
  84. // if time.Now().Unix() - r.UpdateTime.Unix() > 60 * 60 * 8 { // 小时
  85. // println("User_tokey 过期了")
  86. // return false,r
  87. // }
  88. // r.UpdateTime = time.Now()
  89. // o.Update(&r, "UpdateTime");
  90. //
  91. // return true,r
  92. //}
  93. //
  94. // 添加 Tokey
  95. //func Add_Tokey(Admin_uuid string) (User_tokey string) {
  96. // o := orm.NewOrm()
  97. // for true{
  98. // User_tokey = uuid.NewV4().String()
  99. // r := Tokey{User_tokey: User_tokey}
  100. // err := o.Read(&r, "User_tokey") // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
  101. // if err != nil {
  102. // break;
  103. // }
  104. // fmt.Print("申请 TOKEY 重复!重新生成。",User_tokey)
  105. // }
  106. // o.Insert(&Tokey{Admin_uuid:Admin_uuid,User_tokey: User_tokey})
  107. //
  108. // return User_tokey
  109. //}