123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212 |
- package system
- import (
- "errors"
- "fmt"
- "server/dao/system"
- "time"
- "github.com/gofrs/uuid/v5"
- "gorm.io/gorm"
- "server/global"
- "server/model/common/request"
- "server/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) {
- _, err = system.QueryUserByUserName(u.Username)
- if !errors.Is(err, gorm.ErrRecordNotFound) { // 判断用户名是否注册
- return userInter, errors.New("用户名已注册")
- }
- // 否则 附加uuid 密码hash加密 注册
- u.Password = utils.BcryptHash(u.Password)
- u.UUID = uuid.Must(uuid.NewV4())
- err = u.CreateUser()
- 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.GVA_DB {
- return nil, fmt.Errorf("db not init")
- }
- user, err := system.QueryUserAndAuthorityByUserName(u.Username)
- if err == nil {
- if ok := utils.BcryptCheck(u.Password, user.Password); !ok {
- return nil, errors.New("密码错误")
- }
- MenuServiceApp.UserAuthorityDefaultRouter(&user)
- }
- return &user, 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) {
- user, err := system.QueryUserByUserId(u.ID)
- if err != nil {
- return nil, err
- }
- if ok := utils.BcryptCheck(u.Password, user.Password); !ok {
- return nil, errors.New("原密码错误")
- }
- user.Password = utils.BcryptHash(newPassword)
- err = u.UpdateUser()
- 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)
- userList, total, err := system.QueryUserInfoList(limit, offset)
- 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 := system.QueryUserAuthority(id, authorityId)
- if errors.Is(assignErr, gorm.ErrRecordNotFound) {
- return errors.New("该用户无此角色")
- }
- return system.UpdateUserAuthority(id, authorityId)
- }
- //@author: [piexlmax](https://github.com/piexlmax)
- //@function: SetUserAuthorities
- //@description: 设置一个用户的权限
- //@param: id uint, authorityIds []string
- //@return: err error
- func (userService *UserService) SetUserAuthorities(id uint, authorityId uint) (err error) {
- return system.SetUserAuthorities(id, authorityId)
- }
- //@author: [piexlmax](https://github.com/piexlmax)
- //@function: DeleteUser
- //@description: 删除用户
- //@param: id float64
- //@return: err error
- func (userService *UserService) DeleteUser(id int) (err error) {
- return system.DeleteUser(id)
- }
- //@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 {
- req.UpdatedAt = time.Now()
- return req.SetUserInfo()
- }
- //@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 req.SetUserInfoById()
- }
- //@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) {
- reqUser, err := system.GetUserInfoByUUID(uuid)
- 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) {
- u, err := system.QueryUserByUserId(uint(id))
- 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(uuidStr string) (user *system.SysUser, err error) {
- id, err := uuid.FromString(uuidStr)
- if err != nil {
- fmt.Println("Error parsing UUID:", err)
- return
- }
- u, err := system.GetUserInfoByUUID(id)
- if 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) error {
- return system.SysUser{
- GVA_MODEL: global.GVA_MODEL{
- ID: ID,
- },
- Password: utils.BcryptHash("123456"),
- }.SetUserInfoById()
- }
|