|
1 | 1 | // components/agent-ui-new/chatFIle/chatFile.js |
2 | | -import {getCloudInstance} from '../tools' |
| 2 | +import { getCloudInstance, compareVersions } from "../tools"; |
3 | 3 | Component({ |
4 | 4 | lifetimes: { |
5 | 5 | attached: async function () { |
6 | | - console.log('enableDel', this.data.enableDel) |
7 | | - const { tempFileName, rawFileName, rawType, tempPath, fileId, botId, parsed } = this.data.fileData |
8 | | - const type = this.getFileType(rawFileName || tempFileName) |
9 | | - console.log('type', type) |
| 6 | + console.log("enableDel", this.data.enableDel); |
| 7 | + const { tempFileName, rawFileName, rawType, tempPath, fileId, botId, parsed } = this.data.fileData; |
| 8 | + const type = this.getFileType(rawFileName || tempFileName); |
| 9 | + console.log("type", type); |
10 | 10 | if (!fileId) { |
11 | 11 | this.setData({ |
12 | | - formatSize: '上传中', |
13 | | - iconPath: '../imgs/' + type + '.svg' |
14 | | - }) |
| 12 | + formatSize: "上传中", |
| 13 | + iconPath: "../imgs/" + type + ".svg", |
| 14 | + }); |
15 | 15 | } |
16 | 16 |
|
17 | 17 | if (fileId && parsed) { |
18 | 18 | this.setData({ |
19 | 19 | formatSize: this.transformSize(this.data.fileData.fileSize), |
20 | | - iconPath: '../imgs/' + type + '.svg' |
21 | | - }) |
22 | | - return |
| 20 | + iconPath: "../imgs/" + type + ".svg", |
| 21 | + }); |
| 22 | + return; |
23 | 23 | } |
24 | | - const cloudInstance= await getCloudInstance() |
| 24 | + const cloudInstance = await getCloudInstance(); |
25 | 25 | // console.log('file', cloudInstance) |
26 | 26 | // 上传云存储获取 fileId |
27 | 27 | // console.log('rawFileName tempFileName tempPath', rawFileName, tempFileName, tempPath) |
28 | 28 | cloudInstance.uploadFile({ |
29 | | - cloudPath: this.generateCosUploadPath(botId, rawFileName ? (rawFileName.split('.')[0] + '-' + tempFileName) : tempFileName), // 云上文件路径 |
| 29 | + cloudPath: this.generateCosUploadPath( |
| 30 | + botId, |
| 31 | + rawFileName ? rawFileName.split(".")[0] + "-" + tempFileName : tempFileName |
| 32 | + ), // 云上文件路径 |
30 | 33 | filePath: tempPath, |
31 | | - success: async res => { |
32 | | - const { token } = await cloudInstance.extend.AI.bot.tokenManager.getToken() |
33 | | - const fileId = res.fileID |
| 34 | + success: async (res) => { |
| 35 | + const appBaseInfo = wx.getAppBaseInfo(); |
| 36 | + const fileId = res.fileID; |
34 | 37 | this.setData({ |
35 | | - formatSize: '解析中', |
36 | | - }) |
37 | | - wx.request({ |
38 | | - url: `https://${cloudInstance.env||cloudInstance.extend.AI.bot.context.env}.api.tcloudbasegateway.com/v1/aibot/bots/${botId}/files`, |
39 | | - data: { |
40 | | - fileList: [{ |
41 | | - fileName: rawFileName || tempFileName, |
42 | | - fileId, |
43 | | - type: rawType |
44 | | - }] |
45 | | - }, |
46 | | - header: { |
47 | | - Authorization: `Bearer ${token}` |
48 | | - }, |
49 | | - method: 'POST', |
50 | | - success: (res) => { |
51 | | - console.log('resolve agent file res', res) |
52 | | - this.setData({ |
53 | | - formatSize: this.transformSize(this.data.fileData.fileSize), |
54 | | - }) |
55 | | - this.triggerEvent('changeChild', { tempId: this.data.fileData.tempId, fileId, parsed: true }) |
56 | | - }, |
57 | | - fail(e) { |
58 | | - console.log('resolve agent file e', e) |
59 | | - } |
60 | | - }) |
| 38 | + formatSize: "解析中", |
| 39 | + }); |
| 40 | + |
| 41 | + console.log("当前版本", appBaseInfo.SDKVersion); |
| 42 | + // 3.8.1 及以上版本走sdk 内置方法 |
| 43 | + if (compareVersions(appBaseInfo.SDKVersion, "3.8.1") < 0) { |
| 44 | + const { token } = await cloudInstance.extend.AI.bot.tokenManager.getToken(); |
| 45 | + wx.request({ |
| 46 | + url: `https://${ |
| 47 | + cloudInstance.env || cloudInstance.extend.AI.bot.context.env |
| 48 | + }.api.tcloudbasegateway.com/v1/aibot/bots/${botId}/files`, |
| 49 | + data: { |
| 50 | + fileList: [ |
| 51 | + { |
| 52 | + fileName: rawFileName || tempFileName, |
| 53 | + fileId, |
| 54 | + type: rawType, |
| 55 | + }, |
| 56 | + ], |
| 57 | + }, |
| 58 | + header: { |
| 59 | + Authorization: `Bearer ${token}`, |
| 60 | + }, |
| 61 | + method: "POST", |
| 62 | + success: (res) => { |
| 63 | + console.log("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 }); |
| 68 | + }, |
| 69 | + fail(e) { |
| 70 | + console.log("resolve agent file e", e); |
| 71 | + }, |
| 72 | + }); |
| 73 | + } else { |
| 74 | + const ai = cloudInstance.extend.AI; |
| 75 | + ai.request({ |
| 76 | + path: `bots/${botId}/files`, // 填写 "v1/aibot/" 后面的内容 |
| 77 | + data: { |
| 78 | + fileList: [ |
| 79 | + { |
| 80 | + fileName: rawFileName || tempFileName, |
| 81 | + fileId, |
| 82 | + type: rawType, |
| 83 | + }, |
| 84 | + ], |
| 85 | + }, // any |
| 86 | + method: "POST", |
| 87 | + timeout: 30000, |
| 88 | + success: (res) => { |
| 89 | + 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 }); |
| 94 | + }, |
| 95 | + fail: (e) => { |
| 96 | + console.log("e", e); |
| 97 | + }, |
| 98 | + complete: () => {}, |
| 99 | + header: {}, |
| 100 | + }); |
| 101 | + } |
| 102 | + }, |
| 103 | + fail: (err) => { |
| 104 | + console.error("上传失败:", err); |
61 | 105 | }, |
62 | | - fail: err => { |
63 | | - console.error('上传失败:', err); |
64 | | - } |
65 | 106 | }); |
66 | | - } |
| 107 | + }, |
67 | 108 | }, |
68 | 109 | observers: { |
69 | | - 'fileData.fileId': function (fileId) { |
| 110 | + "fileData.fileId": function (fileId) { |
70 | 111 | this.setData({ |
71 | | - formatSize: fileId ? this.transformSize(this.data.fileData.fileSize) : '上传中' |
72 | | - }) |
| 112 | + formatSize: fileId ? this.transformSize(this.data.fileData.fileSize) : "上传中", |
| 113 | + }); |
73 | 114 | }, |
74 | | - 'fileData.parsed': function (parsed) { |
| 115 | + "fileData.parsed": function (parsed) { |
75 | 116 | this.setData({ |
76 | | - formatSize: parsed ? this.transformSize(this.data.fileData.fileSize) : '解析中' |
77 | | - }) |
78 | | - } |
| 117 | + formatSize: parsed ? this.transformSize(this.data.fileData.fileSize) : "解析中", |
| 118 | + }); |
| 119 | + }, |
79 | 120 | }, |
80 | 121 | /** |
81 | 122 | * 组件的属性列表 |
82 | 123 | */ |
83 | 124 | properties: { |
84 | 125 | enableDel: { |
85 | 126 | type: Boolean, |
86 | | - value: false |
| 127 | + value: false, |
87 | 128 | }, |
88 | 129 | fileData: { |
89 | 130 | type: Object, |
90 | 131 | value: { |
91 | | - tempId: '', |
92 | | - rawType: '', |
93 | | - tempFileName: '', |
94 | | - rawFileName: '', |
95 | | - tempPath: '', |
| 132 | + tempId: "", |
| 133 | + rawType: "", |
| 134 | + tempFileName: "", |
| 135 | + rawFileName: "", |
| 136 | + tempPath: "", |
96 | 137 | fileSize: 0, |
97 | | - fileUrl: '', |
98 | | - fileId: '', |
99 | | - parsed: false |
100 | | - } |
101 | | - } |
| 138 | + fileUrl: "", |
| 139 | + fileId: "", |
| 140 | + parsed: false, |
| 141 | + }, |
| 142 | + }, |
102 | 143 | }, |
103 | 144 |
|
104 | 145 | /** |
105 | 146 | * 组件的初始数据 |
106 | 147 | */ |
107 | 148 | data: { |
108 | | - formatSize: '', |
109 | | - iconPath: '../imgs/file.svg' |
| 149 | + formatSize: "", |
| 150 | + iconPath: "../imgs/file.svg", |
110 | 151 | }, |
111 | 152 | /** |
112 | 153 | * 组件的方法列表, |
113 | 154 | */ |
114 | 155 | methods: { |
115 | 156 | generateCosUploadPath: function (botId, fileName) { |
116 | | - return `agent_file/${botId}/${fileName}` |
| 157 | + return `agent_file/${botId}/${fileName}`; |
117 | 158 | }, |
118 | 159 | // 提取文件后缀 |
119 | 160 | getFileType: function (fileName) { |
120 | | - let index = fileName.lastIndexOf('.'); |
| 161 | + let index = fileName.lastIndexOf("."); |
121 | 162 | const fileExt = fileName.substring(index + 1); |
122 | | - if (fileExt === 'docx' || fileExt === 'doc') { |
123 | | - return 'word' |
| 163 | + if (fileExt === "docx" || fileExt === "doc") { |
| 164 | + return "word"; |
124 | 165 | } |
125 | | - if (fileExt === 'xlsx' || fileExt === 'xls' || fileExt === 'csv') { |
126 | | - return 'excel' |
| 166 | + if (fileExt === "xlsx" || fileExt === "xls" || fileExt === "csv") { |
| 167 | + return "excel"; |
127 | 168 | } |
128 | | - if (fileExt === 'png' || fileExt === 'jpg' || fileExt === 'jpeg' || fileExt === 'svg') { |
129 | | - return 'image' |
| 169 | + if (fileExt === "png" || fileExt === "jpg" || fileExt === "jpeg" || fileExt === "svg") { |
| 170 | + return "image"; |
130 | 171 | } |
131 | 172 |
|
132 | | - if (fileExt === 'ppt' || fileExt === 'pptx') { |
133 | | - return 'ppt' |
| 173 | + if (fileExt === "ppt" || fileExt === "pptx") { |
| 174 | + return "ppt"; |
134 | 175 | } |
135 | 176 |
|
136 | | - if (fileExt === 'pdf') { |
137 | | - return 'pdf' |
| 177 | + if (fileExt === "pdf") { |
| 178 | + return "pdf"; |
138 | 179 | } |
139 | | - return 'file' |
| 180 | + return "file"; |
140 | 181 | }, |
141 | 182 | // 转换文件大小(原始单位为B) |
142 | 183 | transformSize: function (size) { |
143 | 184 | if (size < 1024) { |
144 | | - return size + 'B'; |
| 185 | + return size + "B"; |
145 | 186 | } else if (size < 1024 * 1024) { |
146 | | - return (size / 1024).toFixed(2) + 'KB'; |
| 187 | + return (size / 1024).toFixed(2) + "KB"; |
147 | 188 | } else { |
148 | | - return (size / 1024 / 1024).toFixed(2) + 'MB'; |
| 189 | + return (size / 1024 / 1024).toFixed(2) + "MB"; |
149 | 190 | } |
150 | 191 | }, |
151 | 192 | removeFileFromParents: function () { |
152 | | - console.log('remove', this.data.fileData) |
153 | | - this.triggerEvent('removeChild', { tempId: this.data.fileData.tempId }) |
| 193 | + console.log("remove", this.data.fileData); |
| 194 | + this.triggerEvent("removeChild", { tempId: this.data.fileData.tempId }); |
154 | 195 | }, |
155 | 196 | openFileByWx: function (tempPath) { |
156 | | - const fileExt = tempPath.split('.')[1] |
157 | | - if (['doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'pdf'].includes(fileExt)) { |
| 197 | + const fileExt = tempPath.split(".")[1]; |
| 198 | + if (["doc", "docx", "xls", "xlsx", "ppt", "pptx", "pdf"].includes(fileExt)) { |
158 | 199 | wx.openDocument({ |
159 | 200 | filePath: tempPath, |
160 | 201 | success: function (res) { |
161 | | - console.log('打开文档成功') |
| 202 | + console.log("打开文档成功"); |
162 | 203 | }, |
163 | 204 | fail: function (err) { |
164 | | - console.log('打开文档失败', err) |
165 | | - } |
166 | | - }) |
| 205 | + console.log("打开文档失败", err); |
| 206 | + }, |
| 207 | + }); |
167 | 208 | } else { |
168 | 209 | wx.showModal({ |
169 | 210 | content: "当前支持预览文件类型为 pdf、doc、docx、ppt、pptx、xls、xlsx", |
170 | 211 | showCancel: false, |
171 | | - confirmText: '确定' |
172 | | - }) |
| 212 | + confirmText: "确定", |
| 213 | + }); |
173 | 214 | } |
174 | 215 | }, |
175 | 216 | previewImageByWx: function (fileId) { |
176 | 217 | wx.previewImage({ |
177 | 218 | urls: [fileId], |
178 | 219 | showmenu: true, |
179 | 220 | success: function (res) { |
180 | | - console.log('previewImage res', res) |
| 221 | + console.log("previewImage res", res); |
181 | 222 | }, |
182 | 223 | fail: function (e) { |
183 | | - console.log('previewImage e', e) |
184 | | - } |
185 | | - }) |
| 224 | + console.log("previewImage e", e); |
| 225 | + }, |
| 226 | + }); |
186 | 227 | }, |
187 | 228 | openFile: async function () { |
188 | 229 | if (this.data.fileData.tempPath) { |
189 | 230 | // 本地上传的文件 |
190 | | - if (this.data.fileData.rawType === 'file') { |
191 | | - this.openFileByWx(this.data.fileData.tempPath) |
| 231 | + if (this.data.fileData.rawType === "file") { |
| 232 | + this.openFileByWx(this.data.fileData.tempPath); |
192 | 233 | } else { |
193 | | - console.log('fileId', this.data.fileData.fileId) |
| 234 | + console.log("fileId", this.data.fileData.fileId); |
194 | 235 | if (this.data.fileData.fileId) { |
195 | | - this.previewImageByWx(this.data.fileData.fileId) |
| 236 | + this.previewImageByWx(this.data.fileData.fileId); |
196 | 237 | } |
197 | 238 | } |
198 | 239 | } else if (this.data.fileData.fileId) { |
199 | 240 | // 针对历史记录中带cloudID的处理(历史记录中附带的文件) |
200 | | - const cloudInsatnce=await getCloudInstance() |
| 241 | + const cloudInsatnce = await getCloudInstance(); |
201 | 242 | cloudInsatnce.downloadFile({ |
202 | 243 | fileID: this.data.fileData.fileId, |
203 | | - success: res => { |
204 | | - console.log('download res', res) |
205 | | - if (this.data.fileData.rawType === 'file') { |
206 | | - this.openFileByWx(res.tempFilePath) |
| 244 | + success: (res) => { |
| 245 | + console.log("download res", res); |
| 246 | + if (this.data.fileData.rawType === "file") { |
| 247 | + this.openFileByWx(res.tempFilePath); |
207 | 248 | } else { |
208 | | - this.previewImageByWx(this.data.fileData.fileId) |
| 249 | + this.previewImageByWx(this.data.fileData.fileId); |
209 | 250 | } |
210 | 251 | }, |
211 | | - fail: err => { |
212 | | - console.log('download err', err) |
213 | | - } |
214 | | - }) |
| 252 | + fail: (err) => { |
| 253 | + console.log("download err", err); |
| 254 | + }, |
| 255 | + }); |
215 | 256 | } |
216 | | - } |
| 257 | + }, |
217 | 258 | }, |
218 | 259 | }); |
0 commit comments