Node/Koa2[73]:开发@页面
·
Yin灏
创建 at 页面路由
// src/routes/view/blog.js
const { getAtMeBlogList } = require("../../controller/blog-at");
router.get("/at-me", loginRedirect, async () => {
const { id: userId } = ctx.session.userInfo;
const atCountResult = await getAtMeCount(userId);
const { count: atCount } = atCountResult.data;
// 获取第一页列表
// 调用 controller
const result = await getAtMeBlogList(userId);
const { isEmpty, blogList, pageSize, pageIndex, count } = result.data;
// 渲染页面
await ctx.render("atMe", {
atCount,
blogData: {
isEmpty,
blogList,
pageSize,
pageIndex,
count,
},
});
// 标记为已读
if (atCount > 0) {
}
});
controller
// src/controller/blog-at.js
const { getAtUserBlogList } = require("../services/at-relation");
async function getAtMeBlogList(userId, pageIndex = 0) {
// 调用 services
const result = await getAtUserBlogList({
userId,
pageIndex,
pageSize: PAGE_SIZE,
});
const { count, blogList } = result;
return new SuccessModel({
isEmpty: blogList.length === 0,
blogList,
pageSize: PAGE_SIZE,
pageIndex,
count,
});
}
module.exports = {
//...
getAtMeBlogList,
};
services
// src/services/at-relation.js
async function getAtUserBlogList({ userId, pageIndex, pageSize }) {
// 查询微博列表
const result = await Blog.findAndCountAll({
limit: pageSize,
offset: pageSize * pageIndex,
order: [["id", "desc"]],
include: [
{
model: AtRelation,
attributes: ["userId", "blogId"],
where: {
userId,
},
},
{
model: User,
attributes: ["userName", "nickName", "picture"],
},
],
});
let blogList = result.rows.map((row) => row.dataValues);
blogList = formatBlog(blogList);
blogList = blogList.map((blogItem) => {
blogItem.user = formatUser(blogItem.user.dataValues);
return blogItem;
});
return {
count: result.count,
blogList,
};
}
module.exports = {
//...
getAtUserBlogList,
};