package handler

import (
	"cicv-data-closedloop/common/config/c_log"
	"cicv-data-closedloop/common/entity"
	"github.com/gin-gonic/gin"
	"net/http"
)

var token = "U9yKpD6kZZDDe4LFKK6myAxBUT1XRrDM"

func ValidateHeaders() gin.HandlerFunc {
	return func(c *gin.Context) {
		acceptToken := c.GetHeader("Authorization")
		if acceptToken == "" {
			c_log.GlobalLogger.Errorf("未添加请求头 Authorization：%v", acceptToken)
			c.JSON(http.StatusOK, entity.HttpResult{Status: false, Code: "1001", Message: "未添加请求头 Authorization。"})
			c.Abort()
			return
		}
		if acceptToken != token {
			c_log.GlobalLogger.Errorf("请求头 Authorization 校验失败：%v --> %v", acceptToken[0], token)
			c.JSON(http.StatusOK, entity.HttpResult{Status: false, Code: "1002", Message: "请求头 Authorization 校验失败。"})
			c.Abort()
			return
		}
		c.Next()
	}
}
