common.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /**
  2. * 显示消息提示框
  3. * @param content 提示的标题
  4. */
  5. export function toast(content) {
  6. uni.showToast({
  7. icon: 'none',
  8. title: content
  9. })
  10. }
  11. /**
  12. * 显示模态弹窗
  13. * @param content 提示的标题
  14. */
  15. export function showConfirm(content) {
  16. return new Promise((resolve, reject) => {
  17. uni.showModal({
  18. title: '提示',
  19. content: content,
  20. cancelText: '取消',
  21. confirmText: '确定',
  22. success: function(res) {
  23. resolve(res)
  24. }
  25. })
  26. })
  27. }
  28. /**
  29. * 参数处理
  30. * @param params 参数
  31. */
  32. export function tansParams(params) {
  33. let result = ''
  34. // FIXME 拼接参数
  35. return result
  36. }
  37. export function getStaticUrl(url) {
  38. let result = '';
  39. // #ifdef H5
  40. result = `/mdist/${url}`
  41. // #endif
  42. // #ifdef APP-PLUS
  43. result = url
  44. // #endif
  45. return result;
  46. }
  47. /************* 将时间格式化成 年月+日 ***********/
  48. export function formatDateToYearMonthDay(dateStr) {
  49. let date = new Date(dateStr.replace(/-/g, '/'));
  50. let dataStr1 = `${date.getFullYear()}-${(date.getMonth() + 1).toString().padStart(2, '0')}-${date.getDate().toString().padStart(2, '0')}`
  51. const yearMonth = dataStr1.substring(0, 7); // "2025-06"
  52. const day = dataStr1.substring(8); // "01"
  53. return [yearMonth, day]
  54. }
  55. export function getStringByHtml3(html) {
  56. return html ? html.replace(/<img [^.]*">/g, '')
  57. .replace(/<(style|script|iframe)[^>]*?>[\s\S]+?<\/\1\s*>/gi, '')
  58. .replace(/<[^>]+?>/g, '')
  59. .replace(/\s+/g, ' ')
  60. .replace(/ /g, ' ')
  61. .replace(/>/g, ' ')
  62. .replace(/<?img[^>]*>/g, '')
  63. .replace(/<video[^>]*>.*?<\/video>/gi, '') : '';
  64. }
  65. // 在页面/组件的 onReady 或 mounted 生命周期中添加
  66. export function addClassToWebViewIframe() {
  67. const targetSrc = "/hybrid/html/web/viewer.html"; // 替换为你的 WebView URL
  68. const className = "custom-iframe-class"; // 要添加的类名
  69. // 检查是否已存在目标 iframe
  70. const existingIframe = Array.from(document.querySelectorAll('iframe')).find(
  71. iframe => iframe.src.includes(targetSrc)
  72. );
  73. if (existingIframe) {
  74. existingIframe.classList.add(className);
  75. return;
  76. }
  77. // 使用 MutationObserver 监听新增 iframe
  78. const observer = new MutationObserver(mutations => {
  79. mutations.forEach(mutation => {
  80. mutation.addedNodes.forEach(node => {
  81. if (node.tagName === 'IFRAME' && node.src.includes(targetSrc)) {
  82. node.classList.add(className);
  83. observer.disconnect(); // 找到后停止监听
  84. }
  85. });
  86. });
  87. });
  88. // 监听整个 body 的子元素变化
  89. observer.observe(document.body, {
  90. childList: true,
  91. subtree: false
  92. });
  93. // 设置超时停止监听(防止内存泄漏)
  94. setTimeout(() => observer.disconnect(), 5000);
  95. }