This commit is contained in:
cansnow
2025-12-11 22:33:31 +08:00
parent 375917f06c
commit 5a086fa1fa
44 changed files with 1978 additions and 359 deletions
+71 -2
View File
@@ -2,6 +2,7 @@
//import i18n from '@/locales'
import base from '@/common/config';
//import store from "@/store";
import IMSDK from "openim-uniapp-polyfill";
const isString = (v)=> {
return typeof v === 'string' || v instanceof String;
},
@@ -222,5 +223,73 @@ export default{
v = parseFloat(v).toFixed(wei || 2);
return parseFloat(v);
},
}
imapi(method,data){
return IMSDK.asyncApi(method,IMSDK.uuid(),data);
},
async fileExsit(fn){
return await new Promise((resolve) => {
plus.io.resolveLocalFileSystemURL(fn, function(entry) { resolve(true); }, function() { resolve(false); });
})
},
downloadFile (url, savepath, successCb, errorCb, progressCb) {
if (!url) {
errorCb && errorCb(new Error('empty url'));
return;
}
const startDownload = () => {
try {
const task = plus.downloader.createDownload(url, { filename: savepath, timeout: 120000 }, function(d, status) {
if (status === 200) {
const local = d && d.filename ? d.filename : savepath;
successCb && successCb(local);
} else {
errorCb && errorCb(new Error('download status ' + status));
}
});
try {
if (task && typeof task.addEventListener === 'function') {
task.addEventListener('statechanged', function(t, status) {
if (t.state === 3) {
var downloaded = t.downloadedSize || t.downloaded || 0;
var total = t.totalSize || t.total || 0;
var prog = 0;
if (total > 0) prog = Math.min(100, Math.floor(downloaded / total * 100));
progressCb && progressCb(prog);
}
});
}
} catch (e) {
// ignore
}
task.start();
} catch (e) {
errorCb && errorCb(e);
}
};
// 确保父目录存在
try {
const parent = savepath.substring(0, savepath.lastIndexOf('/'));
plus.io.resolveLocalFileSystemURL(parent, function(entry) {
startDownload();
}, function() {
// 目录不存在,尝试创建(针对 _doc/<conversationID> 结构)
let rel = parent;
if (rel.indexOf('_doc/') === 0) rel = rel.replace(/^_doc\//, '');
plus.io.requestFileSystem(plus.io.PRIVATE_DOC, function(fs) {
fs.root.getDirectory(rel, { create: true }, function(entry) {
startDownload();
}, function(e) {
// 创建失败也尝试下载,可能运行时会自动创建
startDownload();
});
}, function(e) {
startDownload();
});
});
} catch (e) {
startDownload();
}
}
}