From c903bf77a77e6259d61598674ac6ae030ab49153 Mon Sep 17 00:00:00 2001 From: callmeyan Date: Thu, 26 May 2022 10:56:41 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=96=87=E7=AB=A0=E7=AE=A1?= =?UTF-8?q?=E7=90=86=E5=8F=8A=E5=B1=95=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- modules/article.js | 54 +++++++++++++++++++------------- public/assets/manage.js | 31 ++++++++++++++++--- public/manage.html | 68 ++++++++++++++++++++++++++--------------- 3 files changed, 101 insertions(+), 52 deletions(-) diff --git a/modules/article.js b/modules/article.js index 271549c..d545af7 100644 --- a/modules/article.js +++ b/modules/article.js @@ -1,4 +1,5 @@ const db = require("./db"); // +const dayjs = require("dayjs"); // 文章模块 const router = require("express").Router(); // 用户网站模块 @@ -11,13 +12,14 @@ router.get("/web/list", async function (req, res) { const start = (page - 1) * size; const search = "%" + (req.query["search"] || "") + "%"; // 搜索关键字 try { + const listResult = await db.query( `select id,title,category,publish_time,cover,description,view_count,comment_count - from article where (title like ? or content like ? ) and status = 1 limit ?,?`, + from article where status = 1 and publish_time <= now() and (title like ? or content like ?) order by id desc 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 status = 1 and publish_time <= now() and (title like ? or content like ?)`, [search, search] ); // 执行获取总条数 res.send({ @@ -66,7 +68,7 @@ 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 > 0 limit ?,?`, + `select * from article where (title like ? or content like ? ) and status > 0 order by id desc limit ?,? `, [search, search, start, size] ); // 执行查询语句并等待结果出来后赋值给变量 const totalCount = await db.query( @@ -84,6 +86,9 @@ router.get("/admin/list", async function (req, res) { } }); router.post("/admin/save", async function (req, res) { + req.body.publish_time = dayjs(req.body.publish_time).format( + "YYYY-MM-DD HH:mm:ss" + ); // 从请求参数中获取要更新的数据 const { id, @@ -94,36 +99,41 @@ router.post("/admin/save", async function (req, res) { description, view_count, comment_count, + publish_time, } = req.body; - const article = await db.query( - `update article set title=?, + + if (id > 0) { + // 有id说明在更新数据 + await db.query( + `update article set title=?, category=?, content=?, cover=?, description=?, - view_count=?,comment_count=? where id =?`, - [ - title, - category, - content, - cover, - description, - view_count, - comment_count, - id - ] - ); + view_count=?,comment_count=?,publish_time=? where id =?`, + [ + title, + category, + content, + cover, + description, + view_count, + comment_count, + publish_time, + id, + ] + ); + } else { + await db.query("insert into article set ?", req.body); + } 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] - ); + const { id, status } = req.query; + await db.query(`update article set status=? where id =?`, [status, id]); res.send({ status: true }); }); module.exports = router; diff --git a/public/assets/manage.js b/public/assets/manage.js index dd4510e..5d8e13b 100644 --- a/public/assets/manage.js +++ b/public/assets/manage.js @@ -32,11 +32,25 @@ export default { }); this.list = ret.list; }, - showEdit(a) { - // 不能直接赋值一个对象 - this.form = { ...a }; - this.showDialog = true; + showAdd(){ + // 不能直接赋值一个对象 + this.form = { + category: "", + comment_count: 0, + content: "", + cover: "", + description: "", + id: 0, + publish_time: null, + status: 1, + title: "", + view_count: 0, + }; + this.showDialog = true; + this.showEditor('') + }, + showEditor(content){ if (editor == null) { // 将回调延迟到下次 DOM 更新循环之后执行 this.$nextTick(() => { @@ -48,9 +62,16 @@ export default { }); } else { // 给编辑器设置内容 - editor.html(a.content); + editor.html(content); } }, + showEdit(a) { + // 不能直接赋值一个对象 + this.form = { ...a }; + this.showDialog = true; + this.showEditor(a.content) + + }, saveArticle() { this.form.content = editor.html(); // 获取编辑器的内容 request("./api/article/admin/save", this.form, "POST").then( diff --git a/public/manage.html b/public/manage.html index c5bc675..8bc3d3a 100644 --- a/public/manage.html +++ b/public/manage.html @@ -36,7 +36,8 @@ text-align: left; border-bottom: solid 1px #ddd; } - .content-div{ + + .content-div { border: 1px solid #DCDFE6; border-radius: 3px; overflow: hidden; @@ -48,7 +49,9 @@
-
+
+ 新增 +
@@ -75,15 +78,11 @@ @@ -96,14 +95,6 @@ - - - - - - - - @@ -115,14 +106,41 @@ - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -135,7 +153,7 @@ - +
修改 - + 删除 - - + + {{art.status==1?'设为草稿':'发布文章'}}