d13-tianpan-index.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #!/usr/bin/env node
  2. /**
  3. * D13: tianpan/index.vue 家庭天盘页清理 (debt 78)
  4. *
  5. * 1) 修复字符串 bug:'BrandColors.*' -> 变量 + import
  6. * 2) script canvas/colorMap 色收编 BrandColors.*
  7. * 3) style 全局映射 4 色 + 局部变量 5 色
  8. */
  9. const fs = require('fs');
  10. const F = 'pages/tianpan/index.vue';
  11. let src = fs.readFileSync(F, 'utf8');
  12. let replaced = 0;
  13. function repl(from, to) {
  14. const cnt = src.split(from).length - 1;
  15. if (cnt === 0) { console.warn('NOT FOUND:', from.slice(0, 80)); return; }
  16. src = src.split(from).join(to);
  17. replaced += cnt;
  18. }
  19. // ---- 1. import ----
  20. repl(
  21. "<script>\nexport default {",
  22. "<script>\nimport { BrandColors } from '../../utils/brand-colors.js'\n\nexport default {"
  23. );
  24. // ---- 2. script canvas ----
  25. repl("ctx.fillStyle = '#FFF8F0'", "ctx.fillStyle = BrandColors.bodyTint");
  26. repl("var elementColors = ['BrandColors.success', '#FF6B9D', 'BrandColors.body', 'BrandColors.wisdom', '#F59E0B']",
  27. "var elementColors = [BrandColors.success, BrandColors.mind, BrandColors.body, BrandColors.wisdom, BrandColors.wealth]");
  28. repl("ctx.fillStyle = '#1F2937'", "ctx.fillStyle = BrandColors.textGray800");
  29. repl("ctx.fillStyle = '#6B7280'", "ctx.fillStyle = BrandColors.textSlate");
  30. repl("ctx.strokeStyle = '#FFF'", "ctx.strokeStyle = BrandColors.white");
  31. repl("ctx.fillStyle = '#FFF'", "ctx.fillStyle = BrandColors.white");
  32. repl("ctx.strokeStyle = '#F59E0B'", "ctx.strokeStyle = BrandColors.wealth");
  33. // ---- 3. colorMap ----
  34. repl(" 'parent': 'BrandColors.primary',\n 'child': '#3B82F6',\n 'teacher': '#8B5CF6'",
  35. " 'parent': BrandColors.primary,\n 'child': BrandColors.infoBlue,\n 'teacher': BrandColors.violet");
  36. repl("return colorMap[role] || '#6B7280'", "return colorMap[role] || BrandColors.textSlate");
  37. repl("var colors = { body: 'BrandColors.body', mind: '#FF6B9D', wisdom: 'BrandColors.wisdom', action: 'BrandColors.success', wealth: '#F59E0B' }",
  38. "var colors = { body: BrandColors.body, mind: BrandColors.mind, wisdom: BrandColors.wisdom, action: BrandColors.success, wealth: BrandColors.wealth }");
  39. repl("return colors[key] || '#6B7280'", "return colors[key] || BrandColors.textSlate");
  40. // ---- 4. style 块 ----
  41. const styleIdx = src.indexOf('<style');
  42. const before = src.slice(0, styleIdx);
  43. let styleBody = src.slice(styleIdx);
  44. const GLOBAL = [
  45. ['#FFF8F0', '$dim-body-tint'],
  46. ['#D1D5DB', '$app-border-strong'],
  47. ['#F5F0FF', '$facade-violet-tint'],
  48. ['#7C3AED', '$facade-violet-deep'],
  49. ];
  50. const PAL = [
  51. ['#FDBA74', '$c-tp-orange'],
  52. ['#7C2D12', '$c-tp-brown-deep'],
  53. ['#9A3412', '$c-tp-brown'],
  54. ['#E0D4FF', '$c-tp-violet-soft'],
  55. ['#6D28D9', '$c-tp-violet-text'],
  56. ];
  57. for (const [f, t] of [...GLOBAL, ...PAL]) {
  58. const esc = f.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
  59. styleBody = styleBody.replace(new RegExp(esc + '(?![0-9a-fA-F])', 'g'), t);
  60. }
  61. const palette = [
  62. '// ===== 天盘页专属色阶(暖橙提示 + 紫罗兰软阶;单页专用,约定见 uni.scss §14)=====',
  63. '$c-tp-orange: #FDBA74; // 暖橙(徽章渐变档)',
  64. '$c-tp-brown-deep: #7C2D12; // 深棕文字(暖卡内)',
  65. '$c-tp-brown: #9A3412; // 棕橙文字(橙卡内)',
  66. '$c-tp-violet-soft: #E0D4FF; // 紫罗兰软底渐变终',
  67. '$c-tp-violet-text: #6D28D9; // 紫罗兰文字',
  68. ''
  69. ].join('\n');
  70. styleBody = styleBody.replace(/(<style[^>]*>\s*\n)/, '$1' + palette + '\n');
  71. src = before + styleBody;
  72. fs.writeFileSync(F, src);
  73. console.log('total replaced:', replaced);
  74. // 校验
  75. const lines = src.split('\n');
  76. let n = 0;
  77. for (let i = 0; i < lines.length; i++) {
  78. const L = lines[i].trim();
  79. if (/#[0-9a-fA-F]{3,8}\b/.test(L) && !/^\$c-/.test(L)) { console.log('HEX ' + (i + 1) + ': ' + L); n++; }
  80. }
  81. console.log('remaining non-palette hex:', n);