Node/Koa2[72]:显示@数量
·
Yin灏
新建 controller
// src/controller/blog-at.js
const { getAtRelationCount } = require("../services/at-relation");
async function getAtMeCount(userId) {
// 调用 services
const count = await getAtRelationCount(userId);
return new SuccessModel({ count });
}
module.exports = {
getAtMeCount,
};
services
// src/services/at-relation.js
//...
async function getAtRelationCount(userId) {
const result = await AtRelation.findAndCountAll({
where: {
userId,
isRead: false,
},
});
return result.count;
}
//...
将获得的 count 输出到页面中
// src/routes/view/blog.js
const { getHomeBlogList } = require("../../controller/blog-home");
const { getAtMeCount } = require("../../controller/blog-at");
router.get("/", loginRedirect, async (ctx, next) => {
const userInfo = ctx.session.userInfo;
const { id: userId } = userInfo;
// 获取第一页数据
// 调用 controller
const result = await getHomeBlogList(userId);
const { isEmpty, blogList, pageSize, pageIndex, count } = result.data;
// 获取粉丝
const fansResult = await getFans(userId);
const { count: fansCount, fansList } = fansResult.data;
// 获取关注人列表
const followersResult = await getFollowers(userId);
const { count: followersCount, followersList } = followersResult.data;
// 获取 @ 数量
const atCountResult = await getAtMeCount(userId);
const { count: atCount } = atCountResult.data;
await ctx.render("index", {
userData: {
userInfo,
fansData: {
count: fansCount,
list: fansList,
},
followersData: {
count: followersCount,
list: followersList,
},
atCount
},
blogData: {
isEmpty,
blogList,
pageSize,
pageIndex,
count,
},
});
});