| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- import { createHashHistory } from 'history'
- const history = createHashHistory()
- export default history
- // 判断是手机端还是pc端
- export const isMobiileFu = () => {
- if (
- window.navigator.userAgent.match(
- /(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i
- )
- ) {
- return true
- } else return false
- }
- // 监听路由
- let routerLength = 0
- history.listen((_: any, listener: any) => {
- if (listener === 'PUSH') {
- routerLength += 1
- } else if (listener === 'POP') {
- if (routerLength >= 1) routerLength -= 1
- }
- })
- // 点击返回
- export const backPageFu = (path: string) => {
- if (routerLength) history.go(-1)
- else window.location.replace(path)
- }
- // 调用iframe方法
- export const callIframeFu = (fuName: string, params: any) => {
- const dom = document.getElementById('modalIframe') as HTMLIFrameElement | null;
- const iframeWindow = dom?.contentWindow as Window & { [key: string]: (...args: any[]) => void } | undefined;
- if (iframeWindow && typeof iframeWindow[fuName] === 'function') {
- if (params === 0) {
- // 直接传递 0(参数为 0 时执行)
- iframeWindow[fuName](params);
- } else if (params === undefined || params === null) {
- // 参数为 undefined/null 时,使用默认空对象并调用方法
- iframeWindow[fuName]({});
- } else {
- // 其他参数直接传递
- iframeWindow[fuName](params);
- }
- // 修复日志显示:直接打印 params,避免 0 被 || '' 转换为空字符串
- console.log(`执行了方法: ${fuName}(${params})`);
- } else {
- console.warn(`iframe 方法调用失败: 方法不存在或 iframe 未加载`);
- }
- };
|