User.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package Account
  2. import (
  3. "Yunlot/conf"
  4. "Yunlot/logs"
  5. "Yunlot/models"
  6. "encoding/json"
  7. "fmt"
  8. "github.com/astaxie/beego/cache"
  9. _ "github.com/astaxie/beego/cache/redis"
  10. "github.com/beego/beego/v2/adapter/orm"
  11. _ "github.com/go-sql-driver/mysql"
  12. "time"
  13. )
  14. type User struct {
  15. T_uuid string `orm:"size(100);pk"`
  16. T_user string `orm:"size(256);null"` // 用户名
  17. T_pass string `orm:"size(256);null"` // 密码
  18. T_hash string `orm:"size(256);null"` // hash
  19. T_State int `orm:"size(200);1"` // 1 正常 2 删除\禁用
  20. CreateTime models.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now 每次 model 保存时都会对时间自动更新
  21. UpdateTime models.Time `orm:"column(update_time);type(timestamp);null;auto_now"` //auto_now_add 第一次保存时才设置时间
  22. }
  23. type VCode struct {
  24. T_Code string
  25. }
  26. func (t *User) TableName() string {
  27. return "User" // 数据库名称 // ************** 替换 FormulaList **************
  28. }
  29. var redis_User cache.Cache
  30. func init() {
  31. //注册模型
  32. orm.RegisterModel(new(User))
  33. config := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`,
  34. "redis_User", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password)
  35. var err error
  36. redis_User, err = cache.NewCache("redis", config)
  37. if err != nil || redis_User == nil {
  38. logs.Println(config)
  39. panic(any(err))
  40. }
  41. }
  42. // ---------------- Redis -------------------
  43. func (t *User) redis_Set() (err error) {
  44. //json序列化
  45. str, err := json.Marshal(t)
  46. if err != nil {
  47. logs.PrintlnError("Redis_Set", err)
  48. return
  49. }
  50. err = redis_User.Put(t.T_uuid, str, 24*time.Hour)
  51. if err != nil {
  52. logs.Println("set key:", t.T_uuid, ",value:", str, err)
  53. }
  54. return
  55. }
  56. func (t *User) redis_Get(key string) (is bool) {
  57. if redis_User.IsExist(key) {
  58. //println("找到key:",key)
  59. v := redis_User.Get(key)
  60. if v == nil {
  61. return false
  62. }
  63. json.Unmarshal(v.([]byte), t)
  64. return true
  65. }
  66. //println("没有 找到key:",key)
  67. return false
  68. }
  69. func (t *User) redis_DelK() (err error) {
  70. err = redis_User.Delete(t.T_uuid)
  71. return
  72. }
  73. // ---------------- 方法 -------------------
  74. // 获取 ById
  75. func (t *User) Read(T_uuid string) (r User, is bool) {
  76. if r.redis_Get(T_uuid) {
  77. return r, true
  78. }
  79. o := orm.NewOrm()
  80. r = User{T_uuid: T_uuid}
  81. err := o.Read(&r, "T_uuid") // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
  82. if err != nil {
  83. return r, false
  84. }
  85. r.redis_Set() // Redis 更新缓存
  86. return r, true
  87. }
  88. // 添加
  89. func (t *User) Add() (id int64, is bool) {
  90. o := orm.NewOrm()
  91. id, err := o.Insert(t)
  92. if err != nil {
  93. return 0, false
  94. }
  95. t.redis_Set()
  96. return id, true
  97. }
  98. // 修改
  99. func (t *User) Update(cols ...string) bool {
  100. o := orm.NewOrm()
  101. if num, err := o.Update(&t, cols...); err == nil {
  102. fmt.Println("Number of records updated in database:", num)
  103. t.redis_Set() // Redis 更新缓存
  104. return true
  105. }
  106. return false
  107. }