代码拉取完成,页面将自动刷新
/*
* @Author: yudidayeye 908737208@qq.com
* @Date: 2024-02-27 14:43:18
* @LastEditors: yudidayeye 908737208@qq.com
* @LastEditTime: 2024-03-12 13:47:00
* @FilePath: \email-template\index.js
* @Description: index.js
*/
const express = require("express");
const https = require("node:https");
const {
readdir,
writeFile,
readFile,
rename,
unlink,
} = require("node:fs/promises");
var qs = require("qs");
const admin = {
username: "yudidayeye",
email: "908737208@qq.com",
};
const ejs = require("ejs");
const app = express();
const multer = require("multer");
const axios = require("axios");
app.use(express.static("wwwroot"));
app.set("views", "./wwwroot/views");
app.set("views engine", "html");
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
const upload = multer({ dest: "./wwwroot/temp" });
async function renderIndex(req, res, data) {
const templates = await readdir("./wwwroot/views");
ejs.renderFile("./index.ejs", { templates, data }, function (err, str) {
if (err) {
console.log(err);
} else {
res.writeHead(200, { "Content-Type": "text/html" });
res.write(str);
res.end();
}
});
}
app.get("/", async (req, res) => {
renderIndex(req, res);
});
// 登录拦截
app.use(function (req, res, next) {
let data = qs.stringify({
action: "get_user_info",
});
console.log("---req.cookies", req.headers.cookie);
let config = {
method: "post",
url: "http://112.124.62.174/wordpress/wp-admin/admin-ajax.php",
headers: {
Cookie: req.headers.cookie,
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
},
data: data,
};
let obj = {
code: 401,
error: "用户认证失败",
};
axios(config)
.then(function (response) {
let result = response.data
if (
result &&
result.success &&
result.data.username == admin.username &&
result.data.email == admin.email
) {
next();
} else {
res.json(obj);
}
})
.catch(function (error) {
console.log("----------error", error.message);
res.json(obj);
});
});
app.post("/upEmail", upload.any(), async (req, res) => {
let data = req.body;
obj = {
code: 500,
error: "文件无法上传!",
data: {
filename: data.name,
},
};
if (data.upType === "code" && data.code) {
const templates = await readdir("./wwwroot/views");
if (templates.includes(`${data.name}.html`)) {
obj = {
code: 500,
error: "文件名重复!请重新上传!",
data: {
filename: data.name,
},
};
} else {
// 文件写入
await writeFile(`./wwwroot/views/${data.name}.html`, data.code);
obj = {
code: 200,
message: "模板添加成功!",
data: {
filename: data.name,
},
};
}
} else if (data.upType === "file") {
let file = req.files[0];
const templates = await readdir("./wwwroot/views");
if (templates.includes(`${data.name}.html`)) {
obj = {
code: 500,
error: "文件名重复!请重新上传!",
data: {
filename: data.name,
},
};
} else {
// 文件上传
let fileData = await readFile(file.path);
await writeFile(`./wwwroot/views/${data.name}.html`, fileData);
obj = {
code: 200,
message: "模板上传成功!",
data: {
filename: data.name,
},
};
}
}
res.json(obj);
});
app.post("/changeName", async (req, res) => {
let data = req.body;
obj = {
code: 500,
error: "名称修改失败!",
data: {
newValue: data.newValue,
oldValue: data.oldValue,
},
};
if (data.newValue == data.oldValue) {
obj = {
code: 500,
error: "名称未修改",
...obj.data,
};
return res.json(obj);
}
if (data.newValue) {
// 重命名操作
try {
await rename(
`./wwwroot/views/${data.oldValue}`,
`./wwwroot/views/${data.newValue}`
);
obj = {
code: 200,
message: "名称修改成功!",
data: {
newValue: data.newValue,
oldValue: data.oldValue,
},
};
res.json(obj);
} catch (error) {
res.json(obj);
}
} else {
res.json(obj);
}
});
app.post("/deleteTemplate", async (req, res) => {
let data = req.body;
obj = {
code: 500,
error: "模板删除失败!",
data: {
name: data.name,
},
};
if (!data.name) {
obj = {
code: 500,
error: "未获取到模板名称!",
...obj.data,
};
return res.json(obj);
}
if (data.name) {
// 重命名操作
try {
await unlink(`./wwwroot/views/${data.name}`);
obj = {
code: 200,
message: "模板删除成功!",
...obj.data,
};
res.json(obj);
} catch (error) {
res.json(obj);
}
} else {
res.json(obj);
}
});
// 错误处理中间件
app.use((err, req, res, next) => {
res.status(500).send(err.message);
});
app.listen(3000, () => {
console.log("==>listening on port 3000, http://localhost:3000");
});
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。