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.

177 lines
5.4 KiB

  1. package api
  2. import (
  3. "fmt"
  4. "github.com/dgrijalva/jwt-go"
  5. "github.com/gin-gonic/gin"
  6. "main/controller/servers"
  7. "main/middleware"
  8. "main/model/dbModel"
  9. "main/model/modelInterface"
  10. "mime/multipart"
  11. "time"
  12. )
  13. var (
  14. USER_HEADER_IMG_PATH string = "http://qmplusimg.henrongyi.top"
  15. USER_HEADER_BUCKET string = "qm-plus-img"
  16. )
  17. type RegistAndLoginStuct struct {
  18. Username string `json:"username"`
  19. Password string `json:"password"`
  20. }
  21. // @Tags Base
  22. // @Summary 用户注册账号
  23. // @Produce application/json
  24. // @Param data body api.RegistAndLoginStuct true "用户注册接口"
  25. // @Success 200 {string} json "{"success":true,"data":{},"msg":"注册成功"}"
  26. // @Router /base/regist [post]
  27. func Regist(c *gin.Context) {
  28. var R RegistAndLoginStuct
  29. _ = c.BindJSON(&R)
  30. U := &dbModel.User{Username: R.Username, Password: R.Password}
  31. err, user := U.Regist()
  32. if err != nil {
  33. servers.ReportFormat(c, false, fmt.Sprintf("%v", err), gin.H{
  34. "user": user,
  35. })
  36. } else {
  37. servers.ReportFormat(c, true, "创建成功", gin.H{
  38. "user": user,
  39. })
  40. }
  41. }
  42. // @Tags Base
  43. // @Summary 用户登录
  44. // @Produce application/json
  45. // @Param data body api.RegistAndLoginStuct true "用户登录接口"
  46. // @Success 200 {string} json "{"success":true,"data":{},"msg":"登陆成功"}"
  47. // @Router /base/login [post]
  48. func Login(c *gin.Context) {
  49. var L RegistAndLoginStuct
  50. _ = c.BindJSON(&L)
  51. U := &dbModel.User{Username: L.Username, Password: L.Password}
  52. if err, user := U.Login(); err != nil {
  53. servers.ReportFormat(c, false, fmt.Sprintf("用户名密码错误或%v", err), gin.H{})
  54. } else {
  55. tokenNext(c, *user)
  56. }
  57. }
  58. //登录以后签发jwt
  59. func tokenNext(c *gin.Context, user dbModel.User) {
  60. j := &middleware.JWT{
  61. []byte("qmPlus"), // 唯一签名
  62. }
  63. clams := middleware.CustomClaims{
  64. UUID: user.UUID,
  65. ID: user.ID,
  66. NickName: user.NickName,
  67. AuthorityId: user.AuthorityId,
  68. StandardClaims: jwt.StandardClaims{
  69. NotBefore: int64(time.Now().Unix() - 1000), // 签名生效时间
  70. ExpiresAt: int64(time.Now().Unix() + 60*60*24*7), // 过期时间 一周
  71. Issuer: "qmPlus", //签名的发行者
  72. },
  73. }
  74. token, err := j.CreateToken(clams)
  75. if err != nil {
  76. servers.ReportFormat(c, false, "获取token失败", gin.H{})
  77. } else {
  78. servers.ReportFormat(c, true, "登录成功", gin.H{"user": user, "token": token, "expiresAt": clams.StandardClaims.ExpiresAt * 1000})
  79. }
  80. }
  81. type ChangePasswordStutrc struct {
  82. Username string `json:"username"`
  83. Password string `json:"password"`
  84. NewPassword string `json:"newPassword"`
  85. }
  86. // @Tags User
  87. // @Summary 用户修改密码
  88. // @Security ApiKeyAuth
  89. // @Produce application/json
  90. // @Param data body api.ChangePasswordStutrc true "用户修改密码"
  91. // @Success 200 {string} json "{"success":true,"data":{},"msg":"修改成功"}"
  92. // @Router /user/changePassword [post]
  93. func ChangePassword(c *gin.Context) {
  94. var params ChangePasswordStutrc
  95. _ = c.BindJSON(&params)
  96. U := &dbModel.User{Username: params.Username, Password: params.Password}
  97. if err, _ := U.ChangePassword(params.NewPassword); err != nil {
  98. servers.ReportFormat(c, false, "修改失败,请检查用户名密码", gin.H{})
  99. } else {
  100. servers.ReportFormat(c, true, "修改成功", gin.H{})
  101. }
  102. }
  103. type UserHeaderImg struct {
  104. HeaderImg multipart.File `json:"headerImg"`
  105. }
  106. // @Tags User
  107. // @Summary 用户上传头像
  108. // @Security ApiKeyAuth
  109. // @accept multipart/form-data
  110. // @Produce application/json
  111. // @Param headerImg formData file true "用户上传头像"
  112. // @Param username formData string true "用户上传头像"
  113. // @Success 200 {string} json "{"success":true,"data":{},"msg":"上传成功"}"
  114. // @Router /user/uploadHeaderImg [post]
  115. func UploadHeaderImg(c *gin.Context) {
  116. claims, _ := c.Get("claims")
  117. //获取头像文件
  118. // 这里我们通过断言获取 claims内的所有内容
  119. waitUse := claims.(*middleware.CustomClaims)
  120. fmt.Println(waitUse.NickName)
  121. _, header, err := c.Request.FormFile("headerImg")
  122. //便于找到用户 以后从jwt中取
  123. username := c.PostForm("username")
  124. if err != nil {
  125. servers.ReportFormat(c, false, fmt.Sprintf("上传文件失败,%v", err), gin.H{})
  126. } else {
  127. //文件上传后拿到文件路径
  128. err, filePath := servers.Upload(header, USER_HEADER_BUCKET, USER_HEADER_IMG_PATH)
  129. if err != nil {
  130. servers.ReportFormat(c, false, fmt.Sprintf("接收返回值失败,%v", err), gin.H{})
  131. } else {
  132. //修改数据库后得到修改后的user并且返回供前端使用
  133. err, user := new(dbModel.User).UploadHeaderImg(username, filePath)
  134. if err != nil {
  135. servers.ReportFormat(c, false, fmt.Sprintf("修改数据库链接失败,%v", err), gin.H{})
  136. } else {
  137. servers.ReportFormat(c, true, "上传成功", gin.H{"user": user})
  138. }
  139. }
  140. }
  141. }
  142. // @Tags User
  143. // @Summary 分页获取用户列表
  144. // @Security ApiKeyAuth
  145. // @accept application/json
  146. // @Produce application/json
  147. // @Param data body modelInterface.PageInfo true "分页获取用户列表"
  148. // @Success 200 {string} json "{"success":true,"data":{},"msg":"获取成功"}"
  149. // @Router /user/getInfoList [post]
  150. func GetInfoList(c *gin.Context) {
  151. var pageInfo modelInterface.PageInfo
  152. _ = c.BindJSON(&pageInfo)
  153. err, list, total := new(dbModel.User).GetInfoList(pageInfo)
  154. if err != nil {
  155. servers.ReportFormat(c, false, fmt.Sprintf("获取数据失败,%v", err), gin.H{})
  156. } else {
  157. servers.ReportFormat(c, true, "获取数据成功", gin.H{
  158. "userList": list,
  159. "total": total,
  160. "page": pageInfo.Page,
  161. "pageSize": pageInfo.PageSize,
  162. })
  163. }
  164. }