统一表创建表的方式
This commit is contained in:
@@ -51,47 +51,13 @@ func Md5Str(str string) string {
|
||||
return hashString2
|
||||
}
|
||||
|
||||
func HashUserPass(user *TabUser_) {
|
||||
switch ConfigsUser.PassHashType {
|
||||
case "text":
|
||||
break
|
||||
case "md5":
|
||||
user.Pass = Md5Str(user.Pass)
|
||||
|
||||
case "md5salt":
|
||||
if user.Salt == "" {
|
||||
user.Salt = RandStr32()
|
||||
}
|
||||
user.Pass = Md5Str(Md5Str(user.Pass) + user.Salt)
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func IsExpired(expireTime time.Time) bool {
|
||||
return expireTime.Before(time.Now())
|
||||
}
|
||||
|
||||
func CheckCookiesAndUpdate(cookie *TabCookie_) bool {
|
||||
if !IsExpired(cookie.ExpiresAt) {
|
||||
if cookie.Remember {
|
||||
cookiewhere := TabCookie_{
|
||||
ID: cookie.ID,
|
||||
}
|
||||
cookieupdata := TabCookie_{
|
||||
UpdatedAt: time.Now(),
|
||||
ExpiresAt: time.Now().Add(time.Duration(ConfigsUser.CookieTimeout) * time.Second),
|
||||
}
|
||||
DB.Where(&cookiewhere).Updates(&cookieupdata)
|
||||
|
||||
}
|
||||
return true
|
||||
} else {
|
||||
//以过期
|
||||
return false
|
||||
}
|
||||
//return false
|
||||
}
|
||||
|
||||
// 判断邮箱是否合法
|
||||
func IsEmailValid(email string) bool {
|
||||
|
||||
@@ -35,16 +35,16 @@ func GetRealIP(c *gin.Context) string {
|
||||
return c.ClientIP()
|
||||
}
|
||||
|
||||
func LogAdd(c *gin.Context, msg string) {
|
||||
// func LogAdd(c *gin.Context, msg string) {
|
||||
|
||||
var logtemp APIRequestLog_
|
||||
// var logtemp APIRequestLog_
|
||||
|
||||
logtemp.IPAddress = GetRealIP(c)
|
||||
logtemp.Path = c.Request.URL.Path
|
||||
logtemp.Method = c.Request.Method
|
||||
logtemp.Message = msg
|
||||
// logtemp.IPAddress = GetRealIP(c)
|
||||
// logtemp.Path = c.Request.URL.Path
|
||||
// logtemp.Method = c.Request.Method
|
||||
// logtemp.Message = msg
|
||||
|
||||
//fmt.Println(logtemp)
|
||||
DB.Create(&logtemp)
|
||||
// //fmt.Println(logtemp)
|
||||
// DB.Create(&logtemp)
|
||||
|
||||
}
|
||||
// }
|
||||
|
||||
@@ -2,7 +2,6 @@ package models
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/glebarez/sqlite"
|
||||
"gorm.io/driver/mysql"
|
||||
@@ -12,70 +11,16 @@ import (
|
||||
|
||||
var DB *gorm.DB
|
||||
|
||||
type TabUser_ struct {
|
||||
ID uint `gorm:"primaryKey;autoIncrement"` // 自增主键
|
||||
Name string `gorm:"size:100;uniqueIndex"` // 唯一约束索引
|
||||
Email string `gorm:"size:255;index"` // 字符串长度限制100 索引
|
||||
Pass string `gorm:"size:128"` // 建议存储哈希后的密码
|
||||
Type string `gorm:"size:64;default:user"` //
|
||||
Salt string `gorm:"size:64;"`
|
||||
Date time.Time `gorm:"type:datetime;default:CURRENT_TIMESTAMP"` // 默认当前时间
|
||||
}
|
||||
|
||||
type TabUserGroups_ struct {
|
||||
ID uint `gorm:"primaryKey;autoIncrement"` // 自增主键
|
||||
Name string `gorm:"size:100;uniqueIndex"` // 唯一约束索引
|
||||
Email string `gorm:"size:255;index"` // 字符串长度限制100 索引
|
||||
Type string `gorm:"size:64;default:usergroup"` //
|
||||
Date time.Time `gorm:"type:datetime;default:CURRENT_TIMESTAMP"` // 默认当前时间
|
||||
}
|
||||
|
||||
type TabUserGroupBinds_ struct {
|
||||
ID uint `gorm:"primaryKey;autoIncrement"` // 自增主键
|
||||
UserID uint `gorm:"index"`
|
||||
GroupID uint `gorm:"index"`
|
||||
Date time.Time `gorm:"type:datetime;default:CURRENT_TIMESTAMP"` // 默认当前时间
|
||||
}
|
||||
|
||||
type TabUserInfo_ struct {
|
||||
ID uint `gorm:"primaryKey;autoIncrement"`
|
||||
UserID uint `gorm:"not null;uniqueIndex"`
|
||||
FirstName string `gorm:"size:50;null"`
|
||||
Username string `gorm:"size:30;null"`
|
||||
Birthdate time.Time `gorm:"type:datetime;null"`
|
||||
Gender string `gorm:"type:char(1);check:gender IN ('M', 'F', 'U');default:'U'"`
|
||||
AvatarPath string `gorm:"size:255"`
|
||||
Region string `gorm:"size:50"`
|
||||
Language string `gorm:"size:10;default:'zh-CN'"`
|
||||
CreatedAt time.Time `gorm:"type:datetime;default:CURRENT_TIMESTAMP;column:created_at"`
|
||||
}
|
||||
|
||||
// var def_user_info = User_info{
|
||||
// ID:0,
|
||||
// UserID:0,
|
||||
// type APIRequestLog_ struct {
|
||||
// ID int64 `gorm:"primaryKey;column:id" json:"id"`
|
||||
// IPAddress string `gorm:"column:ip_address;size:45;not null" json:"ip_address"`
|
||||
// Path string `gorm:"column:path;size:500;not null" json:"path"`
|
||||
// Method string `gorm:"column:method;size:10;not null" json:"method"`
|
||||
// StatusCode int `gorm:"column:status_code;index" json:"status_code"`
|
||||
// Message string `gorm:"column:error_message;type:text" json:"error_message"`
|
||||
// CreatedAt time.Time `gorm:"column:created_at;type:datetime;default:CURRENT_TIMESTAMP" json:"created_at"`
|
||||
// }
|
||||
|
||||
type TabCookie_ struct {
|
||||
ID uint `gorm:"primaryKey;autoIncrement"`
|
||||
UserID uint `gorm:"not null"`
|
||||
Name string `gorm:"size:255;not null;index"`
|
||||
Value string `gorm:"size:255;not null;index"`
|
||||
ExpiresAt time.Time `gorm:"type:datetime;index"`
|
||||
CreatedAt time.Time `gorm:"type:datetime;not null;default:CURRENT_TIMESTAMP"`
|
||||
UpdatedAt time.Time `gorm:"type:datetime;index;not null;default:CURRENT_TIMESTAMP"`
|
||||
Remember bool `gorm:"default:false"`
|
||||
}
|
||||
|
||||
type APIRequestLog_ struct {
|
||||
ID int64 `gorm:"primaryKey;column:id" json:"id"`
|
||||
IPAddress string `gorm:"column:ip_address;size:45;not null" json:"ip_address"`
|
||||
Path string `gorm:"column:path;size:500;not null" json:"path"`
|
||||
Method string `gorm:"column:method;size:10;not null" json:"method"`
|
||||
StatusCode int `gorm:"column:status_code;index" json:"status_code"`
|
||||
Message string `gorm:"column:error_message;type:text" json:"error_message"`
|
||||
CreatedAt time.Time `gorm:"column:created_at;type:datetime;default:CURRENT_TIMESTAMP" json:"created_at"`
|
||||
}
|
||||
|
||||
func DatabaseInit() error {
|
||||
var err error
|
||||
fmt.Println("database_init")
|
||||
@@ -102,18 +47,7 @@ func DatabaseInit() error {
|
||||
panic("数据库连接失败")
|
||||
}
|
||||
|
||||
// 自动创建表结构
|
||||
DB.AutoMigrate(&TabUser_{})
|
||||
|
||||
DB.AutoMigrate(&TabUserGroups_{})
|
||||
|
||||
DB.AutoMigrate(&TabUserGroupBinds_{})
|
||||
|
||||
DB.AutoMigrate(&TabUserInfo_{})
|
||||
|
||||
DB.AutoMigrate(&TabCookie_{})
|
||||
|
||||
DB.AutoMigrate(&APIRequestLog_{})
|
||||
//DB.AutoMigrate(&APIRequestLog_{})
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user