comment-placeholder.js
3.89 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
import {get, trim} from 'lodash';
import {createNamespacedHelpers} from 'vuex';
import YAS from 'utils/yas-constants';
const {mapActions: articleMapActions, mapMutations: articleMapMutations } = createNamespacedHelpers('article');
const {mapActions: commentMapActions} = createNamespacedHelpers('comment');
export default {
name: 'CommentPlaceholder',
props: {
tag: String,
destId: Number,
addType: Number,
columnType: {
type: Number,
default: 1001
},
user: {
type: String,
default: ''
},
share: Boolean,
posId: Number,
articleId: Number,
rootId: Number,
autoUpdate: {
type: Boolean,
default: true
}
},
data() {
return {
posting: false
};
},
methods: {
...commentMapActions(['postComment']),
...articleMapActions(['fetchArticleUpdate']),
...articleMapMutations(['UPDATE_ARTICLE_COMMENT_COUNT']),
async openComentInput() {
if (this.share) {
return this.$links.toDownloadApp();
}
const user = await this.$sdk.getUser();
if (user && user.uid) {
const hint = this.user ? `回复 ${this.user}:` : '写评论...';
if (this.$yoho.isYohoBuy) {
this.$yoho.getInput({hint}, (content) => {
this.comment(content);
}, (e) => {
console.error(e);
});
} else if (process.env.NODE_ENV !== 'production') {
this.$createDialog({
type: 'prompt',
title: '测试-输入',
prompt: {
placeholder: hint
},
onConfirm: (e, promptValue) => {
this.comment(promptValue);
}
}).show();
} else {
return this.$links.toDownloadApp();
}
} else {
this.$sdk.goLogin();
}
},
async comment(content) {
content = trim(content);
if (!content) {
this.$createToast({
txt: '请输入评论内容',
type: 'warn',
time: 1000
}).show();
return;
}
if (this.posting) {
return;
}
this.posting = true;
const result = await this.postComment({
content: content,
destId: this.addType === 0 ? this.destId : void 0,
commentId: this.addType === 1 ? this.destId : void 0,
addType: this.addType,
columnType: this.columnType
});
this.posting = false;
let toastInfo = {
txt: '评论成功',
type: 'success',
time: 1000
};
if (result.code === 200) {
this.UPDATE_ARTICLE_COMMENT_COUNT({articleId: this.articleId});
this.$emit('on-comment', {
destId: this.destId,
parentId: this.rootId || void 0,
addType: this.addType,
columnType: this.columnType,
comment: {
content,
destId: get(result, 'data', ''),
columnType: this.columnType,
parentId: this.addType === 1 ? this.destId : void 0,
rootId: this.rootId || void 0
}
});
if (this.autoUpdate && this.addType === 0) {
await this.fetchArticleUpdate({articleId: this.articleId});
}
this.reportComment(this.destId);
} else {
Object.assign(toastInfo, {
txt: result.message || '服务器开小差了',
type: 'warn',
});
}
this.$createToast(toastInfo).show();
},
reportComment(comId) {
this.$store.dispatch('reportYas', {
params: {
appop: YAS.eventName.comment,
param: {
POS_ID: this.posId,
ARTICLE_ID: this.articleId,
COM_ID: this.addType === 1 ? comId : void 0
}
}
});
}
},
render(h) {
return h(this.tag || 'div', {
on: {
click: () => {
this.openComentInput();
}
}
}, this.$slots.default);
}
};