完成文章的删除

This commit is contained in:
LittleBoy 2022-05-26 09:49:20 +08:00
parent 4667279624
commit 2ef57d2cdc
5 changed files with 51 additions and 13 deletions

View File

@ -66,11 +66,11 @@ router.get("/admin/list", async function (req, res) {
const search = "%" + (req.query["search"] || "") + "%"; // 搜索关键字
try {
const listResult = await db.query(
`select * from article where (title like ? or content like ? ) and status = 1 limit ?,?`,
`select * from article where (title like ? or content like ? ) and status > 0 limit ?,?`,
[search, search, start, size]
); // 执行查询语句并等待结果出来后赋值给变量
const totalCount = await db.query(
`select count(*) as total from article where (title like ? or content like ?) and status = 1`,
`select count(*) as total from article where (title like ? or content like ?) and status > 0`,
[search, search]
); // 执行获取总条数
res.send({
@ -116,4 +116,14 @@ router.post("/admin/save", async function (req, res) {
res.send({ status: true });
});
// 更新状态
router.all("/admin/update", async function (req, res) {
// 从请求参数中获取要更新的数据
const {id,status} = req.query;
await db.query(
`update article set status=? where id =?`,
[status,id]
);
res.send({ status: true });
});
module.exports = router;

View File

@ -31,6 +31,8 @@ router.post('/web/add', async (req, res) => {
commentData.ip = getIp(req.ip);
console.log(commentData)
await db.query('insert into comment set ?', commentData);
db.query('update article set comment_count=comment_count + 1 where id=?',
[commentData.article_id])
res.send({ status: true })
} catch (e) {
console.log(e)
@ -53,7 +55,7 @@ function getIpAddress(ip) {
return '内部网络';
}
if (!result.province) return result.country;
return (result.country != '中国' ? result.country : '') + "-" + result.province + "-" + result.city;
return (result.country != '中国' ? result.country + "-" : '') + result.province + "-" + result.city;
}

View File

@ -33,8 +33,8 @@ export default {
this.list = ret.list;
},
showEdit(a) {
// 不能直接赋值一个对象
this.form = {...a};
// 不能直接赋值一个对象
this.form = { ...a };
this.showDialog = true;
if (editor == null) {
@ -53,15 +53,36 @@ export default {
},
saveArticle() {
this.form.content = editor.html(); // 获取编辑器的内容
request("./api/article/admin/save", this.form, "POST").then((ret) =>{
if(ret.status){
this.$message('更新成功')
this.showDialog = false // 隐藏对话框
this.loadArticleList()
}else{
this.$message('更新出错了')
request("./api/article/admin/save", this.form, "POST").then(
(ret) => {
if (ret.status) {
this.$message("更新成功");
this.showDialog = false; // 隐藏对话框
this.loadArticleList();
} else {
this.$message("更新出错了");
}
}
);
},
async deleteArticlebyId(id) {
// status为0表示文章被删除
const ret = await request("/api/article/admin/update", {
id,
status: 0,
});
this.loadArticleList(); // 重新加载文章列表
},
deleteArticle(art) {
// 原生的confirm
// if(confirm('此操作将永久删除该文章数据,是否继续?')){
// this.deleteArticlebyId(art.id)
// }
// this.$confirm("此操作将永久删除该文件, 是否继续?", "提示")
// .then(() => { // 点击了确定按钮
// this.deleteArticlebyId(art.id)
// })
// .catch(() => {});
},
},
};

View File

@ -132,6 +132,7 @@
if(ret.status){
this.$message.success('发表评论成功')
this.a.comment_count ++; // 直接对评论数+1
this.loadCommentList()
// this.commentContent = '' // 将评论内容清空
}else{

View File

@ -73,7 +73,11 @@
<td>
<!-- 不需要传参则可以不写方法后的小括号 -->
<el-link @click="showEdit(art)">修改</el-link>
<el-link>删除</el-link>
<el-popconfirm
title="此操作将永久删除该文章数据,是否继续?"
@confirm="deleteArticlebyId(art.id)">
<el-link slot="reference">删除</el-link>
</el-popconfirm>
</td>
</tr>
</tbody>