wangpengfei
2023-07-04 1142bc88cebcfedac1617749bc195a2615799518
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
package v1
 
import (
    "aps_crm/conf"
    "aps_crm/constvar"
    "aps_crm/model"
    "aps_crm/model/request"
    "aps_crm/model/response"
    _ "aps_crm/model/response"
    "aps_crm/pkg/contextx"
    "aps_crm/pkg/convertx"
    "aps_crm/pkg/ecode"
    "aps_crm/pkg/encrypt"
    "aps_crm/pkg/logx"
    "aps_crm/pkg/snowflake"
    "aps_crm/utils"
    "fmt"
    "github.com/gin-gonic/gin"
    "github.com/go-redis/redis/v8"
    "github.com/mojocn/base64Captcha"
    "time"
)
 
// 当开启多服务器部署时,替换下面的配置,使用redis共享存储验证码
// var store = captcha.NewDefaultRedisStore()
var store = base64Captcha.DefaultMemStore
 
// Login
//    @Tags        Base
//    @Summary    用户登录
//    @Produce    application/json
//    @Param        object    body        request.Login                                    true    "查询参数"
//    @Success    200        {object}    contextx.Response{data=response.LoginResponse}    "成功"
//    @Router        /api/base/login [post]
func (slf *BaseApi) Login(c *gin.Context) {
    var params request.Login
    ctx, ok := contextx.NewContext(c, &params)
    if !ok {
        return
    }
 
    // 判断验证码是否开启
    key := c.ClientIP()
    openCaptcha := conf.Conf.Captcha.OpenCaptcha               // 是否开启防爆次数
    openCaptchaTimeOut := conf.Conf.Captcha.OpenCaptchaTimeOut // 缓存超时时间
    v, ok := constvar.BlackCache.Get(key)
    if !ok {
        constvar.BlackCache.Set(key, 1, time.Second*time.Duration(openCaptchaTimeOut))
    }
 
    var oc bool = openCaptcha == 0 || convertx.InterfaceToInt(v) > openCaptcha // 0 表示每次登录都需要验证码 或者当前次数已超过防爆次数
 
    if !oc || store.Verify(params.CaptchaId, params.Captcha, true) {
        u := &model.User{Username: params.Username, Password: params.Password}
        user, errCode := userService.Login(u)
        if errCode != ecode.OK {
            logx.Errorf("登陆失败! 用户名不存在或者密码错误! errCode:%v", errCode)
            // 验证码次数+1
            _ = constvar.BlackCache.Increment(key, 1)
            ctx.Fail(errCode)
            return
        }
        if !user.Enable {
            logx.Errorf("登陆失败! 用户被禁止登录!")
            // 验证码次数+1
            _ = constvar.BlackCache.Increment(key, 1)
            ctx.Fail(ecode.UserForbidden)
            return
        }
        // 赋值菜单ID列表
        //user.MenuIds, _ = menuService.GetUserMenuIds(user.ID, user.UserType)
        slf.TokenNext(ctx, *user)
        return
    }
 
    // 验证码次数+1
    _ = constvar.BlackCache.Increment(key, 1)
    ctx.Fail(ecode.CaptchaErr)
}
 
// TokenNext 登录以后签发jwt
func (slf *BaseApi) TokenNext(ctx *contextx.Context, user model.User) {
    logx.Infof("TokenNext user:%+v", user)
    j := &utils.JWT{SigningKey: []byte(conf.Conf.JWT.SigningKey)} // 唯一签名
    claims := j.CreateClaims(request.BaseClaims{
        UserId:   user.ID,
        Username: user.Username,
        ParentId: user.ParentName,
        UserType: user.UserType,
    })
    token, err := j.CreateToken(claims)
    if err != nil {
        logx.Errorf("创建token失败! err:%v", err)
        ctx.Fail(ecode.CreateTokenErr)
        return
    }
    if !conf.Conf.System.UseMultipoint { // 不允许多点登录
        ctx.OkWithDetailed(response.LoginResponse{
            User:  user,
            Token: token,
            //ExpiresAt: claims.StandardClaims.ExpiresAt * 1000,
        })
        return
    }
 
    if jwtStr, err := jwtService.GetRedisJWT(user.Username); err == redis.Nil { // redis无JWT数据
        if err := jwtService.SetRedisJWT(token, user.Username); err != nil {
            logx.Errorf("设置登录状态失败! err:%v", err)
            ctx.Fail(ecode.RedisErr)
            return
        }
        ctx.OkWithDetailed(response.LoginResponse{
            User:  user,
            Token: token,
            //ExpiresAt: claims.StandardClaims.ExpiresAt * 1000,
        })
    } else if err != nil { // redis获取JWT报错
        logx.Errorf("设置登录状态失败! err:%v", err)
        ctx.Fail(ecode.RedisErr)
    } else { // 成功获取redis的JWT,旧的作废
        var blackJWT model.JwtBlacklist
        blackJWT.Jwt = jwtStr
        if err := jwtService.JsonInBlacklist(blackJWT); err != nil {
            ctx.Fail(ecode.DBErr)
            return
        }
        if err := jwtService.SetRedisJWT(token, user.Username); err != nil {
            ctx.Fail(ecode.RedisErr)
            return
        }
        ctx.OkWithDetailed(response.LoginResponse{
            User:  user,
            Token: token,
            //ExpiresAt: claims.StandardClaims.ExpiresAt * 1000,
        })
    }
}
 
