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.

322 lines
11 KiB

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