代码仓库:https://github.com/changeclass/koa2-weibo

艾特功能

艾特功能使用at.js库。

实现接口

1
2
3
4
5
6
7
8
9
10
// 获取 at 列表
router.get('/getAtList', loginCheck, async (ctx, next) => {
const { id: userId } = ctx.session.userInfo
const result = await getFollowers(userId)
const { followersList } = result.data
const list = followersList.map((user) => {
return `${user.nickName}-${user.userName}`
})
ctx.body = list
})

艾特用户转为链接

在格式化微博时将@符号转为链接

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/**
* 格式化微博内容
* @param {Object} obj 微博数据对象
*/
function _formatContent(obj) {
obj.contentFormat = obj.content

// 格式化 @
// from '哈喽 @张三 - zhangsan 你好'
// to '哈喽 <a href="/profile/zhangsan">张三</a> 你好'
obj.contentFormat = obj.contentFormat.replace(
REG_FOR_AT_WHO,
(matchStr, nickName, userName) => {
return `<a href="/profile/${userName}">@${nickName}</a>`
}
)

return obj
}

正则表达式/@(.+?)\s-\s(\w+?)\b/g