package cn.palmte.work.utils;


import cn.palmte.work.config.Constant;
import cn.palmte.work.model.Admin;
import cn.palmte.work.shiro.ShiroPrincipal;
import org.apache.shiro.SecurityUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.Optional;


public class InterfaceUtil {

    public static void genRSAKeyPair(HttpServletRequest request) {
        /* 生成KeyPair */
        Map<String, Object> keyMap;
        try {
            keyMap = RSAUtils.genKeyPair();
            String publicKey = RSAUtils.getPublicKey(keyMap);
            String privateKey = RSAUtils.getPrivateKey(keyMap);
            /* 保存私钥到session */
            request.getSession().setAttribute(Constant.PRIVATEKEY, privateKey);
            /* 公钥给前端页面 */
            request.setAttribute(Constant.PUBLICKEY, publicKey);
        } catch (Exception e) {
        }
    }

    /**
     * 获取登录人的id
     */
    public static int getAdminId() {
        return getAdmin().getId();
    }

    /**
     * 获取当前登录用户
     */
    public static Admin getAdmin() {
        ShiroPrincipal principal = (ShiroPrincipal) SecurityUtils.getSubject().getPrincipal();
        return principal.getAdmin();
    }

    /*public static int getRegionId() {
        return Optional.ofNullable(getAdmin()).map(Admin::getRegionId).orElse(0);
    }*/

}
