123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- package Admin
- import (
- "Cold_Api/conf"
- "fmt"
- "github.com/astaxie/beego/cache"
- _ "github.com/astaxie/beego/cache/redis"
- uuid "github.com/satori/go.uuid"
- "time"
- )
- //
- //type Tokey struct {
- // Id int `orm:"column(ID);size(11);auto;pk"`
- // Admin_uuid string `orm:"size(256);null"` //
- // User_tokey string `orm:"size(256);null"` //
- // User_num int `orm:"size(200);null"` // 操作次数
- // CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now 每次 model 保存时都会对时间自动更新
- // UpdateTime time.Time `orm:"column(update_time);type(timestamp);null;auto_now"` //auto_now_add 第一次保存时才设置时间
- //}
- //func (t *Tokey) TableName() string {
- // return "Tokey" // 数据库名称 // ************** 替换 FormulaList **************
- //}
- var redisCache_Tokey cache.Cache
- func init() {
- //注册模型
- //orm.RegisterModel(new(Tokey))
- config := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`,
- "redisCache_Tokey", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password)
- fmt.Println(config)
- var err error
- redisCache_Tokey, err = cache.NewCache("redis", config)
- if err != nil || redisCache_Tokey == nil {
- errMsg := "failed to init redis"
- fmt.Println(errMsg, err)
- }
- }
- // ---------------- Redis -------------------
- //Redis_Set(m.T_sn,m) // Redis 更新缓存
- func Add_Tokey_Set(Uuid string) string {
- var Tokey string
- for true {
- Tokey = uuid.NewV4().String()
- if !redisCache_Tokey.IsExist(Tokey) {
- break
- }
- fmt.Print("申请 TOKEY 重复!重新生成。", Tokey)
- }
- redisCache_Tokey.Put(Tokey, Uuid, 2*time.Hour)
- return Tokey
- }
- //if r,is :=Redis_Get(T_sn);is{
- //return r,nil
- //}
- func Redis_Tokey_Get(Tokey string) (string, bool) {
- if len(Tokey) < 10 {
- return "", false
- }
- if redisCache_Tokey.IsExist(Tokey) {
- //println("找到key:",key)
- v := redisCache_Tokey.Get(Tokey)
- value := string(v.([]byte)) //这里的转换很重要,Get返回的是interface
- redisCache_Tokey.Put(Tokey, value, 2*time.Hour) // 重新计次
- return value, true
- }
- //println("没有 找到key:",key)
- return "", false
- }
- //
- //// 验证 TOKEY
- //func Read_Verification_Tokey(User_tokey string) (bool, Tokey) {
- // r := Tokey{}
- // o := orm.NewOrm()
- // qs := o.QueryTable(new(Tokey))
- // err := qs.Filter("User_tokey",User_tokey).One(&r)
- //
- // if err != nil {
- // fmt.Println(err)
- // return false,r
- // }
- //
- // //println(time.Now().String(),time.Now().Unix())
- // //println(r.UpdateTime.String(),r.UpdateTime.Unix())
- // //
- // //println(r.Id,time.Now().Unix() - r.UpdateTime.Unix())
- // if time.Now().Unix() - r.UpdateTime.Unix() > 60 * 60 * 8 { // 小时
- // println("User_tokey 过期了")
- // return false,r
- // }
- // r.UpdateTime = time.Now()
- // o.Update(&r, "UpdateTime");
- //
- // return true,r
- //}
- //
- // 添加 Tokey
- //func Add_Tokey(Admin_uuid string) (User_tokey string) {
- // o := orm.NewOrm()
- // for true{
- // User_tokey = uuid.NewV4().String()
- // r := Tokey{User_tokey: User_tokey}
- // err := o.Read(&r, "User_tokey") // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
- // if err != nil {
- // break;
- // }
- // fmt.Print("申请 TOKEY 重复!重新生成。",User_tokey)
- // }
- // o.Insert(&Tokey{Admin_uuid:Admin_uuid,User_tokey: User_tokey})
- //
- // return User_tokey
- //}
|