package system import ( "errors" "fmt" "github.com/sirupsen/logrus" systemReq "lc-base-frame/model/system/request" "lc-base-frame/model/system/response" "time" "github.com/gofrs/uuid/v5" "gorm.io/gorm" "lc-base-frame/global" "lc-base-frame/model/common/request" "lc-base-frame/model/system" "lc-base-frame/utils" ) //@author: [piexlmax](https://github.com/piexlmax) //@function: Register //@description: 用户注册 //@param: u model.SysUser //@return: userInter system.SysUser, err error type UserService struct{} func (userService *UserService) Register(u system.SysUser) (userInter system.SysUser, err error) { var user system.SysUser if !errors.Is(global.Db.Where("username = ?", u.Username).First(&user).Error, gorm.ErrRecordNotFound) { // 判断用户名是否注册 return userInter, errors.New("用户名已注册") } // 否则 附加uuid 密码hash加密 注册 u.Password = utils.BcryptHash(u.Password) u.UUID = uuid.Must(uuid.NewV4()) err = global.Db.Create(&u).Error return u, err } //@author: [piexlmax](https://github.com/piexlmax) //@author: [SliverHorn](https://github.com/SliverHorn) //@function: Login //@description: 用户登录 //@param: u *model.SysUser //@return: err error, userInter *model.SysUser func (userService *UserService) Login(u *system.SysUser) (userInter *system.SysUser, err error) { if nil == global.Db { return nil, fmt.Errorf("db not init") } var user system.SysUser err = global.Db.Where("username = ?", u.Username).Preload("Authorities").Preload("Authority").First(&user).Error if err == nil { if ok := utils.BcryptCheck(u.Password, user.Password); !ok { return nil, errors.New("密码错误") } MenuServiceApp.UserAuthorityDefaultRouter(&user) } return &user, err //return nil, err } //@author: [piexlmax](https://github.com/piexlmax) //@function: ChangePassword //@description: 修改用户密码 //@param: u *model.SysUser, newPassword string //@return: userInter *model.SysUser,err error func (userService *UserService) ChangePassword(u *system.SysUser, newPassword string) (userInter *system.SysUser, err error) { var user system.SysUser if err = global.Db.Where("id = ?", u.ID).First(&user).Error; err != nil { return nil, err } if ok := utils.BcryptCheck(u.Password, user.Password); !ok { return nil, errors.New("原密码错误") } user.Password = utils.BcryptHash(newPassword) err = global.Db.Save(&user).Error return &user, err } //@author: [piexlmax](https://github.com/piexlmax) //@function: GetUserInfoList //@description: 分页获取数据 //@param: info request.PageInfo //@return: err error, list interface{}, total int64 func (userService *UserService) GetUserInfoList(info request.PageInfo) (list interface{}, total int64, err error) { limit := info.PageSize offset := info.PageSize * (info.Page - 1) db := global.Db.Model(&system.SysUser{}) var userList []system.SysUser err = db.Count(&total).Error if err != nil { return } err = db.Limit(limit).Offset(offset).Preload("Authorities").Preload("Authority").Find(&userList).Error return userList, total, err } //@author: [piexlmax](https://github.com/piexlmax) //@function: SetUserAuthority //@description: 设置一个用户的权限 //@param: uuid uuid.UUID, authorityId string //@return: err error func (userService *UserService) SetUserAuthority(id uint, authorityId uint) (err error) { assignErr := global.Db.Where("sys_user_id = ? AND sys_authority_authority_id = ?", id, authorityId).First(&system.SysUserAuthority{}).Error if errors.Is(assignErr, gorm.ErrRecordNotFound) { return errors.New("该用户无此角色") } err = global.Db.Where("id = ?", id).First(&system.SysUser{}).Update("authority_id", authorityId).Error return err } //@author: [piexlmax](https://github.com/piexlmax) //@function: SetUserAuthorities //@description: 设置一个用户的权限 //@param: id uint, authorityIds []string //@return: err error func (userService *UserService) SetUserAuthorities(id uint, authorityIds []uint) (err error) { return global.Db.Transaction(func(tx *gorm.DB) error { TxErr := tx.Delete(&[]system.SysUserAuthority{}, "sys_user_id = ?", id).Error if TxErr != nil { return TxErr } var useAuthority []system.SysUserAuthority for _, v := range authorityIds { useAuthority = append(useAuthority, system.SysUserAuthority{ SysUserId: id, SysAuthorityAuthorityId: v, }) } TxErr = tx.Create(&useAuthority).Error if TxErr != nil { return TxErr } TxErr = tx.Where("id = ?", id).First(&system.SysUser{}).Update("authority_id", authorityIds[0]).Error if TxErr != nil { return TxErr } // 返回 nil 提交事务 return nil }) } //@author: [piexlmax](https://github.com/piexlmax) //@function: DeleteUser //@description: 删除用户 //@param: id float64 //@return: err error func (userService *UserService) DeleteUser(id int) (err error) { var user system.SysUser err = global.Db.Where("id = ?", id).Delete(&user).Error if err != nil { return err } err = global.Db.Delete(&[]system.SysUserAuthority{}, "sys_user_id = ?", id).Error return err } //@author: [piexlmax](https://github.com/piexlmax) //@function: SetUserInfo //@description: 设置用户信息 //@param: reqUser model.SysUser //@return: err error, user model.SysUser func (userService *UserService) SetUserInfo(req system.SysUser) error { return global.Db.Model(&system.SysUser{}). Select("updated_at", "nick_name", "header_img", "phone", "email", "sideMode", "enable"). Where("id=?", req.ID). Updates(map[string]interface{}{ "updated_at": time.Now(), "nick_name": req.NickName, "header_img": req.HeaderImg, "phone": req.Phone, "email": req.Email, "side_mode": req.SideMode, "enable": req.Enable, }).Error } //@author: [piexlmax](https://github.com/piexlmax) //@function: SetUserInfo //@description: 设置用户信息 //@param: reqUser model.SysUser //@return: err error, user model.SysUser func (userService *UserService) SetSelfInfo(req system.SysUser) error { return global.Db.Model(&system.SysUser{}). Where("id=?", req.ID). Updates(req).Error } //@author: [piexlmax](https://github.com/piexlmax) //@author: [SliverHorn](https://github.com/SliverHorn) //@function: GetUserInfo //@description: 获取用户信息 //@param: uuid uuid.UUID //@return: err error, user system.SysUser func (userService *UserService) GetUserInfo(uuid uuid.UUID) (user system.SysUser, err error) { var reqUser system.SysUser err = global.Db.Preload("Authorities").Preload("Authority").First(&reqUser, "uuid = ?", uuid).Error if err != nil { return reqUser, err } MenuServiceApp.UserAuthorityDefaultRouter(&reqUser) return reqUser, err } //@author: [SliverHorn](https://github.com/SliverHorn) //@function: FindUserById //@description: 通过id获取用户信息 //@param: id int //@return: err error, user *model.SysUser func (userService *UserService) FindUserById(id int) (user *system.SysUser, err error) { var u system.SysUser err = global.Db.Where("`id` = ?", id).First(&u).Error return &u, err } //@author: [SliverHorn](https://github.com/SliverHorn) //@function: FindUserByUuid //@description: 通过uuid获取用户信息 //@param: uuid string //@return: err error, user *model.SysUser func (userService *UserService) FindUserByUuid(uuid string) (user *system.SysUser, err error) { var u system.SysUser if err = global.Db.Where("`uuid` = ?", uuid).First(&u).Error; err != nil { return &u, errors.New("用户不存在") } return &u, nil } //@author: [piexlmax](https://github.com/piexlmax) //@function: resetPassword //@description: 修改用户密码 //@param: ID uint //@return: err error func (userService *UserService) ResetPassword(ID uint) (err error) { err = global.Db.Model(&system.SysUser{}).Where("id = ?", ID).Update("password", utils.BcryptHash("123456")).Error return err } func (userService *UserService) UserId(ID uint) (ids []response.IdName, err error) { db := global.Db.Select("id, username").Model(&system.SysUser{}) if ID > 1 { db.Where("id = ?", ID) } err = db.Find(&ids).Error return } func (userService *UserService) SetAlarmEmail(uid uint, emails []systemReq.Email) string { var html = ` 邮箱确认

