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.

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