Skip to content

Commit 5c21561

Browse files
committed
fix: 支持request 域名校验
1 parent 5530142 commit 5c21561

File tree

6 files changed

+80
-37
lines changed

6 files changed

+80
-37
lines changed

apps/miniprogram-agent-ui/miniprogram/app.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ App({
1010
// env 参数决定接下来小程序发起的云开发调用(wx.cloud.xxx)会默认请求到哪个云环境的资源
1111
// 此处请填入环境 ID, 环境 ID 可打开云控制台查看
1212
// 如不填则使用默认环境(第一个创建的环境)
13-
env: "luke-agent-dev-7g1nc8tqc2ab76af",
13+
env: "luke-personal-test-new-8d0d90f5f",
1414
traceUser: true,
1515
});
1616
}

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/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+
}

apps/miniprogram-agent-ui/miniprogram/pages/chatBot/chatBot.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Page({
1414
// resourceEnv: "chriscc-demo-7ghlpjf846d46d2d",
1515
// },
1616
agentConfig: {
17-
botId: "bot-db3cab4a", // agent id,
17+
botId: "bot-c5167aab", // agent id,
1818
allowWebSearch: true, // 允许客户端选择启用联网搜索
1919
allowUploadFile: true, // 允许上传文件
2020
allowPullRefresh: true, // 允许下拉刷新

0 commit comments

Comments
 (0)