/** * System Function Service * 系統功能列表服務 */ const API_BASE_URL = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:10181'; export interface SystemFunctionNode { id: number; code: string; name: string; function_type: number; // 1=NODE, 2=FUNCTION order: number; function_icon: string; module_code: string | null; module_functions: string[]; // ["View", "Create", "Read", "Update", "Delete"] description: string; children: SystemFunctionNode[]; } export const systemFunctionService = { /** * 取得功能列表樹狀結構 * @param isSysmana 是否為系統管理公司 * @returns Promise */ async getMenuTree(isSysmana: boolean = false): Promise { try { const url = `${API_BASE_URL}/api/v1/system-functions/menu/tree?is_sysmana=${isSysmana}`; console.log('[SystemFunctionService] Fetching menu tree:', url); console.log('[SystemFunctionService] is_sysmana parameter:', isSysmana); const response = await fetch(url, { method: 'GET', headers: { 'Content-Type': 'application/json', }, credentials: 'include', // 包含 cookies (用於認證) }); if (!response.ok) { throw new Error(`Failed to fetch menu tree: ${response.statusText}`); } const data = await response.json(); console.log('[SystemFunctionService] Received menu items:', data.length); console.log('[SystemFunctionService] Menu data:', data); return data; } catch (error) { console.error('Error fetching menu tree:', error); throw error; } }, /** * 將 code 轉換為路由路徑 * @param code 功能代碼 (例如: tenant_departments) * @returns 路由路徑 (例如: /tenant-departments) */ codeToRoute(code: string): string { return `/${code.replace(/_/g, '-')}`; }, /** * 檢查功能是否有特定操作權限 * @param moduleFunctions 功能操作列表 * @param operation 操作名稱 (View, Create, Read, Update, Delete) * @returns boolean */ hasOperation(moduleFunctions: string[], operation: string): boolean { return moduleFunctions.includes(operation); }, };