1 Star 0 Fork 0

yudidayeye/email-template

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
index.js 5.36 KB
一键复制 编辑 原始数据 按行查看 历史
yudidayeye 提交于 2024-03-12 13:48 . fix: 修改用户认证拦截
/*
* @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");
});
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/yudidayeye/email-template.git
git@gitee.com:yudidayeye/email-template.git
yudidayeye
email-template
email-template
master

搜索帮助