Files
cunkebao_v3/Cunkebao/src/utils/common.ts

71 lines
1.8 KiB
TypeScript
Raw Normal View History

2025-08-12 09:27:50 +08:00
import { Modal } from "antd-mobile";
import { getSetting } from "@/store/module/settings";
/**
* js调用弹窗Promise风格
* @param content
* @param config title, cancelText, confirmText
* @returns Promise<void>
*/
export const comfirm = (
content: string,
config?: {
title?: string;
cancelText?: string;
confirmText?: string;
},
): Promise<void> => {
return new Promise((resolve, reject) => {
Modal.show({
title: config?.title || "提示",
content,
closeOnAction: true,
actions: [
{
key: "cancel",
text: config?.cancelText || "取消",
onClick: () => reject(),
},
{
key: "confirm",
text: config?.confirmText || "确认",
danger: true,
onClick: () => resolve(),
},
],
});
});
};
export function getSafeAreaHeight() {
// 1. 优先使用 CSS 环境变量
if (CSS.supports("padding-top", "env(safe-area-inset-top)")) {
const safeAreaTop = getComputedStyle(
document.documentElement,
).getPropertyValue("env(safe-area-inset-top)");
const height = parseInt(safeAreaTop) || 0;
if (height > 0) return height;
}
// 2. 设备检测
const isIOS = /iPad|iPhone|iPod/.test(navigator.userAgent);
const isAndroid = /Android/.test(navigator.userAgent);
const isAppMode = getSetting("isAppMode");
if (isIOS && isAppMode) {
// iOS 设备
const isIPhoneX = window.screen.height >= 812;
return isIPhoneX ? 44 : 20;
} else if (isAndroid) {
// Android 设备
return 24;
}
// 3. 默认值
return 0;
}
// 设置全局 CSS 变量
export function initSafeArea() {
const root = document.documentElement;
const height = getSafeAreaHeight();
root.style.setProperty("--safe-area-top", `${height}px`);
}