直接上代码.这里主要是展示一种思想,将功能近乎相同的小页面抽离为一种配置即可完成的东西,防止自己做无用代码

const fs = require('fs');
const path = require('path');
const outFileBasePath = path.join(__dirname, '../static');
const dirCache = {};
let pageRouter = [
{
title: '',
description: '',
fileName: '/introduce/payroll_calculate_incomeTax', // 路径
style: '',
script: '',
width: 750,
imgList: [
{
url: 'xxxxxx.png',
alt: '产品介绍图',
}
]
},
{
title: '邀请企业,赚大额现金',
description: '',
fileName: '/invitationFriend', // 路径
style: `
<style>
.content {
background: #FF4A44;
padding-bottom: 67px;
}
</style>
`,
script: '',
width: 500,
imgList: [
{
url: 'xxxxxxxx.png',
alt: '邀请企业福利介绍',
}
]
}
];
pageRouter.forEach(router => {
let filePath = outFileBasePath + router.fileName;
let fileContent = `<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link rel="icon" href="./favicon.ico">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>${router.title || '页面标题'}</title>
<style>
* {
margin: 0;
padding: 0;
overflow-x: hidden;
-ms-overflow-style: none;
overflow: -moz-scrollbars-none;
}

*::-webkit-scrollbar {
display: none;
}

.content {
width: 100vw;
min-height: 100vh;
background: #ffffff;
display: flex;
justify-content: flex-start;
flex-direction: column;
align-items: center;
}

#mask {
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
z-index: 1;
background-color: #ffffff;
display: flex;
align-items: center;
justify-content: center;
}

.loading {
position: relative;
width: 30px;
height: 30px;
border: 2px solid rgba(0, 0, 0, 0.2);
border-left-color: #000;
border-radius: 100%;
animation: circle infinite 0.75s linear;
}

@keyframes circle {
0% {
transform: rotate(0);
}

100% {
transform: rotate(360deg);
}
}
</style>
${router.style}
</head>

<body>
<div class="content">
${router.imgList.map(item => {
return `<img src="" alt="${item.alt}">`
}).join('')}
</div>
<div id="mask">
<div class="loading"></div>
</div>
<script>
let flag = 0;
let imgList = ${JSON.stringify(router.imgList)};
let imgs = document.querySelectorAll('.content img');
let mask = document.querySelector('#mask')
imgs.forEach((e, i) => {
let url = imgList[i].url + '?' + new Date().getTime();
e.src = url;
e.onload = function () {
flag++;
if(flag === ${router.imgList.length}){
handleImgWidth();
window.addEventListener("resize", handleImgWidth);
mask.style.display = 'none'
}
}
})
function handleImgWidth() {
let imgArr = document.querySelectorAll('.content img');
let clientWidth = document.querySelector('.content').clientWidth;
if (clientWidth > 750) {
for (let i = 0; i < imgArr.length; i++) {
imgArr[i].style.width = '${router.width}px'
}
} else {
for (let i = 0; i < imgArr.length; i++) {
imgArr[i].style.width = '100vw'
}
}
}
</script>
${router.script}
</body>

</html>
`
mkdir(filePath + '/index.html', fileContent)
});
function mkdir(filePath, data) {
const arr = filePath.split('/');
let dir = arr[0];
for (let i = 1; i < arr.length; i++) {
if (dir && !dirCache[dir] && !fs.existsSync(dir)) {
dirCache[dir] = true;
fs.mkdirSync(dir);
}
dir = dir + '/' + arr[i];
}
fs.writeFileSync(filePath, data, (err) => {
console.log(err)
})
}
$ "build": "node server/statusPage.js && cross-env BASE_URL='https://www.xxxxx.cn' nuxt build ",