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.

200 lines
6.2 KiB

  1. package api
  2. import (
  3. "fmt"
  4. "github.com/dgrijalva/jwt-go"
  5. "github.com/gin-gonic/gin"
  6. uuid "github.com/satori/go.uuid"
  7. "main/controller/servers"
  8. "main/middleware"
  9. "main/model/sysModel"
  10. "main/model/modelInterface"
  11. "mime/multipart"
  12. "time"
  13. )
  14. var (
  15. USER_HEADER_IMG_PATH string = "http://qmplusimg.henrongyi.top"
  16. USER_HEADER_BUCKET string = "qm-plus-img"
  17. )
  18. type RegistAndLoginStuct struct {
  19. Username string `json:"username"`
  20. Password string `json:"password"`
  21. }
  22. // @Tags Base
  23. // @Summary 用户注册账号
  24. // @Produce application/json
  25. // @Param data body api.RegistAndLoginStuct true "用户注册接口"
  26. // @Success 200 {string} json "{"success":true,"data":{},"msg":"注册成功"}"
  27. // @Router /base/regist [post]
  28. func Regist(c *gin.Context) {
  29. var R RegistAndLoginStuct
  30. _ = c.BindJSON(&R)
  31. U := &sysModel.SysUser{Username: R.Username, Password: R.Password}
  32. err, user := U.Regist()
  33. if err != nil {
  34. servers.ReportFormat(c, false, fmt.Sprintf("%v", err), gin.H{
  35. "user": user,
  36. })
  37. } else {
  38. servers.ReportFormat(c, true, "创建成功", gin.H{
  39. "user": user,
  40. })
  41. }
  42. }
  43. // @Tags Base
  44. // @Summary 用户登录
  45. // @Produce application/json
  46. // @Param data body api.RegistAndLoginStuct true "用户登录接口"
  47. // @Success 200 {string} json "{"success":true,"data":{},"msg":"登陆成功"}"
  48. // @Router /base/login [post]
  49. func Login(c *gin.Context) {
  50. var L RegistAndLoginStuct
  51. _ = c.BindJSON(&L)
  52. U := &sysModel.SysUser{Username: L.Username, Password: L.Password}
  53. if err, user := U.Login(); err != nil {
  54. servers.ReportFormat(c, false, fmt.Sprintf("用户名密码错误或%v", err), gin.H{})
  55. } else {
  56. tokenNext(c, *user)
  57. }
  58. }
  59. //登录以后签发jwt
  60. func tokenNext(c *gin.Context, user sysModel.SysUser) {
  61. j := &middleware.JWT{
  62. []byte("qmPlus"), // 唯一签名
  63. }
  64. clams := middleware.CustomClaims{
  65. UUID: user.UUID,
  66. ID: user.ID,
  67. NickName: user.NickName,
  68. AuthorityId: user.AuthorityId,
  69. StandardClaims: jwt.StandardClaims{
  70. NotBefore: int64(time.Now().Unix() - 1000), // 签名生效时间
  71. ExpiresAt: int64(time.Now().Unix() + 60*60*24*7), // 过期时间 一周
  72. Issuer: "qmPlus", //签名的发行者
  73. },
  74. }
  75. token, err := j.CreateToken(clams)
  76. if err != nil {
  77. servers.ReportFormat(c, false, "获取token失败", gin.H{})
  78. } else {
  79. servers.ReportFormat(c, true, "登录成功", gin.H{"user": user, "token": token, "expiresAt": clams.StandardClaims.ExpiresAt * 1000})
  80. }
  81. }
  82. type ChangePasswordStutrc struct {
  83. Username string `json:"username"`
  84. Password string `json:"password"`
  85. NewPassword string `json:"newPassword"`
  86. }
  87. // @Tags SysUser
  88. // @Summary 用户修改密码
  89. // @Security ApiKeyAuth
  90. // @Produce application/json
  91. // @Param data body api.ChangePasswordStutrc true "用户修改密码"
  92. // @Success 200 {string} json "{"success":true,"data":{},"msg":"修改成功"}"
  93. // @Router /user/changePassword [post]
  94. func ChangePassword(c *gin.Context) {
  95. var params ChangePasswordStutrc
  96. _ = c.BindJSON(&params)
  97. U := &sysModel.SysUser{Username: params.Username, Password: params.Password}
  98. if err, _ := U.ChangePassword(params.NewPassword); err != nil {
  99. servers.ReportFormat(c, false, "修改失败,请检查用户名密码", gin.H{})
  100. } else {
  101. servers.ReportFormat(c, true, "修改成功", gin.H{})
  102. }
  103. }
  104. type UserHeaderImg struct {
  105. HeaderImg multipart.File `json:"headerImg"`
  106. }
  107. // @Tags SysUser
  108. // @Summary 用户上传头像
  109. // @Security ApiKeyAuth
  110. // @accept multipart/form-data
  111. // @Produce application/json
  112. // @Param headerImg formData file true "用户上传头像"
  113. // @Param username formData string true "用户上传头像"
  114. // @Success 200 {string} json "{"success":true,"data":{},"msg":"上传成功"}"
  115. // @Router /user/uploadHeaderImg [post]
  116. func UploadHeaderImg(c *gin.Context) {
  117. claims, _ := c.Get("claims")
  118. //获取头像文件
  119. // 这里我们通过断言获取 claims内的所有内容
  120. waitUse := claims.(*middleware.CustomClaims)
  121. uuid := waitUse.UUID
  122. _, header, err := c.Request.FormFile("headerImg")
  123. //便于找到用户 以后从jwt中取
  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(sysModel.SysUser).UploadHeaderImg(uuid, 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 SysUser
  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/getUserList [post]
  150. func GetUserList(c *gin.Context) {
  151. var pageInfo modelInterface.PageInfo
  152. _ = c.BindJSON(&pageInfo)
  153. err, list, total := new(sysModel.SysUser).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. }
  165. type SetUserAuth struct {
  166. UUID uuid.UUID `json:"uuid"`
  167. AuthorityId string `json:"authorityId"`
  168. }
  169. // @Tags SysUser
  170. // @Summary 设置用户权限
  171. // @Security ApiKeyAuth
  172. // @accept application/json
  173. // @Produce application/json
  174. // @Param data body api.SetUserAuth true "设置用户权限"
  175. // @Success 200 {string} json "{"success":true,"data":{},"msg":"修改成功"}"
  176. // @Router /user/setUserAuthority [post]
  177. func SetUserAuthority(c *gin.Context) {
  178. var sua SetUserAuth
  179. _ = c.BindJSON(&sua)
  180. err := new(sysModel.SysUser).SetUserAuthority(sua.UUID, sua.AuthorityId)
  181. if err != nil {
  182. servers.ReportFormat(c, false, fmt.Sprintf("修改失败,%v", err), gin.H{})
  183. } else {
  184. servers.ReportFormat(c, true, "修改成功", gin.H{})
  185. }
  186. }