From 9aee2898ffad7e44fd1f54c0396c7047579efc9d Mon Sep 17 00:00:00 2001 From: laowang Date: Tue, 17 May 2022 17:47:18 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=AF=84=E8=AE=BA=E5=B1=95?= =?UTF-8?q?=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.js | 4 +++- modules/article.js | 20 ++++++++-------- modules/comment.js | 32 +++++++++++++++++++++++++ public/assets/style.css | 4 ++++ public/detail.html | 53 ++++++++++++++++++++++++++++++++++++----- public/index.html | 2 +- 6 files changed, 97 insertions(+), 18 deletions(-) diff --git a/index.js b/index.js index 1c53ca1..c6db4a6 100644 --- a/index.js +++ b/index.js @@ -1,13 +1,15 @@ const express = require("express") const user = require('./modules/user') // 用户模块 const article = require('./modules/article') // 文章 +const comment = require('./modules/comment') // 评论 const app = express(); // 创建express应用 - +app.use(express.json()); app.use(express.static('public')) // 设置静态文件路径 // 使用路由模块 app.use('/api/user',user) app.use('/api/article',article) +app.use('/api/comment',comment) app.listen(3000); // 监听端口 console.log('server run success! http://localhost:3000') \ No newline at end of file diff --git a/modules/article.js b/modules/article.js index 43ecf7e..ba03783 100644 --- a/modules/article.js +++ b/modules/article.js @@ -24,23 +24,23 @@ router.get("/web/list", async function (req, res) { } }); // 2.查询单个文章详情 -router.all('/web/detail',async (req,res)=>{ +router.all('/web/detail', async (req, res) => { let id = req.query['id']; - if(!id || id <1){ + if (!id || id < 1) { res.send([]) - }else{ - const article = await db.query('select * from article where id =? and status = 1',[id]); + } else { + const article = await db.query('select * from article where id =? and status = 1', [id]); res.send(article) } }); // 3.更新阅读数量 -router.all('/web/update',async (req,res)=>{ +router.all('/web/update', async (req, res) => { let id = req.query['id']; - if(!id || id <1){ - res.send({status:false,message:"参数不对"}) - }else{ - const article = await db.query('update article set view_count=view_count + 1 where id =?',[id]); - res.send({status:true}) + if (!id || id < 1) { + res.send({ status: false, message: "参数不对" }) + } else { + const article = await db.query('update article set view_count=view_count + 1 where id =?', [id]); + res.send({ status: true }) } }); // 管理后台模块 diff --git a/modules/comment.js b/modules/comment.js index e69de29..14721ad 100644 --- a/modules/comment.js +++ b/modules/comment.js @@ -0,0 +1,32 @@ +const dayjs = require('dayjs'); +const db = require('./db') +const router = require('express').Router(); + +// 完整的请求接口地址: /api/comment/web/query +router.all('/web/query', async (req, res) => { + let id = req.query['id']; // 获取要查询文章的编号 + if (!id || id < 1) { + res.send({ status: false, message: "参数不对" }) + } else { + const list = await db.query('select * from comment where article_id =?', [id]); + res.send(list) + } +}); +router.post('/web/add', async (req, res) => { + let commentData = req.body; + console.log(commentData) + if (!commentData || !commentData.article_id || !commentData.content) { + res.send({ status: false, message: "参数不对" }) + } else { + try { + commentData.publish_time = dayjs().format("YYYY-MM-DD HH:mm:ss") + commentData.ip = '127.0.0.1' + await db.query('insert into comment set ?', commentData); + res.send({ status: true }) + } catch (e) { + console.log(e) + res.send({ status: false, message: e.message }) + } + } +}); +module.exports = router \ No newline at end of file diff --git a/public/assets/style.css b/public/assets/style.css index 0d25142..543fe12 100644 --- a/public/assets/style.css +++ b/public/assets/style.css @@ -21,9 +21,13 @@ header { font-size: 30px; flex: 1; } +.logo a{ + color: #fff; +} .site-nav { text-align: right; + margin-left: 20px; } .site-nav a { diff --git a/public/detail.html b/public/detail.html index 0f205c8..35ff3f0 100644 --- a/public/detail.html +++ b/public/detail.html @@ -14,7 +14,7 @@
- +
+
+

所有评论

+
+
+ +
+
+ 评论 +
+
+
+
+
{{c.content}}
+
+ 评论IP:{{c.ip}} + 评论时间:{{c.publish_time}} +
+
+
+
@@ -57,10 +78,11 @@ el: '#detail', // 设置挂载对象 data() { return { - isError:false, + xxx:'', + isError: false, a: { "id": 0, - "title": "", + "title": null, "cover": "", "publish_time": "", "description": "", @@ -69,25 +91,44 @@ "view_count": 0, "comment_count": 0, "status": 1 - } + }, + // 所有的评论列表 + commentList: [] } }, async created() { const params = qs.parse(location.search); if (!params.id) { + this.setTitle() this.isError = true; return; } - const id = params.id; + const id = this.a.id = params.id; await request('./api/article/web/update', { id }) const articleList = await request('./api/article/web/detail', { id }); if (articleList.length != 1) { this.isError = true; + this.setTitle() return; } this.a = articleList[0]; + this.setTitle() + + this.loadCommentList() + }, methods: { + async loadCommentList() { + const id = this.a.id; + const list = await request('./api/comment/web/query', { id }) + this.commentList = list; + }, + back() { + history.back() + }, + setTitle(title = null) { + document.title = this.a.title || '加载数据失败' + }, formatDate(time) { return dayjs(time).format('YYYY-MM-DD HH:mm') }, diff --git a/public/index.html b/public/index.html index 8147fe7..beef2dc 100644 --- a/public/index.html +++ b/public/index.html @@ -60,7 +60,7 @@ // vue 通过 Vue.createApp(选项)创建vue实例 import request from './modules/request.js' - const HomeApp = window.xxxx = new Vue({ + new Vue({ el: '#home', // 设置挂载对象 data() { return {