verify.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package middware
  2. import (
  3. "net/http"
  4. "adm-data-gateway/errors"
  5. "adm-data-gateway/model"
  6. "github.com/gin-gonic/gin"
  7. "git.getensh.com/common/gopkgsv2/database"
  8. "google.golang.org/grpc/codes"
  9. "google.golang.org/grpc/status"
  10. "gorm.io/gorm"
  11. )
  12. // 响应数据
  13. type Response struct {
  14. Code codes.Code `json:"code" default:"10001"`
  15. Message string `json:"message" default:"success"`
  16. }
  17. // 用户身份验证
  18. func Verify() gin.HandlerFunc {
  19. return func(ctx *gin.Context) {
  20. appKey := ctx.GetHeader("appkey")
  21. s := status.New(10001, "内部服务错误")
  22. if appKey == "" {
  23. if v, ok := status.FromError(errors.AppKeyNotExistError); ok {
  24. s = v
  25. }
  26. ctx.JSON(http.StatusOK, Response{s.Code(), s.Message()})
  27. ctx.Abort()
  28. return
  29. }
  30. _, err := model.NewAdmKeyManagementModel().Get(database.DB().Where("`key` = ?", appKey))
  31. if err != nil && err != gorm.ErrRecordNotFound {
  32. if v, ok := status.FromError(errors.SystemError); ok {
  33. s = v
  34. }
  35. ctx.JSON(http.StatusOK, Response{s.Code(), s.Message()})
  36. ctx.Abort()
  37. return
  38. }
  39. if err == gorm.ErrRecordNotFound {
  40. if v, ok := status.FromError(errors.AppKeyNotExist1Error); ok {
  41. s = v
  42. }
  43. ctx.JSON(http.StatusOK, Response{s.Code(), s.Message()})
  44. ctx.Abort()
  45. return
  46. }
  47. ctx.Next()
  48. }
  49. }