七、从零模拟新浪微博-个人主页
代码仓库:https://github.com/changeclass/koa2-weibo
用户数据
视图路由层
1
2
3
4router.get('/profile', loginRedirect, async (ctx, next) => {
const { userName } = ctx.session.userInfo
ctx.redirect(`/profile/${userName}`)
})当访问路由没有用户名时,默认跳转到自己用户名下。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27router.get('/profile/:userName', loginRedirect, async (ctx, next) => {
const { userName: curUserName } = ctx.params
// 获取微博第一页数据
// controller
const result = await getProfileBlogList(curUserName, 0)
const { isEmpty, blogList, pageSize, pageIndex, count } = result.data
await ctx.render('profile', {
blogData: {
isEmpty,
blogList,
pageSize,
pageIndex,
count
},
userData: {
userInfo: ctx.session.userInfo,
fansData: {
count: 0,
list: []
},
followersData: {
count: 0,
list: []
}
}
})
})控制器层
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32/**
* @description: 个人主页
* @author: 小康
* @url: https://xiaokang.me
* @Date: 2020-12-19 09:47:36
* @LastEditTime: 2020-12-19 09:47:36
* @LastEditors: 小康
*/
const { getBlogListByUser } = require('../services/blog')
const { PAGE_SIZE } = require('../config/constant')
const { SuccessModel } = require('../model/ResModel')
/**
* @author: 小康
* @url: https://xiaokang.me
* @param {string} userName 用户名
* @param {number} pageIndex 当前页面索引 默认0
* @description: 创建微博
*/
async function getProfileBlogList(userName, pageIndex = 0) {
const result = await getBlogListByUser({ userName, pageIndex, PAGE_SIZE })
const blogList = result.blogList
return new SuccessModel({
isEmpty: blogList.length === 0,
blogList,
pageSize: PAGE_SIZE,
pageIndex,
count: result.count
})
}
module.exports = { getProfileBlogList }服务层
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41const { Blog, User } = require('../db/model/index')
const { formatUser } = require('./_format')
/**
* @author: 小康
* @url: https://xiaokang.me
* @param {*} userName 用户名
* @param {*} pageIndex 页面索引
* @param {*} pageSize 页面大小
* @description: 根据用户名查询微博
*/
async function getBlogListByUser({ userName, pageIndex = 0, pageSize = 10 }) {
// 拼接查询条件
const userWhereOpts = {}
if (userName) {
userWhereOpts.userName = userName
}
// 执行查询
const result = await Blog.findAndCountAll({
limit: pageSize, // 每页多少条
offset: pageSize * pageIndex, // 跳过多少条
order: [['id', 'desc']],
include: [
{
model: User,
attributes: ['userName', 'nickName', 'picture'],
where: userWhereOpts
}
]
})
// 获取dataValues
let blogList = result.rows.map((row) => row.dataValues)
blogList = blogList.map((blogItem) => {
const user = blogItem.user.dataValues
blogItem.user = formatUser(user)
return blogItem
})
return {
count: result.count,
blogList
}
}
格式化时间、用户数据
用户数据主页是判断访问的用户是否存在。
1 | const { isExist } = require('../../controller/user') |
格式化时间主要是格式化微博内容,处理在服务层。
blog.js
1 | const { formatUser, formatBlog } = require('./_format') |
_format
1 | /** |
dt.js
1 | /** |
加载更多
api
路由层1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28/**
* @description: 个人主页 API 路由
* @author: 小康
* @url: https://xiaokang.me
* @Date: 2020-12-19 11:20:02
* @LastEditTime: 2020-12-19 11:20:02
* @LastEditors: 小康
*/
const router = require('koa-router')()
const { getProfileBlogList } = require('../../controller/blog-profile')
const { loginCheck } = require('../../middlewares/loginChecks')
const { getBlogListStr } = require('../../utils/blog')
router.prefix('/api/profile')
// 加载更多
router.get('/loadMore/:userName/:pageIndex', loginCheck, async (ctx, next) => {
let { userName, pageIndex } = ctx.params
pageIndex = parseInt(pageIndex)
const result = await getProfileBlogList(userName, pageIndex)
result.data.blogListTpl = getBlogListStr(result.data.blogList)
ctx.body = result
})
module.exports = router工具方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33/**
* @description: 微博数据相关的工具方法
* @author: 小康
* @url: https://xiaokang.me
* @Date: 2020-12-19 11:30:22
* @LastEditTime: 2020-12-19 11:30:22
* @LastEditors: 小康
*/
const fs = require('fs')
const path = require('path')
const ejs = require('ejs')
// 获取 blog-list.ejs 文件内容
const BLOG_LIST_TPL = fs
.readFileSync(path.join(__dirname, '..', 'views', 'widgets', 'blog-list.ejs'))
.toString()
/**
* @author: 小康
* @url: https://xiaokang.me
* @param {Array} blogList 微博列表
* @param {Boolean} canReply 是否可以回复
* @description: 根据blogList渲染出HTML字符串
*/
function getBlogListStr(blogList = [], canReply = false) {
return ejs.render(BLOG_LIST_TPL, {
blogList,
canReply
})
}
module.exports = { getBlogListStr }
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 小康博客!
评论
TwikooWaline