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.

239 lines
7.8 KiB

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