UNPKG

vite-uni-dev-tool

Version:

vite-uni-dev-tool, debug, uni-app, 一处编写,到处调试

122 lines (114 loc) 3.42 kB
/** * 获取文件路径 * * @export * @return {*} */ export function getFilePath() { return new Promise((resolve, reject) => { if (plus) { plus.io.resolveLocalFileSystemURL('_downloads', function (dirEntry) { resolve(dirEntry.fullPath); }); } else { reject(); } }); } /** * 写入文件 * * @param {string} filePath * @param {string} content */ function writeContentToFile(filePath: string, content: string) { plus.io.requestFileSystem( plus.io.PUBLIC_DOCUMENTS, function (fs) { fs.root?.getFile( filePath, { create: true }, function (fileEntry) { fileEntry.createWriter(function (writer) { writer.onwriteend = function () { console.log('[DevTool] 写入文件成功' + filePath); uni.showToast({ icon: 'none', title: '写入文件成功' + filePath, duration: 10 * 1000, }); }; writer.onerror = function (e) { console.log('[DevTool] 写入文件失败' + JSON.stringify(e)); uni.showToast({ icon: 'none', title: '写入文件失败', }); }; writer.write(content); }); }, function (e) { uni.showToast({ icon: 'none', title: '获取文件失败', }); console.log('[DevTool] 获取文件失败' + JSON.stringify(e)); }, ); }, function (e) { uni.showToast({ icon: 'none', title: '获取文件系统失败', }); console.log('[DevTool] 获取文件系统失败' + JSON.stringify(e)); }, ); } export async function saveTxtFileApp(data: string, fileName: string) { try { const filePath = (await getFilePath()) + `${Date.now()}_${fileName}.txt`; writeContentToFile(filePath, data); } catch (error) { console.log('[DevTool] ' + JSON.stringify(error)); } } export async function saveTextFileH5(data: string, fileName: string) { // 创建 Blob 对象 const blob = new Blob([data], { type: 'text/plain' }); // 生成临时 URL const url = URL.createObjectURL(blob); // 创建下载链接 const link = document.createElement('a'); link.href = url; link.download = fileName; // 触发点击下载 link.click(); } export async function saveTextFileMicro(data: string, fileName: string) { const fs = uni.getFileSystemManager(); const fileNameDate = `${fileName}_${Date.now()}.txt`; const filePath = `${uni?.env?.USER_DATA_PATH ?? 'Download'}/${fileNameDate}`; fs.writeFile({ data, filePath, success: (res) => { console.log('[DevTool] 写入文件成功' + filePath); console.log( `[DevTool] 文件位置:内部存储/Android/data/comm.tencent.mm/MicroMsg/wxanewfiles/手动查找最新文件夹/${fileNameDate}`, ); uni.showToast({ title: '写入文件成功' + filePath, icon: 'none', }); }, fail: (error) => { console.log('[DevTool] 写入文件失败' + JSON.stringify(error)); uni.showToast({ title: '写入文件失败' + JSON.stringify(error), icon: 'none', }); }, }); }