sys_user.go 11.0 KB
Newer Older
1
package system
2 3

import (
S
songzhibin97 已提交
4 5
	"strconv"

6 7 8 9 10 11 12
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
	"github.com/flipped-aurora/gin-vue-admin/server/model/system"
	systemReq "github.com/flipped-aurora/gin-vue-admin/server/model/system/request"
	systemRes "github.com/flipped-aurora/gin-vue-admin/server/model/system/response"
	"github.com/flipped-aurora/gin-vue-admin/server/utils"
S
songzhibin97 已提交
13

14
	"github.com/gin-gonic/gin"
Mr.奇淼('s avatar
Mr.奇淼( 已提交
15
	"github.com/go-redis/redis/v8"
16
	"go.uber.org/zap"
17 18
)

Mr.奇淼('s avatar
Mr.奇淼( 已提交
19
// @Tags Base
20 21
// @Summary 用户登录
// @Produce  application/json
22
// @Param data body systemReq.Login true "用户名, 密码, 验证码"
23
// @Success 200 {string} string "{"success":true,"data":{},"msg":"登陆成功"}"
Mr.奇淼('s avatar
Mr.奇淼( 已提交
24
// @Router /base/login [post]
25 26
func (b *BaseApi) Login(c *gin.Context) {
	var l systemReq.Login
27 28
	_ = c.ShouldBindJSON(&l)
	if err := utils.Verify(l, utils.LoginVerify); err != nil {
29
		response.FailWithMessage(err.Error(), c)
30 31
		return
	}
32
	if store.Verify(l.CaptchaId, l.Captcha, true) {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
33
		u := &system.SysUser{Username: l.Username, Password: l.Password}
34
		if err, user := userService.Login(u); err != nil {
Sliver_Horn's avatar
Sliver_Horn 已提交
35
			global.GVA_LOG.Error("登陆失败! 用户名不存在或者密码错误!", zap.Error(err))
Mr.奇淼('s avatar
Mr.奇淼( 已提交
36
			response.FailWithMessage("用户名不存在或者密码错误", c)
37
		} else {
38
			b.tokenNext(c, *user)
39
		}
40
	} else {
41
		response.FailWithMessage("验证码错误", c)
Mr.奇淼('s avatar
Mr.奇淼( 已提交
42 43 44
	}
}

45
// 登录以后签发jwt
46
func (b *BaseApi) tokenNext(c *gin.Context, user system.SysUser) {
47
	j := &utils.JWT{SigningKey: []byte(global.GVA_CONFIG.JWT.SigningKey)} // 唯一签名
48
	claims := j.CreateClaims(systemReq.BaseClaims{
Mr.奇淼('s avatar
Mr.奇淼( 已提交
49 50 51
		UUID:        user.UUID,
		ID:          user.ID,
		NickName:    user.NickName,
Mr.奇淼('s avatar
Mr.奇淼( 已提交
52
		Username:    user.Username,
Mr.奇淼('s avatar
Mr.奇淼( 已提交
53
		AuthorityId: user.AuthorityId,
54
	})
55
	token, err := j.CreateToken(claims)
Mr.奇淼('s avatar
Mr.奇淼( 已提交
56
	if err != nil {
Sliver_Horn's avatar
Sliver_Horn 已提交
57
		global.GVA_LOG.Error("获取token失败!", zap.Error(err))
58
		response.FailWithMessage("获取token失败", c)
59 60 61
		return
	}
	if !global.GVA_CONFIG.System.UseMultipoint {
62
		response.OkWithDetailed(systemRes.LoginResponse{
63 64
			User:      user,
			Token:     token,
65
			ExpiresAt: claims.StandardClaims.ExpiresAt * 1000,
66
		}, "登录成功", c)
67 68
		return
	}
69

70 71
	if err, jwtStr := jwtService.GetRedisJWT(user.Username); err == redis.Nil {
		if err := jwtService.SetRedisJWT(token, user.Username); err != nil {
Sliver_Horn's avatar
Sliver_Horn 已提交
72
			global.GVA_LOG.Error("设置登录状态失败!", zap.Error(err))
73 74 75
			response.FailWithMessage("设置登录状态失败", c)
			return
		}
76
		response.OkWithDetailed(systemRes.LoginResponse{
77 78
			User:      user,
			Token:     token,
79
			ExpiresAt: claims.StandardClaims.ExpiresAt * 1000,
80
		}, "登录成功", c)
81
	} else if err != nil {
Sliver_Horn's avatar
Sliver_Horn 已提交
82
		global.GVA_LOG.Error("设置登录状态失败!", zap.Error(err))
83
		response.FailWithMessage("设置登录状态失败", c)
84
	} else {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
85
		var blackJWT system.JwtBlacklist
86
		blackJWT.Jwt = jwtStr
87
		if err := jwtService.JsonInBlacklist(blackJWT); err != nil {
88 89 90
			response.FailWithMessage("jwt作废失败", c)
			return
		}
91
		if err := jwtService.SetRedisJWT(token, user.Username); err != nil {
92 93
			response.FailWithMessage("设置登录状态失败", c)
			return
94
		}
95
		response.OkWithDetailed(systemRes.LoginResponse{
96 97
			User:      user,
			Token:     token,
98
			ExpiresAt: claims.StandardClaims.ExpiresAt * 1000,
99
		}, "登录成功", c)
100 101 102
	}
}

103 104 105
// @Tags SysUser
// @Summary 用户注册账号
// @Produce  application/json
106
// @Param data body systemReq.Register true "用户名, 昵称, 密码, 角色ID"
107 108
// @Success 200 {string} string "{"success":true,"data":{},"msg":"注册成功"}"
// @Router /user/register [post]
109 110
func (b *BaseApi) Register(c *gin.Context) {
	var r systemReq.Register
111 112
	_ = c.ShouldBindJSON(&r)
	if err := utils.Verify(r, utils.RegisterVerify); err != nil {
113
		response.FailWithMessage(err.Error(), c)
114 115
		return
	}
116 117 118 119 120 121 122
	var authorities []system.SysAuthority
	for _, v := range r.AuthorityIds {
		authorities = append(authorities, system.SysAuthority{
			AuthorityId: v,
		})
	}
	user := &system.SysUser{Username: r.Username, NickName: r.NickName, Password: r.Password, HeaderImg: r.HeaderImg, AuthorityId: r.AuthorityId, Authorities: authorities}
123
	err, userReturn := userService.Register(*user)
124
	if err != nil {
Sliver_Horn's avatar
Sliver_Horn 已提交
125
		global.GVA_LOG.Error("注册失败!", zap.Error(err))
126
		response.FailWithDetailed(systemRes.SysUserResponse{User: userReturn}, "注册失败", c)
127
	} else {
128
		response.OkWithDetailed(systemRes.SysUserResponse{User: userReturn}, "注册成功", c)
129 130 131
	}
}

132
// @Tags SysUser
133
// @Summary 用户修改密码
Mr.奇淼('s avatar
Mr.奇淼( 已提交
134
// @Security ApiKeyAuth
135
// @Produce  application/json
136
// @Param data body systemReq.ChangePasswordStruct true "用户名, 原密码, 新密码"
137
// @Success 200 {string} string "{"success":true,"data":{},"msg":"修改成功"}"
138
// @Router /user/changePassword [post]
139 140
func (b *BaseApi) ChangePassword(c *gin.Context) {
	var user systemReq.ChangePasswordStruct
141 142 143
	_ = c.ShouldBindJSON(&user)
	if err := utils.Verify(user, utils.ChangePasswordVerify); err != nil {
		response.FailWithMessage(err.Error(), c)
144 145
		return
	}
Mr.奇淼('s avatar
Mr.奇淼( 已提交
146
	u := &system.SysUser{Username: user.Username, Password: user.Password}
147
	if err, _ := userService.ChangePassword(u, user.NewPassword); err != nil {
Sliver_Horn's avatar
Sliver_Horn 已提交
148
		global.GVA_LOG.Error("修改失败!", zap.Error(err))
149
		response.FailWithMessage("修改失败,原密码与当前账户不符", c)
150
	} else {
151
		response.OkWithMessage("修改成功", c)
152 153 154
	}
}

155
// @Tags SysUser
Mr.奇淼('s avatar
Mr.奇淼( 已提交
156 157 158 159
// @Summary 分页获取用户列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
160
// @Param data body request.PageInfo true "页码, 每页大小"
161
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
162
// @Router /user/getUserList [post]
163
func (b *BaseApi) GetUserList(c *gin.Context) {
164
	var pageInfo request.PageInfo
165
	_ = c.ShouldBindJSON(&pageInfo)
166 167
	if err := utils.Verify(pageInfo, utils.PageInfoVerify); err != nil {
		response.FailWithMessage(err.Error(), c)
168 169
		return
	}
170
	if err, list, total := userService.GetUserInfoList(pageInfo); err != nil {
Sliver_Horn's avatar
Sliver_Horn 已提交
171
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
m0_50812349's avatar
m0_50812349 已提交
172
		response.FailWithMessage("获取失败", c)
Mr.奇淼('s avatar
Mr.奇淼( 已提交
173
	} else {
174
		response.OkWithDetailed(response.PageResult{
Mr.奇淼('s avatar
Mr.奇淼( 已提交
175 176 177 178
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
m0_50812349's avatar
m0_50812349 已提交
179
		}, "获取成功", c)
Mr.奇淼('s avatar
Mr.奇淼( 已提交
180 181
	}
}
Mr.奇淼('s avatar
Mr.奇淼( 已提交
182

183
// @Tags SysUser
184
// @Summary 更改用户权限
Mr.奇淼('s avatar
Mr.奇淼( 已提交
185 186 187
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
188
// @Param data body systemReq.SetUserAuth true "用户UUID, 角色ID"
189
// @Success 200 {string} string "{"success":true,"data":{},"msg":"修改成功"}"
Mr.奇淼('s avatar
Mr.奇淼( 已提交
190
// @Router /user/setUserAuthority [post]
191 192
func (b *BaseApi) SetUserAuthority(c *gin.Context) {
	var sua systemReq.SetUserAuth
193
	_ = c.ShouldBindJSON(&sua)
194
	if UserVerifyErr := utils.Verify(sua, utils.SetUserAuthorityVerify); UserVerifyErr != nil {
195 196 197
		response.FailWithMessage(UserVerifyErr.Error(), c)
		return
	}
198 199 200
	userID := utils.GetUserID(c)
	uuid := utils.GetUserUuid(c)
	if err := userService.SetUserAuthority(userID, uuid, sua.AuthorityId); err != nil {
Sliver_Horn's avatar
Sliver_Horn 已提交
201
		global.GVA_LOG.Error("修改失败!", zap.Error(err))
202 203 204
		response.FailWithMessage(err.Error(), c)
	} else {
		claims := utils.GetUserInfo(c)
205
		j := &utils.JWT{SigningKey: []byte(global.GVA_CONFIG.JWT.SigningKey)} // 唯一签名
206 207
		claims.AuthorityId = sua.AuthorityId
		if token, err := j.CreateToken(*claims); err != nil {
Sliver_Horn's avatar
Sliver_Horn 已提交
208
			global.GVA_LOG.Error("修改失败!", zap.Error(err))
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
			response.FailWithMessage(err.Error(), c)
		} else {
			c.Header("new-token", token)
			c.Header("new-expires-at", strconv.FormatInt(claims.ExpiresAt, 10))
			response.OkWithMessage("修改成功", c)
		}

	}
}

// @Tags SysUser
// @Summary 设置用户权限
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body systemReq.SetUserAuthorities true "用户UUID, 角色ID"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"修改成功"}"
// @Router /user/setUserAuthorities [post]
func (b *BaseApi) SetUserAuthorities(c *gin.Context) {
	var sua systemReq.SetUserAuthorities
	_ = c.ShouldBindJSON(&sua)
	if err := userService.SetUserAuthorities(sua.ID, sua.AuthorityIds); err != nil {
Sliver_Horn's avatar
Sliver_Horn 已提交
231
		global.GVA_LOG.Error("修改失败!", zap.Error(err))
232
		response.FailWithMessage("修改失败", c)
Mr.奇淼('s avatar
Mr.奇淼( 已提交
233
	} else {
234
		response.OkWithMessage("修改成功", c)
Mr.奇淼('s avatar
Mr.奇淼( 已提交
235 236
	}
}
237 238 239 240 241 242

// @Tags SysUser
// @Summary 删除用户
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
243
// @Param data body request.GetById true "用户ID"
244
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
245
// @Router /user/deleteUser [delete]
246
func (b *BaseApi) DeleteUser(c *gin.Context) {
247 248
	var reqId request.GetById
	_ = c.ShouldBindJSON(&reqId)
249 250
	if err := utils.Verify(reqId, utils.IdVerify); err != nil {
		response.FailWithMessage(err.Error(), c)
251 252
		return
	}
253
	jwtId := utils.GetUserID(c)
254
	if jwtId == uint(reqId.ID) {
m0_50812349's avatar
m0_50812349 已提交
255 256 257
		response.FailWithMessage("删除失败, 自杀失败", c)
		return
	}
258
	if err := userService.DeleteUser(reqId.ID); err != nil {
Sliver_Horn's avatar
Sliver_Horn 已提交
259
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
260
		response.FailWithMessage("删除失败", c)
261 262 263 264
	} else {
		response.OkWithMessage("删除成功", c)
	}
}
265 266

// @Tags SysUser
267
// @Summary 设置用户信息
268 269 270
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
Mr.奇淼('s avatar
Mr.奇淼( 已提交
271
// @Param data body system.SysUser true "ID, 用户名, 昵称, 头像链接"
272
// @Success 200 {string} string "{"success":true,"data":{},"msg":"设置成功"}"
273
// @Router /user/setUserInfo [put]
274
func (b *BaseApi) SetUserInfo(c *gin.Context) {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
275
	var user system.SysUser
276
	_ = c.ShouldBindJSON(&user)
277
	if err := utils.Verify(user, utils.IdVerify); err != nil {
278
		response.FailWithMessage(err.Error(), c)
279 280
		return
	}
281
	if err, ReqUser := userService.SetUserInfo(user); err != nil {
Sliver_Horn's avatar
Sliver_Horn 已提交
282
		global.GVA_LOG.Error("设置失败!", zap.Error(err))
283
		response.FailWithMessage("设置失败", c)
284
	} else {
285
		response.OkWithDetailed(gin.H{"userInfo": ReqUser}, "设置成功", c)
286 287
	}
}
288 289 290 291 292 293 294 295 296 297 298

// @Tags SysUser
// @Summary 获取用户信息
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /user/getUserInfo [get]
func (b *BaseApi) GetUserInfo(c *gin.Context) {
	uuid := utils.GetUserUuid(c)
	if err, ReqUser := userService.GetUserInfo(uuid); err != nil {
Sliver_Horn's avatar
Sliver_Horn 已提交
299
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
300 301 302 303 304
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(gin.H{"userInfo": ReqUser}, "获取成功", c)
	}
}
Mr.奇淼('s avatar
Mr.奇淼( 已提交
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322

// @Tags SysUser
// @Summary 用户修改密码
// @Security ApiKeyAuth
// @Produce  application/json
// @Param data body system.SysUser true "ID"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"修改成功"}"
// @Router /user/resetPassword [post]
func (b *BaseApi) ResetPassword(c *gin.Context) {
	var user system.SysUser
	_ = c.ShouldBindJSON(&user)
	if err := userService.ResetPassword(user.ID); err != nil {
		global.GVA_LOG.Error("重置失败!", zap.Error(err))
		response.FailWithMessage("重置失败"+err.Error(), c)
	} else {
		response.OkWithMessage("重置成功", c)
	}
}