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.

89 lines
2.9 KiB

package dbModel
import (
"github.com/jinzhu/gorm"
"github.com/pkg/errors"
uuid "github.com/satori/go.uuid"
"main/controller/servers"
"main/init/qmsql"
"main/model/modelInterface"
"main/tools"
)
type User struct {
gorm.Model `json:"-"`
UUID uuid.UUID `json:"uuid"`
UserName string `json:"userName"`
PassWord string `json:"passWord"`
NickName string `json:"nickName" gorm:"default:'QMPlusUser'"`
HeaderImg string `json:"headerImg" gorm:"default:'http://www.henrongyi.top/avatar/lufu.jpg'"`
Authority Authority `json:"authority" form:"ForeignKey:AuthorityId;AssociationForeignKey:AuthorityId"`
AuthorityId float64 `json:"authorityId" gorm:"default:888"`
//Propertie // 多余属性自行添加
//PropertieId uint // 自动关联 Propertie 的Id 附加属性过多 建议创建一对一关系
}
//type Propertie struct {
// gorm.Model
//}
//注册接口model方法
func (u *User) Regist() (err error, userInter *User) {
var user User
//判断用户名是否注册
findErr := qmsql.DEFAULTDB.Where("user_name = ?", u.UserName).First(&user).Error
//err为nil表明读取到了 不能注册
if findErr == nil {
return errors.New("用户名已注册"), nil
} else {
// 否则 附加uuid 密码md5简单加密 注册
u.PassWord = tools.MD5V(u.PassWord)
u.UUID = uuid.NewV4()
err = qmsql.DEFAULTDB.Create(u).Error
}
return err, u
}
//修改用户密码
func (u *User) ChangePassWord(newPassWord string) (err error, userInter *User) {
var user User
//后期修改jwt+password模式
u.PassWord = tools.MD5V(u.PassWord)
err = qmsql.DEFAULTDB.Where("user_name = ? AND pass_word = ?", u.UserName, u.PassWord).First(&user).Update("pass_word", tools.MD5V(newPassWord)).Error
return err, u
}
//用户更新接口
func (u *User) UpdataUser() (err error, userInter *User) {
err = qmsql.DEFAULTDB.Create(u).Error
return err, u
}
//用户登录
func (u *User) Login() (err error, userInter *User) {
var user User
u.PassWord = tools.MD5V(u.PassWord)
err = qmsql.DEFAULTDB.Where("user_name = ? AND pass_word = ?", u.UserName, u.PassWord).First(&user).Error
err = qmsql.DEFAULTDB.Model(&user).Related(&user.Authority).Error
return err, &user
}
// 用户头像上传更新地址
func (u *User) UploadHeaderImg(userName string, filePath string) (err error, userInter *User) {
var user User
err = qmsql.DEFAULTDB.Where("user_name = ?", userName).First(&user).Update("header_img", filePath).First(&user).Error
return err, &user
}
// 分页获取数据 需要分页实现这个接口即可
func (u *User) GetInfoList(info modelInterface.PageInfo) (err error, list interface{}, total int) {
// 封装分页方法 调用即可 传入 当前的结构体和分页信息
err, db, total := servers.PagingServer(u, info)
if err != nil {
return
} else {
var userList []User
err = db.Find(&userList).Error
return err, userList, total
}
}