gocron/models/user.go

119 lines
3.1 KiB
Go
Raw Normal View History

2017-03-10 09:24:06 +00:00
package models
import (
2017-04-07 01:22:00 +00:00
"github.com/ouqiang/gocron/modules/utils"
2017-04-02 02:38:49 +00:00
"time"
2017-03-10 09:24:06 +00:00
)
2017-04-02 02:19:52 +00:00
const PasswordSaltLength = 6
2017-03-10 09:24:06 +00:00
// 用户model
2017-04-02 02:19:52 +00:00
type User struct {
2017-04-02 02:38:49 +00:00
Id int `xorm:"pk autoincr notnull "`
Name string `xorm:"varchar(32) notnull unique"` // 用户名
Password string `xorm:"char(32) notnull "` // 密码
Salt string `xorm:"char(6) notnull "` // 密码盐值
Email string `xorm:"varchar(50) notnull unique default '' "` // 邮箱
Created time.Time `xorm:"datetime notnull created"`
Updated time.Time `xorm:"datetime updated"`
Deleted time.Time `xorm:"datetime deleted"`
IsAdmin int8 `xorm:"tinyint notnull default 0"` // 是否是管理员 1:管理员 0:普通用户
Status Status `xorm:"tinyint notnull default 1"` // 1: 正常 0:禁用
Page int `xorm:"-"`
PageSize int `xorm:"-"`
2017-03-10 09:24:06 +00:00
}
// 新增
2017-04-02 02:19:52 +00:00
func (user *User) Create() (insertId int, err error) {
2017-04-02 02:38:49 +00:00
user.Status = Enabled
user.Salt = user.generateSalt()
user.Password = user.encryptPassword(user.Password, user.Salt)
2017-03-10 09:24:06 +00:00
2017-04-02 02:38:49 +00:00
_, err = Db.Insert(user)
if err == nil {
insertId = user.Id
}
2017-03-24 09:55:44 +00:00
2017-04-02 02:38:49 +00:00
return
2017-03-10 09:24:06 +00:00
}
// 更新
2017-04-02 02:19:52 +00:00
func (user *User) Update(id int, data CommonMap) (int64, error) {
2017-04-02 02:38:49 +00:00
return Db.Table(user).ID(id).Update(data)
2017-03-10 09:24:06 +00:00
}
// 删除
2017-04-02 02:19:52 +00:00
func (user *User) Delete(id int) (int64, error) {
2017-04-02 02:38:49 +00:00
return Db.Id(id).Delete(user)
2017-03-10 09:24:06 +00:00
}
// 禁用
2017-04-02 02:19:52 +00:00
func (user *User) Disable(id int) (int64, error) {
2017-04-02 02:38:49 +00:00
return user.Update(id, CommonMap{"status": Disabled})
2017-03-10 09:24:06 +00:00
}
// 激活
2017-04-02 02:19:52 +00:00
func (user *User) Enable(id int) (int64, error) {
2017-04-02 02:38:49 +00:00
return user.Update(id, CommonMap{"status": Enabled})
2017-03-10 09:24:06 +00:00
}
// 验证用户名和密码
2017-04-02 02:19:52 +00:00
func (user *User) Match(username, password string) bool {
2017-04-02 02:38:49 +00:00
where := "(name = ? OR email = ?)"
_, err := Db.Where(where, username, username).Get(user)
if err != nil {
return false
}
hashPassword := user.encryptPassword(password, user.Salt)
if hashPassword != user.Password {
return false
}
return true
2017-03-10 09:24:06 +00:00
}
// 用户名是否存在
2017-04-02 02:19:52 +00:00
func (user *User) UsernameExists(username string) (int64, error) {
2017-04-02 02:38:49 +00:00
return Db.Where("name = ?", username).Count(user)
2017-03-10 09:24:06 +00:00
}
// 邮箱地址是否存在
2017-04-02 02:19:52 +00:00
func (user *User) EmailExists(email string) (int64, error) {
2017-04-02 02:38:49 +00:00
return Db.Where("email = ?", email).Count(user)
2017-03-10 09:24:06 +00:00
}
2017-04-02 02:19:52 +00:00
func (user *User) List() ([]User, error) {
2017-04-02 02:38:49 +00:00
user.parsePageAndPageSize()
list := make([]User, 0)
err := Db.Desc("id").Limit(user.PageSize, user.pageLimitOffset()).Find(&list)
2017-03-10 09:24:06 +00:00
2017-04-02 02:38:49 +00:00
return list, err
2017-03-10 09:24:06 +00:00
}
2017-04-02 02:19:52 +00:00
func (user *User) Total() (int64, error) {
2017-04-02 02:38:49 +00:00
return Db.Count(user)
2017-03-10 09:24:06 +00:00
}
2017-04-02 02:19:52 +00:00
func (user *User) parsePageAndPageSize() {
2017-04-02 02:38:49 +00:00
if user.Page <= 0 {
user.Page = Page
}
if user.PageSize >= 0 || user.PageSize > MaxPageSize {
user.PageSize = PageSize
}
2017-03-10 09:24:06 +00:00
}
2017-04-02 02:19:52 +00:00
func (user *User) pageLimitOffset() int {
2017-04-02 02:38:49 +00:00
return (user.Page - 1) * user.PageSize
2017-03-10 09:24:06 +00:00
}
// 密码加密
2017-04-02 02:19:52 +00:00
func (user *User) encryptPassword(password, salt string) string {
2017-04-02 02:38:49 +00:00
return utils.Md5(password + salt)
2017-03-10 09:24:06 +00:00
}
// 生成密码盐值
2017-04-02 02:19:52 +00:00
func (user *User) generateSalt() string {
2017-04-02 02:38:49 +00:00
return utils.RandString(PasswordSaltLength)
2017-04-02 02:19:52 +00:00
}