package middleware import ( "MeterService/database/appApi" "strconv" "github.com/gin-gonic/gin" ) func UseCheckApp() gin.HandlerFunc { return func(c *gin.Context) { appIdStr := c.Request.Header.Get("AppId") appSecret := c.Request.Header.Get("Secret") if appIdStr == "" || appSecret == "" { c.AbortWithStatusJSON(401, gin.H{ "code": 401, "msg": "appId和secret不能为空", }) return } appId, err := strconv.Atoi(appIdStr) if err != nil { c.AbortWithStatusJSON(401, gin.H{ "code": 401, "msg": "appId必须为数字", }) return } dbAppApi := appApi.NewAppApiDb() if !dbAppApi.CheckSecret(&appApi.App{ AppId: appId, AppSecret: appSecret, }) { c.AbortWithStatusJSON(401, gin.H{ "code": 401, "msg": "appId和secret不匹配", }) return } c.Next() } }