123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- package model
- import (
- "context"
- "project_management/app/e"
- "project_management/global"
- "project_management/unity"
- "project_management/utils"
- "time"
- )
- type AppUser struct {
- //gorm.Model
- utils.BaseModel
- Username string `gorm:"type:varchar(50);index:username_name,unique" json:"username" validate:"required" min:"3" max:"50"` // 用户名
- Phone string `gorm:"type:varchar(50);" json:"phone" min:"11" max:"11"` // 手机号
- Nickname string `gorm:"type:varchar(50);" json:"nickname"` // 昵称
- Password string `gorm:"type:varchar(50);" json:"password" validate:"required"` // 密码
- LastLoginTime string `gorm:"type:datetime;" json:"last_login_time"` // 最后登录时间
- State int `gorm:"type:int;" json:"state"` // 状态 0:正常 1:禁用
- AppID string `gorm:"type:varchar(50);" json:"app_id" validate:"required"` // 应用id
- RegistMethod int `gorm:"type:int;" json:"regist_method"` // 注册方式 1 短信登录 2 微信登录 3 系统添加
- }
- type AppUserRegist struct {
- Username string `gorm:"type:varchar(50);index:username_name,unique" json:"username" validate:"required" min:"3" max:"50"` // 用户名
- Phone string `gorm:"type:varchar(50);" json:"phone" validate:"required" min:"11" max:"11"` // 手机号
- Nickname string `gorm:"type:varchar(50);" json:"nickname"` // 昵称
- Password string `gorm:"type:varchar(50);" json:"password" validate:"required" min:"6" max:"20"` // 密码
- AppID string `gorm:"type:varchar(50);not null;unique" json:"app_id" validate:"required"` // 应用id
- Code string `json:"code" validate:"required" min:"6" max:"6"` // 验证码
- RegistMethod int `gorm:"type:int;" json:"regist_method"` // 注册方式 1 短信登录 2 微信登录 3 系统添加
- }
- // RegistAppUser 注册用户
- func (a AppUser) RegistAppUser(appUser AppUserRegist) e.Rescode {
- //TODO implement me
- tx := global.DBLink.Select("phone").Where("phone = ?", appUser.Phone).First(&a)
- ctx := context.Background()
- result, err := global.Rdb.Get(ctx, appUser.Phone).Result()
- if err != nil {
- return e.TheVerificationCodeWasNotSent
- } else if result != appUser.Code {
- return e.CodeIsError
- }
- if tx.RowsAffected == 0 {
- a.AppID = appUser.AppID
- a.State = 0
- a.Phone = appUser.Phone
- a.Username = appUser.Username
- a.Nickname = "游客" + unity.RandomId()
- md5 := utils.MD5(appUser.Password)
- a.Password = md5
- a.LastLoginTime = time.Now().Format("2006-01-02 15:04:05")
- a.RegistMethod = appUser.RegistMethod
- tableName := "appUser_" + appUser.AppID
- //验证表是否已经创建,没有则创建
- table := global.DBLink.Migrator().HasTable(tableName)
- if !table {
- global.DBLink.Table(tableName).AutoMigrate(&AppUser{})
- }
- db := global.DBLink.Table(tableName).Create(&a)
- if db.Error != nil {
- return e.RegistrationFailed
- }
- return e.SUCCESS
- }
- return e.AlreadyExists
- }
- // AddAppUser 添加用户
- func (a AppUser) AddAppUser(appUser AppUser) e.Rescode {
- //TODO implement me
- //验证表是否已经创建,没有则创建
- tableName := "appUser_" + appUser.AppID
- table := global.DBLink.Migrator().HasTable(tableName)
- if !table {
- global.DBLink.Table(tableName).AutoMigrate(&AppUser{})
- }
- a.AppID = appUser.AppID
- a.State = 0
- a.Nickname = appUser.Nickname
- a.Username = appUser.Username
- a.Password = utils.MD5(appUser.Password)
- a.LastLoginTime = time.Now().Format("2006-01-02 15:04:05")
- a.RegistMethod = 3
- if err := global.DBLink.Table(tableName).Create(&a).Error; err != nil {
- return e.AddAppUserFail
- }
- return e.SUCCESS
- }
|