From 44585f62fb5c6b75bbf196d7968173bee9f35176 Mon Sep 17 00:00:00 2001 From: pplokijuhyg <1162963624@qq.com> Date: Thu, 26 Dec 2019 16:27:14 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8D=A2=E7=B3=BB=E7=BB=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 38 +++--- bin/router.js | 94 ++++++------- controllers/article.js | 290 ++++++++++++++++++++--------------------- 3 files changed, 211 insertions(+), 211 deletions(-) diff --git a/README.md b/README.md index 1da51cb..d684c3f 100644 --- a/README.md +++ b/README.md @@ -1,20 +1,20 @@ -# blog-server -#### 目录结构 -- bin 基础文件存放 -~~~ - mongobd.js 链接mongodb基础 和 对于mongodb操作封装 - router.js 对于路由文件引入 -~~~ - -- controllers 路由文件存放 -~~~ - admin.js 后台管理路由 - article.js 文章操作路由 - grouping.js 分组路由 - label.js 标签路由 - login.js 登录路由 -~~~ - -- description 说明文档 - +# blog-server +#### 目录结构 +- bin 基础文件存放 +~~~ + mongobd.js 链接mongodb基础 和 对于mongodb操作封装 + router.js 对于路由文件引入 +~~~ + +- controllers 路由文件存放 +~~~ + admin.js 后台管理路由 + article.js 文章操作路由 + grouping.js 分组路由 + label.js 标签路由 + login.js 登录路由 +~~~ + +- description 说明文档 + [mongodb.js](http://git.luyuan.tk/pplokijuhyg/blog-server/src/branch/master/description/mongodb.js.md) \ No newline at end of file diff --git a/bin/router.js b/bin/router.js index 7cad897..e1bd5c0 100644 --- a/bin/router.js +++ b/bin/router.js @@ -1,47 +1,47 @@ -const fs = require("fs") -const os = require('os'); - -function addMapping(router, mapping) { - for (var url in mapping) { - if (url.startsWith('GET ')) { - var path = url.substring(4); - router.get(path, mapping[url]); - // console.log(`register URL mapping: GET ${path}`); - } else if (url.startsWith('POST ')) { - var path = url.substring(5); - router.post(path, mapping[url]); - // console.log(`register URL mapping: POST ${path}`); - } else { - console.log(`invalid URL: ${url}`); - } - } -} - -function addControllers(router, controllers_dir) { - let ostype = os.type() == "Windows_NT" - let dir; - if (ostype) { - dir = __dirname.replace("\\bin", "") - } else { - dir = __dirname.replace("/bin", "") - } - - var files = fs.readdirSync(dir + "/" + controllers_dir); - var js_files = files.filter((f) => { - return f.endsWith('.js'); - }); - - for (var f of js_files) { - // console.log(`process controller: ${f}...`); - let mapping = require(dir + `/${controllers_dir}/` + f); - addMapping(router, mapping); - } -} -module.exports = function (dir) { - let - controllers_dir = dir || 'controllers', // 如果不传参数,扫描目录默认为'controllers' - router = require('koa-router')(); - addControllers(router, controllers_dir); - return router.routes(); - // return 1; -}; +const fs = require("fs") +const os = require('os'); + +function addMapping(router, mapping) { + for (var url in mapping) { + if (url.startsWith('GET ')) { + var path = url.substring(4); + router.get(path, mapping[url]); + // console.log(`register URL mapping: GET ${path}`); + } else if (url.startsWith('POST ')) { + var path = url.substring(5); + router.post(path, mapping[url]); + // console.log(`register URL mapping: POST ${path}`); + } else { + console.log(`invalid URL: ${url}`); + } + } +} + +function addControllers(router, controllers_dir) { + let ostype = os.type() == "Windows_NT" + let dir; + if (ostype) { + dir = __dirname.replace("\\bin", "") + } else { + dir = __dirname.replace("/bin", "") + } + + var files = fs.readdirSync(dir + "/" + controllers_dir); + var js_files = files.filter((f) => { + return f.endsWith('.js'); + }); + + for (var f of js_files) { + // console.log(`process controller: ${f}...`); + let mapping = require(dir + `/${controllers_dir}/` + f); + addMapping(router, mapping); + } +} +module.exports = function (dir) { + let + controllers_dir = dir || 'controllers', // 如果不传参数,扫描目录默认为'controllers' + router = require('koa-router')(); + addControllers(router, controllers_dir); + return router.routes(); + // return 1; +}; diff --git a/controllers/article.js b/controllers/article.js index 073c19c..4c8d82b 100644 --- a/controllers/article.js +++ b/controllers/article.js @@ -1,146 +1,146 @@ -// title 文章标题 contern 文章内容 -// 获取所有文章列表 -var articlefind = async (ctx, next) => { - let articlelist=[]; - let tags=[] - - await dbs.find("articlelists").then(async (res) => { - let articlelists=res.data - await dbs.find("group").then((res)=>{ - let groups=res.data - // 遍历 group表,检测 传入的groupid是不是在 group表中存在 - // 如果不在,就返回所有文章 - // 如果在group表中,就返回该分组的文章 - for(let i in groups){ - if(parseInt(ctx.query.groupid)==groups[i].num_key){ - // console.log(groups[i]) - // 判断 该分组是不是最后一层的分组 如果不是返回所有文章 - // 如果是最后一层,(即没有 children),就返回该分组的文章 也就是 articlelist 的初始值 - if(groups[i].fuid!=0){ - for(let m in articlelists){ - if(articlelists[m].groupid==parseInt(ctx.query.groupid)){ - articlelist.push(articlelists[m]) - } - // articlelist.push() - } - } - - } - } - if(articlelist.length==0){ - articlelist=articlelists - } - }) - - // articlelist = res.data - - await dbs.find("articletag").then( async (res)=>{ - // 给articlelist 添加tag字段 从articletag表中取出每个文章的对应tagid - for(let i in articlelist){ - - for(let m in res.data){ - // console.log(res.data[m].articleid,articlelist[i].num_key,res.data[m].tagid) - if(res.data[m].articleid==articlelist[i].num_key){ - // console.log(res.data[m].articleid,articlelist[i].num_key,res.data[m].tagid) - // console.log(res.data[m].articleid) - // console.log(res.data[m].articleid,res.data[m].tagid) - // tags.push([res.data[m].articleid]) - tags.push(res.data[m].tagid) - articlelist[i].tag=tags - - }else{ - tags=[] - } - } - - // console.log(tags) - - - } - - // articlelist的tag已经写入 tagid了,根据 label表 把tagid转化成labelname - await dbs.find("label").then((res)=>{ - for(let i in articlelist){ - for(let m in articlelist[i].tag){ - console.log(articlelist[i].tag[m]) - for(let n in res.data){ - // label的num_key 和articlelist 的tagid比对 - if(res.data[n].num_key==articlelist[i].tag[m]){ - articlelist[i].tag[m]=res.data[n].lablename - } - } - } - } - }) - }) - - ctx.body=articlelist - }) - next() -} -// 添加文章 -// 参数: title 文章标题, content 文章内容 tags 选择的标签id 形式: 1,2, group 分组id -var articleadd = async (ctx, next) => { - let articleid=0 - let month=parseInt(new Date(Date.now()).getMonth())+1 - // await dbs.add("articletag",{articleid:ctx.query.articleid,tagid:ctx.query.tagid}) - await dbs.add("articlelists", { articletitle:ctx.request.body.title, date: new Date(Date.now()).getFullYear ()+ '/' + month+ '/' + new Date(Date.now()).getDate() + ' ' + new Date(Date.now()).getHours() + ':' + new Date(Date.now()).getMinutes() + ':' + new Date(Date.now()).getSeconds(),content:ctx.request.body.content,uid:1,groupid:ctx.request.body.groupid}) - - - await dbs.find("label").then(async (res)=>{ - // console.log(res.data) - await dbs.find("idadd").then((res)=>{ - // console.log(res) - for(let i in res.data){ - // console.log(res.data[i].set) - if(res.data[i].set=='articlelists'){ - console.log(res.data[i].num_key-1,"article") - articleid=res.data[i].num_key-1 - // 取 文章id: 从 idadd表 取 set=articlelsits 的 numkey-1就是 新文章的id - } - } - }) - - let tags=ctx.request.body.tags.split(",") - for(let i in tags){ - // console.log(tags[i]) - for(let m in res.data){ - // console.log(res.data[m].num_key,parseInt(tags[i])) - if(res.data[m].num_key==parseInt(tags[i])){ - console.log(res.data[m].lablename,77774) - // 比对 label的num_key 和 传进来的tagsid 相等就往articletag写入 记录 文章id和标签id - await dbs.add("articletag",{articleid:articleid,tagid:parseInt(tags[i])}) - } - } - } - }) - console.log(ctx.request.body) - ctx.body = "添加成功" - next() -} -// 删除文章 -var articledel=async(ctx,next)=>{ - await dbs.remove("articlelists",{num_key:parseInt(ctx.query.key)}).then((res)=>{ - console.log(res) - console.log(ctx.query) - }) - ctx.body="删除成功" - next() -} -// 修改文章 -var articleupdate=async (ctx,next)=>{ - let month=parseInt(new Date(Date.now()).getMonth())+1 - - await dbs.update("articlelists",{num_key:parseInt(ctx.query.key)},{articletitle:ctx.query.title, udate: new Date(Date.now()).getFullYear ()+ '/' + month+ '/' + new Date(Date.now()).getDate() + ' ' + new Date(Date.now()).getHours() + ':' + new Date(Date.now()).getMinutes() + ':' + new Date(Date.now()).getSeconds()}) - ctx.body="修改成功" - next() - // console.log(new Date(Date.now()).getFullYear ()+ '/' + month+ '/' + new Date(Date.now()).getDate() + ' ' + new Date(Date.now()).getHours() + ':' + new Date(Date.now()).getMinutes()) - // console.log(new Date(Date.now()).getDate()) -} - -module.exports = { - 'POST /articleadd': articleadd, - 'GET /articlefind': articlefind, - 'GET /articledel': articledel, - 'POST /articleupdate': articleupdate +// title 文章标题 contern 文章内容 +// 获取所有文章列表 +var articlefind = async (ctx, next) => { + let articlelist=[]; + let tags=[] + + await dbs.find("articlelists").then(async (res) => { + let articlelists=res.data + await dbs.find("group").then((res)=>{ + let groups=res.data + // 遍历 group表,检测 传入的groupid是不是在 group表中存在 + // 如果不在,就返回所有文章 + // 如果在group表中,就返回该分组的文章 + for(let i in groups){ + if(parseInt(ctx.query.groupid)==groups[i].num_key){ + // console.log(groups[i]) + // 判断 该分组是不是最后一层的分组 如果不是返回所有文章 + // 如果是最后一层,(即没有 children),就返回该分组的文章 也就是 articlelist 的初始值 + if(groups[i].fuid!=0){ + for(let m in articlelists){ + if(articlelists[m].groupid==parseInt(ctx.query.groupid)){ + articlelist.push(articlelists[m]) + } + // articlelist.push() + } + } + + } + } + if(articlelist.length==0){ + articlelist=articlelists + } + }) + + // articlelist = res.data + + await dbs.find("articletag").then( async (res)=>{ + // 给articlelist 添加tag字段 从articletag表中取出每个文章的对应tagid + for(let i in articlelist){ + + for(let m in res.data){ + // console.log(res.data[m].articleid,articlelist[i].num_key,res.data[m].tagid) + if(res.data[m].articleid==articlelist[i].num_key){ + // console.log(res.data[m].articleid,articlelist[i].num_key,res.data[m].tagid) + // console.log(res.data[m].articleid) + // console.log(res.data[m].articleid,res.data[m].tagid) + // tags.push([res.data[m].articleid]) + tags.push(res.data[m].tagid) + articlelist[i].tag=tags + + }else{ + tags=[] + } + } + + // console.log(tags) + + + } + + // articlelist的tag已经写入 tagid了,根据 label表 把tagid转化成labelname + await dbs.find("label").then((res)=>{ + for(let i in articlelist){ + for(let m in articlelist[i].tag){ + console.log(articlelist[i].tag[m]) + for(let n in res.data){ + // label的num_key 和articlelist 的tagid比对 + if(res.data[n].num_key==articlelist[i].tag[m]){ + articlelist[i].tag[m]=res.data[n].lablename + } + } + } + } + }) + }) + + ctx.body=articlelist + }) + next() +} +// 添加文章 +// 参数: title 文章标题, content 文章内容 tags 选择的标签id 形式: 1,2, group 分组id +var articleadd = async (ctx, next) => { + let articleid=0 + let month=parseInt(new Date(Date.now()).getMonth())+1 + // await dbs.add("articletag",{articleid:ctx.query.articleid,tagid:ctx.query.tagid}) + await dbs.add("articlelists", { articletitle:ctx.request.body.title, date: new Date(Date.now()).getFullYear ()+ '/' + month+ '/' + new Date(Date.now()).getDate() + ' ' + new Date(Date.now()).getHours() + ':' + new Date(Date.now()).getMinutes() + ':' + new Date(Date.now()).getSeconds(),content:ctx.request.body.content,uid:1,groupid:ctx.request.body.groupid}) + + + await dbs.find("label").then(async (res)=>{ + // console.log(res.data) + await dbs.find("idadd").then((res)=>{ + // console.log(res) + for(let i in res.data){ + // console.log(res.data[i].set) + if(res.data[i].set=='articlelists'){ + console.log(res.data[i].num_key-1,"article") + articleid=res.data[i].num_key-1 + // 取 文章id: 从 idadd表 取 set=articlelsits 的 numkey-1就是 新文章的id + } + } + }) + + let tags=ctx.request.body.tags.split(",") + for(let i in tags){ + // console.log(tags[i]) + for(let m in res.data){ + // console.log(res.data[m].num_key,parseInt(tags[i])) + if(res.data[m].num_key==parseInt(tags[i])){ + console.log(res.data[m].lablename,77774) + // 比对 label的num_key 和 传进来的tagsid 相等就往articletag写入 记录 文章id和标签id + await dbs.add("articletag",{articleid:articleid,tagid:parseInt(tags[i])}) + } + } + } + }) + console.log(ctx.request.body) + ctx.body = "添加成功" + next() +} +// 删除文章 +var articledel=async(ctx,next)=>{ + await dbs.remove("articlelists",{num_key:parseInt(ctx.query.key)}).then((res)=>{ + console.log(res) + console.log(ctx.query) + }) + ctx.body="删除成功" + next() +} +// 修改文章 +var articleupdate=async (ctx,next)=>{ + let month=parseInt(new Date(Date.now()).getMonth())+1 + + await dbs.update("articlelists",{num_key:parseInt(ctx.query.key)},{articletitle:ctx.query.title, udate: new Date(Date.now()).getFullYear ()+ '/' + month+ '/' + new Date(Date.now()).getDate() + ' ' + new Date(Date.now()).getHours() + ':' + new Date(Date.now()).getMinutes() + ':' + new Date(Date.now()).getSeconds()}) + ctx.body="修改成功" + next() + // console.log(new Date(Date.now()).getFullYear ()+ '/' + month+ '/' + new Date(Date.now()).getDate() + ' ' + new Date(Date.now()).getHours() + ':' + new Date(Date.now()).getMinutes()) + // console.log(new Date(Date.now()).getDate()) +} + +module.exports = { + 'POST /articleadd': articleadd, + 'GET /articlefind': articlefind, + 'GET /articledel': articledel, + 'POST /articleupdate': articleupdate }; \ No newline at end of file