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.

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