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.

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