/** * Kintone API 工具模块 * 提供与 Kintone 应用程序交互的通用工具函数 */ /** * 从当前 URL 中提取 Guest Space ID * @returns {string|undefined} Guest Space ID,如果未找到则为 undefined */ export const getGuestSpaceId = () => { try { const match = window.location.pathname.match(/\/guest\/([0-9]+)\//); return match ? match[1] : undefined; } catch (error) { console.warn(error); return undefined; } }; const getAppIdFromUrl = () => { try { const match = window.location.search.match(/\?app=([0-9]+)/); return match ? Number(match[1]) : undefined; } catch (error) { console.warn(error); return undefined; } }; /** * 从 Kintone 获取当前 APP ID * @returns {number|null} APP ID,如果没有为 null */ export const getAppId = () => { try { if (isInAdminPage()) { return getAppIdFromUrl() || null; } if (!isGlobalKintoneExist()) { return null; } const appId = kintone.app.getId(); if (!appId || isNaN(appId)) { console.warn('Retrieved app ID is invalid:', appId); return null; } return appId; } catch (error) { console.warn('Failed to get app ID: ', error); return null; } }; export const isGlobalKintoneExist = () => { return typeof kintone !== 'undefined' && typeof kintone.app !== 'undefined'; }; /** * 是否在 Kintone App 中 * @returns {boolean} 是否在 App 中 */ export const isInAppPage = () => { return isGlobalKintoneExist() && /^\/k\/\d+/.test(window.location.pathname); }; /** * 是否在 Kintone App Detail 中 * @returns {boolean} 是否在 App 中 */ export const isInDetailPage = () => { return isGlobalKintoneExist() && /^\/k\/\d+\/show/.test(window.location.pathname); }; /** * 是否在 Kintone App 的 /admin 页面 * @returns {boolean} 是否在 App /admin 页面 */ export const isInAdminPage = () => { return !isGlobalKintoneExist() && window.location.pathname.includes('/k/admin'); }; /** * 是否在 Kintone App 的 /admin Form 页面 * @returns {boolean} 是否在 App /admin Form 页面 */ export const isInAdminFormPage = () => { return isInAdminPage() && window.location.hash.includes('#section=form'); }; /** * 是否在 Kintone 的 space 页面 * @returns {boolean} 是否在 space 页面 */ export const isInSpacePage = () => { return isGlobalKintoneExist() && window.location.pathname.includes('/k/#/space'); };