package service
|
|
import (
|
"apsClient/constvar"
|
"apsClient/model"
|
"apsClient/model/request"
|
"apsClient/pkg/ecode"
|
"apsClient/pkg/encrypt"
|
"errors"
|
"gorm.io/gorm"
|
)
|
|
type UserService struct{}
|
|
func (userService *UserService) Login(u *model.User) (userInter *model.User, errCode int) {
|
userInter, err := model.NewUserSearch(nil).SetUserName(u.Username).First()
|
if err != nil {
|
return nil, ecode.UserNotExist
|
}
|
|
if ok := encrypt.BcryptCheck(u.Password, userInter.Password); !ok {
|
return nil, ecode.PasswordErr
|
}
|
|
if userInter.UserType == constvar.UserTypeSub {
|
parentUser, _ := model.NewUserSearch(nil).SetId(userInter.ParentId).First()
|
userInter.CompanyLogo = parentUser.CompanyLogo
|
userInter.SystemName = parentUser.SystemName
|
}
|
|
return userInter, ecode.OK
|
}
|
|
func (userService *UserService) Register(u *model.User) (userInter *model.User, errCode int) {
|
_, err := model.NewUserSearch(nil).SetUserName(u.Username).First()
|
if err != gorm.ErrRecordNotFound {
|
return userInter, ecode.UserNameExistErr
|
}
|
|
err = model.NewUserSearch(nil).Create(u)
|
return u, ecode.OK
|
}
|
|
func (userService *UserService) ChangePassword(u *model.User, newPassword string) (userInter *model.User, errCode int) {
|
user, err := model.NewUserSearch(nil).SetId(u.ID).First()
|
if err != nil {
|
return nil, ecode.UserNotExist
|
}
|
|
if ok := encrypt.BcryptCheck(u.Password, user.Password); !ok {
|
return nil, ecode.PasswordErr
|
}
|
|
user.Password = encrypt.BcryptHash(newPassword)
|
err = model.NewUserSearch(nil).SetId(u.ID).UpdateByMap(map[string]interface{}{
|
"password": user.Password})
|
|
return user, ecode.OK
|
|
}
|
|
func (userService *UserService) DeleteUser(id string) (err error) {
|
user, err := model.NewUserSearch(nil).SetId(id).First()
|
if err != nil {
|
return err
|
}
|
if user.Ip != "" {
|
return errors.New("该用户已配置集群,无法删除")
|
}
|
if user.UserType != constvar.UserTypeSub {
|
return errors.New("该用户非子账户,无法删除")
|
}
|
|
err = model.NewUserSearch(nil).SetId(id).Delete()
|
if err != nil {
|
return err
|
}
|
|
return model.NewUserMenuSearch(nil).SetUserId(id).Delete()
|
}
|
|
func (userService *UserService) SetUserInfo(req model.User) error {
|
return model.NewUserSearch(nil).SetId(req.ID).UpdateByMap(map[string]interface{}{
|
"nick_name": req.NickName,
|
"phone": req.Phone,
|
"pos": req.Pos,
|
})
|
}
|
|
func (userService *UserService) SetSelfInfo(userId string, req request.SetSelfInfo) error {
|
return model.NewUserSearch(nil).SetId(userId).UpdateByMap(map[string]interface{}{
|
"nick_name": req.NickName,
|
"phone": req.Phone,
|
"companyName": req.CompanyName,
|
"companyEmail": req.CompanyEmail,
|
"companyContact": req.CompanyContact,
|
"companyProvince": req.CompanyProvince,
|
"companyCity": req.CompanyCity,
|
"companyTrade": req.CompanyTrade,
|
"headerImage": req.HeaderImage,
|
})
|
}
|
|
func (userService *UserService) GetUserInfo(id string) (user *model.User, err error) {
|
user, err = model.NewUserSearch(nil).SetId(id).First()
|
if err != nil {
|
return
|
}
|
|
if user.UserType == constvar.UserTypeSub {
|
parentUser, _ := model.NewUserSearch(nil).SetId(user.ParentId).First()
|
user.CompanyLogo = parentUser.CompanyLogo
|
user.SystemName = parentUser.SystemName
|
}
|
return
|
}
|
|
func (userService *UserService) ResetPassword(id string) (err error) {
|
return model.NewUserSearch(nil).SetId(id).UpdateByMap(map[string]interface{}{
|
"password": encrypt.BcryptHash("123456"),
|
})
|
}
|
|
func (userService *UserService) SetOtherInfo(userId string, req request.SetOtherInfo) error {
|
return model.NewUserSearch(nil).SetId(userId).UpdateByMap(map[string]interface{}{
|
"company_logo": req.CompanyLogo,
|
"system_name": req.SystemName,
|
})
|
}
|