package middware import ( "net/http" "adm-data-gateway/errors" "adm-data-gateway/model" "github.com/gin-gonic/gin" "git.getensh.com/common/gopkgsv2/database" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" "gorm.io/gorm" ) // 响应数据 type Response struct { Code codes.Code `json:"code" default:"10001"` Message string `json:"message" default:"success"` } // 用户身份验证 func Verify() gin.HandlerFunc { return func(ctx *gin.Context) { appKey := ctx.GetHeader("appkey") s := status.New(10001, "内部服务错误") if appKey == "" { if v, ok := status.FromError(errors.AppKeyNotExistError); ok { s = v } ctx.JSON(http.StatusOK, Response{s.Code(), s.Message()}) ctx.Abort() return } _, err := model.NewAdmKeyManagementModel().Get(database.DB().Where("`key` = ?", appKey)) if err != nil && err != gorm.ErrRecordNotFound { if v, ok := status.FromError(errors.SystemError); ok { s = v } ctx.JSON(http.StatusOK, Response{s.Code(), s.Message()}) ctx.Abort() return } if err == gorm.ErrRecordNotFound { if v, ok := status.FromError(errors.AppKeyNotExist1Error); ok { s = v } ctx.JSON(http.StatusOK, Response{s.Code(), s.Message()}) ctx.Abort() return } ctx.Next() } }