| | |
| | | return `${dateValue} ${isEnd ? '23:59:59' : '00:00:00'}`; |
| | | }; |
| | | |
| | | const arrayBufferToBase64 = (buffer) => { |
| | | if (!(buffer instanceof ArrayBuffer)) { |
| | | return ''; |
| | | } |
| | | |
| | | const bytes = new Uint8Array(buffer); |
| | | const chunkSize = 0x8000; |
| | | let binary = ''; |
| | | |
| | | for (let index = 0; index < bytes.length; index += chunkSize) { |
| | | const chunk = bytes.subarray(index, index + chunkSize); |
| | | binary += String.fromCharCode.apply(null, chunk); |
| | | } |
| | | |
| | | return btoa(binary); |
| | | }; |
| | | |
| | | const saveExportArrayBufferToLocal = (buffer, fileName) => { |
| | | return new Promise((resolve, reject) => { |
| | | // #ifdef APP-PLUS |
| | | try { |
| | | if (typeof plus === 'undefined' || !plus.android || !plus.io) { |
| | | reject(new Error('当前环境不支持原生文件写入')); |
| | | return; |
| | | } |
| | | |
| | | const base64Content = arrayBufferToBase64(buffer); |
| | | if (!base64Content) { |
| | | reject(new Error('导出内容为空')); |
| | | return; |
| | | } |
| | | |
| | | const Base64 = plus.android.importClass('android.util.Base64'); |
| | | const File = plus.android.importClass('java.io.File'); |
| | | const FileOutputStream = plus.android.importClass('java.io.FileOutputStream'); |
| | | const exportDirPath = plus.io.convertLocalFileSystemURL('_doc/exports'); |
| | | const exportDir = new File(exportDirPath); |
| | | |
| | | if (!exportDir.exists()) { |
| | | exportDir.mkdirs(); |
| | | } |
| | | |
| | | const absoluteFilePath = `${exportDirPath}/${fileName}`; |
| | | const exportFile = new File(absoluteFilePath); |
| | | const outputStream = new FileOutputStream(exportFile); |
| | | |
| | | try { |
| | | const fileBytes = Base64.decode(base64Content, Base64.DEFAULT); |
| | | outputStream.write(fileBytes); |
| | | outputStream.flush(); |
| | | } finally { |
| | | outputStream.close(); |
| | | } |
| | | |
| | | resolve(absoluteFilePath); |
| | | } catch (error) { |
| | | reject(error); |
| | | } |
| | | // #endif |
| | | |
| | | // #ifndef APP-PLUS |
| | | reject(new Error('当前环境不支持原生文件写入')); |
| | | // #endif |
| | | }); |
| | | }; |
| | | |
| | | const getSelectedAreaLeafName = (areaValue = '') => { |
| | | if (!areaValue) { |
| | | return ''; |
| | |
| | | return; |
| | | } |
| | | |
| | | // 保存到本地 |
| | | plus.io.resolveLocalFileSystemURL('_doc/', (docEntry) => { |
| | | docEntry.getFile(fileName, { create: true }, (fileEntry) => { |
| | | fileEntry.createWriter((writer) => { |
| | | writer.onwrite = () => { |
| | | uni.hideLoading(); |
| | | const filePath = fileEntry.toLocalURL(); |
| | | console.log('访客记录导出成功,文件路径:', filePath); |
| | | uni.showToast({ title: '导出成功,文件地址:' + filePath, icon: 'none' }); |
| | | }; |
| | | writer.onerror = (err) => { |
| | | uni.hideLoading(); |
| | | uni.showToast({ title: '文件保存失败', icon: 'none' }); |
| | | console.error('文件保存失败:', err); |
| | | }; |
| | | writer.seek(0); |
| | | writer.truncate(0); |
| | | writer.writeAsBinary(res); // 使用 writeAsBinary 写入 ArrayBuffer |
| | | }, (err) => { |
| | | uni.hideLoading(); |
| | | console.error('创建文件写入器失败:', err); |
| | | }); |
| | | }, (err) => { |
| | | uni.hideLoading(); |
| | | console.error('创建导出文件失败:', err); |
| | | }); |
| | | }, (err) => { |
| | | try { |
| | | const savedFilePath = await saveExportArrayBufferToLocal(res, fileName); |
| | | uni.hideLoading(); |
| | | console.error('访问_doc目录失败:', err); |
| | | }); |
| | | console.log('访客记录导出成功,文件路径:', savedFilePath); |
| | | uni.showToast({ title: '导出成功,文件地址:' + savedFilePath, icon: 'none' }); |
| | | } catch (err) { |
| | | uni.hideLoading(); |
| | | uni.showToast({ title: '文件保存失败', icon: 'none' }); |
| | | console.error('文件保存失败:', err); |
| | | } |
| | | // #endif |
| | | return; |
| | | } |