Node/Koa2[58]: 判断关注状态
·
Yin灏
处理路由
src/routes/view/blog.js
//...
const { getProfileBlogList } = require("../../controller/blog-profile");
const { getFans } = require("../../controller/user-relation");
// 个人主页
//...
router.get("/profile", loginRedirect, async (ctx, next) => {
const { userName } = ctx.session.userInfo;
ctx.redirect(`/profile/${userName}`);
});
router.get("/profile/:userName", loginRedirect, async (ctx, next) => {
// 已经登录用户的信息
const myUserInfo = ctx.session.userInfo;
const myUserName = myUserInfo.userName;
let curUserInfo;
const { userName: curUserName } = ctx.params;
const isMe = myUserName === curUserName;
if (isMe) {
curUserInfo = myUserInfo;
} else {
const existResult = await isExist(curUserName);
if (existResult.error !== 0) {
return; // 用户名不存在
}
// 用户名存在
curUserInfo = existResult.data;
}
// controller 层
const result = await getProfileBlogList(curUserName, 0);
const { isEmpty, blogList, pageSize, pageIndex, count } = result.data;
// 获取粉丝
// controller 层
const fansResult = await getFans(curUserInfo.id);
const { count: fansCount, userList: fansList } = fansResult.data;
// 我是否关注了此人?
const amIFollowed = fansList.some((item) => {
return item.userName === myUserName;
});
await ctx.render("profile", {
blogData: {
isEmpty,
blogList,
pageSize,
pageIndex,
count,
},
userData: {
userInfo: curUserInfo,
isMe,
amIFollowed,
fansData: {
count: fansCount,
list: fansList
},
},
});
});
//...