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.

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