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

import (
4
	"strconv"
5 6
	"time"

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

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

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

48
// 登录以后签发jwt
49
func (b *BaseApi) tokenNext(c *gin.Context, user system.SysUser) {
50
	j := &middleware.JWT{SigningKey: []byte(global.GVA_CONFIG.JWT.SigningKey)} // 唯一签名
51
	claims := systemReq.CustomClaims{
Mr.奇淼('s avatar
Mr.奇淼( 已提交
52 53 54
		UUID:        user.UUID,
		ID:          user.ID,
		NickName:    user.NickName,
Mr.奇淼('s avatar
Mr.奇淼( 已提交
55
		Username:    user.Username,
Mr.奇淼('s avatar
Mr.奇淼( 已提交
56
		AuthorityId: user.AuthorityId,
57
		BufferTime:  global.GVA_CONFIG.JWT.BufferTime, // 缓冲时间1天 缓冲时间内会获得新的token刷新令牌 此时一个用户会存在两个有效令牌 但是前端只留一个 另一个会丢失
Mr.奇淼('s avatar
Mr.奇淼( 已提交
58
		StandardClaims: jwt.StandardClaims{
59 60 61
			NotBefore: time.Now().Unix() - 1000,                              // 签名生效时间
			ExpiresAt: time.Now().Unix() + global.GVA_CONFIG.JWT.ExpiresTime, // 过期时间 7天  配置文件
			Issuer:    "qmPlus",                                              // 签名的发行者
Mr.奇淼('s avatar
Mr.奇淼( 已提交
62 63
		},
	}
64
	token, err := j.CreateToken(claims)
Mr.奇淼('s avatar
Mr.奇淼( 已提交
65
	if err != nil {
何秀钢 已提交
66
		global.GVA_LOG.Error("获取token失败!", zap.Any("err", err))
67
		response.FailWithMessage("获取token失败", c)
68 69 70
		return
	}
	if !global.GVA_CONFIG.System.UseMultipoint {
71
		response.OkWithDetailed(systemRes.LoginResponse{
72 73
			User:      user,
			Token:     token,
74
			ExpiresAt: claims.StandardClaims.ExpiresAt * 1000,
75
		}, "登录成功", c)
76 77
		return
	}
78 79
	if err, jwtStr := jwtService.GetRedisJWT(user.Username); err == redis.Nil {
		if err := jwtService.SetRedisJWT(token, user.Username); err != nil {
何秀钢 已提交
80
			global.GVA_LOG.Error("设置登录状态失败!", zap.Any("err", err))
81 82 83
			response.FailWithMessage("设置登录状态失败", c)
			return
		}
84
		response.OkWithDetailed(systemRes.LoginResponse{
85 86
			User:      user,
			Token:     token,
87
			ExpiresAt: claims.StandardClaims.ExpiresAt * 1000,
88
		}, "登录成功", c)
89
	} else if err != nil {
何秀钢 已提交
90
		global.GVA_LOG.Error("设置登录状态失败!", zap.Any("err", err))
91
		response.FailWithMessage("设置登录状态失败", c)
92
	} else {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
93
		var blackJWT system.JwtBlacklist
94
		blackJWT.Jwt = jwtStr
95
		if err := jwtService.JsonInBlacklist(blackJWT); err != nil {
96 97 98
			response.FailWithMessage("jwt作废失败", c)
			return
		}
99
		if err := jwtService.SetRedisJWT(token, user.Username); err != nil {
100 101
			response.FailWithMessage("设置登录状态失败", c)
			return
102
		}
103
		response.OkWithDetailed(systemRes.LoginResponse{
104 105
			User:      user,
			Token:     token,
106
			ExpiresAt: claims.StandardClaims.ExpiresAt * 1000,
107
		}, "登录成功", c)
108 109 110
	}
}

111 112 113
// @Tags SysUser
// @Summary 用户注册账号
// @Produce  application/json
114
// @Param data body systemReq.Register true "用户名, 昵称, 密码, 角色ID"
115 116
// @Success 200 {string} string "{"success":true,"data":{},"msg":"注册成功"}"
// @Router /user/register [post]
117 118
func (b *BaseApi) Register(c *gin.Context) {
	var r systemReq.Register
119 120
	_ = c.ShouldBindJSON(&r)
	if err := utils.Verify(r, utils.RegisterVerify); err != nil {
121
		response.FailWithMessage(err.Error(), c)
122 123
		return
	}
124 125 126 127 128 129 130
	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}
131
	err, userReturn := userService.Register(*user)
132
	if err != nil {
何秀钢 已提交
133
		global.GVA_LOG.Error("注册失败!", zap.Any("err", err))
134
		response.FailWithDetailed(systemRes.SysUserResponse{User: userReturn}, "注册失败", c)
135
	} else {
136
		response.OkWithDetailed(systemRes.SysUserResponse{User: userReturn}, "注册成功", c)
137 138 139
	}
}

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

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

191
// @Tags SysUser
192
// @Summary 更改用户权限
Mr.奇淼('s avatar
Mr.奇淼( 已提交
193 194 195
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
196
// @Param data body systemReq.SetUserAuth true "用户UUID, 角色ID"
197
// @Success 200 {string} string "{"success":true,"data":{},"msg":"修改成功"}"
Mr.奇淼('s avatar
Mr.奇淼( 已提交
198
// @Router /user/setUserAuthority [post]
199 200
func (b *BaseApi) SetUserAuthority(c *gin.Context) {
	var sua systemReq.SetUserAuth
201
	_ = c.ShouldBindJSON(&sua)
202
	if UserVerifyErr := utils.Verify(sua, utils.SetUserAuthorityVerify); UserVerifyErr != nil {
203 204 205
		response.FailWithMessage(UserVerifyErr.Error(), c)
		return
	}
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
	userID := utils.GetUserID(c)
	uuid := utils.GetUserUuid(c)
	if err := userService.SetUserAuthority(userID, uuid, sua.AuthorityId); err != nil {
		global.GVA_LOG.Error("修改失败!", zap.Any("err", err))
		response.FailWithMessage(err.Error(), c)
	} else {
		claims := utils.GetUserInfo(c)
		j := &middleware.JWT{SigningKey: []byte(global.GVA_CONFIG.JWT.SigningKey)} // 唯一签名
		claims.AuthorityId = sua.AuthorityId
		if token, err := j.CreateToken(*claims); err != nil {
			global.GVA_LOG.Error("修改失败!", zap.Any("err", err))
			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 {
何秀钢 已提交
239
		global.GVA_LOG.Error("修改失败!", zap.Any("err", err))
240
		response.FailWithMessage("修改失败", c)
Mr.奇淼('s avatar
Mr.奇淼( 已提交
241
	} else {
242
		response.OkWithMessage("修改成功", c)
Mr.奇淼('s avatar
Mr.奇淼( 已提交
243 244
	}
}
245 246 247 248 249 250

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

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

// @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 {
		global.GVA_LOG.Error("获取失败!", zap.Any("err", err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(gin.H{"userInfo": ReqUser}, "获取成功", c)
	}
}