Files
kintone-helper-extenstion/utils/kintone-utils.js
2025-10-17 14:39:35 +08:00

101 lines
2.5 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 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');
};