点击链接绑定龙弛防溺水报警邮箱通知,10分钟内有效

` //todo 修改ip var result string //已绑定的邮箱 v4, _ := uuid.NewV4() for _, v := range emails { //已绑定,continue var id uint global.Db.Select("id").Table("sys_user_email").Where("uid = ? AND email = ?", uid, v.Email).Scan(&id) if id != 0 { result += v.Email continue } //未绑定,发送确认邮件 s := v4.String() global.BlackCache.Set(s, v.Email, time.Minute*10) url := fmt.Sprintf(`http://192.168.110.69:8889/user/confirm?id=%d&code=%s`, uid, s) utils.Email(v.Email, "邮箱确认", fmt.Sprintf(html, url)) } return result } func (userService *UserService) ConfirmEmail(id string, code string) string { var html = ` 成功页面

邮箱绑定成功!

` var fail = `

绑定失败,请联系管理员。

` v, ok := global.BlackCache.Get(code) if !ok { return fail } email := v.(string) err := global.Db.Table("sys_user_email").Create(map[string]interface{}{"uid": id, "email": email, "enable": 0}).Error if err != nil { logrus.Error("邮箱绑定失败", err) return fail } return html } // UnbindEmail 解绑邮箱 func (userService *UserService) UnbindEmail(uid uint, emails []systemReq.Email) string { var errStr string for _, email := range emails { err := global.Db.Table("sys_user_email").Where("uid = ? AND email = ?", uid, email.Email).Update("enable", 1).Error if err != nil { errStr += email.Email + " " } } return errStr }