package model import ( "context" "errors" "project_management/app/e" "project_management/global" "project_management/unity" "project_management/utils" "strings" "time" ) type AppUser struct { //gorm.Model utils.BaseModel Username string `gorm:"type:varchar(50);index:username_name,unique" json:"username" 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"` // 密码 LastLoginTime utils.Time `gorm:"type:datetime;" json:"last_login_time"` // 最后登录时间 State int `gorm:"type:int;" json:"state"` // 状态 1:正常 2:禁用 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"` // 手机号 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"` // 验证码 RegistMethod int `gorm:"type:int;" json:"regist_method"` // 注册方式 1 短信登录 2 微信登录 3 系统添加 } type AppUserDel struct { ID int `json:"id" validate:"required"` AppID string `json:"app_id" validate:"required"` } // AccountLogin 账号登录 func (a AppUser) AccountLogin(appUser AppUserRegist) (e.Rescode, string) { //TODO implement me tableName := "appUser_" + appUser.AppID tx := global.DBLink.Table(tableName) if tx.Error != nil { if strings.Contains(tx.Error.Error(), "Error 1146 (42S02)") { return e.NOTTABLE, "" } return e.LOGINFAIL, "" } md5 := utils.MD5(appUser.Password) tx.Select("username", "id", "password", "nickname", "phone"). Where("username=? and password=?", appUser.Username, md5). Where("state=?", 1).First(&a) token, err := utils.CreateToken(a.ID, a.Username, "user") token = "interior:" + token if err != nil { return e.TokenIsFaild, "" } if tx.RowsAffected > 0 { global.DBLink.Table(tableName).Where("id=?", a.ID).Update("last_login_time", utils.Time(time.Now())) return e.SUCCESS, token } return e.ThePasswordIsWrongOrThePhoneNumberIsIncorrect, "" } // CodeLogin 验证码登录 func (a AppUser) CodeLogin(appUser AppUserRegist) (e.Rescode, string) { //TODO implement me result, err := global.Rdb.Get(context.Background(), appUser.Phone).Result() if err != nil { return e.TheVerificationCodeWasNotSent, "" } if result != appUser.Code { return e.CodeIsError, "" } tableName := "appUser_" + appUser.AppID tx := global.DBLink.Table(tableName) tx.Select("username", "id", "password", "nickname", "phone"). Where("phone=?", appUser.Phone).First(&a) token, err := utils.CreateToken(a.ID, a.Username, "user") token = "interior:" + token if a.Phone != "" { global.DBLink.Table(tableName).Where("id=?", a.ID).Update("last_login_time", utils.Time(time.Now())) return e.SUCCESS, token } else { return e.NOTREGIST, "" } } // DeleteAppUserByID 删除用户 func (a AppUser) DeleteAppUserByID(id int, tableName string) e.Rescode { //TODO implement me tx := global.DBLink.Table(tableName).Where("id=?", id).Delete(&a) if tx.Error != nil { return e.DELETEFAIL } return e.SUCCESS } // UpdateAppUser 更新用户 func (a AppUser) UpdateAppUser(appUser AppUser, tabaleName string) e.Rescode { //TODO implement me tx := global.DBLink.Table(tabaleName).Updates(appUser) if tx.Error != nil { return e.UPDATEFAIL } return e.SUCCESS } // GetAppUserList 获取用户列表 func (a AppUser) GetAppUserList(params unity.CapQueryPageParams, tableName string, queryCond string) (result []AppUser, total int64, err error) { query := global.DBLink.Table(tableName) if params.Query != "%%" { query = query.Where(queryCond, params.Query) } if params.State != "" { query = query.Where("state=?", params.State) } if err = query.Count(&total).Error; err != nil { if strings.Contains(err.Error(), "Error 1146 (42S02)") { return nil, 0, errors.New("当前系统没有该表,请检查appid是否正确") } return nil, 0, err } offset := (params.Page - 1) * params.Size if err = query.Offset(offset).Limit(params.Size).Order(params.Desc).Find(&result).Error; err != nil { return nil, 0, err } return result, total, nil } // 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 = 1 a.Phone = appUser.Phone a.Username = appUser.Username a.Nickname = "游客" + unity.RandomId() md5 := utils.MD5(appUser.Password) a.Password = md5 a.LastLoginTime = utils.Time(time.Now()) 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 = 1 a.Nickname = appUser.Nickname a.Username = appUser.Username a.Password = utils.MD5(appUser.Password) a.LastLoginTime = utils.Time(time.Now()) a.RegistMethod = 3 if err := global.DBLink.Table(tableName).Create(&a).Error; err != nil { if strings.Contains(err.Error(), "Duplicate entry") { return e.TheUserAlreadyExists } return e.AddAppUserFail } return e.SUCCESS } // GetAllUserCount 获取用户总数 func GetAllUserCount(tableName string) (e.Rescode, int64) { var count int64 tx := global.DBLink.Table(tableName).Count(&count) if tx.Error != nil { return e.ERROR, 0 } return e.SUCCESS, count }