package routers

import (
	"FollowUp_Notice/conf"
	"FollowUp_Notice/lib"
	"FollowUp_Notice/logs"
	"FollowUp_Notice/models/Account"
	"github.com/beego/beego/v2/server/web/context"
	"strings"
)

var (
	filterExcludeURLMap        = make(map[string]int) // 不鉴权的URL
	filterOnlyLoginCheckURLMap = make(map[string]int) // 只鉴权登录的URL
)

func init() {
	//初始化配置 不鉴权的URL和只鉴权登录的URL
	logs.Println("=========== 初始化路由筛选信息 =========")

	excludeUrl := conf.FilterExcludeURL
	if len(excludeUrl) > 0 {
		excludeUrlSlice := strings.Split(excludeUrl, ",")
		if len(excludeUrlSlice) > 0 {
			for _, v := range excludeUrlSlice {
				filterExcludeURLMap[v] = 1
			}
		}
	}
	checkLoginUrl := conf.FilterOnlyLoginCheckURL
	if len(checkLoginUrl) > 0 {
		checkLoginUrlSlice := strings.Split(checkLoginUrl, ",")
		if len(checkLoginUrlSlice) > 0 {
			for _, v := range checkLoginUrlSlice {
				filterOnlyLoginCheckURLMap[v] = 1
			}
		}
	}
}

func RBACFilter(ctx *context.Context) {
	//判断URL是否排除
	if _, ok := filterExcludeURLMap[ctx.Request.URL.Path]; ok {
		return
	}
	is, user_r := Account.Verification(ctx.GetCookie("User_tokey"), ctx.Input.Query("User_tokey"))
	if !is {
		ctx.Output.JSON(lib.JSONS{Code: 201, Msg: "请重新登陆!"}, true, false)
		return
	}

	Account.User_r = &user_r

}
