user.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package controller
  2. import (
  3. "fmt"
  4. "github.com/gin-gonic/gin"
  5. "iot_manager_service/app/user/model"
  6. "iot_manager_service/app/user/service"
  7. "iot_manager_service/util/cache"
  8. "iot_manager_service/util/common"
  9. "iot_manager_service/util/token"
  10. "net/http"
  11. )
  12. var UserController = new(user)
  13. type user struct{}
  14. func (u *user) Login(c *gin.Context) {
  15. loginUser := model.LoginUser{}
  16. if err := c.ShouldBindJSON(&loginUser); err != nil {
  17. c.JSON(http.StatusOK, common.ParamsInvalidResponse(err.Error(), nil))
  18. return
  19. }
  20. info := service.UserService.Login(loginUser.Account, loginUser.PassWord)
  21. c.JSON(http.StatusOK, info)
  22. }
  23. func (u *user) GetPublicKey(c *gin.Context) {
  24. key := service.UserService.GetPublicKey()
  25. c.JSON(http.StatusOK, key)
  26. }
  27. func (u *user) GetDeptUsers(c *gin.Context) {
  28. depts := model.ReqDepts{}
  29. err := c.ShouldBindJSON(&depts)
  30. if err != nil {
  31. c.JSON(http.StatusOK, common.ParamsInvalidResponse(err.Error(), nil))
  32. return
  33. }
  34. users := service.UserService.GetDeptUsers(depts.Depts)
  35. c.JSON(http.StatusOK, users)
  36. }
  37. func (u *user) Logout(c *gin.Context) {
  38. header := c.GetHeader("Authorization")
  39. claims, _ := token.JwtClaims.ParseJwtToken(header)
  40. err := cache.DeleteToken(claims.ID)
  41. if err != nil {
  42. c.JSON(http.StatusOK, common.ParamsInvalidResponse(err.Error(), nil))
  43. return
  44. }
  45. c.JSON(http.StatusOK, common.SuccessResponse("退出成功", nil))
  46. }
  47. func (u *user) GetUser(c *gin.Context) {
  48. fmt.Println("demo。。。")
  49. }