const Article = require('../models/article.model')
const {
handleSuccess,
handleError
} = require('../utils/handle')
const authIsVerified = require('../utils/auth')
const request = require('request')
const config = require('../config/index')
class ArticleController {
// 获取文章列表
static async getArts (ctx) {
const {
current_page = 1,
page_size = 10,
keyword = '',
state = 1,
publish = 1,
tag,
type,
date,
hot } = ctx.query
// 过滤条件
const options = {
sort: { create_at: -1 },
page: Number(current_page),
limit: Number(page_size),
populate: ['tag'],
select: '-content'
}
// 参数
const querys = {}
// 关键词查询
if (keyword) {
const keywordReg = new RegExp(keyword)
querys['$or'] = [
{ 'title': keywordReg },
{ 'content': keywordReg },
{ 'description': keywordReg }
]
}
// 按照state查询
if (['1', '2'].includes(state)) {
querys.state = state
}
// 按照公开程度查询
if (['1', '2'].includes(publish)) {
querys.publish = publish
}
// 按照类型程度查询
if (['1', '2', '3'].includes(type)) {
querys.type = type
}
// 按热度排行
if (hot) {
options.sort = {
'meta.views': -1,
'meta.likes': -1,
'meta.comments': -1
}
}
// 时间查询
if (date) {
const getDate = new Date(date)
if(!Object.is(getDate.toString(), 'Invalid Date')) {
querys.create_at = {
"$gte": new Date((getDate / 1000 - 60 * 60 * 8) * 1000),
"$lt": new Date((getDate / 1000 + 60 * 60 * 16) * 1000)
}
}
}
if (tag) querys.tag = tag
// 如果是前台请求,则重置公开状态和发布状态
if (!authIsVerified(ctx.request)) {
querys.state = 1
querys.publish = 1
}
// 查询
const result = await Article
.paginate(querys, options)
.catch(err => ctx.throw(500, '服务器内部错误'))
if (result) {
handleSuccess({
ctx,
result: {
pagination: {
total: result.total,
current_page: result.page,
total_page: result.pages,
page_size: result.limit
},
list: result.docs
},
message: '列表数据获取成功!'
})
} else handleError({ ctx, message: '获取列表数据失败'})
}
// 添加文章
static async postArt (ctx) {
const res = Article.createArticle(ctx.request.body)
if (res) {
handleSuccess({ ctx, message: '添加文章成功' })
// 百度 seo push
// request.post({
// url: `http://data.zz.baidu.com/urls?site=${config.BAIDU.site}&token=${config.BAIDU.token}`,
// headers: { 'Content-Type': 'text/plain' },
// body: `${config.INFO.site}/article/${res._id}`
// }, (error, response, body) => {
// console.log('推送结果:', body)
// })
} else handleError({ ctx, message: '添加文章失败' })
}
// 根据文章id 获取内容
static async getArt (ctx) {
const _id = ctx.params.id
if (!_id) {
handleError({ ctx, message: '无效参数' })
return false
}
const res = await Article
.findById(_id)
.populate('tag')
.catch(err => ctx.throw(500, '服务器内部错误'))
if (res) {
// 每次请求,views 都增加一次
res.meta.views += 1
res.save()
handleSuccess({ ctx, message: '文章获取成功', result: res })
} else handleError({ ctx, message: '获取文章失败' })
}
// 删除文章
static async deleteArt (ctx) {
const _id = ctx.params.id
if (!_id) {
handleError({ ctx, message: '无效参数' })
return false
}
const res = await Article
.findByIdAndRemove(_id)
.catch(err => ctx.throw(500, '服务器内部错误'))
if (res) {
handleSuccess({ ctx, message: '删除文章成功' })
// 百度推送
request.post({
url: `http://data.zz.baidu.com/del?site=${config.BAIDU.site}&token=${config.BAIDU.token}`,
headers: { 'Content-Type': 'text/plain' },
body: `${config.INFO.site}/article/${_id}`
}, (error, response, body) => {
console.log('百度删除结果:', body);
})
} else handleError({ ctx, message: '删除文章失败' })
}
// 修改文章
static async putArt (ctx) {
const _id = ctx.params.id
const { title, keyword, tag } = ctx.request.body
delete ctx.request.body.create_at
delete ctx.request.body.update_at
delete ctx.request.body.meta
if (!_id) {
handleError({ ctx, message: '无效参数' })
return false
}
if(!title || !keyword) {
handleError({ ctx, message: 'title, keyword必填' })
return false
}
const res = await Article
.findByIdAndUpdate(_id, ctx.request.body)
.catch(err => ctx.throw(500, '服务器内部错误'))
if (res) {
handleSuccess({ ctx, message: '更新文章成功' })
// 百度推送
// request.post({
// url: `http://data.zz.baidu.com/update?site=${config.BAIDU.site}&token=${config.BAIDU.token}`,
// headers: { 'Content-Type': 'text/plain' },
// body: `${config.INFO.site}/article/${_id}`
// }, (error, response, body) => {
// console.log('百度删除结果:', body);
// })
} else handleError({ ctx, message: '更新文章失败' })
}
// 修改文章状态
static async patchArt (ctx) {
const _id = ctx.params.id
const { state, publish } = ctx.request.body
const querys = {}
if (state) querys.state = state
if (publish) querys.publish = publish
if (!_id) {
handleError({ ctx, message: '无效参数'})
return false
}
const res = await Article
.findByIdAndUpdate(_id, querys)
.catch(err => ctx.throw(500, '服务器内部错误'))
if (res) handleSuccess({ ctx, message: '更新文章状态成功'})
else handleError({ ctx, message: '更新文章状态失败' })
}
// 文章归档
static async getAllArts (ctx) {
const current_page = 1
const page_size = 10000
// 过滤条件
const options = {
sort: { create_at: -1 },
page: Number(current_page),
limit: Number(page_size),
populate: ['tag'],
select: '-content'
}
// 参数
const querys = {
state: 1,
publish: 1
}
// 查询
const article = await Article.aggregate([
{ $match: { state: 1, publish: 1 } },
{
$project: {
year: { $year: '$create_at' },
month: { $month: '$create_at' },
title: 1,
create_at: 1
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
软件开发设计:应用软件开发、系统软件开发、移动应用开发、网站开发C++、Java、python、web、C#等语言的项目开发与学习资料 硬件与设备:单片机、EDA、proteus、RTOS、包括计算机硬件、服务器、网络设备、存储设备、移动设备等 操作系统:LInux、树莓派、安卓开发、微机操作系统、网络操作系统、分布式操作系统等。此外,还有嵌入式操作系统、智能操作系统等。 网络与通信:数据传输、信号处理、网络协议、网络与通信硬件、网络安全网络与通信是一个非常广泛的领域,它涉及到计算机科学、电子工程、数学等多个学科的知识。 云计算与大数据:包括云计算平台、大数据分析、人工智能、机器学习等,云计算是一种基于互联网的计算方式,通过这种方式,共享的软硬件资源和信息可以按需提供给计算机和其他设备。
资源推荐
资源详情
资源评论
收起资源包目录
node 项目部署实践.zip (38个子文件)
cm
app.js 1011B
controller
comment.controller.js 0B
category.controller.js 623B
tag.controller.js 0B
article.controller.js 9KB
option.controller.js 0B
index.js 389B
announcement.controller.js 0B
auth.controller.js 2KB
utils
auth.js 913B
email.js 0B
statusCode.js 672B
sitemap.js 0B
handle.js 945B
route
index.js 4KB
.editconfig 147B
ecosystem.json 604B
package.json 888B
middlewares
initAdmin.js 405B
interceptor.js 2KB
schema
option.schema.js 905B
announcement.schema.js 1KB
auth.schema.js 581B
category.schema.js 1KB
comment.schema.js 2KB
tag.schema.js 1KB
article.schema.js 2KB
models
option.model.js 52B
article.model.js 591B
category.model.js 581B
tag.schema.js 52B
comment.model.js 52B
announcement.model.js 52B
auth.model.js 1KB
.gitignore 56B
config
index.js 499B
db
mysql.js 603B
mongodb.js 592B
共 38 条
- 1
资源评论
妄北y
- 粉丝: 1w+
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功