@syfei49/mini-dynamic-renderer 1.0.7 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +181 -130
- package/components/ly-ai-assistant/ly-ai-assistant.vue +916 -557
- package/index.js +16 -16
- package/package.json +27 -27
- package/src/adapters/request.js +148 -106
- package/src/chat/parser.js +152 -55
- package/src/chat/service.js +497 -297
- package/src/config.js +54 -51
- package/src/event.js +43 -43
- package/src/handler.js +43 -43
- package/src/init.js +38 -38
- package/src/interceptor.js +105 -105
package/src/chat/service.js
CHANGED
|
@@ -1,297 +1,497 @@
|
|
|
1
|
-
var requestAdapter = require('../adapters/request');
|
|
2
|
-
var parser = require('./parser');
|
|
3
|
-
var event = require('../event');
|
|
4
|
-
var configModule = require('../config');
|
|
5
|
-
|
|
6
|
-
var state = {
|
|
7
|
-
messages: [],
|
|
8
|
-
conversationId: '',
|
|
9
|
-
loading: false,
|
|
10
|
-
uploading: false,
|
|
11
|
-
iconPickMode: false,
|
|
12
|
-
pendingIconCategory: null,
|
|
13
|
-
pendingImageUrl: '',
|
|
14
|
-
inputText: ''
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
state.
|
|
29
|
-
state.
|
|
30
|
-
state.
|
|
31
|
-
state.
|
|
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
|
-
function
|
|
82
|
-
var
|
|
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
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
});
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
function
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
}
|
|
297
|
-
|
|
1
|
+
var requestAdapter = require('../adapters/request');
|
|
2
|
+
var parser = require('./parser');
|
|
3
|
+
var event = require('../event');
|
|
4
|
+
var configModule = require('../config');
|
|
5
|
+
|
|
6
|
+
var state = {
|
|
7
|
+
messages: [],
|
|
8
|
+
conversationId: '',
|
|
9
|
+
loading: false,
|
|
10
|
+
uploading: false,
|
|
11
|
+
iconPickMode: false,
|
|
12
|
+
pendingIconCategory: null,
|
|
13
|
+
pendingImageUrl: '',
|
|
14
|
+
inputText: '',
|
|
15
|
+
renderConfig: null,
|
|
16
|
+
pendingInputs: {},
|
|
17
|
+
lastUserQuery: ''
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
var quickQuestions = [
|
|
21
|
+
'你好,你能帮我做什么?',
|
|
22
|
+
'帮我新增商品',
|
|
23
|
+
'上架商品:杏',
|
|
24
|
+
'在「保健品」下新增二级分类:维生素,排序0'
|
|
25
|
+
];
|
|
26
|
+
|
|
27
|
+
function reset() {
|
|
28
|
+
state.messages = [];
|
|
29
|
+
state.conversationId = '';
|
|
30
|
+
state.loading = false;
|
|
31
|
+
state.uploading = false;
|
|
32
|
+
state.iconPickMode = false;
|
|
33
|
+
state.pendingIconCategory = null;
|
|
34
|
+
state.pendingImageUrl = '';
|
|
35
|
+
state.inputText = '';
|
|
36
|
+
state.renderConfig = null;
|
|
37
|
+
state.pendingInputs = {};
|
|
38
|
+
state.lastUserQuery = '';
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function ensureLogin() {
|
|
42
|
+
var config = configModule.getConfig();
|
|
43
|
+
if (!config.requireLogin) {
|
|
44
|
+
return true;
|
|
45
|
+
}
|
|
46
|
+
var token = typeof config.getToken === 'function' ? config.getToken() : '';
|
|
47
|
+
if (token) {
|
|
48
|
+
return true;
|
|
49
|
+
}
|
|
50
|
+
uni.showToast({
|
|
51
|
+
title: '请先登录',
|
|
52
|
+
icon: 'none'
|
|
53
|
+
});
|
|
54
|
+
setTimeout(function () {
|
|
55
|
+
uni.navigateTo({
|
|
56
|
+
url: config.loginPage
|
|
57
|
+
});
|
|
58
|
+
}, 800);
|
|
59
|
+
return false;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function notifyUpdate() {
|
|
63
|
+
event.emit('chat:update', getSnapshot());
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function getSnapshot() {
|
|
67
|
+
return {
|
|
68
|
+
messages: state.messages.slice(),
|
|
69
|
+
loading: state.loading,
|
|
70
|
+
uploading: state.uploading,
|
|
71
|
+
iconPickMode: state.iconPickMode,
|
|
72
|
+
pendingIconCategory: state.pendingIconCategory,
|
|
73
|
+
pendingImageUrl: state.pendingImageUrl,
|
|
74
|
+
inputText: state.inputText,
|
|
75
|
+
renderConfig: state.renderConfig,
|
|
76
|
+
pendingInputs: Object.assign({}, state.pendingInputs),
|
|
77
|
+
quickQuestions: quickQuestions.slice()
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function createAssistantMessage(content) {
|
|
82
|
+
var parsed = parser.parseAssistantMessage(content);
|
|
83
|
+
return {
|
|
84
|
+
role: 'assistant',
|
|
85
|
+
content: parsed.content,
|
|
86
|
+
imageUrls: parsed.imageUrls
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function appendActionResultMessage(actionResult) {
|
|
91
|
+
if (!actionResult) {
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
var text = '';
|
|
95
|
+
try {
|
|
96
|
+
var result = typeof actionResult === 'string' ? JSON.parse(actionResult) : actionResult;
|
|
97
|
+
if (result && (result.code === 200 || result.success === true)) {
|
|
98
|
+
text = result.msg || result.message || '操作已成功完成';
|
|
99
|
+
} else if (result && (result.msg || result.message)) {
|
|
100
|
+
text = result.msg || result.message;
|
|
101
|
+
}
|
|
102
|
+
} catch (error) {
|
|
103
|
+
text = String(actionResult);
|
|
104
|
+
}
|
|
105
|
+
if (text) {
|
|
106
|
+
state.messages.push(createAssistantMessage(text));
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function handleAssistantAnswer(answer, apiRenderConfig) {
|
|
111
|
+
var needIcon = parser.parseNeedIcon(answer);
|
|
112
|
+
if (needIcon) {
|
|
113
|
+
state.pendingIconCategory = {
|
|
114
|
+
name: needIcon.name,
|
|
115
|
+
sort: needIcon.sort
|
|
116
|
+
};
|
|
117
|
+
state.iconPickMode = true;
|
|
118
|
+
state.renderConfig = null;
|
|
119
|
+
state.messages.push({
|
|
120
|
+
role: 'assistant',
|
|
121
|
+
content: needIcon.msg,
|
|
122
|
+
imageUrls: []
|
|
123
|
+
});
|
|
124
|
+
notifyUpdate();
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
var renderConfig = apiRenderConfig || parser.parseRenderConfig(answer);
|
|
129
|
+
if (renderConfig) {
|
|
130
|
+
state.iconPickMode = false;
|
|
131
|
+
state.pendingIconCategory = null;
|
|
132
|
+
state.renderConfig = renderConfig;
|
|
133
|
+
if (renderConfig.inputs && typeof renderConfig.inputs === 'object') {
|
|
134
|
+
state.pendingInputs = Object.assign({}, renderConfig.inputs);
|
|
135
|
+
} else if (renderConfig.type === 'confirm') {
|
|
136
|
+
state.pendingInputs = Object.assign({}, state.pendingInputs);
|
|
137
|
+
}
|
|
138
|
+
var cleanedAnswer = parser.stripMarkers(answer);
|
|
139
|
+
var formHint = renderConfig.title || renderConfig.description || '请填写以下信息';
|
|
140
|
+
state.messages.push(createAssistantMessage(cleanedAnswer || formHint));
|
|
141
|
+
notifyUpdate();
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
state.iconPickMode = false;
|
|
146
|
+
state.pendingIconCategory = null;
|
|
147
|
+
state.renderConfig = null;
|
|
148
|
+
state.messages.push(createAssistantMessage(parser.stripMarkers(answer) || answer));
|
|
149
|
+
notifyUpdate();
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function handleAssistantResponse(data) {
|
|
153
|
+
var apiRenderConfig = parser.parseApiRenderConfig(data && data.renderConfig);
|
|
154
|
+
var answer = (data && data.answer) ? data.answer : '';
|
|
155
|
+
if (!answer && apiRenderConfig) {
|
|
156
|
+
answer = apiRenderConfig.title || apiRenderConfig.description || apiRenderConfig.message || '';
|
|
157
|
+
}
|
|
158
|
+
if (!answer && !(data && data.actionResult)) {
|
|
159
|
+
answer = '暂无回复';
|
|
160
|
+
}
|
|
161
|
+
var actionResult = data && data.actionResult;
|
|
162
|
+
if (actionResult) {
|
|
163
|
+
state.renderConfig = null;
|
|
164
|
+
state.iconPickMode = false;
|
|
165
|
+
state.pendingIconCategory = null;
|
|
166
|
+
appendActionResultMessage(actionResult);
|
|
167
|
+
notifyUpdate();
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
handleAssistantAnswer(answer, apiRenderConfig);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
function setInputText(text) {
|
|
174
|
+
state.inputText = text;
|
|
175
|
+
notifyUpdate();
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
function clearPendingImage() {
|
|
179
|
+
state.pendingImageUrl = '';
|
|
180
|
+
notifyUpdate();
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
function chooseImage() {
|
|
184
|
+
if (state.loading || state.uploading || state.iconPickMode) {
|
|
185
|
+
return Promise.resolve();
|
|
186
|
+
}
|
|
187
|
+
return new Promise(function (resolve, reject) {
|
|
188
|
+
uni.chooseImage({
|
|
189
|
+
count: 1,
|
|
190
|
+
sizeType: ['compressed'],
|
|
191
|
+
sourceType: ['album', 'camera'],
|
|
192
|
+
success: function (chooseRes) {
|
|
193
|
+
var filePath = chooseRes.tempFilePaths && chooseRes.tempFilePaths[0];
|
|
194
|
+
if (!filePath) {
|
|
195
|
+
reject(new Error('未选择图片'));
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
state.uploading = true;
|
|
199
|
+
notifyUpdate();
|
|
200
|
+
requestAdapter.uploadImage(filePath)
|
|
201
|
+
.then(function (imageUrl) {
|
|
202
|
+
state.pendingImageUrl = imageUrl;
|
|
203
|
+
state.uploading = false;
|
|
204
|
+
notifyUpdate();
|
|
205
|
+
resolve(imageUrl);
|
|
206
|
+
})
|
|
207
|
+
.catch(function (error) {
|
|
208
|
+
state.uploading = false;
|
|
209
|
+
notifyUpdate();
|
|
210
|
+
reject(error);
|
|
211
|
+
});
|
|
212
|
+
},
|
|
213
|
+
fail: function (err) {
|
|
214
|
+
reject(err || new Error('选择图片失败'));
|
|
215
|
+
}
|
|
216
|
+
});
|
|
217
|
+
});
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
function pickCategoryIcon(source) {
|
|
221
|
+
if (state.loading || state.uploading || !state.pendingIconCategory) {
|
|
222
|
+
return Promise.resolve();
|
|
223
|
+
}
|
|
224
|
+
if (!ensureLogin()) {
|
|
225
|
+
return Promise.resolve();
|
|
226
|
+
}
|
|
227
|
+
var sourceType = source === 'camera' ? ['camera'] : ['album'];
|
|
228
|
+
return new Promise(function (resolve, reject) {
|
|
229
|
+
uni.chooseImage({
|
|
230
|
+
count: 1,
|
|
231
|
+
sizeType: ['compressed'],
|
|
232
|
+
sourceType: sourceType,
|
|
233
|
+
success: function (chooseRes) {
|
|
234
|
+
var filePath = chooseRes.tempFilePaths && chooseRes.tempFilePaths[0];
|
|
235
|
+
if (!filePath) {
|
|
236
|
+
reject(new Error('未选择图片'));
|
|
237
|
+
return;
|
|
238
|
+
}
|
|
239
|
+
state.uploading = true;
|
|
240
|
+
notifyUpdate();
|
|
241
|
+
requestAdapter.uploadImage(filePath)
|
|
242
|
+
.then(function (imageUrl) {
|
|
243
|
+
var category = state.pendingIconCategory;
|
|
244
|
+
var query = '新增一级分类:' + category.name + ',排序' + category.sort + ',图标 ' + imageUrl;
|
|
245
|
+
state.iconPickMode = false;
|
|
246
|
+
state.pendingIconCategory = null;
|
|
247
|
+
state.uploading = false;
|
|
248
|
+
notifyUpdate();
|
|
249
|
+
return sendMessage(query, imageUrl, '[图片] ' + category.name);
|
|
250
|
+
})
|
|
251
|
+
.then(function () {
|
|
252
|
+
resolve();
|
|
253
|
+
})
|
|
254
|
+
.catch(function (error) {
|
|
255
|
+
state.uploading = false;
|
|
256
|
+
notifyUpdate();
|
|
257
|
+
reject(error);
|
|
258
|
+
});
|
|
259
|
+
},
|
|
260
|
+
fail: function (err) {
|
|
261
|
+
reject(err || new Error('选择图片失败'));
|
|
262
|
+
}
|
|
263
|
+
});
|
|
264
|
+
});
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
function cancelIconPick() {
|
|
268
|
+
state.iconPickMode = false;
|
|
269
|
+
state.pendingIconCategory = null;
|
|
270
|
+
notifyUpdate();
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
function sendQuick(text) {
|
|
274
|
+
state.inputText = text;
|
|
275
|
+
notifyUpdate();
|
|
276
|
+
return handleSend();
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
var DIFY_TEXT_INPUT_KEYS = ['confirmed', 'intent', 'name', 'parent_name', 'icon', 'productName', 'mainImg', 'unit'];
|
|
280
|
+
var DIFY_NUMBER_INPUT_KEYS = ['level', 'pid', 'sort', 'threeCategoryId', 'price', 'stock', 'productId'];
|
|
281
|
+
|
|
282
|
+
function normalizeDifyInputs(inputs) {
|
|
283
|
+
if (!inputs || typeof inputs !== 'object') {
|
|
284
|
+
return {};
|
|
285
|
+
}
|
|
286
|
+
var normalized = Object.assign({}, inputs);
|
|
287
|
+
DIFY_TEXT_INPUT_KEYS.forEach(function (key) {
|
|
288
|
+
if (normalized[key] === undefined || normalized[key] === null) {
|
|
289
|
+
return;
|
|
290
|
+
}
|
|
291
|
+
if (key === 'confirmed') {
|
|
292
|
+
normalized[key] = normalized[key] === true || normalized[key] === 'true' ? 'true' : 'false';
|
|
293
|
+
return;
|
|
294
|
+
}
|
|
295
|
+
normalized[key] = String(normalized[key]);
|
|
296
|
+
});
|
|
297
|
+
DIFY_NUMBER_INPUT_KEYS.forEach(function (key) {
|
|
298
|
+
if (normalized[key] === undefined || normalized[key] === null || normalized[key] === '') {
|
|
299
|
+
return;
|
|
300
|
+
}
|
|
301
|
+
var num = Number(normalized[key]);
|
|
302
|
+
if (!isNaN(num)) {
|
|
303
|
+
normalized[key] = num;
|
|
304
|
+
}
|
|
305
|
+
});
|
|
306
|
+
return normalized;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
function doSendChat(payload) {
|
|
310
|
+
state.loading = true;
|
|
311
|
+
notifyUpdate();
|
|
312
|
+
if (payload && payload.inputs) {
|
|
313
|
+
payload.inputs = normalizeDifyInputs(payload.inputs);
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
return requestAdapter.sendChat(payload)
|
|
317
|
+
.then(function (res) {
|
|
318
|
+
var data = res && res.data ? res.data : res;
|
|
319
|
+
if (data.conversationId) {
|
|
320
|
+
state.conversationId = data.conversationId;
|
|
321
|
+
}
|
|
322
|
+
state.loading = false;
|
|
323
|
+
handleAssistantResponse(data);
|
|
324
|
+
notifyUpdate();
|
|
325
|
+
})
|
|
326
|
+
.catch(function (error) {
|
|
327
|
+
state.loading = false;
|
|
328
|
+
state.messages.push(createAssistantMessage(error && (error.msg || error.message) ? (error.msg || error.message) : '发送失败,请稍后重试'));
|
|
329
|
+
notifyUpdate();
|
|
330
|
+
});
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
function sendMessage(query, imageUrl, displayContent) {
|
|
334
|
+
if (!ensureLogin()) {
|
|
335
|
+
return Promise.resolve();
|
|
336
|
+
}
|
|
337
|
+
state.lastUserQuery = displayContent || query || '';
|
|
338
|
+
state.messages.push({
|
|
339
|
+
role: 'user',
|
|
340
|
+
content: displayContent || query,
|
|
341
|
+
imageUrls: imageUrl ? [imageUrl] : []
|
|
342
|
+
});
|
|
343
|
+
|
|
344
|
+
return doSendChat({
|
|
345
|
+
conversationId: state.conversationId || undefined,
|
|
346
|
+
query: query || undefined,
|
|
347
|
+
imageUrl: imageUrl || undefined,
|
|
348
|
+
inputs: state.pendingInputs || {}
|
|
349
|
+
}).then(function () {
|
|
350
|
+
state.pendingInputs = {};
|
|
351
|
+
});
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
function validateFormFields(fields, inputs) {
|
|
355
|
+
var missing = [];
|
|
356
|
+
(fields || []).forEach(function (field) {
|
|
357
|
+
if (!field || field.type === 'hidden' || !field.required) {
|
|
358
|
+
return;
|
|
359
|
+
}
|
|
360
|
+
var val = inputs[field.name];
|
|
361
|
+
if (val === undefined || val === null || val === '') {
|
|
362
|
+
missing.push(field.label || field.name);
|
|
363
|
+
}
|
|
364
|
+
});
|
|
365
|
+
return missing;
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
function submitInputs(inputs) {
|
|
369
|
+
inputs = inputs || {};
|
|
370
|
+
if (state.renderConfig && state.renderConfig.type === 'form' && state.renderConfig.fields) {
|
|
371
|
+
var intent = inputs.intent || (state.pendingInputs && state.pendingInputs.intent);
|
|
372
|
+
if (intent === 'product_online' || intent === 'product_offline') {
|
|
373
|
+
if (!inputs.productName && !inputs.productId) {
|
|
374
|
+
uni.showToast({
|
|
375
|
+
title: '请填写商品名称或商品ID',
|
|
376
|
+
icon: 'none'
|
|
377
|
+
});
|
|
378
|
+
return Promise.resolve();
|
|
379
|
+
}
|
|
380
|
+
} else {
|
|
381
|
+
var missing = validateFormFields(state.renderConfig.fields, inputs);
|
|
382
|
+
if (missing.length) {
|
|
383
|
+
uni.showToast({
|
|
384
|
+
title: '请填写:' + missing.join('、'),
|
|
385
|
+
icon: 'none'
|
|
386
|
+
});
|
|
387
|
+
return Promise.resolve();
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
var hasOwn = Object.prototype.hasOwnProperty;
|
|
392
|
+
for (var key in inputs) {
|
|
393
|
+
if (hasOwn.call(inputs, key)) {
|
|
394
|
+
state.pendingInputs[key] = inputs[key];
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
state.renderConfig = null;
|
|
398
|
+
state.pendingImageUrl = '';
|
|
399
|
+
notifyUpdate();
|
|
400
|
+
|
|
401
|
+
if (!ensureLogin()) {
|
|
402
|
+
return Promise.resolve();
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
return doSendChat({
|
|
406
|
+
conversationId: state.conversationId || undefined,
|
|
407
|
+
query: state.lastUserQuery || undefined,
|
|
408
|
+
inputs: state.pendingInputs
|
|
409
|
+
}).then(function () {
|
|
410
|
+
state.pendingInputs = {};
|
|
411
|
+
});
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
function cancelForm() {
|
|
415
|
+
state.renderConfig = null;
|
|
416
|
+
state.pendingInputs = {};
|
|
417
|
+
state.pendingImageUrl = '';
|
|
418
|
+
notifyUpdate();
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
function submitConfirm(confirmed) {
|
|
422
|
+
if (!state.renderConfig || state.renderConfig.type !== 'confirm') {
|
|
423
|
+
return Promise.resolve();
|
|
424
|
+
}
|
|
425
|
+
if (!confirmed) {
|
|
426
|
+
cancelForm();
|
|
427
|
+
return Promise.resolve();
|
|
428
|
+
}
|
|
429
|
+
var inputs = Object.assign(
|
|
430
|
+
{},
|
|
431
|
+
state.pendingInputs,
|
|
432
|
+
state.renderConfig.inputs || {},
|
|
433
|
+
{ confirmed: 'true' }
|
|
434
|
+
);
|
|
435
|
+
return submitInputs(inputs);
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
function handleSend() {
|
|
439
|
+
var query = (state.inputText || '').trim();
|
|
440
|
+
var imageUrl = state.pendingImageUrl;
|
|
441
|
+
if ((!query && !imageUrl) || state.loading || state.uploading || state.iconPickMode || state.renderConfig) {
|
|
442
|
+
return Promise.resolve();
|
|
443
|
+
}
|
|
444
|
+
var displayContent = query || (imageUrl ? '[图片]' : '');
|
|
445
|
+
state.inputText = '';
|
|
446
|
+
state.pendingImageUrl = '';
|
|
447
|
+
notifyUpdate();
|
|
448
|
+
return sendMessage(query, imageUrl, displayContent);
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
function openConversation() {
|
|
452
|
+
if (!ensureLogin()) {
|
|
453
|
+
return false;
|
|
454
|
+
}
|
|
455
|
+
event.emit('chat:open');
|
|
456
|
+
return true;
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
function closeConversation() {
|
|
460
|
+
event.emit('chat:close');
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
module.exports = {
|
|
464
|
+
quickQuestions: quickQuestions,
|
|
465
|
+
reset: reset,
|
|
466
|
+
getSnapshot: getSnapshot,
|
|
467
|
+
setInputText: setInputText,
|
|
468
|
+
clearPendingImage: clearPendingImage,
|
|
469
|
+
chooseImage: chooseImage,
|
|
470
|
+
pickCategoryIcon: pickCategoryIcon,
|
|
471
|
+
cancelIconPick: cancelIconPick,
|
|
472
|
+
submitInputs: submitInputs,
|
|
473
|
+
cancelForm: cancelForm,
|
|
474
|
+
submitConfirm: submitConfirm,
|
|
475
|
+
sendQuick: sendQuick,
|
|
476
|
+
handleSend: handleSend,
|
|
477
|
+
openConversation: openConversation,
|
|
478
|
+
closeConversation: closeConversation,
|
|
479
|
+
onUpdate: function (handler) {
|
|
480
|
+
event.on('chat:update', handler);
|
|
481
|
+
},
|
|
482
|
+
offUpdate: function (handler) {
|
|
483
|
+
event.off('chat:update', handler);
|
|
484
|
+
},
|
|
485
|
+
onOpen: function (handler) {
|
|
486
|
+
event.on('chat:open', handler);
|
|
487
|
+
},
|
|
488
|
+
offOpen: function (handler) {
|
|
489
|
+
event.off('chat:open', handler);
|
|
490
|
+
},
|
|
491
|
+
onClose: function (handler) {
|
|
492
|
+
event.on('chat:close', handler);
|
|
493
|
+
},
|
|
494
|
+
offClose: function (handler) {
|
|
495
|
+
event.off('chat:close', handler);
|
|
496
|
+
}
|
|
497
|
+
};
|