71 lines
1.7 KiB
TypeScript
71 lines
1.7 KiB
TypeScript
import { Modal } from "antd-mobile";
|
||
|
||
/**
|
||
* 通用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);
|
||
|
||
if (isIOS) {
|
||
// 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`);
|
||
}
|