// import { auth } from "./config"; // import { request } from "./request"; type CodeMap = Record; // 其中,string为菜单编码,boolean的值都是true,无code时表示无权限 type TabItem = Record; let codeMap: CodeMap = {}; let skipAuthCheck = false; // 是否需要检查权限 export const getBtnAuthMap = async ({ SoaClient, ignoreAuth = false, // 忽略授权 }: { SoaClient: { getSoa: ( a: string, b: string ) => { getHasPmsResourceCode: () => Promise<{ data: CodeMap }>; }; }; ignoreAuth?: boolean; }) => { if (ignoreAuth) { skipAuthCheck = true; return; } const resp = await SoaClient.getSoa( "java_sso_server", "v1/bizesource" ).getHasPmsResourceCode(); codeMap = resp.data; }; /** * 按钮显隐跟权限绑定 * @param code 菜单编码 * @returns 是否显示 */ export function hasBtnAuth(code: number | string) { if (skipAuthCheck) { // 无需检查权限时,直接返回true,即显示按钮 return true; } return !!codeMap?.[code]; } /** * 得到第一个有权限的tab key * @param tabs tab列表,如[{man: hasBtnAuth(live.manList)}, {woman: hasBtnAuth(live.womanList)}] * @returns 第一个有权限的tab key,如 woman */ export function getFirstAuthTab(tabs: TabItem[]): string { const hasAuthFirstTab = tabs.find((item) => !!Object.values(item)[0]); return (hasAuthFirstTab && Object.keys(hasAuthFirstTab)[0]) || ""; }