// Register
//    @Tags        User
//    @Summary    注册账号
//    @Produce    application/json
//    @Param        object    body        request.Register                                true    "查询参数"
//    @Success    200        {object}    contextx.Response{data=response.UserResponse}    "成功"
//    @Router        /api/user/register [post]
func (slf *BaseApi) Register(c *gin.Context) {
    var params request.Register
    ctx, ok := contextx.NewContext(c, &params)
    if !ok {
        return
    }
 
    userInfo := utils.GetUserInfo(c)
    if len(userInfo.UserId) <= 0 {
        ctx.Fail(ecode.UnknownErr)
        return
    }
 
    if len(params.Username) == 0 || len(params.Password) == 0 || len(params.Phone) == 0 || len(params.NickName) == 0 {
        ctx.Fail(ecode.ParamsErr)
        return
    }
 
    var userId = fmt.Sprintf("u%v", snowflake.GenerateId())
    var passWord = encrypt.BcryptHash(params.Password)
    var userType constvar.UserType
    var parentId string
    var parentName string
    if userInfo.UserType == constvar.UserTypeSuper {
        userType = constvar.UserTypePrimary // 主账户的父用户ID是自己
        parentId = userId
        parentName = params.Username
    } else if userInfo.UserType == constvar.UserTypePrimary {
        userType = constvar.UserTypeSub
        parentId = userInfo.UserId
        parentName = userInfo.ParentId
    } else {
        ctx.Fail(ecode.NoPowerErr)
        return
    }
 
    user := &model.User{ID: userId, Username: params.Username, UserType: userType, NickName: params.NickName, Password: passWord, HeaderImg: params.HeaderImg, Enable: true, ParentId: parentId, ParentName: parentName, Phone: params.Phone, Email: params.Email}
    userReturn, errCode := userService.Register(user)
    if errCode != ecode.OK {
        ctx.Fail(errCode)
        return
    }
    if user.UserType == constvar.UserTypePrimary { // 主账户创建对应的数据库用户和排程数据库
        err := model.NewMysql().CreateDatabase(user.Username)
        if err != nil {
            ctx.Fail(ecode.CreateDatabaseErr)
            return
        }
 
        defaultPwd := fmt.Sprintf("%v@Basic2023", user.Username)
        err = model.NewMysql().CreateUser(user.Username, defaultPwd, user.Username)
        if err != nil {
            ctx.Fail(ecode.CreateDatabaseUserErr)
            return
        }
    }
 
    ctx.OkWithDetailed(response.UserResponse{User: *userReturn})
}
 
// ChangePassword
//    @Tags        User
//    @Summary    用户修改密码
//    @Produce    application/json
//    @Param        object    body        request.ChangePasswordReq    true    "查询参数"
//    @Success    200        {object}    contextx.Response{}            "成功"
//    @Router        /api/user/changePassword [post]
func (slf *BaseApi) ChangePassword(c *gin.Context) {
    var params request.ChangePasswordReq
    ctx, ok := contextx.NewContext(c, &params)
    if !ok {
        return
    }
 
    u := &model.User{ID: utils.GetUserID(c), Password: params.Password}
    _, errCode := userService.ChangePassword(u, params.NewPassword)
    if errCode != ecode.OK {
        ctx.Fail(errCode)
        return
    }
    ctx.Ok()
}
 
// GetUserList
//    @Tags        User
//    @Summary    分页获取用户列表(不传分页参数,获取全部)
//    @Produce    application/json
//    @Param        object    body        request.GetUserList                            true    "查询参数"
//    @Success    200        {object}    contextx.Response{data=response.PageResult}    "成功"
//    @Router        /api/user/getUserList [post]
func (slf *BaseApi) GetUserList(c *gin.Context) {
    var params request.GetUserList
    ctx, ok := contextx.NewContext(c, &params)
    if !ok {
        return
    }
 
    userInfo := utils.GetUserInfo(c)
    if len(userInfo.UserId) <= 0 || len(userInfo.ParentId) == 0 {
        ctx.Fail(ecode.UnknownErr)
        return
    }
 
    ctx.OkWithDetailed(response.PageResult{
        Page:     params.Page,
        PageSize: params.PageSize,
    })
}
 
