comment-list.vue
4.24 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<template>
<div class="comment-list">
<div class="comment-content">
<Scroll ref="scroll" :data="commentList" :options="scrollOption" @pulling-up="onPullingUp">
<CommentItem
v-for="(comment, index) in commentList"
:key="index"
:parent-comment="comment.parentComment"
:children-comments="comment.childrenComments"
:column-type="columnType"
@on-reply="onReply">
</CommentItem>
</Scroll>
</div>
<div class="comment-footer">
<div class="comment-input" @click="onComment">参与评论</div>
</div>
</div>
</template>
<script>
import CommentItem from './comment-item.vue';
import {Scroll} from 'cube-ui';
import {createNamespacedHelpers} from 'vuex';
import {get} from 'lodash';
const {mapActions} = createNamespacedHelpers('comment');
export default {
name: 'CommentList',
props: {
destId: Number,
columnType: {
type: Number,
default: 1001
}
},
data() {
return {
page: 1,
commentList: [],
scrollOption: {
pullUpLoad: {
threshold: 200,
txt: {
more: '上拉加载',
noMore: '- 已经到底了 -'
}
}
}
};
},
mounted() {
this.fetchComments();
},
methods: {
...mapActions(['fetchCommentList', 'fetchReplayList', 'postComment']),
async fetchComments(refresh) {
const result = await this.fetchCommentList({
destId: this.destId,
columnType: this.columnType,
page: this.page,
});
let dirty = true;
if (result.code === 200) {
const appendComments = get(result, 'data.commentInfos', []);
if (refresh) {
this.commentList = appendComments;
} else {
this.commentList = this.commentList.concat(appendComments);
}
if (appendComments.length) {
this.page++;
} else {
dirty = false;
}
this.$emit('on-page-change', {
page: this.page,
size: result.data.total
});
} else {
this.$createToast && this.$createToast({
txt: result.message || '服务器开小差了',
type: 'warn',
time: 1000
}).show();
}
this.$refs.scroll.forceUpdate(dirty);
return result;
},
onPullingUp() {
this.fetchComments();
},
async onComment() {
const result = await this.postComment({
content: '这还是一条测试回复1',
destId: this.destId,
addType: 0,
columnType: this.columnType
});
if (result.code === 200) {
this.onRefresh();
} else {
this.$createToast({
txt: result.message || '服务器开小差了',
type: 'warn',
time: 1000
}).show();
}
},
async onRefresh() {
this.page = 1;
await this.fetchComments(true);
this.$refs.scroll.forceUpdate(true);
},
async onReply({commentId}) {
const result = await this.fetchReplayList({
commentId
});
if (result.code === 200) {
this.commentList.forEach(comment => {
if (comment.parentComment.id === commentId) {
comment.childrenComments = get(result, 'data.childrenComments');
}
});
} else {
this.$createToast({
txt: result.message || '服务器开小差了',
type: 'warn',
time: 1000
}).show();
}
}
},
components: {Scroll, CommentItem}
};
</script>
<style scoped lang="scss">
.comment-list {
width: 100%;
height: 100%;
overflow: hidden;
display: flex;
flex-direction: column;
.comment-content {
flex: 1;
padding-top: 40px;
padding-left: 30px;
padding-right: 30px;
overflow: hidden;
/deep/ .before-trigger {
color: #b0b0b0;
}
}
.comment-footer {
width: 100%;
height: 100px;
overflow: hidden;
padding: 14px 30px;
background-color: #fff;
border-top: solid 1px #e0e0e0;
.comment-input {
width: 100%;
height: 100%;
background: #f0f0f0;
border: 1px solid #e0e0e0;
border-radius: 4PX;
color: #b0b0b0;
padding-left: 22px;
display: flex;
align-items: center;
}
}
}
</style>