|
@@ -456,13 +456,12 @@ const handleFile = async (fileData) => {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
} catch (e) {
|
|
} catch (e) {
|
|
|
-
|
|
|
|
|
|
|
+ const errMsg = e?.message || (typeof e === 'string' ? e : '文件处理失败')
|
|
|
|
|
+ console.error('handleFile failed:', e)
|
|
|
uni.showToast({
|
|
uni.showToast({
|
|
|
- title: '文件处理失败',
|
|
|
|
|
|
|
+ title: errMsg,
|
|
|
icon: 'none'
|
|
icon: 'none'
|
|
|
})
|
|
})
|
|
|
-
|
|
|
|
|
- console.error(e)
|
|
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
const startUpload = async () => {
|
|
const startUpload = async () => {
|
|
@@ -645,71 +644,50 @@ const uploadBase64 = (fileItem) => {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
-const base64ToTempFile = (base64, fileName = 'file.png') => {
|
|
|
|
|
|
|
+/** 从 data URL 中取出 base64 正文,避免对大字符串做正则匹配 */
|
|
|
|
|
+const getBase64Body = (dataUrl) => {
|
|
|
|
|
+ if (!dataUrl || typeof dataUrl !== 'string') return ''
|
|
|
|
|
+ const marker = 'base64,'
|
|
|
|
|
+ const markerIdx = dataUrl.indexOf(marker)
|
|
|
|
|
+ if (markerIdx !== -1) {
|
|
|
|
|
+ return dataUrl.substring(markerIdx + marker.length)
|
|
|
|
|
+ }
|
|
|
|
|
+ const commaIdx = dataUrl.indexOf(',')
|
|
|
|
|
+ return commaIdx !== -1 ? dataUrl.substring(commaIdx + 1) : dataUrl
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
|
|
+const base64ToTempFile = (base64, fileName = 'file.png') => {
|
|
|
return new Promise((resolve, reject) => {
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
+ // #ifndef APP-PLUS
|
|
|
|
|
+ reject(new Error('仅 APP 环境支持'))
|
|
|
|
|
+ return
|
|
|
|
|
+ // #endif
|
|
|
|
|
|
|
|
- const matches = base64.match(/^data:(.+);base64,(.+)$/)
|
|
|
|
|
-
|
|
|
|
|
- if (!matches) {
|
|
|
|
|
- reject('base64格式错误')
|
|
|
|
|
|
|
+ const base64Data = getBase64Body(base64)
|
|
|
|
|
+ if (!base64Data) {
|
|
|
|
|
+ reject(new Error('base64格式错误'))
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- const base64Data = matches[2]
|
|
|
|
|
-
|
|
|
|
|
- const filePath =
|
|
|
|
|
- `${plus.io.convertLocalFileSystemURL('_doc/')}${Date.now()}_${fileName}`
|
|
|
|
|
-
|
|
|
|
|
- plus.io.resolveLocalFileSystemURL(
|
|
|
|
|
- '_doc/',
|
|
|
|
|
- (entry) => {
|
|
|
|
|
-
|
|
|
|
|
- entry.getFile(
|
|
|
|
|
- `${Date.now()}_${fileName}`,
|
|
|
|
|
- { create: true },
|
|
|
|
|
-
|
|
|
|
|
- (fileEntry) => {
|
|
|
|
|
-
|
|
|
|
|
- fileEntry.createWriter((writer) => {
|
|
|
|
|
-
|
|
|
|
|
- writer.onwrite = () => {
|
|
|
|
|
- resolve(fileEntry.toLocalURL())
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ const safeName = `${Date.now()}_${(fileName || 'file').replace(/[/\\]/g, '_')}`
|
|
|
|
|
+ const dirName = 'cwg_upload'
|
|
|
|
|
|
|
|
- writer.onerror = reject
|
|
|
|
|
-
|
|
|
|
|
- const bitmap = new plus.nativeObj.Bitmap()
|
|
|
|
|
-
|
|
|
|
|
- bitmap.loadBase64Data(
|
|
|
|
|
- base64,
|
|
|
|
|
-
|
|
|
|
|
- () => {
|
|
|
|
|
-
|
|
|
|
|
- bitmap.save(
|
|
|
|
|
- fileEntry.toLocalURL(),
|
|
|
|
|
-
|
|
|
|
|
- {},
|
|
|
|
|
-
|
|
|
|
|
- () => {
|
|
|
|
|
- resolve(fileEntry.toLocalURL())
|
|
|
|
|
- },
|
|
|
|
|
-
|
|
|
|
|
- reject
|
|
|
|
|
- )
|
|
|
|
|
- },
|
|
|
|
|
-
|
|
|
|
|
- reject
|
|
|
|
|
- )
|
|
|
|
|
- })
|
|
|
|
|
- },
|
|
|
|
|
-
|
|
|
|
|
- reject
|
|
|
|
|
- )
|
|
|
|
|
- },
|
|
|
|
|
-
|
|
|
|
|
- reject
|
|
|
|
|
- )
|
|
|
|
|
|
|
+ plus.io.resolveLocalFileSystemURL('_doc', (docEntry) => {
|
|
|
|
|
+ docEntry.getDirectory(dirName, { create: true, exclusive: false }, (dirEntry) => {
|
|
|
|
|
+ dirEntry.getFile(safeName, { create: true, exclusive: false }, (fileEntry) => {
|
|
|
|
|
+ fileEntry.createWriter((writer) => {
|
|
|
|
|
+ writer.onwrite = () => {
|
|
|
|
|
+ resolve(fileEntry.toLocalURL())
|
|
|
|
|
+ }
|
|
|
|
|
+ writer.onerror = (err) => {
|
|
|
|
|
+ reject(err || new Error('写入临时文件失败'))
|
|
|
|
|
+ }
|
|
|
|
|
+ writer.seek(0)
|
|
|
|
|
+ writer.writeAsBinary(base64Data)
|
|
|
|
|
+ }, reject)
|
|
|
|
|
+ }, reject)
|
|
|
|
|
+ }, reject)
|
|
|
|
|
+ }, reject)
|
|
|
})
|
|
})
|
|
|
}
|
|
}
|
|
|
|
|
|