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.

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