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.

255 lines
8.9 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. "gin-vue-admin/global"
  4. "gin-vue-admin/middleware"
  5. "gin-vue-admin/model/common/request"
  6. "gin-vue-admin/model/common/response"
  7. "gin-vue-admin/model/system"
  8. systemReq "gin-vue-admin/model/system/request"
  9. systemRes "gin-vue-admin/model/system/response"
  10. "gin-vue-admin/utils"
  11. "time"
  12. "github.com/dgrijalva/jwt-go"
  13. "github.com/gin-gonic/gin"
  14. "github.com/go-redis/redis"
  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 := &middleware.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. user := &system.SysUser{Username: r.Username, NickName: r.NickName, Password: r.Password, HeaderImg: r.HeaderImg, AuthorityId: r.AuthorityId}
  118. err, userReturn := userService.Register(*user)
  119. if err != nil {
  120. global.GVA_LOG.Error("注册失败!", zap.Any("err", err))
  121. response.FailWithDetailed(systemRes.SysUserResponse{User: userReturn}, "注册失败", c)
  122. } else {
  123. response.OkWithDetailed(systemRes.SysUserResponse{User: userReturn}, "注册成功", c)
  124. }
  125. }
  126. // @Tags SysUser
  127. // @Summary 用户修改密码
  128. // @Security ApiKeyAuth
  129. // @Produce application/json
  130. // @Param data body systemReq.ChangePasswordStruct true "用户名, 原密码, 新密码"
  131. // @Success 200 {string} string "{"success":true,"data":{},"msg":"修改成功"}"
  132. // @Router /user/changePassword [put]
  133. func (b *BaseApi) ChangePassword(c *gin.Context) {
  134. var user systemReq.ChangePasswordStruct
  135. _ = c.ShouldBindJSON(&user)
  136. if err := utils.Verify(user, utils.ChangePasswordVerify); err != nil {
  137. response.FailWithMessage(err.Error(), c)
  138. return
  139. }
  140. u := &system.SysUser{Username: user.Username, Password: user.Password}
  141. if err, _ := userService.ChangePassword(u, user.NewPassword); err != nil {
  142. global.GVA_LOG.Error("修改失败!", zap.Any("err", err))
  143. response.FailWithMessage("修改失败,原密码与当前账户不符", c)
  144. } else {
  145. response.OkWithMessage("修改成功", c)
  146. }
  147. }
  148. // @Tags SysUser
  149. // @Summary 分页获取用户列表
  150. // @Security ApiKeyAuth
  151. // @accept application/json
  152. // @Produce application/json
  153. // @Param data body request.PageInfo true "页码, 每页大小"
  154. // @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
  155. // @Router /user/getUserList [post]
  156. func (b *BaseApi) GetUserList(c *gin.Context) {
  157. var pageInfo request.PageInfo
  158. _ = c.ShouldBindJSON(&pageInfo)
  159. if err := utils.Verify(pageInfo, utils.PageInfoVerify); err != nil {
  160. response.FailWithMessage(err.Error(), c)
  161. return
  162. }
  163. if err, list, total := userService.GetUserInfoList(pageInfo); err != nil {
  164. global.GVA_LOG.Error("获取失败!", zap.Any("err", err))
  165. response.FailWithMessage("获取失败", c)
  166. } else {
  167. response.OkWithDetailed(response.PageResult{
  168. List: list,
  169. Total: total,
  170. Page: pageInfo.Page,
  171. PageSize: pageInfo.PageSize,
  172. }, "获取成功", c)
  173. }
  174. }
  175. // @Tags SysUser
  176. // @Summary 设置用户权限
  177. // @Security ApiKeyAuth
  178. // @accept application/json
  179. // @Produce application/json
  180. // @Param data body systemReq.SetUserAuth true "用户UUID, 角色ID"
  181. // @Success 200 {string} string "{"success":true,"data":{},"msg":"修改成功"}"
  182. // @Router /user/setUserAuthority [post]
  183. func (b *BaseApi) SetUserAuthority(c *gin.Context) {
  184. var sua systemReq.SetUserAuth
  185. _ = c.ShouldBindJSON(&sua)
  186. if UserVerifyErr := utils.Verify(sua, utils.SetUserAuthorityVerify); UserVerifyErr != nil {
  187. response.FailWithMessage(UserVerifyErr.Error(), c)
  188. return
  189. }
  190. if err := userService.SetUserAuthority(sua.UUID, sua.AuthorityId); err != nil {
  191. global.GVA_LOG.Error("修改失败!", zap.Any("err", err))
  192. response.FailWithMessage("修改失败", c)
  193. } else {
  194. response.OkWithMessage("修改成功", c)
  195. }
  196. }
  197. // @Tags SysUser
  198. // @Summary 删除用户
  199. // @Security ApiKeyAuth
  200. // @accept application/json
  201. // @Produce application/json
  202. // @Param data body request.GetById true "用户ID"
  203. // @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
  204. // @Router /user/deleteUser [delete]
  205. func (b *BaseApi) DeleteUser(c *gin.Context) {
  206. var reqId request.GetById
  207. _ = c.ShouldBindJSON(&reqId)
  208. if err := utils.Verify(reqId, utils.IdVerify); err != nil {
  209. response.FailWithMessage(err.Error(), c)
  210. return
  211. }
  212. jwtId := utils.GetUserID(c)
  213. if jwtId == uint(reqId.ID) {
  214. response.FailWithMessage("删除失败, 自杀失败", c)
  215. return
  216. }
  217. if err := userService.DeleteUser(reqId.ID); err != nil {
  218. global.GVA_LOG.Error("删除失败!", zap.Any("err", err))
  219. response.FailWithMessage("删除失败", c)
  220. } else {
  221. response.OkWithMessage("删除成功", c)
  222. }
  223. }
  224. // @Tags SysUser
  225. // @Summary 设置用户信息
  226. // @Security ApiKeyAuth
  227. // @accept application/json
  228. // @Produce application/json
  229. // @Param data body model.SysUser true "ID, 用户名, 昵称, 头像链接"
  230. // @Success 200 {string} string "{"success":true,"data":{},"msg":"设置成功"}"
  231. // @Router /user/setUserInfo [put]
  232. func (b *BaseApi) SetUserInfo(c *gin.Context) {
  233. var user system.SysUser
  234. _ = c.ShouldBindJSON(&user)
  235. if err := utils.Verify(user, utils.IdVerify); err != nil {
  236. response.FailWithMessage(err.Error(), c)
  237. return
  238. }
  239. if err, ReqUser := userService.SetUserInfo(user); err != nil {
  240. global.GVA_LOG.Error("设置失败!", zap.Any("err", err))
  241. response.FailWithMessage("设置失败", c)
  242. } else {
  243. response.OkWithDetailed(gin.H{"userInfo": ReqUser}, "设置成功", c)
  244. }
  245. }