history.ts 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import { createHashHistory } from 'history'
  2. const history = createHashHistory()
  3. export default history
  4. // 判断是手机端还是pc端
  5. export const isMobiileFu = () => {
  6. if (
  7. window.navigator.userAgent.match(
  8. /(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i
  9. )
  10. ) {
  11. return true
  12. } else return false
  13. }
  14. // 监听路由
  15. let routerLength = 0
  16. history.listen((_: any, listener: any) => {
  17. if (listener === 'PUSH') {
  18. routerLength += 1
  19. } else if (listener === 'POP') {
  20. if (routerLength >= 1) routerLength -= 1
  21. }
  22. })
  23. // 点击返回
  24. export const backPageFu = (path: string) => {
  25. if (routerLength) history.go(-1)
  26. else window.location.replace(path)
  27. }
  28. // 调用iframe方法
  29. export const callIframeFu = (fuName: string, params: any) => {
  30. const dom = document.getElementById('modalIframe') as HTMLIFrameElement | null;
  31. const iframeWindow = dom?.contentWindow as Window & { [key: string]: (...args: any[]) => void } | undefined;
  32. if (iframeWindow && typeof iframeWindow[fuName] === 'function') {
  33. if (params === 0) {
  34. // 直接传递 0(参数为 0 时执行)
  35. iframeWindow[fuName](params);
  36. } else if (params === undefined || params === null) {
  37. // 参数为 undefined/null 时,使用默认空对象并调用方法
  38. iframeWindow[fuName]({});
  39. } else {
  40. // 其他参数直接传递
  41. iframeWindow[fuName](params);
  42. }
  43. // 修复日志显示:直接打印 params,避免 0 被 || '' 转换为空字符串
  44. console.log(`执行了方法: ${fuName}(${params})`);
  45. } else {
  46. console.warn(`iframe 方法调用失败: 方法不存在或 iframe 未加载`);
  47. }
  48. };