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