user.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package controller
  2. import (
  3. "encoding/base64"
  4. "fmt"
  5. "github.com/gin-gonic/gin"
  6. "iot_manager_service/app/user/model"
  7. "iot_manager_service/app/user/service"
  8. "iot_manager_service/util/cache"
  9. "iot_manager_service/util/common"
  10. "iot_manager_service/util/rsa"
  11. "iot_manager_service/util/token"
  12. "iot_manager_service/util/websocket"
  13. "net/http"
  14. "strconv"
  15. )
  16. var SysUserController = new(user)
  17. type user struct{}
  18. func (u *user) Login(c *gin.Context) {
  19. loginSysUser := model.LoginSysUser{}
  20. if err := c.ShouldBindJSON(&loginSysUser); err != nil {
  21. c.JSON(http.StatusOK, common.ParamsInvalidResponse(err.Error(), nil))
  22. return
  23. }
  24. info := service.SysUserService.Login(loginSysUser.UserName, loginSysUser.PassWord)
  25. c.JSON(http.StatusOK, info)
  26. }
  27. func (u *user) GetPublicKey(c *gin.Context) {
  28. key := service.SysUserService.GetPublicKey()
  29. r := rsa.RSA{}
  30. publicKey := r.ReturnPublicKey()
  31. fmt.Print("key", publicKey)
  32. encryption := r.Encryption([]byte("123456"))
  33. toString := base64.StdEncoding.EncodeToString(encryption)
  34. fmt.Print("?????+++", toString)
  35. c.JSON(http.StatusOK, key)
  36. }
  37. func (u *user) GetDeptSysUsers(c *gin.Context) {
  38. depts := model.ReqDepts{}
  39. err := c.ShouldBindJSON(&depts)
  40. if err != nil {
  41. c.JSON(http.StatusOK, common.ParamsInvalidResponse(err.Error(), nil))
  42. return
  43. }
  44. users := service.SysUserService.GetDeptSysUsers(depts.Depts)
  45. c.JSON(http.StatusOK, users)
  46. }
  47. func (u *user) GetNewTokens(c *gin.Context) {
  48. header := c.GetHeader("Authorization")
  49. claims, _ := token.JwtClaims.ParseJwtToken(header)
  50. nowSysUser, _ := cache.GetNowSysUser(claims.ID)
  51. accessToken, _ := token.JwtClaims.CreateJwtToken(strconv.Itoa(nowSysUser.ID), nowSysUser.Username, nowSysUser.UUID, 1)
  52. refreshToken, _ := token.JwtClaims.CreateJwtToken(strconv.Itoa(nowSysUser.ID), nowSysUser.Username, nowSysUser.UUID, 24*7)
  53. tokens := make(map[string]interface{})
  54. tokens["accessToken"] = accessToken
  55. tokens["refreshToken"] = refreshToken
  56. c.JSON(http.StatusOK, common.SuccessResponse("获取成功", tokens))
  57. }
  58. func (u *user) Logout(c *gin.Context) {
  59. header := c.GetHeader("Authorization")
  60. claims, _ := token.JwtClaims.ParseJwtToken(header)
  61. err := cache.DeleteToken(claims.ID)
  62. if err != nil {
  63. c.JSON(http.StatusOK, common.ParamsInvalidResponse(err.Error(), nil))
  64. return
  65. }
  66. //关闭websocket连接
  67. websocket.CloseConn(claims.ID)
  68. c.JSON(http.StatusOK, common.SuccessResponse("退出成功", nil))
  69. }
  70. func (u *user) GetSysUser(c *gin.Context) {
  71. fmt.Println("demo。。。")
  72. }