emailAdapter.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /**
  2. * UniApp APP 邮件模板适配器
  3. * 适用于 cwg-rich-text 组件展示邮件内容
  4. * rich-text组件对HTML支持有限,需要简化处理
  5. * @param {string} html - 原始邮件HTML字符串
  6. * @returns {string} - 适配后的HTML字符串
  7. */
  8. export function adaptCWGEmailForUniApp(html) {
  9. if (!html || typeof html !== 'string') return '';
  10. let result = html;
  11. // ==============================================
  12. // 1. 【修复HTML语法错误】处理不规范的标签
  13. // ==============================================
  14. // 修复 <img ... / style="..."> 的问题 - 移除斜杠后的空格
  15. result = result.replace(/<img([^>]*?)\s*\/\s*style=/gi, '<img$1 style=');
  16. // ==============================================
  17. // 2. 【宽度适配】修复640px固定宽度导致的横向滚动问题
  18. // ==============================================
  19. // 将最外层的width:640px改为100%
  20. result = result.replace(/<div\s+style="([^"]*?)width:\s*640px([^"]*?)"/gi, function (_, before, after) {
  21. return `<div style="${before}width:100%;max-width:640px;margin:0 auto;${after}"`;
  22. });
  23. // 处理内联样式中的width:640px
  24. result = result.replace(/width:\s*640px/gi, 'width:100%;max-width:640px');
  25. // ==============================================
  26. // 3. 【图片响应式】确保所有图片自适应
  27. // ==============================================
  28. result = result.replace(/<img([^>]*?)>/gi, function (match, attrs) {
  29. // 提取现有样式
  30. const styleMatch = attrs.match(/style=["']([^"']*)["']/i);
  31. let existingStyle = styleMatch ? styleMatch[1] : '';
  32. // 添加响应式样式
  33. let newStyle = existingStyle;
  34. // 确保样式末尾有分号
  35. if (newStyle && !newStyle.endsWith(';')) {
  36. newStyle += ';';
  37. }
  38. // 添加缺失的样式
  39. if (!existingStyle.includes('max-width')) {
  40. newStyle += 'max-width:100%;';
  41. }
  42. if (!existingStyle.includes('height:')) {
  43. newStyle += 'height:auto;';
  44. }
  45. if (!existingStyle.includes('display:')) {
  46. newStyle += 'display:block;';
  47. }
  48. // 移除多余的分号
  49. newStyle = newStyle.replace(/;;+/g, ';');
  50. if (styleMatch) {
  51. return match.replace(/style=["']([^"']*)["']/i, `style="${newStyle}"`);
  52. } else {
  53. // 确保标签正确闭合
  54. if (attrs.trim().endsWith('/')) {
  55. return `<img${attrs.replace(/\s*\/$/, '')} style="${newStyle}" />`;
  56. } else {
  57. return `<img${attrs} style="${newStyle}" />`;
  58. }
  59. }
  60. });
  61. // ==============================================
  62. // 4. 【容器适配】移除大尺寸固定宽度限制
  63. // ==============================================
  64. result = result.replace(/style="([^"]*?)width:\s*(\d+)px([^"]*?)"/gi, function (match, before, width, after) {
  65. // 保留小尺寸的宽度(如logo等),只处理大尺寸
  66. if (parseInt(width) > 200) {
  67. let newStyle = `${before}width:100%;max-width:${width}px;${after}`;
  68. newStyle = newStyle.replace(/;;+/g, ';');
  69. return `style="${newStyle}"`;
  70. }
  71. return match;
  72. });
  73. // ==============================================
  74. // 5. 【清理多余分号】修复样式中的双分号问题
  75. // ==============================================
  76. result = result.replace(/style="([^"]*)"/gi, function (_, style) {
  77. return `style="${style.replace(/;;+/g, ';').replace(/;$/, '')}"`;
  78. });
  79. // ==============================================
  80. // 6. 【修复特殊字符】处理HTML实体
  81. // ==============================================
  82. result = result.replace(/&rsquo;/g, "'");
  83. result = result.replace(/&nbsp;/g, ' ');
  84. result = result.replace(/&amp;/g, '&');
  85. // ==============================================
  86. // 7. 【移除script/style标签】rich-text不支持
  87. // ==============================================
  88. result = result.replace(/<script[\s\S]*?<\/script>/gi, '');
  89. result = result.replace(/<style[\s\S]*?<\/style>/gi, '');
  90. // ==============================================
  91. // 8. 【简化标签】移除rich-text不支持的标签
  92. // ==============================================
  93. // 移除HTML文档结构标签
  94. result = result.replace(/<\/?html[^>]*>/gi, '');
  95. result = result.replace(/<\/?head[^>]*>/gi, '');
  96. result = result.replace(/<\/?body[^>]*>/gi, '');
  97. result = result.replace(/<meta[^>]*>/gi, '');
  98. result = result.replace(/<!DOCTYPE[^>]*>/gi, '');
  99. // ==============================================
  100. // 9. 【添加内联样式包装】确保容器不溢出
  101. // ==============================================
  102. result = `
  103. <div style="width:100%;max-width:640px;margin:0 auto;background:#fff;border-radius:8px;overflow:hidden;">
  104. <div style="padding:8px;">
  105. ${result}
  106. </div>
  107. </div>
  108. `;
  109. return result;
  110. }