Skip to content

Commit 9a930cc

Browse files
authored
Merge pull request #41 from TencentCloudBase/dev
Dev
2 parents 5530142 + 9180db1 commit 9a930cc

File tree

5 files changed

+87
-39
lines changed

5 files changed

+87
-39
lines changed

apps/miniprogram-agent-ui/miniprogram/components/agent-ui/chatFile/index.js

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
// components/agent-ui-new/chatFIle/chatFile.js
2-
import { getCloudInstance, compareVersions } from "../tools";
2+
import { getCloudInstance, compareVersions, commonRequest } from "../tools";
33
Component({
44
lifetimes: {
55
attached: async function () {
66
console.log("enableDel", this.data.enableDel);
7-
const { tempFileName, rawFileName, rawType, tempPath, fileId, botId, parsed } = this.data.fileData;
7+
const { tempFileName, rawFileName, rawType, tempPath, fileId, botId, status } = this.data.fileData;
88
const type = this.getFileType(rawFileName || tempFileName);
99
console.log("type", type);
1010
if (!fileId) {
1111
this.setData({
12-
formatSize: "上传中",
1312
iconPath: "../imgs/" + type + ".svg",
1413
});
14+
15+
this.triggerEvent("changeChild", { tempId: this.data.fileData.tempId, status: "uploading" });
1516
}
1617

17-
if (fileId && parsed) {
18+
if (fileId && status === "parsed") {
1819
this.setData({
19-
formatSize: this.transformSize(this.data.fileData.fileSize),
2020
iconPath: "../imgs/" + type + ".svg",
2121
});
2222
return;
@@ -34,15 +34,13 @@ Component({
3434
success: async (res) => {
3535
const appBaseInfo = wx.getAppBaseInfo();
3636
const fileId = res.fileID;
37-
this.setData({
38-
formatSize: "解析中",
39-
});
37+
this.triggerEvent("changeChild", { tempId: this.data.fileData.tempId, status: "parsing" });
4038

4139
console.log("当前版本", appBaseInfo.SDKVersion);
4240
// 3.8.1 及以上版本走sdk 内置方法
4341
if (compareVersions(appBaseInfo.SDKVersion, "3.8.1") < 0) {
4442
const { token } = await cloudInstance.extend.AI.bot.tokenManager.getToken();
45-
wx.request({
43+
commonRequest({
4644
url: `https://${
4745
cloudInstance.env || cloudInstance.extend.AI.bot.context.env
4846
}.api.tcloudbasegateway.com/v1/aibot/bots/${botId}/files`,
@@ -61,13 +59,11 @@ Component({
6159
method: "POST",
6260
success: (res) => {
6361
console.log("old resolve agent file res", res);
64-
this.setData({
65-
formatSize: this.transformSize(this.data.fileData.fileSize),
66-
});
67-
this.triggerEvent("changeChild", { tempId: this.data.fileData.tempId, fileId, parsed: true });
62+
this.triggerEvent("changeChild", { tempId: this.data.fileData.tempId, fileId, status: "parsed" });
6863
},
69-
fail(e) {
64+
fail: (e) => {
7065
console.log("resolve agent file e", e);
66+
this.triggerEvent("changeChild", { tempId: this.data.fileData.tempId, fileId, status: "parseFailed" });
7167
},
7268
});
7369
} else {
@@ -87,13 +83,11 @@ Component({
8783
timeout: 30000,
8884
success: (res) => {
8985
console.log("resolve agent file res", res);
90-
this.setData({
91-
formatSize: this.transformSize(this.data.fileData.fileSize),
92-
});
93-
this.triggerEvent("changeChild", { tempId: this.data.fileData.tempId, fileId, parsed: true });
86+
this.triggerEvent("changeChild", { tempId: this.data.fileData.tempId, fileId, status: "parsed" });
9487
},
9588
fail: (e) => {
9689
console.log("e", e);
90+
this.triggerEvent("changeChild", { tempId: this.data.fileData.tempId, fileId, status: "parseFailed" });
9791
},
9892
complete: () => {},
9993
header: {},
@@ -107,14 +101,9 @@ Component({
107101
},
108102
},
109103
observers: {
110-
"fileData.fileId": function (fileId) {
111-
this.setData({
112-
formatSize: fileId ? this.transformSize(this.data.fileData.fileSize) : "上传中",
113-
});
114-
},
115-
"fileData.parsed": function (parsed) {
104+
"fileData.status": function (status) {
116105
this.setData({
117-
formatSize: parsed ? this.transformSize(this.data.fileData.fileSize) : "解析中",
106+
statusTxt: this.getFormatStatusText(status),
118107
});
119108
},
120109
},
@@ -137,7 +126,7 @@ Component({
137126
fileSize: 0,
138127
fileUrl: "",
139128
fileId: "",
140-
parsed: false,
129+
status: "",
141130
},
142131
},
143132
},
@@ -148,11 +137,23 @@ Component({
148137
data: {
149138
formatSize: "",
150139
iconPath: "../imgs/file.svg",
140+
statusTextMap: {
141+
uploading: "上传中",
142+
parsing: "解析中",
143+
parseFailed: "解析失败",
144+
},
145+
statusTxt: "",
151146
},
152147
/**
153148
* 组件的方法列表,
154149
*/
155150
methods: {
151+
getFormatStatusText: function (status) {
152+
if (status === "parsed") {
153+
return this.transformSize(this.data.fileData.fileSize);
154+
}
155+
return this.data.statusTextMap[status] || "";
156+
},
156157
generateCosUploadPath: function (botId, fileName) {
157158
return `agent_file/${botId}/${fileName}`;
158159
},

apps/miniprogram-agent-ui/miniprogram/components/agent-ui/chatFile/index.wxml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
<image class="chat_file__icon" src="{{iconPath}}" />
66
<view class="chat_file__info">
77
<view class="chat_file__name">{{fileData.rawFileName || fileData.tempFileName}}</view>
8-
<view class="chat_file__size">{{formatSize}}
9-
<image wx:if="{{!fileData.fileId || !fileData.parsed}}" style="width: 15px;height:15px;margin-left: 5px" src="../imgs/loading.svg" mode=""/>
8+
<view class="chat_file__size">{{statusTxt}}
9+
<image wx:if="{{fileData.status === 'uploading' || fileData.status === 'parsing'}}" style="width: 15px;height:15px;margin-left: 5px" src="../imgs/loading.svg" mode=""/>
1010
</view>
1111
</view>
1212
</view>

apps/miniprogram-agent-ui/miniprogram/components/agent-ui/index.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ Component({
460460
};
461461
if (item.role === "user" && item.fileInfos) {
462462
transformItem.fileList = item.fileInfos.map((item) => ({
463-
parsed: true,
463+
status: "parsed",
464464
rawFileName: item.fileName,
465465
rawType: item.type,
466466
fileId: item.cloudId,
@@ -569,7 +569,7 @@ Component({
569569
fileUrl: "",
570570
fileId: "",
571571
botId: self.data.agentConfig.botId,
572-
parsed: false,
572+
status: "",
573573
};
574574
});
575575

@@ -679,7 +679,7 @@ Component({
679679
fileUrl: "",
680680
fileId: "",
681681
botId: self.data.agentConfig.botId,
682-
parsed: false,
682+
status: "",
683683
};
684684
});
685685
// 过滤掉已选择中的 image 文件(保留file)
@@ -780,7 +780,7 @@ Component({
780780
},
781781
handleSendMessage: async function (event) {
782782
// 发送消息前校验所有文件上传状态
783-
if (this.data.sendFileList.some((item) => !item.fileId || !item.parsed)) {
783+
if (this.data.sendFileList.some((item) => !item.fileId || item.status !== "parsed")) {
784784
wx.showToast({
785785
title: "文件上传解析中",
786786
icon: "error",
@@ -1255,15 +1255,21 @@ Component({
12551255
},
12561256
handleChangeChild: function (e) {
12571257
console.log("change", e.detail);
1258-
const { fileId, tempId, parsed } = e.detail;
1258+
const { fileId, tempId, status } = e.detail;
12591259
// const curFile = this.data.sendFileList.find(item => item.tempId === tempId)
12601260
// curFile.fileId = fileId
12611261
const newSendFileList = this.data.sendFileList.map((item) => {
12621262
if (item.tempId === tempId) {
1263+
const obj = {}
1264+
if(fileId) {
1265+
obj.fileId = fileId
1266+
}
1267+
if(status) {
1268+
obj.status = status
1269+
}
12631270
return {
12641271
...item,
1265-
fileId,
1266-
parsed,
1272+
...obj
12671273
};
12681274
}
12691275
return item;

apps/miniprogram-agent-ui/miniprogram/components/agent-ui/index.wxss

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
flex-direction: column;
88
overflow:hidden;
99
color: #333;
10+
background-color: #fff;
1011
}
1112

1213
.nav {
@@ -19,7 +20,7 @@
1920
}
2021

2122
.navBar {
22-
height: 72px;
23+
height: 62px;
2324
width: 100%;
2425
box-shadow: 0 16px 16px #fff;
2526
/* background: linear-gradient(to bottom,
@@ -70,7 +71,7 @@
7071
}
7172

7273
.nav-content {
73-
height:100%;
74+
height:62px;
7475
display: flex;
7576
align-items: center;
7677
justify-content: space-around;
@@ -128,7 +129,7 @@
128129

129130
.footer {
130131
width: 100%;
131-
min-height: 80px;
132+
min-height: 65px;
132133
max-height: 380px;
133134
flex-shrink: 0;
134135
/* background-color: aquamarine; */
@@ -488,14 +489,18 @@
488489
flex-wrap: nowrap;
489490
overflow-x: scroll;
490491
/* margin-bottom: 15px; */
491-
padding: 20rpx 0rpx;
492+
padding: 20rpx 0rpx 0rpx;
492493
}
493494

494495
.tool_box .function {
495496
/* flex: 0 0 calc(25% - 20px); */
496497
background-color: #f3f4f6;
497498
}
498499

500+
.function:active {
501+
transform: scale(0.95);
502+
}
503+
499504
.webSearchSwitch {
500505
width: 200rpx;
501506
height: 30px;

apps/miniprogram-agent-ui/miniprogram/components/agent-ui/tools.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,39 @@ export const compareVersions = (version1, version2) => {
8989
}
9090
return 0;
9191
};
92+
93+
let isDomainWarn = false
94+
95+
export const commonRequest = (options) => {
96+
const self = this
97+
return wx.request({
98+
...options,
99+
fail: (e) => {
100+
if(options.fail) {
101+
options.fail.bind(self)(e)
102+
if(e.errno === 600002 || e.errMsg.includes("url not in domain list")) {
103+
const { url } = options
104+
let msg = `请前往微信公众平台 request 合法域名配置中添加云开发域名 https://{envid}.api.tcloudbasegateway.com`
105+
if(url) {
106+
const regex = /^(https?:\/\/[^/?#]+)/i;
107+
const matches = url.match(regex);
108+
console.log('matches', matches)
109+
if(matches[1]) {
110+
msg = `请前往微信公众平台 request 合法域名配置中添加云开发域名 ${matches[1]}`
111+
}
112+
}
113+
if(!isDomainWarn) {
114+
isDomainWarn = true
115+
wx.showModal({
116+
title: "提示",
117+
content: msg,
118+
complete: () => {
119+
isDomainWarn = false
120+
}
121+
})
122+
}
123+
}
124+
}
125+
}
126+
})
127+
}

0 commit comments

Comments
 (0)