blog-server/controllers/article.js

146 lines
6.1 KiB
JavaScript
Raw Normal View History

2019-12-02 16:08:44 +08:00
// title 文章标题 contern 文章内容
2019-12-06 10:56:53 +08:00
// 获取所有文章列表
2019-11-13 12:47:16 +08:00
var articlefind = async (ctx, next) => {
2019-12-06 10:56:53 +08:00
let articlelist=[];
2019-12-06 08:59:26 +08:00
let tags=[]
2019-12-06 10:56:53 +08:00
2019-12-06 08:59:26 +08:00
await dbs.find("articlelists").then(async (res) => {
2019-12-06 10:56:53 +08:00
let articlelists=res.data
await dbs.find("group").then((res)=>{
let groups=res.data
2019-12-06 14:49:54 +08:00
// 遍历 group表检测 传入的groupid是不是在 group表中存在
// 如果不在,就返回所有文章
// 如果在group表中就返回该分组的文章
2019-12-06 10:56:53 +08:00
for(let i in groups){
if(parseInt(ctx.query.groupid)==groups[i].num_key){
// console.log(groups[i])
2019-12-06 14:49:54 +08:00
// 判断 该分组是不是最后一层的分组 如果不是返回所有文章
// 如果是最后一层,(即没有 children,就返回该分组的文章 也就是 articlelist 的初始值
2019-12-06 10:56:53 +08:00
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
2019-12-06 14:49:54 +08:00
2019-12-06 09:10:12 +08:00
await dbs.find("articletag").then( async (res)=>{
2019-12-06 14:49:54 +08:00
// 给articlelist 添加tag字段 从articletag表中取出每个文章的对应tagid
2019-12-06 08:59:26 +08:00
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){
2019-12-06 09:10:12 +08:00
// console.log(res.data[m].articleid,articlelist[i].num_key,res.data[m].tagid)
2019-12-06 08:59:26 +08:00
// 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=[]
}
}
2019-12-06 09:10:12 +08:00
// console.log(tags)
2019-12-06 08:59:26 +08:00
}
2019-12-06 14:49:54 +08:00
// articlelist的tag已经写入 tagid了根据 label表 把tagid转化成labelname
2019-12-06 09:10:12 +08:00
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){
2019-12-06 14:49:54 +08:00
// label的num_key 和articlelist 的tagid比对
2019-12-06 09:10:12 +08:00
if(res.data[n].num_key==articlelist[i].tag[m]){
articlelist[i].tag[m]=res.data[n].lablename
}
}
}
}
})
2019-12-06 08:59:26 +08:00
})
2019-11-13 12:47:16 +08:00
ctx.body=articlelist
})
2019-12-02 16:08:44 +08:00
next()
2019-11-13 12:47:16 +08:00
}
2019-12-02 16:08:44 +08:00
// 添加文章
2019-12-06 14:49:54 +08:00
// 参数: title 文章标题, content 文章内容 tags 选择的标签id 形式: 1,2 group 分组id
2019-11-13 12:47:16 +08:00
var articleadd = async (ctx, next) => {
2019-12-05 10:42:58 +08:00
let articleid=0
2019-11-13 12:47:16 +08:00
let month=parseInt(new Date(Date.now()).getMonth())+1
2019-12-03 14:50:37 +08:00
// await dbs.add("articletag",{articleid:ctx.query.articleid,tagid:ctx.query.tagid})
2019-12-06 10:56:53 +08:00
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})
2019-12-06 14:49:54 +08:00
2019-12-05 10:42:58 +08:00
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
2019-12-06 14:49:54 +08:00
// 取 文章id: 从 idadd表 取 set=articlelsits 的 numkey-1就是 新文章的id
2019-12-05 10:42:58 +08:00
}
}
})
2019-12-06 14:49:54 +08:00
2019-12-03 14:50:37 +08:00
let tags=ctx.request.body.tags.split(",")
2019-12-05 10:42:58 +08:00
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)
2019-12-06 14:49:54 +08:00
// 比对 label的num_key 和 传进来的tagsid 相等就往articletag写入 记录 文章id和标签id
2019-12-05 10:42:58 +08:00
await dbs.add("articletag",{articleid:articleid,tagid:parseInt(tags[i])})
}
}
2019-12-03 14:50:37 +08:00
}
})
console.log(ctx.request.body)
2019-11-13 12:47:16 +08:00
ctx.body = "添加成功"
2019-12-02 16:08:44 +08:00
next()
2019-11-13 12:47:16 +08:00
}
2019-12-02 16:08:44 +08:00
// 删除文章
2019-11-13 12:47:16 +08:00
var articledel=async(ctx,next)=>{
2019-11-19 15:12:01 +08:00
await dbs.remove("articlelists",{num_key:parseInt(ctx.query.key)}).then((res)=>{
2019-11-13 12:47:16 +08:00
console.log(res)
console.log(ctx.query)
})
ctx.body="删除成功"
2019-12-02 16:08:44 +08:00
next()
2019-11-13 12:47:16 +08:00
}
2019-12-02 16:08:44 +08:00
// 修改文章
2019-11-13 12:47:16 +08:00
var articleupdate=async (ctx,next)=>{
let month=parseInt(new Date(Date.now()).getMonth())+1
2019-11-19 19:02:28 +08:00
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()})
2019-11-19 15:12:01 +08:00
ctx.body="修改成功"
2019-12-02 16:08:44 +08:00
next()
2019-11-19 15:12:01 +08:00
// 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())
2019-11-13 12:47:16 +08:00
}
2019-12-05 10:42:58 +08:00
2019-11-13 12:47:16 +08:00
module.exports = {
2019-12-02 17:10:38 +08:00
'POST /articleadd': articleadd,
2019-11-13 12:47:16 +08:00
'GET /articlefind': articlefind,
'GET /articledel': articledel,
2019-12-02 17:10:38 +08:00
'POST /articleupdate': articleupdate
2019-11-13 12:47:16 +08:00
};