// DeleteUser
//    @Tags        User
//    @Summary    删除用户
//    @Produce    application/json
//    @Param        object    body        request.DeleteUserReq    true    "查询参数"
//    @Success    200        {object}    contextx.Response{}        "成功"
//    @Router        /api/user/deleteUser [delete]
func (slf *BaseApi) DeleteUser(c *gin.Context) {
    var params request.DeleteUserReq
    ctx, ok := contextx.NewContext(c, &params)
    if !ok {
        return
    }
 
    if len(params.UserId) <= 0 {
        ctx.Fail(ecode.ParamsErr)
        return
    }
 
    userInfo := utils.GetUserInfo(c)
    if userInfo.UserType != constvar.UserTypePrimary || userInfo.UserId == params.UserId {
        ctx.Fail(ecode.NoPowerErr)
        return
    }
 
    err := userService.DeleteUser(params.UserId)
    if err != nil {
        logx.Errorf("删除失败! err:%v", err)
        ctx.Fail(ecode.DBErr)
        return
    }
    ctx.Ok()
}
 
// SetUserInfo
//    @Tags        User
//    @Summary    设置用户信息
//    @Produce    application/json
//    @Param        object    body        request.ChangeUserInfo    true    "查询参数"
//    @Success    200        {object}    contextx.Response{}        "成功"
//    @Router        /api/user/setUserInfo [post]
func (slf *BaseApi) SetUserInfo(c *gin.Context) {
    var params request.ChangeUserInfo
    ctx, ok := contextx.NewContext(c, &params)
    if !ok {
        return
    }
 
    err := userService.SetUserInfo(model.User{
        ID:        params.ID,
        NickName:  params.NickName,
        HeaderImg: params.HeaderImg,
        Phone:     params.Phone,
        Email:     params.Email,
        Pos:       params.Pos,
    })
    if err != nil {
        logx.Errorf("设置失败! err:%v", err)
        ctx.Fail(ecode.DBErr)
        return
    }
    ctx.Ok()
}
 
// SetSelfInfo
//    @Tags        User
//    @Summary    设置用户信息
//    @Produce    application/json
//    @Param        object    body        request.ChangeUserInfo    true    "查询参数"
//    @Success    200        {object}    contextx.Response{}        "成功"
//    @Router        /api/user/setSelfInfo [post]
func (slf *BaseApi) SetSelfInfo(c *gin.Context) {
    var params request.ChangeUserInfo
    ctx, ok := contextx.NewContext(c, &params)
    if !ok {
        return
    }
 
    params.ID = utils.GetUserID(c)
    err := userService.SetUserInfo(model.User{
        ID:        params.ID,
        NickName:  params.NickName,
        HeaderImg: params.HeaderImg,
        Phone:     params.Phone,
        Email:     params.Email,
        Pos:       params.Pos,
    })
    if err != nil {
        logx.Errorf("设置失败! err:%v", err)
        ctx.Fail(ecode.DBErr)
        return
    }
    ctx.Ok()
}
 
// GetUserInfo
//    @Tags        User
//    @Summary    获取自身信息
//    @Produce    application/json
//    @Success    200    {object}    contextx.Response{}    "成功"
//    @Router        /api/user/getUserInfo [post]
func (slf *BaseApi) GetUserInfo(c *gin.Context) {
    ctx, ok := contextx.NewContext(c, nil)
    if !ok {
        return
    }
 
    id := utils.GetUserID(c)
    ReqUser, err := userService.GetUserInfo(id)
    if err != nil {
        logx.Errorf("获取失败! err:%v", err)
        ctx.Fail(ecode.DBErr)
        return
    }
    ctx.OkWithDetailed(response.UserResponse{
        User: *ReqUser,
    })
}
 
// ResetPassword
//    @Tags        User
//    @Summary    重置用户密码
//    @Produce    application/json
//    @Param        object    body        model.User            true    "查询参数"
//    @Success    200        {object}    contextx.Response{}    "成功"
//    @Router        /api/user/resetPassword [post]
func (slf *BaseApi) ResetPassword(c *gin.Context) {
    var params model.User
    ctx, ok := contextx.NewContext(c, &params)
    if !ok {
        return
    }
 
    err := userService.ResetPassword(params.ID)
    if err != nil {
        logx.Errorf("重置失败! err:%v", err)
        ctx.Fail(ecode.DBErr)
        return
    }
    ctx.Ok()
}