sys_user.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. "gin-vue-admin/service"
  10. "gin-vue-admin/utils"
  11. "github.com/dchest/captcha"
  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. const (
  19. USER_HEADER_IMG_PATH string = "http://qmplusimg.henrongyi.top"
  20. USER_HEADER_BUCKET string = "qm-plus-img"
  21. )
  22. // @Tags Base
  23. // @Summary 用户注册账号
  24. // @Produce application/json
  25. // @Param data body model.SysUser true "用户注册接口"
  26. // @Success 200 {string} string "{"success":true,"data":{},"msg":"注册成功"}"
  27. // @Router /base/register [post]
  28. func Register(c *gin.Context) {
  29. var R request.RegisterStruct
  30. _ = c.ShouldBindJSON(&R)
  31. user := &model.SysUser{Username: R.Username, NickName: R.NickName, Password: R.Password, HeaderImg: R.HeaderImg, AuthorityId: R.AuthorityId}
  32. err, user := service.Register(user)
  33. if err != nil {
  34. response.Result(response.ERROR, gin.H{
  35. "user": user,
  36. }, fmt.Sprintf("%v", err), c)
  37. } else {
  38. response.Result(response.SUCCESS, gin.H{
  39. "user": user,
  40. }, "注册成功", c)
  41. }
  42. }
  43. // @Tags Base
  44. // @Summary 用户登录
  45. // @Produce application/json
  46. // @Param data body model.RegisterAndLoginStruct true "用户登录接口"
  47. // @Success 200 {string} string "{"success":true,"data":{},"msg":"登陆成功"}"
  48. // @Router /base/login [post]
  49. func Login(c *gin.Context) {
  50. var L request.RegisterAndLoginStruct
  51. _ = c.ShouldBindJSON(&L)
  52. if captcha.VerifyString(L.CaptchaId, L.Captcha) {
  53. U := &model.SysUser{Username: L.Username, Password: L.Password}
  54. if err, user := service.Login(U); err != nil {
  55. response.Result(response.ERROR, gin.H{}, fmt.Sprintf("用户名密码错误或%v", err), c)
  56. } else {
  57. tokenNext(c, *user)
  58. }
  59. } else {
  60. response.Result(response.ERROR, gin.H{}, "验证码错误", c)
  61. }
  62. }
  63. //登录以后签发jwt
  64. func tokenNext(c *gin.Context, user model.SysUser) {
  65. j := &middleware.JWT{
  66. []byte(global.GVA_CONFIG.JWT.SigningKey), // 唯一签名
  67. }
  68. clams := request.CustomClaims{
  69. UUID: user.UUID,
  70. ID: user.ID,
  71. NickName: user.NickName,
  72. AuthorityId: user.AuthorityId,
  73. StandardClaims: jwt.StandardClaims{
  74. NotBefore: int64(time.Now().Unix() - 1000), // 签名生效时间
  75. ExpiresAt: int64(time.Now().Unix() + 60*60*24*7), // 过期时间 一周
  76. Issuer: "qmPlus", //签名的发行者
  77. },
  78. }
  79. token, err := j.CreateToken(clams)
  80. if err != nil {
  81. response.Result(response.ERROR, gin.H{}, "获取token失败", c)
  82. } else {
  83. if global.GVA_CONFIG.System.UseMultipoint {
  84. var loginJwt model.JwtBlacklist
  85. loginJwt.Jwt = token
  86. err, jwtStr := service.GetRedisJWT(user.Username)
  87. if err == redis.Nil {
  88. err2 := service.SetRedisJWT(loginJwt, user.Username)
  89. if err2 != nil {
  90. response.Result(response.ERROR, gin.H{}, "设置登录状态失败", c)
  91. } else {
  92. response.Result(response.SUCCESS, gin.H{"user": user, "token": token, "expiresAt": clams.StandardClaims.ExpiresAt * 1000}, "登录成功", c)
  93. }
  94. } else if err != nil {
  95. response.Result(response.ERROR, gin.H{}, fmt.Sprintf("%v", err), c)
  96. } else {
  97. var blackJWT model.JwtBlacklist
  98. blackJWT.Jwt = jwtStr
  99. err3 := service.JsonInBlacklist(blackJWT)
  100. if err3 != nil {
  101. response.Result(response.ERROR, gin.H{}, "jwt作废失败", c)
  102. } else {
  103. err2 := service.SetRedisJWT(loginJwt, user.Username)
  104. if err2 != nil {
  105. response.Result(response.ERROR, gin.H{}, "设置登录状态失败", c)
  106. } else {
  107. response.Result(response.SUCCESS, gin.H{"user": user, "token": token, "expiresAt": clams.StandardClaims.ExpiresAt * 1000}, "登录成功", c)
  108. }
  109. }
  110. }
  111. } else {
  112. response.Result(response.SUCCESS, gin.H{"user": user, "token": token, "expiresAt": clams.StandardClaims.ExpiresAt * 1000}, "登录成功", c)
  113. }
  114. }
  115. }
  116. // @Tags SysUser
  117. // @Summary 用户修改密码
  118. // @Security ApiKeyAuth
  119. // @Produce application/json
  120. // @Param data body model.ChangePasswordStutrc true "用户修改密码"
  121. // @Success 200 {string} string "{"success":true,"data":{},"msg":"修改成功"}"
  122. // @Router /user/changePassword [put]
  123. func ChangePassword(c *gin.Context) {
  124. var params request.ChangePasswordStruct
  125. _ = c.ShouldBindJSON(&params)
  126. U := &model.SysUser{Username: params.Username, Password: params.Password}
  127. if err, _ := service.ChangePassword(U, params.NewPassword); err != nil {
  128. response.Result(response.ERROR, gin.H{}, "修改失败,请检查用户名密码", c)
  129. } else {
  130. response.Result(response.SUCCESS, gin.H{}, "修改成功", c)
  131. }
  132. }
  133. type UserHeaderImg struct {
  134. HeaderImg multipart.File `json:"headerImg"`
  135. }
  136. // @Tags SysUser
  137. // @Summary 用户上传头像
  138. // @Security ApiKeyAuth
  139. // @accept multipart/form-data
  140. // @Produce application/json
  141. // @Param headerImg formData file true "用户上传头像"
  142. // @Param username formData string true "用户上传头像"
  143. // @Success 200 {string} string "{"success":true,"data":{},"msg":"上传成功"}"
  144. // @Router /user/uploadHeaderImg [post]
  145. func UploadHeaderImg(c *gin.Context) {
  146. claims, _ := c.Get("claims")
  147. //获取头像文件
  148. // 这里我们通过断言获取 claims内的所有内容
  149. waitUse := claims.(*request.CustomClaims)
  150. uuid := waitUse.UUID
  151. _, header, err := c.Request.FormFile("headerImg")
  152. //便于找到用户 以后从jwt中取
  153. if err != nil {
  154. response.Result(response.ERROR, gin.H{}, fmt.Sprintf("上传文件失败,%v", err), c)
  155. } else {
  156. //文件上传后拿到文件路径
  157. err, filePath, _ := utils.Upload(header, USER_HEADER_BUCKET, USER_HEADER_IMG_PATH)
  158. if err != nil {
  159. response.Result(response.ERROR, gin.H{}, fmt.Sprintf("接收返回值失败,%v", err), c)
  160. } else {
  161. //修改数据库后得到修改后的user并且返回供前端使用
  162. err, user := service.UploadHeaderImg(uuid, filePath)
  163. if err != nil {
  164. response.Result(response.ERROR, gin.H{}, fmt.Sprintf("修改数据库链接失败,%v", err), c)
  165. } else {
  166. response.Result(response.SUCCESS, gin.H{"user": user}, "上传成功", c)
  167. }
  168. }
  169. }
  170. }
  171. // @Tags SysUser
  172. // @Summary 分页获取用户列表
  173. // @Security ApiKeyAuth
  174. // @accept application/json
  175. // @Produce application/json
  176. // @Param data body model.PageInfo true "分页获取用户列表"
  177. // @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
  178. // @Router /user/getUserList [post]
  179. func GetUserList(c *gin.Context) {
  180. var pageInfo request.PageInfo
  181. _ = c.ShouldBindJSON(&pageInfo)
  182. err, list, total := service.GetUserInfoList(pageInfo)
  183. if err != nil {
  184. response.Result(response.ERROR, gin.H{}, fmt.Sprintf("获取数据失败,%v", err), c)
  185. } else {
  186. response.Result(response.SUCCESS, gin.H{
  187. "userList": list,
  188. "total": total,
  189. "page": pageInfo.Page,
  190. "pageSize": pageInfo.PageSize,
  191. }, "获取数据成功", c)
  192. }
  193. }
  194. // @Tags SysUser
  195. // @Summary 设置用户权限
  196. // @Security ApiKeyAuth
  197. // @accept application/json
  198. // @Produce application/json
  199. // @Param data body model.SetUserAuth true "设置用户权限"
  200. // @Success 200 {string} string "{"success":true,"data":{},"msg":"修改成功"}"
  201. // @Router /user/setUserAuthority [post]
  202. func SetUserAuthority(c *gin.Context) {
  203. var sua request.SetUserAuth
  204. _ = c.ShouldBindJSON(&sua)
  205. err := service.SetUserAuthority(sua.UUID, sua.AuthorityId)
  206. if err != nil {
  207. response.Result(response.ERROR, gin.H{}, fmt.Sprintf("修改失败,%v", err), c)
  208. } else {
  209. response.Result(response.SUCCESS, gin.H{}, "修改成功", c)
  210. }
  211. }