代码拉取完成,页面将自动刷新
package models
import (
"gitee.com/nuokwan_backend_group/mvc/Base"
"gitee.com/nuokwan_backend_group/mvc/components"
"time"
)
type User struct {
ID int64 `gorm:"primary_key,column:id;comment:'序号ID'" Tr:"id" json:"id"`
Username string `gorm:"unique;column:username;not null;comment;'用户名'" sql:"index" Tr:"username" json:"username" `
Mobile string `gorm:"column:mobile;comment:'手机号'" Tr:"mobile" json:"mobile"`
Email string `gorm:"column:email;comment:'邮箱地址'" Tr:"email" json:"email"`
Sex string `gorm:"column:sex;comment:'性别';default:'未知'" Tr:"sex" json:"sex" `
Password string `gorm:"column:password;comment:'秘文密码';" Tr:"password" json:"password"`
CreatedAt time.Time `gorm:"column:created_at;not null;comment:'创建时间';" Tr:"createdAt" json:"createdAt"`
UpdatedAt time.Time `gorm:"unique;column:updated_at;null;comment:'更新时间';" Tr:"updatedAt" json:"updatedAt"`
DeletedAt time.Time `gorm:"column:deleted_at;null;comment:'软删除时间';" sql:"index" Tr:"deletedAt" json:"deletedAt"`
}
const UserTable = "users"
type UserWrapper struct {
M *User
ModelWrapper
}
func NewUser() *User {
return &User{}
}
func NewUserWrapper(u ...*User) *UserWrapper {
if len(u) == 0 {
return &UserWrapper{M: &User{}}
}
return &UserWrapper{M: u[0]}
}
func (User) TableName() string {
return UserTable
}
func (u *UserWrapper) Table() string {
return u.GetModel().TableName()
}
func (u *UserWrapper) SetModel(user *User) *UserWrapper {
u.M = user
return u
}
func (u *UserWrapper) Model() *User {
return u.GetModel()
}
func (u *UserWrapper) GetModel() *User {
if u.M == nil {
return NewUser()
}
return u.M
}
func (u *UserWrapper) Create(user User) int64 {
user.Password = u.EncodePassword(user.Password)
u.GetOrm().Table(u.Table()).Create(&user)
return user.ID
}
func (u *UserWrapper) EncodePassword(password string) string {
if p, ok := components.NilPassStr.New(password); ok {
return p.ToString()
}
Base.OnError(Base.LogicError("加密密码失败 :" + password))
return ""
}
func (u *UserWrapper) VerifyPassword(password string, pass2 string) bool {
if password == "" || pass2 == "" {
return false
}
return components.EqualPassword(password, pass2)
}
func (u *UserWrapper) GetById(id int64) (*User, error) {
var user = NewUser()
u.GetOrm().Table(u.Table()).First(&user, id)
if user.ID == 0 {
return user, NotExists
}
return user, nil
}
func (u *UserWrapper) Exists(user *User) bool {
if user.ID != 0 {
if _, err := u.GetById(user.ID); err != nil {
return false
}
}
if user.Username != "" {
if _, err := u.GetByUserName(user.Username); err == nil {
return true
}
}
if user.Mobile != "" {
if _, ok := u.GetByMobile(user.Mobile); ok {
return ok
}
}
if user.Email != "" {
if _, ok := u.GetByEmail(user.Email); ok {
return ok
}
}
return false
}
func (u *UserWrapper) GetByUserName(name string) (*User, error) {
var user = NewUser()
u.GetOrm().Table(u.Table()).Where("username=?", name).First(user)
if user.ID == 0 {
return user, Base.LogicError("记录不存在")
}
return user, nil
}
func (u *UserWrapper) GetPasswordByUserName(name string) (string, bool) {
var user, err = u.GetByUserName(name)
if err != nil {
return "", false
}
return user.Password, true
}
func (u *UserWrapper) UpdatePassword(id int64, password string) bool {
if password == "" {
return false
}
u.GetOrm().Table(u.Table()).Where("id=?", id).Update("password", u.EncodePassword(password))
return true
}
func (u *UserWrapper) Update(user *User, update map[string]interface{}) bool {
if user == nil {
return false
}
u.GetOrm().Model(user).Updates(update)
return true
}
func (u *UserWrapper) Save(user *User, data map[string]interface{}) bool {
u.GetOrm().Model(user).Save(data)
return true
}
func (u *UserWrapper) GetByMobile(mobile string) (*User, bool) {
return u.GetByKey("mobile", mobile)
}
func (u *UserWrapper) GetByEmail(email string) (*User, bool) {
return u.GetByKey("email", email)
}
func (u *UserWrapper) GetByKey(key string, value interface{}) (*User, bool) {
var user = NewUser()
u.GetOrm().Table(u.Table()).Where(key+"=?", value).First(&user)
if user.ID == 0 {
return user, false
}
return user, true
}
func (u *UserWrapper) Lists(conditions map[string]interface{}, result interface{}) error {
return nil
}
func (u *UserWrapper) Search(conditions map[string]interface{}, result interface{}) error {
return nil
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。