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.

274 lines
8.7 KiB

  1. package v1
  2. import (
  3. "fmt"
  4. "gin-vue-admin/global"
  5. "gin-vue-admin/global/response"
  6. "gin-vue-admin/middleware"
  7. "gin-vue-admin/model"
  8. "gin-vue-admin/model/request"
  9. resp "gin-vue-admin/model/response"
  10. "gin-vue-admin/service"
  11. "gin-vue-admin/utils"
  12. "github.com/dgrijalva/jwt-go"
  13. "github.com/gin-gonic/gin"
  14. "github.com/go-redis/redis"
  15. "time"
  16. )
  17. // @Tags Base
  18. // @Summary 用户登录
  19. // @Produce application/json
  20. // @Param data body request.RegisterAndLoginStruct true "用户名, 密码, 验证码"
  21. // @Success 200 {string} string "{"success":true,"data":{},"msg":"登陆成功"}"
  22. // @Router /base/login [post]
  23. func Login(c *gin.Context) {
  24. var L request.RegisterAndLoginStruct
  25. _ = c.ShouldBindJSON(&L)
  26. UserVerify := utils.Rules{
  27. "CaptchaId": {utils.NotEmpty()},
  28. "Captcha": {utils.NotEmpty()},
  29. "Username": {utils.NotEmpty()},
  30. "Password": {utils.NotEmpty()},
  31. }
  32. if UserVerifyErr := utils.Verify(L, UserVerify); UserVerifyErr != nil {
  33. response.FailWithMessage(UserVerifyErr.Error(), c)
  34. return
  35. }
  36. if store.Verify(L.CaptchaId, L.Captcha, true) {
  37. U := &model.SysUser{Username: L.Username, Password: L.Password}
  38. if err, user := service.Login(U); err != nil {
  39. response.FailWithMessage(fmt.Sprintf("用户名密码错误或%v", err), c)
  40. } else {
  41. tokenNext(c, *user)
  42. }
  43. } else {
  44. response.FailWithMessage("验证码错误", c)
  45. }
  46. }
  47. // 登录以后签发jwt
  48. func tokenNext(c *gin.Context, user model.SysUser) {
  49. j := &middleware.JWT{SigningKey: []byte(global.GVA_CONFIG.JWT.SigningKey)} // 唯一签名
  50. clams := request.CustomClaims{
  51. UUID: user.UUID,
  52. ID: user.ID,
  53. NickName: user.NickName,
  54. Username: user.Username,
  55. AuthorityId: user.AuthorityId,
  56. BufferTime: 60 * 60 * 24, // 缓冲时间1天 缓冲时间内会获得新的token刷新令牌 此时一个用户会存在两个有效令牌 但是前端只留一个 另一个会丢失
  57. StandardClaims: jwt.StandardClaims{
  58. NotBefore: time.Now().Unix() - 1000, // 签名生效时间
  59. ExpiresAt: time.Now().Unix() + 60*60*24*7, // 过期时间 7天
  60. Issuer: "qmPlus", // 签名的发行者
  61. },
  62. }
  63. token, err := j.CreateToken(clams)
  64. if err != nil {
  65. response.FailWithMessage("获取token失败", c)
  66. return
  67. }
  68. if !global.GVA_CONFIG.System.UseMultipoint {
  69. response.OkWithData(resp.LoginResponse{
  70. User: user,
  71. Token: token,
  72. ExpiresAt: clams.StandardClaims.ExpiresAt * 1000,
  73. }, c)
  74. return
  75. }
  76. err, jwtStr := service.GetRedisJWT(user.Username)
  77. if err == redis.Nil {
  78. if err := service.SetRedisJWT(token, user.Username); err != nil {
  79. response.FailWithMessage("设置登录状态失败", c)
  80. return
  81. }
  82. response.OkWithData(resp.LoginResponse{
  83. User: user,
  84. Token: token,
  85. ExpiresAt: clams.StandardClaims.ExpiresAt * 1000,
  86. }, c)
  87. } else if err != nil {
  88. response.FailWithMessage(fmt.Sprintf("%v", err), c)
  89. } else {
  90. var blackJWT model.JwtBlacklist
  91. blackJWT.Jwt = jwtStr
  92. if err := service.JsonInBlacklist(blackJWT); err != nil {
  93. response.FailWithMessage("jwt作废失败", c)
  94. return
  95. }
  96. if err := service.SetRedisJWT(token, user.Username); err != nil {
  97. response.FailWithMessage("设置登录状态失败", c)
  98. return
  99. }
  100. response.OkWithData(resp.LoginResponse{
  101. User: user,
  102. Token: token,
  103. ExpiresAt: clams.StandardClaims.ExpiresAt * 1000,
  104. }, c)
  105. }
  106. }
  107. // @Tags SysUser
  108. // @Summary 用户注册账号
  109. // @Produce application/json
  110. // @Param data body model.SysUser true "用户名, 昵称, 密码, 角色ID"
  111. // @Success 200 {string} string "{"success":true,"data":{},"msg":"注册成功"}"
  112. // @Router /user/register [post]
  113. func Register(c *gin.Context) {
  114. var R request.RegisterStruct
  115. _ = c.ShouldBindJSON(&R)
  116. UserVerify := utils.Rules{
  117. "Username": {utils.NotEmpty()},
  118. "NickName": {utils.NotEmpty()},
  119. "Password": {utils.NotEmpty()},
  120. "AuthorityId": {utils.NotEmpty()},
  121. }
  122. if UserVerifyErr := utils.Verify(R, UserVerify); UserVerifyErr != nil {
  123. response.FailWithMessage(UserVerifyErr.Error(), c)
  124. return
  125. }
  126. user := &model.SysUser{Username: R.Username, NickName: R.NickName, Password: R.Password, HeaderImg: R.HeaderImg, AuthorityId: R.AuthorityId}
  127. err, userReturn := service.Register(*user)
  128. if err != nil {
  129. response.FailWithDetailed(response.ERROR, resp.SysUserResponse{User: userReturn}, fmt.Sprintf("%v", err), c)
  130. } else {
  131. response.OkDetailed(resp.SysUserResponse{User: userReturn}, "注册成功", c)
  132. }
  133. }
  134. // @Tags SysUser
  135. // @Summary 用户修改密码
  136. // @Security ApiKeyAuth
  137. // @Produce application/json
  138. // @Param data body request.ChangePasswordStruct true "用户名, 原密码, 新密码"
  139. // @Success 200 {string} string "{"success":true,"data":{},"msg":"修改成功"}"
  140. // @Router /user/changePassword [put]
  141. func ChangePassword(c *gin.Context) {
  142. var params request.ChangePasswordStruct
  143. _ = c.ShouldBindJSON(&params)
  144. UserVerify := utils.Rules{
  145. "Username": {utils.NotEmpty()},
  146. "Password": {utils.NotEmpty()},
  147. "NewPassword": {utils.NotEmpty()},
  148. }
  149. if UserVerifyErr := utils.Verify(params, UserVerify); UserVerifyErr != nil {
  150. response.FailWithMessage(UserVerifyErr.Error(), c)
  151. return
  152. }
  153. U := &model.SysUser{Username: params.Username, Password: params.Password}
  154. if err, _ := service.ChangePassword(U, params.NewPassword); err != nil {
  155. response.FailWithMessage("修改失败,请检查用户名密码", c)
  156. } else {
  157. response.OkWithMessage("修改成功", c)
  158. }
  159. }
  160. // @Tags SysUser
  161. // @Summary 分页获取用户列表
  162. // @Security ApiKeyAuth
  163. // @accept application/json
  164. // @Produce application/json
  165. // @Param data body request.PageInfo true "页码, 页大小"
  166. // @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
  167. // @Router /user/getUserList [post]
  168. func GetUserList(c *gin.Context) {
  169. var pageInfo request.PageInfo
  170. _ = c.ShouldBindJSON(&pageInfo)
  171. if PageVerifyErr := utils.Verify(pageInfo, utils.CustomizeMap["PageVerify"]); PageVerifyErr != nil {
  172. response.FailWithMessage(PageVerifyErr.Error(), c)
  173. return
  174. }
  175. err, list, total := service.GetUserInfoList(pageInfo)
  176. if err != nil {
  177. response.FailWithMessage(fmt.Sprintf("获取数据失败,%v", err), c)
  178. } else {
  179. response.OkWithData(resp.PageResult{
  180. List: list,
  181. Total: total,
  182. Page: pageInfo.Page,
  183. PageSize: pageInfo.PageSize,
  184. }, c)
  185. }
  186. }
  187. // @Tags SysUser
  188. // @Summary 设置用户权限
  189. // @Security ApiKeyAuth
  190. // @accept application/json
  191. // @Produce application/json
  192. // @Param data body request.SetUserAuth true "用户UUID, 角色ID"
  193. // @Success 200 {string} string "{"success":true,"data":{},"msg":"修改成功"}"
  194. // @Router /user/setUserAuthority [post]
  195. func SetUserAuthority(c *gin.Context) {
  196. var sua request.SetUserAuth
  197. _ = c.ShouldBindJSON(&sua)
  198. UserVerify := utils.Rules{
  199. "UUID": {utils.NotEmpty()},
  200. "AuthorityId": {utils.NotEmpty()},
  201. }
  202. if UserVerifyErr := utils.Verify(sua, UserVerify); UserVerifyErr != nil {
  203. response.FailWithMessage(UserVerifyErr.Error(), c)
  204. return
  205. }
  206. err := service.SetUserAuthority(sua.UUID, sua.AuthorityId)
  207. if err != nil {
  208. response.FailWithMessage(fmt.Sprintf("修改失败,%v", err), c)
  209. } else {
  210. response.OkWithMessage("修改成功", c)
  211. }
  212. }
  213. // @Tags SysUser
  214. // @Summary 删除用户
  215. // @Security ApiKeyAuth
  216. // @accept application/json
  217. // @Produce application/json
  218. // @Param data body request.GetById true "用户ID"
  219. // @Success 200 {string} string "{"success":true,"data":{},"msg":"修改成功"}"
  220. // @Router /user/deleteUser [delete]
  221. func DeleteUser(c *gin.Context) {
  222. var reqId request.GetById
  223. _ = c.ShouldBindJSON(&reqId)
  224. if IdVerifyErr := utils.Verify(reqId, utils.CustomizeMap["IdVerify"]); IdVerifyErr != nil {
  225. response.FailWithMessage(IdVerifyErr.Error(), c)
  226. return
  227. }
  228. err := service.DeleteUser(reqId.Id)
  229. if err != nil {
  230. response.FailWithMessage(fmt.Sprintf("删除失败,%v", err), c)
  231. } else {
  232. response.OkWithMessage("删除成功", c)
  233. }
  234. }
  235. // @Tags SysUser
  236. // @Summary 设置用户信息
  237. // @Security ApiKeyAuth
  238. // @accept application/json
  239. // @Produce application/json
  240. // @Param data body model.SysUser true "ID, 用户名, 昵称, 头像链接"
  241. // @Success 200 {string} string "{"success":true,"data":{},"msg":"修改成功"}"
  242. // @Router /user/setUserInfo [put]
  243. func SetUserInfo(c *gin.Context) {
  244. var user model.SysUser
  245. UserVerify := utils.Rules{
  246. "ID": {utils.NotEmpty()},
  247. "Username": {utils.NotEmpty()},
  248. "NickName": {utils.NotEmpty()},
  249. "HeaderImg": {utils.NotEmpty()},
  250. }
  251. if UserVerifyErr := utils.Verify(user, UserVerify); UserVerifyErr != nil {
  252. response.FailWithMessage(UserVerifyErr.Error(), c)
  253. return
  254. }
  255. _ = c.ShouldBindJSON(&user)
  256. err, ReqUser := service.SetUserInfo(user)
  257. if err != nil {
  258. response.FailWithMessage(fmt.Sprintf("更新失败,%v", err), c)
  259. } else {
  260. response.OkWithData(gin.H{
  261. "userInfo": ReqUser,
  262. }, c)
  263. }
  264. }