add models and jwt
This commit is contained in:
56
models/init.go
Normal file
56
models/init.go
Normal file
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
@Time : 2020/6/28 21:46
|
||||
@Author : xuyiqing
|
||||
@File : init.py
|
||||
*/
|
||||
|
||||
package models
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"go_blog/conf"
|
||||
"go_blog/pkg/util"
|
||||
"time"
|
||||
|
||||
"github.com/jinzhu/gorm"
|
||||
_ "github.com/jinzhu/gorm/dialects/mysql"
|
||||
)
|
||||
|
||||
var DB *gorm.DB
|
||||
|
||||
func SetUp(isOrmDebug bool) {
|
||||
conUri := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=%s&parseTime=True&loc=Local",
|
||||
conf.DataBase.User,
|
||||
conf.DataBase.Password,
|
||||
conf.DataBase.Host,
|
||||
conf.DataBase.Port,
|
||||
conf.DataBase.DB,
|
||||
conf.DataBase.Charset)
|
||||
db, err := gorm.Open(conf.DataBase.Type, conUri)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
DB = db
|
||||
DB.LogMode(isOrmDebug)
|
||||
gorm.DefaultTableNameHandler = func(db *gorm.DB, defaultTableName string) string {
|
||||
return conf.DataBase.Prefix + defaultTableName
|
||||
}
|
||||
|
||||
DB.AutoMigrate(&Account{})
|
||||
|
||||
}
|
||||
|
||||
type BaseModel struct {
|
||||
ID uint64 `gorm:"primary_key'" json:"id"`
|
||||
CreatedAt time.Time `json:"-"`
|
||||
UpdatedAt time.Time `json:"-"`
|
||||
DeletedAt *time.Time `sql:"index" json:"-"`
|
||||
}
|
||||
|
||||
// 生成全局唯一ID
|
||||
func (m *BaseModel) BeforeCreate(scope *gorm.Scope) error {
|
||||
if m.ID == 0 {
|
||||
m.ID = util.GenSonyFlakeId()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
68
models/users.go
Normal file
68
models/users.go
Normal file
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
@Time : 2020/6/28 22:01
|
||||
@Author : xuyiqing
|
||||
@File : users.py
|
||||
*/
|
||||
|
||||
package models
|
||||
|
||||
import (
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
const PasswordCryptLevel = 12
|
||||
|
||||
type Account struct {
|
||||
BaseModel
|
||||
Username string `gorm:"column:username;not null;unique_index;comment:'用户名'" json:"username" form:"username"`
|
||||
Password string `gorm:"column:password;comment:'密码'" form:"password" json:"-"`
|
||||
Name string `form:"name" json:"name"`
|
||||
IsActive bool `json:"-"`
|
||||
}
|
||||
|
||||
func (a *Account) TableName() string {
|
||||
return "user_accounts"
|
||||
}
|
||||
|
||||
func (a *Account) GetUserByID(id uint) *Account {
|
||||
DB.Model(&Account{}).First(a, id)
|
||||
if a.ID > 0 {
|
||||
return a
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// 设置密码加密
|
||||
func (a *Account) SetPassword(password string) error {
|
||||
p, err := bcrypt.GenerateFromPassword([]byte(password), PasswordCryptLevel)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
a.Password = string(p)
|
||||
return nil
|
||||
}
|
||||
|
||||
// 验证登录帐户密码合法性
|
||||
func (a *Account) CheckPassword() bool {
|
||||
password := a.Password
|
||||
DB.Where("username = ?", a.Username).First(&a)
|
||||
err := bcrypt.CompareHashAndPassword([]byte(a.Password), []byte(password))
|
||||
return err == nil
|
||||
}
|
||||
|
||||
// 验证登录帐户密码合法性
|
||||
func (a *Account) IsPasswordEqual(password string) bool {
|
||||
err := bcrypt.CompareHashAndPassword([]byte(a.Password), []byte(password))
|
||||
return err == nil
|
||||
}
|
||||
|
||||
// 验证用户民重复
|
||||
func (a *Account) CheckDuplicateUsername() bool {
|
||||
var count int
|
||||
if DB.Model(&Account{}).Where("username=?", a.Username).Count(&count); count > 0 {
|
||||
return false
|
||||
} else {
|
||||
return true
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user