文章详情页面-关注作者
# 文章详情页面-关注作者
# 一、UI显示处理
判断当前的登录用户是否包含文章作者的用户ID
// 添加计算属性
isFollowAuthor() {
return this.userInfo && this.userInfo.author_likes_ids.includes(this.articleData.author.id)
}
# 二、事件绑定
传递文章作者ID,及当前登录用户ID
const { msg } = await this.$http.update_follow_author({ authorId: this.articleData.author.id, userId: this.userInfo._id })
# 三、云函数定义
'use strict';
const db = uniCloud.database();
const dbCmd = db.command;
exports.main = async (event, context) => {
const {
userId,
authorId
} = event;
const user = await db.collection('user').doc(userId).get();
const authorLikesIds = user.data[0].author_likes_ids
let returnMsg = ''
let author_ids = null
if (authorLikesIds.includes(authorId)) {
returnMsg = '取消关注成功'
author_ids = dbCmd.pull(authorId)
} else {
returnMsg = '关注作者成功'
author_ids = dbCmd.addToSet(authorId)
}
// 将处理完的内容进行重新插入
await db.collection('user').doc(userId).update({
author_likes_ids: author_ids
})
//返回数据给客户端
return {
code: 0,
data: {
msg: returnMsg
}
}
};
# 四、前端处理用户保存信息
async _followAuthor () {
// 用户检测
await this.checkedIsLogin()
const { msg } = await this.$http.update_follow_author({ authorId: this.articleData.author.id, userId: this.userInfo._id })
uni.showToast({
title: msg,
})
//处理用户存储信息
let followIds = [...this.userInfo.author_likes_ids]
if (followIds.includes(this.articleData.author.id)) {
followIds = followIds.filter(item => item != this.articleData.author.id)
}else {
followIds.push(this.articleData.author.id)
}
this.updateUserInfo({...this.userInfo,author_likes_ids:followIds})
}
# 五、收藏文章处理
- 使用公共组件 SaveLikes
<SaveLikes size="22" class="detail-bottom-icon-box" :item="articleData"></SaveLikes>
- 处理每次请求都进行文章列表加载Bug