123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- package Account
- import (
- "ERP_user/conf"
- "ERP_user/logs"
- "fmt"
- "github.com/astaxie/beego/cache"
- _ "github.com/astaxie/beego/cache/redis"
- uuid "github.com/satori/go.uuid"
- "gogs.baozhida.cn/zoie/ERP_libs/lib"
- "log"
- "time"
- )
- var redisCache_Tokey cache.Cache
- func init() {
- //注册模型
- //orm.RegisterModel(new(Tokey))
- config := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`,
- "redis_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"
- logs.Error(errMsg, err)
- }
- }
- // ---------------- Redis -------------------
- // Redis_Set(m.T_sn,m) // Redis 更新缓存
- func Redis_Tokey_Set(key string, r string) (err error) {
- err = redisCache_Tokey.Put(key, r, 24*time.Hour)
- if err != nil {
- logs.Error("set key:", key, ",value:", r, err)
- }
- return
- }
- // if r,is :=Redis_Get(T_sn);is{
- // return r,nil
- // }
- func Redis_Tokey_Get(key string) (r string, is bool) {
- if redisCache_Tokey.IsExist(key) {
- //println("找到key:",key)
- v := redisCache_Tokey.Get(key)
- value := string(v.([]byte))
- Redis_Tokey_Set(key, value)
- return value, true
- }
- //println("没有 找到key:",key)
- return "", false
- }
- func Redis_DelK(key string) (err error) {
- err = redisCache_Tokey.Delete(key)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- }
- return
- }
- // ---------------- 特殊方法 -------------------
- // 验证 TOKEY
- func Read_Tokey(User_tokey string) (string, bool) {
- return Redis_Tokey_Get(User_tokey)
- }
- // 添加 Tokey
- func Add_Tokey(User_uuid string) (Tokey string) {
- for true {
- Tokey = uuid.NewV4().String()
- _, is := Redis_Tokey_Get(Tokey)
- if !is {
- break
- }
- fmt.Print("申请 TOKEY 重复!重新生成。", Tokey)
- }
- Redis_Tokey_Set(Tokey, User_uuid)
- return Tokey
- }
- // 登录验证
- func Verification(GetCookie string, GetString string) (bool, User) {
- // 自适应 参数
- User_tokey := GetCookie
- if len(User_tokey) == 0 {
- User_tokey = GetString
- }
- if len(User_tokey) == 0 {
- return false, User{}
- }
- // 判断 tokey 是否存在
- tokey, is := Read_Tokey(User_tokey)
- if !is {
- return false, User{}
- }
- user_r, err := Read_User_ByT_uuid(tokey)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return false, User{}
- }
- User_r = &user_r
- log.Println("登录 User_name 为:", user_r.T_name)
- return true, user_r
- }
|