blog-server/controllers/article.js

119 lines
4.7 KiB
JavaScript
Raw Normal View History

2019-12-02 16:08:44 +08:00
// title 文章标题 contern 文章内容
// 获取文章列表
2019-11-13 12:47:16 +08:00
var articlefind = async (ctx, next) => {
let articlelist;
2019-12-06 08:59:26 +08:00
let tags=[]
await dbs.find("articlelists").then(async (res) => {
let num=0
2019-11-13 12:47:16 +08:00
articlelist = res.data
2019-12-06 09:10:12 +08:00
await dbs.find("articletag").then( async (res)=>{
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 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){
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 08:59:26 +08:00
// 参数: title 文章标题, content 文章内容 tags 选择的标签id 形式: 1,2
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-05 10:42:58 +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})
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-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)
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
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;
}
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
};