async destroy() { const { ctx, } = this; const id = +ctx.params.id; const user_id = +ctx.params.user_id; console.log(id, user_id); ctx.body = await ctx.service.comment.del({ id, user_id, }); }
// 删除评论 async remove() { const { ctx, service } = this; let response = { success: false }; // 评论者才能删除自己的评论 if (ctx.session.user.name === ctx.request.body.operator) { response = await service.comment.remove(subObj(['id'], ctx.request.body)); } else { response.msg = '只能删除自己的评论哦'; } ctx.body = response; ctx.status = 200; }
// 新增评论 async save() { const { ctx, service } = this; let response = { success: false, }; const request = subObj(['content', 'author', 'article_id'], ctx.request.body); response = await service.comment.save(request); if (response.success) { const type = 2; const msgRequest = subObj(['content', 'author', 'article_id'], ctx.request.body); service.message.create('message', msgRequest, type); } ctx.body = response; ctx.status = 200; }
async create() { const { ctx, } = this; const { blog_id, user_id, content, } = ctx.request.body; ctx.body = await ctx.service.comment.create({ blog_id, user_id, content, }); }
// 根据ID获取内容信息 async detail() { const { ctx, service } = this; // 调用 service 获取数据 const data = {}; data.article = await service.article.detail('article', subObj(['id'], ctx.query)); if (data.article && data.status !== 2) { // 如果有查到文章(可能id错误或文章被删了) data.comments = await service.comment.query(subObj(['id'], ctx.query)); ctx.body = { success: true, msg: '', code: '', data, }; } else { ctx.body = { success: false, msg: '文章不存在', code: 'article not exist', data: null, }; } ctx.status = 200; }