Skip to content

Commit 32862ac

Browse files
committed
feat: support like the comments in topic detail
1 parent 7be189c commit 32862ac

File tree

5 files changed

+114
-7
lines changed

5 files changed

+114
-7
lines changed

src/dc/api.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,53 @@ export class TopicCollect extends Base {
109109
}
110110
}
111111

112+
// 评论
113+
export class Reply extends Base {
114+
constructor() {
115+
super();
116+
this.sCreateUrl = '/topic/:topic_id/replies';
117+
this.sLikeOrDislikeUrl = '/reply/:reply_id/ups';
118+
}
119+
/**
120+
* 新建评论
121+
* post 参数
122+
* accesstoken String 用户的 accessToken
123+
* content String 评论的主体
124+
* reply_id String 如果这个评论是对另一个评论的回复,请务必带上此字段。这样前端就可以构建出评论线索图。
125+
* @param {Object} oOption
126+
* @param {Function} fnDataModel
127+
*/
128+
create(oOption = {}, fnDataModel) {
129+
return fnDataProcess(
130+
{
131+
url: this.sCreateUrl,
132+
method: 'post',
133+
...oOption
134+
},
135+
fnDataModel
136+
);
137+
}
138+
/**
139+
* 为评论点赞或取消点赞
140+
* 接受 post 参数
141+
* accesstoken String
142+
* 接口会自动判断用户是否已点赞,如果否,则点赞;如果是,则取消点赞。点赞的动作反应在返回数据的 action 字段中,up or down。
143+
* @param {Object} oOption
144+
* @param {Function} fnDataModel
145+
*/
146+
likeOrDislike(oOption = {}, fnDataModel) {
147+
return fnDataProcess(
148+
{
149+
url: this.sLikeOrDislikeUrl,
150+
method: 'post',
151+
...oOption
152+
},
153+
fnDataModel
154+
);
155+
}
156+
}
157+
158+
// 用户
112159
export class User extends Base {
113160
constructor() {
114161
super();

src/dc/base.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
export default class Base {
22
constructor() {
33
this.sListUrl = '';
4+
this.sCreateUrl = '';
45
this.sUpdateUrl = '';
56
this.sDetailUrl = '';
67
this.sDeleteUrl = '';
78
}
89
list() {}
10+
create() {}
911
update() {}
1012
detail() {}
1113
delete() {}

src/dc/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import { Topic, TopicCollect, User } from './api';
1+
import { Topic, TopicCollect, Reply, User } from './api';
22

33
wx.initDataCenter = function() {
44
wx.dc = {
55
topic: new Topic(),
66
topicCollect: new TopicCollect(),
7+
reply: new Reply(),
78
user: new User()
89
};
910
};

src/pages/topic/topic.js

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ Page({
44
data: {
55
bIsReady: false, // 页面是否准备就绪
66
sTopicId: '', // 主题id
7+
oUserInfo: {}, // 当前登录用户的基本信息
78
oTopicDetail: {} // 主题详情
89
},
910
onLoad(options) {
1011
this.fnFetchTopicDetail(options.id);
1112
this.setData({
12-
sTopicId: options.id
13+
sTopicId: options.id,
14+
oUserInfo: wx.getStorageSync('oUserInfo') || {}
1315
});
1416
},
1517
onShow() {
@@ -29,6 +31,50 @@ Page({
2931
this.fnNetSwitchTopicCollectStatus(!this.data.oTopicDetail.is_collect);
3032
}
3133
},
34+
// 切换评论的点赞状态
35+
fnTapLikeCommentOrDislike(e) {
36+
let oDataSet = e.currentTarget.dataset;
37+
this.fnNetLikeCommentOrDislike(oDataSet.id, oDataSet.index);
38+
},
39+
// 切换评论的点赞状态
40+
fnNetLikeCommentOrDislike(sCommentId, nCommentIndex) {
41+
// 显示标题栏加载效果
42+
wx.showNavigationBarLoading();
43+
wx.dc.reply
44+
.likeOrDislike({
45+
urlData: {
46+
reply_id: sCommentId
47+
}
48+
})
49+
.then(res => {
50+
let aCommentUps = this.data.oTopicDetail.replies[nCommentIndex].ups,
51+
oUpdateData = {};
52+
// 点赞评论
53+
if (res.action === 'up') {
54+
// 将当前用户id,加入到该评论的点赞者列表中
55+
aCommentUps.push(this.data.oUserInfo.id);
56+
oUpdateData = {
57+
[`oTopicDetail.replies[${nCommentIndex}].is_uped`]: true,
58+
[`oTopicDetail.replies[${nCommentIndex}].ups`]: aCommentUps
59+
};
60+
}
61+
// 取消点赞评论
62+
if (res.action === 'down') {
63+
// 将当前用户id,从该评论的点赞者列表中移除
64+
aCommentUps = aCommentUps.filter(sUserId => sUserId !== this.data.oUserInfo.id);
65+
oUpdateData = {
66+
[`oTopicDetail.replies[${nCommentIndex}].is_uped`]: false,
67+
[`oTopicDetail.replies[${nCommentIndex}].ups`]: aCommentUps
68+
};
69+
}
70+
// 更新data数据
71+
this.setData(oUpdateData);
72+
wx.hideNavigationBarLoading();
73+
})
74+
.catch(() => {
75+
wx.hideNavigationBarLoading();
76+
});
77+
},
3278
// 切换主题收藏状态
3379
fnNetSwitchTopicCollectStatus(bIsCollect) {
3480
// 显示标题栏加载效果
@@ -118,11 +164,13 @@ Page({
118164
is_collect: oData.is_collect, // 是否收藏
119165
create_at: wx.moment(oData.create_at).fromNow(), // 创建时间
120166
content: oData.content, // 主题内容
121-
reply_count: oData.reply_count // 回复数
167+
reply_count: oData.reply_count // 评论数
122168
};
123-
// 回复列表
169+
// 评论列表
124170
oResult.replies = oData.replies.map(oItem => {
125171
return {
172+
is_uped: oItem.is_uped, // 当前登录用户是否点赞该评论
173+
id: oItem.id, // 评论id
126174
avatar_url: oItem.author.avatar_url, // 评论者头像
127175
loginname: oItem.author.loginname, // 评论者名称
128176
create_at: wx.moment(oItem.create_at).format('YYYY-MM-DD HH:mm:ss'), // 创建时间

src/pages/topic/topic.wxml

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@
2727
</view>
2828
<!-- 评论列表 -->
2929
<view class="topic-comment-list">
30-
<view class="topic-comment-list-header">{{oTopicDetail.replies.length}}条回复</view>
30+
<view class="topic-comment-list-header">{{oTopicDetail.replies.length}}条评论</view>
3131
<view class="topic-comment-item" wx:for="{{oTopicDetail.replies}}" wx:key="{{index}}">
3232
<view class="vh-parent clf">
33+
<!-- 评论者信息 -->
3334
<view class="topic-comment-left fl-left">
3435
<image class="w-p100 h-p100 rounded-p50" src="{{item.avatar_url}}" />
3536
</view>
@@ -41,11 +42,19 @@
4142
<text>{{item.create_at}}</text>
4243
</view>
4344
</view>
44-
<view class="v" style="color: #757575;right: 10rpx;font-size: 24rpx;">
45-
<i class="icon icon-up" style="font-size: 50rpx;margin-right: 10rpx;"></i>
45+
<!-- 点赞icon和点赞数 -->
46+
<view
47+
class="v"
48+
style="color: #757575;right: 10rpx;font-size: 24rpx;"
49+
data-id="{{item.id}}"
50+
data-index="{{index}}"
51+
bindtap="fnTapLikeCommentOrDislike"
52+
>
53+
<i class="icon icon-up {{item.is_uped ? 'color-green' : ''}}" style="font-size: 50rpx;margin-right: 10rpx;"></i>
4654
<text class="inbl vtal-top" style="height: 50rpx;line-height: 60rpx;">{{item.ups.length}}</text>
4755
</view>
4856
</view>
57+
<!-- 评论内容 -->
4958
<view class="topic-comment-content">
5059
<wemark type="wemark" md="{{item.content}}" highlight link></wemark>
5160
</view>

0 commit comments

Comments
 (0)