zhangqian
2023-08-14 676ef551324d415ed5280166407c686481c6f51f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
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,
    })
}