package org.dromara.web.utils;


import lombok.experimental.UtilityClass;

/**
 * @author LGH
 */
@UtilityClass
public class SecurityUtils {

    private static final String USER_REQUEST = "/api/";

    /**
     * 获取用户
     */
    public YamiUser getUser() {
        if (!HttpContextUtils.getHttpServletRequest().getRequestURI().startsWith(USER_REQUEST)) {
            // 用户相关的请求，应该以/p开头！！！
            throw new RuntimeException("登录过期或已失效");
        }

        String accessToken = HttpContextUtils.getHttpServletRequest().getHeader("accessToken");
        TokenStore tokenStore = new TokenStore();
        UserInfoInTokenBO userInfoInTokenBO = tokenStore.getUserInfoByAccessToken(accessToken,false);

        YamiUser yamiUser = new YamiUser();
        yamiUser.setUserId(userInfoInTokenBO.getUserId());
        yamiUser.setBizUserId(userInfoInTokenBO.getBizUserId());
        yamiUser.setEnabled(userInfoInTokenBO.getEnabled());
        yamiUser.setShopId(userInfoInTokenBO.getShopId());
        yamiUser.setStationId(userInfoInTokenBO.getOtherId());
        return yamiUser;
    }

    /**
     * 获取用户ID
     */
    public Long getUserId() {
        Long userId = null;
        try {
            userId = getUser().getUserId();
        } catch (RuntimeException e) {
            // 处理异常
            System.out.println(e.getMessage());
        }
        return userId;
    }
}
