This commit is contained in:
@@ -1,9 +1,3 @@
|
||||
/*
|
||||
@Time : 2020/6/28 22:01
|
||||
@Author : xuyiqing
|
||||
@File : users.py
|
||||
*/
|
||||
|
||||
package models
|
||||
|
||||
import (
|
||||
@@ -43,24 +37,13 @@ func (a *Account) SetPassword(password string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 验证登录帐户密码合法性
|
||||
func (a *Account) CheckPassword() bool {
|
||||
password := a.Password
|
||||
|
||||
aa, err1 := bcrypt.GenerateFromPassword([]byte(a.Password), 12)
|
||||
print(aa, err1)
|
||||
DB.Where("username = ?", a.Username).First(&a)
|
||||
err := bcrypt.CompareHashAndPassword([]byte(a.Password), []byte(password))
|
||||
return err == nil
|
||||
}
|
||||
|
||||
// 验证登录帐户密码合法性
|
||||
// IsPasswordEqual 验证密码是否匹配
|
||||
func (a *Account) IsPasswordEqual(password string) bool {
|
||||
err := bcrypt.CompareHashAndPassword([]byte(a.Password), []byte(password))
|
||||
return err == nil
|
||||
}
|
||||
|
||||
// 验证用户民重复
|
||||
// CheckDuplicateUsername 验证用户名是否重复
|
||||
func (a *Account) CheckDuplicateUsername() bool {
|
||||
var count int64
|
||||
if DB.Model(&Account{}).Where("username=?", a.Username).Count(&count); count > 0 {
|
||||
|
||||
@@ -1,18 +0,0 @@
|
||||
package models
|
||||
|
||||
import "gorm.io/gorm"
|
||||
|
||||
type Article struct {
|
||||
gorm.Model
|
||||
Title string
|
||||
Content string `gorm:"type:text"`
|
||||
AuthorID uint
|
||||
Tags []Tag `gorm:"many2many:article_tags;"`
|
||||
}
|
||||
|
||||
// Tag represents the tag model
|
||||
type Tag struct {
|
||||
gorm.Model
|
||||
Name string `gorm:"uniqueIndex"` // Ensures tag names are unique
|
||||
Articles []Article `gorm:"many2many:article_tags;"` // Reverse relationship
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
package models
|
||||
|
||||
const TableNameContent = "contents"
|
||||
|
||||
// Content 内容模型(文章/页面)
|
||||
type Content struct {
|
||||
Cid int32 `gorm:"column:cid;primaryKey;autoIncrement:true" json:"cid"`
|
||||
Title string `gorm:"column:title" json:"title"`
|
||||
@@ -22,7 +21,7 @@ type Content struct {
|
||||
Parent int32 `gorm:"column:parent" json:"parent"`
|
||||
}
|
||||
|
||||
// TableName Content's table name
|
||||
// TableName 指定表名
|
||||
func (*Content) TableName() string {
|
||||
return TableNameContent
|
||||
return "contents"
|
||||
}
|
||||
|
||||
@@ -1,9 +1,3 @@
|
||||
/*
|
||||
@Time : 2020/6/28 21:46
|
||||
@Author : xuyiqing
|
||||
@File : init.py
|
||||
*/
|
||||
|
||||
package models
|
||||
|
||||
import (
|
||||
@@ -50,12 +44,8 @@ func InitDatabase(conf *config.Config) {
|
||||
}
|
||||
|
||||
DB = db
|
||||
// 3. 自动迁移数据模型
|
||||
db.AutoMigrate(&Account{})
|
||||
db.AutoMigrate(&Content{})
|
||||
// if err := db.AutoMigrate(&models.Article{}, &models.User{}); err != nil {
|
||||
// panic("数据库迁移失败: " + err.Error())
|
||||
// }
|
||||
// 自动迁移数据模型
|
||||
db.AutoMigrate(&Account{}, &Content{}, &Option{})
|
||||
}
|
||||
|
||||
type BaseModel struct {
|
||||
|
||||
@@ -2,8 +2,7 @@ package models
|
||||
|
||||
import "gorm.io/gorm"
|
||||
|
||||
// Option 对应 typecho_options 数据表
|
||||
// 用于存储系统配置项
|
||||
// Option 系统配置项模型
|
||||
type Option struct {
|
||||
Name string `gorm:"column:name;type:varchar(32);primaryKey"` // 配置名称(主键)
|
||||
Value string `gorm:"column:value;type:text"` // 配置值
|
||||
@@ -12,7 +11,7 @@ type Option struct {
|
||||
|
||||
// TableName 指定表名
|
||||
func (Option) TableName() string {
|
||||
return "typecho_options"
|
||||
return "options"
|
||||
}
|
||||
|
||||
// GetOptionValue 根据name和user获取配置值
|
||||
|
||||
@@ -1,18 +0,0 @@
|
||||
package models
|
||||
|
||||
type User struct {
|
||||
Name string
|
||||
Gender string
|
||||
Age int
|
||||
Password string
|
||||
PasswordHash []byte
|
||||
}
|
||||
|
||||
// GetUserByID 根据 ID 查询用户
|
||||
func GetUserByID(id uint) (*User, error) {
|
||||
var user User
|
||||
if err := DB.First(&user, id).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &user, nil
|
||||
}
|
||||
Reference in New Issue
Block a user