/
user.go
234 lines (190 loc) · 5.76 KB
/
user.go
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package user
import (
"context"
"errors"
"fmt"
"github.com/gogf/gf/v2/net/ghttp"
"github.com/iimeta/fastapi-admin/internal/consts"
"github.com/iimeta/fastapi-admin/internal/dao"
"github.com/iimeta/fastapi-admin/internal/model"
"github.com/iimeta/fastapi-admin/internal/model/do"
"github.com/iimeta/fastapi-admin/internal/service"
"github.com/iimeta/fastapi-admin/utility/crypto"
"github.com/iimeta/fastapi-admin/utility/logger"
"github.com/iimeta/fastapi-admin/utility/redis"
"github.com/iimeta/fastapi-admin/utility/util"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
)
type sUser struct{}
func init() {
service.RegisterUser(New())
}
func New() service.IUser {
return &sUser{}
}
// 用户更新信息
func (s *sUser) UpdateInfo(ctx context.Context, params model.UserUpdateInfoReq) error {
if err := dao.User.UpdateById(ctx, service.Session().GetUid(ctx), &do.User{
Name: params.Name,
}); err != nil {
logger.Error(ctx, err)
return err
}
user := service.Session().GetUser(ctx)
user.Name = params.Name
if err := service.Session().UpdateUserSession(ctx, user); err != nil {
logger.Error(ctx, err)
return err
}
return nil
}
// 用户更改密码
func (s *sUser) ChangePassword(ctx context.Context, params model.UserChangePasswordReq) (err error) {
uid := service.Session().GetUserId(ctx)
defer func() {
if err != nil {
val, _ := redis.Incr(ctx, fmt.Sprintf(consts.LOCK_CHANGE_PASSWORD, uid))
if val == 1 {
_, _ = redis.Expire(ctx, fmt.Sprintf(consts.LOCK_CHANGE_PASSWORD, uid), 30*60) // 锁定30分钟
}
} else {
_, _ = redis.Del(ctx, fmt.Sprintf(consts.LOCK_CHANGE_PASSWORD, uid))
}
}()
if val, err := redis.GetInt(ctx, fmt.Sprintf(consts.LOCK_CHANGE_PASSWORD, uid)); err == nil && val >= 5 {
return errors.New("失败次数过多, 请稍后再试")
}
user, err := dao.User.FindUserByUserId(ctx, uid)
if err != nil || user.Id == "" {
return errors.New("用户不存在")
}
account, err := dao.User.FindAccountByUserId(ctx, user.UserId)
if err != nil {
logger.Error(ctx, err)
return errors.New("账号信息有误")
}
if !crypto.VerifyPassword(account.Password, params.OldPassword+account.Salt) {
return errors.New("登录密码有误, 请重新输入")
}
if err = dao.User.ChangePasswordByUserId(ctx, uid, params.NewPassword); err != nil {
logger.Error(ctx, err)
return errors.New("修改密码失败")
}
return nil
}
// 用户更改邮箱
func (s *sUser) ChangeEmail(ctx context.Context, params model.UserChangeEmailReq) error {
if !service.Common().VerifyCode(ctx, consts.CHANNEL_CHANGE_EMAIL, params.Email, params.Code) {
return errors.New("邮件验证码填写错误")
}
user, err := dao.User.FindUserByUserId(ctx, service.Session().GetUserId(ctx))
if err != nil {
logger.Error(ctx, err)
return err
}
account, err := dao.User.FindAccountByUserId(ctx, user.UserId)
if err != nil {
logger.Error(ctx, err)
return errors.New("账号信息有误")
}
if !crypto.VerifyPassword(account.Password, params.Password+account.Salt) {
return errors.New("登录密码有误, 请重新输入")
}
defer func() {
_ = service.Common().DelCode(ctx, consts.CHANNEL_CHANGE_EMAIL, params.Email)
}()
if user.Email == params.Email {
return errors.New("邮箱与原邮箱一致无需修改")
}
if dao.User.IsAccountExist(ctx, params.Email) {
return errors.New(params.Email + " 邮箱已被其它账号使用")
}
if err = dao.User.UpdateById(ctx, user.Id, bson.M{
"email": params.Email,
}); err != nil {
logger.Error(ctx, err)
return errors.New("邮箱修改失败")
}
if account.Account == user.Email {
if err = dao.User.ChangeAccountById(ctx, account.Id, params.Email); err != nil {
logger.Error(ctx, err)
return err
}
} else {
accountInfo, err := dao.User.FindAccount(ctx, user.Email)
if err != nil && !errors.Is(err, mongo.ErrNoDocuments) {
logger.Error(ctx, err)
return err
}
if accountInfo != nil {
if err = dao.User.ChangeAccountById(ctx, accountInfo.Id, params.Email); err != nil {
logger.Error(ctx, err)
return err
}
} else {
if _, err := dao.User.CreateAccount(ctx, &do.Account{
Uid: account.Uid,
UserId: account.UserId,
Account: params.Email,
Password: account.Password,
Salt: account.Salt,
Status: 1,
}); err != nil {
logger.Error(ctx, err)
return err
}
}
}
userSession := service.Session().GetUser(ctx)
userSession.Email = params.Email
if err := service.Session().UpdateUserSession(ctx, userSession); err != nil {
logger.Error(ctx, err)
return err
}
return nil
}
// 用户更改头像
func (s *sUser) ChangeAvatar(ctx context.Context, file *ghttp.UploadFile) error {
if file.Size > 1024*1024*8 {
return errors.New("头像文件过大, 请更换或压缩后再上传")
}
root := "./resource"
path := "/public/avatar/"
filename, err := file.Save(root+path, true)
if err != nil {
logger.Error(ctx, err)
return err
}
if err = dao.User.UpdateById(ctx, service.Session().GetUid(ctx), &do.User{
Avatar: path + filename,
}); err != nil {
logger.Error(ctx, err)
return err
}
user := service.Session().GetUser(ctx)
user.Avatar = path + filename
if err = service.Session().UpdateUserSession(ctx, user); err != nil {
logger.Error(ctx, err)
return err
}
return nil
}
// 根据userId获取用户信息
func (s *sUser) GetUserById(ctx context.Context, userId int) (*model.User, error) {
user, err := dao.User.FindUserByUserId(ctx, userId)
if err != nil {
logger.Error(ctx, err)
return nil, err
}
return &model.User{
Id: user.Id,
UserId: user.UserId,
Name: user.Name,
Avatar: user.Avatar,
Email: user.Email,
Phone: user.Phone,
CreatedAt: util.FormatDatetime(user.CreatedAt),
UpdatedAt: util.FormatDatetime(user.UpdatedAt),
}, nil
}