sys_user.go 7.4 KB

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