You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

312 lines
11 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. package system
  2. import (
  3. "strconv"
  4. "time"
  5. "github.com/flipped-aurora/gin-vue-admin/server/global"
  6. "github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
  7. "github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
  8. "github.com/flipped-aurora/gin-vue-admin/server/model/system"
  9. systemReq "github.com/flipped-aurora/gin-vue-admin/server/model/system/request"
  10. systemRes "github.com/flipped-aurora/gin-vue-admin/server/model/system/response"
  11. "github.com/flipped-aurora/gin-vue-admin/server/utils"
  12. "github.com/dgrijalva/jwt-go"
  13. "github.com/gin-gonic/gin"
  14. "github.com/go-redis/redis/v8"
  15. "go.uber.org/zap"
  16. )
  17. // @Tags Base
  18. // @Summary 用户登录
  19. // @Produce application/json
  20. // @Param data body systemReq.Login true "用户名, 密码, 验证码"
  21. // @Success 200 {string} string "{"success":true,"data":{},"msg":"登陆成功"}"
  22. // @Router /base/login [post]
  23. func (b *BaseApi) Login(c *gin.Context) {
  24. var l systemReq.Login
  25. _ = c.ShouldBindJSON(&l)
  26. if err := utils.Verify(l, utils.LoginVerify); err != nil {
  27. response.FailWithMessage(err.Error(), c)
  28. return
  29. }
  30. if store.Verify(l.CaptchaId, l.Captcha, true) {
  31. u := &system.SysUser{Username: l.Username, Password: l.Password}
  32. if err, user := userService.Login(u); err != nil {
  33. global.GVA_LOG.Error("登陆失败! 用户名不存在或者密码错误!", zap.Any("err", err))
  34. response.FailWithMessage("用户名不存在或者密码错误", c)
  35. } else {
  36. b.tokenNext(c, *user)
  37. }
  38. } else {
  39. response.FailWithMessage("验证码错误", c)
  40. }
  41. }
  42. // 登录以后签发jwt
  43. func (b *BaseApi) tokenNext(c *gin.Context, user system.SysUser) {
  44. j := &utils.JWT{SigningKey: []byte(global.GVA_CONFIG.JWT.SigningKey)} // 唯一签名
  45. claims := systemReq.CustomClaims{
  46. UUID: user.UUID,
  47. ID: user.ID,
  48. NickName: user.NickName,
  49. Username: user.Username,
  50. AuthorityId: user.AuthorityId,
  51. BufferTime: global.GVA_CONFIG.JWT.BufferTime, // 缓冲时间1天 缓冲时间内会获得新的token刷新令牌 此时一个用户会存在两个有效令牌 但是前端只留一个 另一个会丢失
  52. StandardClaims: jwt.StandardClaims{
  53. NotBefore: time.Now().Unix() - 1000, // 签名生效时间
  54. ExpiresAt: time.Now().Unix() + global.GVA_CONFIG.JWT.ExpiresTime, // 过期时间 7天 配置文件
  55. Issuer: "qmPlus", // 签名的发行者
  56. },
  57. }
  58. token, err := j.CreateToken(claims)
  59. if err != nil {
  60. global.GVA_LOG.Error("获取token失败!", zap.Any("err", err))
  61. response.FailWithMessage("获取token失败", c)
  62. return
  63. }
  64. if !global.GVA_CONFIG.System.UseMultipoint {
  65. response.OkWithDetailed(systemRes.LoginResponse{
  66. User: user,
  67. Token: token,
  68. ExpiresAt: claims.StandardClaims.ExpiresAt * 1000,
  69. }, "登录成功", c)
  70. return
  71. }
  72. if err, jwtStr := jwtService.GetRedisJWT(user.Username); err == redis.Nil {
  73. if err := jwtService.SetRedisJWT(token, user.Username); err != nil {
  74. global.GVA_LOG.Error("设置登录状态失败!", zap.Any("err", err))
  75. response.FailWithMessage("设置登录状态失败", c)
  76. return
  77. }
  78. response.OkWithDetailed(systemRes.LoginResponse{
  79. User: user,
  80. Token: token,
  81. ExpiresAt: claims.StandardClaims.ExpiresAt * 1000,
  82. }, "登录成功", c)
  83. } else if err != nil {
  84. global.GVA_LOG.Error("设置登录状态失败!", zap.Any("err", err))
  85. response.FailWithMessage("设置登录状态失败", c)
  86. } else {
  87. var blackJWT system.JwtBlacklist
  88. blackJWT.Jwt = jwtStr
  89. if err := jwtService.JsonInBlacklist(blackJWT); err != nil {
  90. response.FailWithMessage("jwt作废失败", c)
  91. return
  92. }
  93. if err := jwtService.SetRedisJWT(token, user.Username); err != nil {
  94. response.FailWithMessage("设置登录状态失败", c)
  95. return
  96. }
  97. response.OkWithDetailed(systemRes.LoginResponse{
  98. User: user,
  99. Token: token,
  100. ExpiresAt: claims.StandardClaims.ExpiresAt * 1000,
  101. }, "登录成功", c)
  102. }
  103. }
  104. // @Tags SysUser
  105. // @Summary 用户注册账号
  106. // @Produce application/json
  107. // @Param data body systemReq.Register true "用户名, 昵称, 密码, 角色ID"
  108. // @Success 200 {string} string "{"success":true,"data":{},"msg":"注册成功"}"
  109. // @Router /user/register [post]
  110. func (b *BaseApi) Register(c *gin.Context) {
  111. var r systemReq.Register
  112. _ = c.ShouldBindJSON(&r)
  113. if err := utils.Verify(r, utils.RegisterVerify); err != nil {
  114. response.FailWithMessage(err.Error(), c)
  115. return
  116. }
  117. var authorities []system.SysAuthority
  118. for _, v := range r.AuthorityIds {
  119. authorities = append(authorities, system.SysAuthority{
  120. AuthorityId: v,
  121. })
  122. }
  123. user := &system.SysUser{Username: r.Username, NickName: r.NickName, Password: r.Password, HeaderImg: r.HeaderImg, AuthorityId: r.AuthorityId, Authorities: authorities}
  124. err, userReturn := userService.Register(*user)
  125. if err != nil {
  126. global.GVA_LOG.Error("注册失败!", zap.Any("err", err))
  127. response.FailWithDetailed(systemRes.SysUserResponse{User: userReturn}, "注册失败", c)
  128. } else {
  129. response.OkWithDetailed(systemRes.SysUserResponse{User: userReturn}, "注册成功", c)
  130. }
  131. }
  132. // @Tags SysUser
  133. // @Summary 用户修改密码
  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. func (b *BaseApi) ChangePassword(c *gin.Context) {
  140. var user systemReq.ChangePasswordStruct
  141. _ = c.ShouldBindJSON(&user)
  142. if err := utils.Verify(user, utils.ChangePasswordVerify); err != nil {
  143. response.FailWithMessage(err.Error(), c)
  144. return
  145. }
  146. u := &system.SysUser{Username: user.Username, Password: user.Password}
  147. if err, _ := userService.ChangePassword(u, user.NewPassword); err != nil {
  148. global.GVA_LOG.Error("修改失败!", zap.Any("err", err))
  149. response.FailWithMessage("修改失败,原密码与当前账户不符", c)
  150. } else {
  151. response.OkWithMessage("修改成功", c)
  152. }
  153. }
  154. // @Tags SysUser
  155. // @Summary 分页获取用户列表
  156. // @Security ApiKeyAuth
  157. // @accept application/json
  158. // @Produce application/json
  159. // @Param data body request.PageInfo true "页码, 每页大小"
  160. // @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
  161. // @Router /user/getUserList [post]
  162. func (b *BaseApi) GetUserList(c *gin.Context) {
  163. var pageInfo request.PageInfo
  164. _ = c.ShouldBindJSON(&pageInfo)
  165. if err := utils.Verify(pageInfo, utils.PageInfoVerify); err != nil {
  166. response.FailWithMessage(err.Error(), c)
  167. return
  168. }
  169. if err, list, total := userService.GetUserInfoList(pageInfo); err != nil {
  170. global.GVA_LOG.Error("获取失败!", zap.Any("err", err))
  171. response.FailWithMessage("获取失败", c)
  172. } else {
  173. response.OkWithDetailed(response.PageResult{
  174. List: list,
  175. Total: total,
  176. Page: pageInfo.Page,
  177. PageSize: pageInfo.PageSize,
  178. }, "获取成功", c)
  179. }
  180. }
  181. // @Tags SysUser
  182. // @Summary 更改用户权限
  183. // @Security ApiKeyAuth
  184. // @accept application/json
  185. // @Produce application/json
  186. // @Param data body systemReq.SetUserAuth true "用户UUID, 角色ID"
  187. // @Success 200 {string} string "{"success":true,"data":{},"msg":"修改成功"}"
  188. // @Router /user/setUserAuthority [post]
  189. func (b *BaseApi) SetUserAuthority(c *gin.Context) {
  190. var sua systemReq.SetUserAuth
  191. _ = c.ShouldBindJSON(&sua)
  192. if UserVerifyErr := utils.Verify(sua, utils.SetUserAuthorityVerify); UserVerifyErr != nil {
  193. response.FailWithMessage(UserVerifyErr.Error(), c)
  194. return
  195. }
  196. userID := utils.GetUserID(c)
  197. uuid := utils.GetUserUuid(c)
  198. if err := userService.SetUserAuthority(userID, uuid, sua.AuthorityId); err != nil {
  199. global.GVA_LOG.Error("修改失败!", zap.Any("err", err))
  200. response.FailWithMessage(err.Error(), c)
  201. } else {
  202. claims := utils.GetUserInfo(c)
  203. j := &utils.JWT{SigningKey: []byte(global.GVA_CONFIG.JWT.SigningKey)} // 唯一签名
  204. claims.AuthorityId = sua.AuthorityId
  205. if token, err := j.CreateToken(*claims); err != nil {
  206. global.GVA_LOG.Error("修改失败!", zap.Any("err", err))
  207. response.FailWithMessage(err.Error(), c)
  208. } else {
  209. c.Header("new-token", token)
  210. c.Header("new-expires-at", strconv.FormatInt(claims.ExpiresAt, 10))
  211. response.OkWithMessage("修改成功", c)
  212. }
  213. }
  214. }
  215. // @Tags SysUser
  216. // @Summary 设置用户权限
  217. // @Security ApiKeyAuth
  218. // @accept application/json
  219. // @Produce application/json
  220. // @Param data body systemReq.SetUserAuthorities true "用户UUID, 角色ID"
  221. // @Success 200 {string} string "{"success":true,"data":{},"msg":"修改成功"}"
  222. // @Router /user/setUserAuthorities [post]
  223. func (b *BaseApi) SetUserAuthorities(c *gin.Context) {
  224. var sua systemReq.SetUserAuthorities
  225. _ = c.ShouldBindJSON(&sua)
  226. if err := userService.SetUserAuthorities(sua.ID, sua.AuthorityIds); err != nil {
  227. global.GVA_LOG.Error("修改失败!", zap.Any("err", err))
  228. response.FailWithMessage("修改失败", c)
  229. } else {
  230. response.OkWithMessage("修改成功", c)
  231. }
  232. }
  233. // @Tags SysUser
  234. // @Summary 删除用户
  235. // @Security ApiKeyAuth
  236. // @accept application/json
  237. // @Produce application/json
  238. // @Param data body request.GetById true "用户ID"
  239. // @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
  240. // @Router /user/deleteUser [delete]
  241. func (b *BaseApi) DeleteUser(c *gin.Context) {
  242. var reqId request.GetById
  243. _ = c.ShouldBindJSON(&reqId)
  244. if err := utils.Verify(reqId, utils.IdVerify); err != nil {
  245. response.FailWithMessage(err.Error(), c)
  246. return
  247. }
  248. jwtId := utils.GetUserID(c)
  249. if jwtId == uint(reqId.ID) {
  250. response.FailWithMessage("删除失败, 自杀失败", c)
  251. return
  252. }
  253. if err := userService.DeleteUser(reqId.ID); err != nil {
  254. global.GVA_LOG.Error("删除失败!", zap.Any("err", err))
  255. response.FailWithMessage("删除失败", c)
  256. } else {
  257. response.OkWithMessage("删除成功", c)
  258. }
  259. }
  260. // @Tags SysUser
  261. // @Summary 设置用户信息
  262. // @Security ApiKeyAuth
  263. // @accept application/json
  264. // @Produce application/json
  265. // @Param data body system.SysUser true "ID, 用户名, 昵称, 头像链接"
  266. // @Success 200 {string} string "{"success":true,"data":{},"msg":"设置成功"}"
  267. // @Router /user/setUserInfo [put]
  268. func (b *BaseApi) SetUserInfo(c *gin.Context) {
  269. var user system.SysUser
  270. _ = c.ShouldBindJSON(&user)
  271. if err := utils.Verify(user, utils.IdVerify); err != nil {
  272. response.FailWithMessage(err.Error(), c)
  273. return
  274. }
  275. if err, ReqUser := userService.SetUserInfo(user); err != nil {
  276. global.GVA_LOG.Error("设置失败!", zap.Any("err", err))
  277. response.FailWithMessage("设置失败", c)
  278. } else {
  279. response.OkWithDetailed(gin.H{"userInfo": ReqUser}, "设置成功", c)
  280. }
  281. }
  282. // @Tags SysUser
  283. // @Summary 获取用户信息
  284. // @Security ApiKeyAuth
  285. // @accept application/json
  286. // @Produce application/json
  287. // @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
  288. // @Router /user/getUserInfo [get]
  289. func (b *BaseApi) GetUserInfo(c *gin.Context) {
  290. uuid := utils.GetUserUuid(c)
  291. if err, ReqUser := userService.GetUserInfo(uuid); err != nil {
  292. global.GVA_LOG.Error("获取失败!", zap.Any("err", err))
  293. response.FailWithMessage("获取失败", c)
  294. } else {
  295. response.OkWithDetailed(gin.H{"userInfo": ReqUser}, "获取成功", c)
  296. }
  297. }