blog-server/controllers/article.js
2019-12-06 10:56:53 +08:00

141 lines
5.5 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 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
for(let i in groups){
if(parseInt(ctx.query.groupid)==groups[i].num_key){
// console.log(groups[i])
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)=>{
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)
}
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){
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
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
}
}
})
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)
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())
}
function text(s) {
var pattern = new RegExp("[`~!@#$^&*()=|{}':;',\\[\\].<>/?~@#¥……&*()——|{}【】‘;:”“'。,、?]");
var rs = "";
for (var i = 0; i < s.length; i++) {
rs = rs+s.substr(i, 1).replace(pattern, '');
}
return rs;
}
module.exports = {
'POST /articleadd': articleadd,
'GET /articlefind': articlefind,
'GET /articledel': articledel,
'POST /articleupdate': articleupdate
};