You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

87 lines
2.0 KiB

2 years ago
/**
* @Auth:ShenZ
* @Description:
* @CreateDate:2022/06/15 14:57:55
*/
package models
import (
"fmt"
"ginchat/utils"
"time"
"gorm.io/gorm"
)
type UserBasic struct {
gorm.Model
Name string
PassWord string
Phone string `valid:"matches(^1[3-9]{1}\\d{9}$)"`
Email string `valid:"email"`
Avatar string //头像
Identity string
ClientIp string
ClientPort string
Salt string
LoginTime time.Time
HeartbeatTime time.Time
LoginOutTime time.Time `gorm:"column:login_out_time" json:"login_out_time"`
IsLogout bool
DeviceInfo string
}
func (table *UserBasic) TableName() string {
return "user_basic"
}
func GetUserList() []*UserBasic {
data := make([]*UserBasic, 10)
utils.DB.Find(&data)
for _, v := range data {
fmt.Println(v)
}
return data
}
func FindUserByNameAndPwd(name string, password string) UserBasic {
user := UserBasic{}
utils.DB.Where("name = ? and pass_word=?", name, password).First(&user)
//token加密
str := fmt.Sprintf("%d", time.Now().Unix())
temp := utils.MD5Encode(str)
utils.DB.Model(&user).Where("id = ?", user.ID).Update("identity", temp)
return user
}
func FindUserByName(name string) UserBasic {
user := UserBasic{}
utils.DB.Where("name = ?", name).First(&user)
return user
}
func FindUserByPhone(phone string) *gorm.DB {
user := UserBasic{}
return utils.DB.Where("Phone = ?", phone).First(&user)
}
func FindUserByEmail(email string) *gorm.DB {
user := UserBasic{}
return utils.DB.Where("email = ?", email).First(&user)
}
func CreateUser(user UserBasic) *gorm.DB {
return utils.DB.Create(&user)
}
func DeleteUser(user UserBasic) *gorm.DB {
return utils.DB.Delete(&user)
}
func UpdateUser(user UserBasic) *gorm.DB {
return utils.DB.Model(&user).Updates(UserBasic{Name: user.Name, PassWord: user.PassWord, Phone: user.Phone, Email: user.Email, Avatar: user.Avatar})
}
//查找某个用户
func FindByID(id uint) UserBasic {
user := UserBasic{}
utils.DB.Where("id = ?", id).First(&user)
return user
}