comment-list.vue
4.42 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
187
188
189
190
191
192
193
194
195
196
197
198
<template>
<div class="comment-list">
<div class="comment-content-flex">
<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>
<div class="comment-footer">
<CommentPlaceholder
class="comment-input"
:dest-id="destId"
:add-type="0"
:column-type="columnType"
@on-comment="onComment">
参与评论
</CommentPlaceholder>
</div>
</div>
</template>
<script>
import CommentItem from './comment-item.vue';
import {Scroll} from 'cube-ui';
import {get} from 'lodash';
import {createNamespacedHelpers} from 'vuex';
const {mapActions} = createNamespacedHelpers('comment');
export default {
name: 'CommentList',
props: {
destId: Number,
columnType: {
type: Number,
default: 1001
}
},
data() {
return {
page: 1,
commentList: [],
scrollOption: {
bounce: false,
pullUpLoad: {
threshold: 200,
txt: {
more: '上拉加载',
noMore: '- 已经到底了 -'
}
}
},
show: false
};
},
watch: {
destId() {
this.init();
}
},
mounted() {
this.init();
},
methods: {
...mapActions(['fetchCommentList', 'fetchReplayList', 'postComment']),
async fetchComments(lazy) {
const result = await this.fetchCommentList({
destId: this.destId,
columnType: this.columnType,
page: this.page,
});
let dirty = true;
if (result.code === 200) {
const comments = get(result, 'data.commentInfos', []);
setTimeout(() => {
if (this.page === 1) {
this.commentList = comments;
} else {
this.commentList = this.commentList.concat(comments);
}
if (comments.length) {
this.page++;
} else {
dirty = false;
}
this.$emit('on-page-change', {
page: this.page,
size: result.data.total
});
this.$refs.scroll.forceUpdate(dirty);
}, lazy ? 200 : 0);
} else {
this.$createToast && this.$createToast({
txt: result.message || '服务器开小差了',
type: 'warn',
time: 1000
}).show();
}
return result;
},
onPullingUp() {
this.fetchComments();
},
async onComment() {
this.page = 1;
this.$refs.scroll.scrollTo(0, 0, 200);
this.fetchComments(false);
},
async init() {
this.page = 1;
this.commentList = [];
this.fetchComments(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 {
flex: 1;
overflow: hidden;
position: relative;
/deep/ .before-trigger {
color: #b0b0b0;
}
.comment-content {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
padding-top: 40px;
padding-left: 30px;
padding-right: 30px;
}
}
.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>