package com.ruoyi.common.core.content;


import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.enums.ExamType;
import org.apache.commons.lang3.StringUtils;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

/**
 * 身份验证信息
 * 
 * @author ruoyi
 */
public class VistorContextHolder
{
    private static final ThreadLocal<SysUser> contextHolder = new ThreadLocal<>();

    public static SysUser getContext()
    {
        return contextHolder.get();
    }

    public static void setContext(SysUser context)
    {
        contextHolder.set(context);
    }

    public static void clearContext()
    {
        contextHolder.remove();
    }

    public static ExamType getExamType() {
        return getContext().getExamType();
    }

    public static String getLocation() {
        SysUser context = getContext();
        if (context == null) {
            return null;
        }
        String location = context.getLocation();
        if (StringUtils.isBlank(location)) {
            return location;
        }
        // 如果 location 是 URL 编码的（包含 %），则进行解码
        if (location.contains("%")) {
            try {
                return URLDecoder.decode(location, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                // 如果解码失败，返回原始值
                return location;
            }
        }
        return location;
    }
}
