This commit is contained in:
cansnow
2025-12-17 08:47:58 +08:00
parent 916cb22ecc
commit cf1ad1c24b
68 changed files with 2423 additions and 6104 deletions
+189 -172
View File
@@ -51,10 +51,192 @@ isSet=(v) =>{
isWeakMap=(v) =>{
return v instanceof WeakMap;
},
isWeakSet=(v) =>{
isWeakSet=(v) => {
return v instanceof WeakSet;
},
_goto = (url,type) => {
//console.log(url);
type = type || '0'; //0 navigateTo 1 redirectTo
if(url){
if(isInteger(url)){
uni.navigateBack({
delta:url,
})
}else{
url+="";
if(url.substr(0,6) != '/pages'){
url='/pages'+url
}
if(type == '1'){
return uni.redirectTo({
url:url
});
}
if(type == '2'){
return uni.switchTab({
url:url
});
}
uni.navigateTo({
url:url
})
}
}
};
const showToast = (msg,url,icon) => {
//msg = i18n.t(msg);
// #ifdef APP
plus.nativeUI.closeToast();
plus.nativeUI.toast(msg,{
align:'center',
verticalAlign:"center",
style:"inline",
icon:icon=='error' ? '/static/img/common/error.png' : '/static/img/common/success.png',
iconWidth:24,
iconHeight:24
});
if(url){
setTimeout(()=>{
_goto(url);
},3000)
}
// #endif
// #ifndef APP
uni.showToast({
//image:icon=='error' ? '/static/img/common/error.png' : '/static/img/common/success.png',
icon:icon=='error' ? icon : 'success',
title:msg,
showToast:3000,
complete:()=>{
if(url){
setTimeout(()=>{
_goto(url);
},3000)
}
}
});
// #endif
}
const error = (msg,url) => {
showToast(msg,url,'error');
}
const success = (msg,url) => {
showToast(msg,url,'success');
}
const scan = ()=>{
uni.scanCode({
success(res){
/**
* result 所扫码的内容
scanType 所扫码的类型 App、微信小程序、百度小程序、QQ小程序、京东小程序、支付宝小程序
charSet 所扫码的字符集 App、微信小程序、百度小程序(所扫码的字符集,仅支持 Android 系统)、QQ小程序、京东小程序
path 当所扫的码为当前应用的合法二维码时,会返回此字段,内容为二维码携带的 path。 微信小程序、QQ小程序、京东小程序
rawData 原始数据,base64 编码 微信小程序、QQ小程序、京东小程序、支付宝小程序
code 扫码所得数据 支付宝小程序
qrCode 扫描二维码时返回二维码数据 支付宝小程序
barCode 扫描条形码时返回条形码数据 支付宝小程序
imageChannel 来源 支付宝小程序
*/
if(res.result){
if(res.result.indexOf('blackcatp:/')){
uni.navigateTo({
url:res.result.substring(11)
})
}else{
success(res.result)
}
}
},
fail(res){
},
complete(res){
console.log(res)
}
});
}
const fileExsit = async(fn)=>{
return await new Promise((resolve) => {
plus.io.resolveLocalFileSystemURL("_doc/"+fn, function(entry) {
resolve(true);
}, function() {
resolve(false);
});
})
}
const downloadFile = (url, savepath, successCb, errorCb, progressCb) => {
const root_dir = "_doc/";
if (!url) {
errorCb && errorCb.call(this,new Error('empty url'));
return;
}
const startDownload = () => {
console.log(33333333333333,root_dir+savepath);
const task = plus.downloader.createDownload(url, { filename: root_dir+savepath, timeout: 2 }, function(d, status) {
console.log('completedCB');
console.log(d);
console.log(status);
if (status === 200) {
const local = d && d.filename ? d.filename : root_dir+savepath;
successCb && successCb.call(this,local);
} else {
errorCb && errorCb.call(this,new Error('download status ' + status));
}
});
task.addEventListener('statechanged', function(t, status) {
console.log('statechanged',d,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.call(this,prog);
}
});
try{
task.start();
plus.downloader.startAll();
}catch(e){
console.log('e',e);
}
plus.downloader.enumerate((downloads )=>{
for(var i =0;i<downloads.length;i++){
//downloads[i].abort();
console.log(downloads[i]);
}
})
};
// 目录不存在,尝试创建(针对 _doc/<conversationID> 结构)
const fns = savepath.split('/');
plus.io.requestFileSystem(plus.io.PRIVATE_DOC, function(fs) {
fs.root.getDirectory(fns[0], { create: true }, function(entry) {
startDownload();
}, function(e) {
// 创建失败也尝试下载,可能运行时会自动创建
startDownload();
});
}, function(e) {
startDownload();
});
}
const cacheFile = async (url, savepath, successCb, errorCb, progressCb) => {
//plus.downloader.clear();
//return ;
const coverExists = await fileExsit(savepath);
if(coverExists){
successCb && successCb.call(this,"_doc/"+savepath);
}else{
downloadFile(url, savepath, successCb, errorCb, progressCb)
}
}
export default{
fileExsit,
downloadFile,
cacheFile,
isString :isString,
isNumber :isNumber,
isInteger :isInteger,
@@ -88,110 +270,11 @@ export default{
}
return "";
},
goto(url,type){
//console.log(url);
type = type || '0'; //0 navigateTo 1 redirectTo
if(url){
if(isInteger(url)){
uni.navigateBack({
delta:url,
})
}else{
url+="";
if(url.substr(0,6) != '/pages'){
url='/pages'+url
}
if(type == '1'){
return uni.redirectTo({
url:url
});
}
if(type == '2'){
return uni.switchTab({
url:url
});
}
uni.navigateTo({
url:url
})
}
}
},
showToast(msg,url,icon){
const _this = this;
//msg = i18n.t(msg);
// #ifdef APP
plus.nativeUI.closeToast();
plus.nativeUI.toast(msg,{
align:'center',
verticalAlign:"center",
style:"inline",
icon:icon=='error' ? '/static/img/common/error.png' : '/static/img/common/success.png',
iconWidth:24,
iconHeight:24
});
if(url){
setTimeout(()=>{
_this.goto(url);
},3000)
}
// #endif
// #ifndef APP
uni.showToast({
//image:icon=='error' ? '/static/img/common/error.png' : '/static/img/common/success.png',
icon:icon=='error' ? icon : 'success',
title:msg,
showToast:3000,
complete:()=>{
if(url){
setTimeout(()=>{
_this.goto(url);
},3000)
}
}
});
// #endif
},
error(msg,url){
this.showToast(msg,url,'error');
},
success(msg,url){
this.showToast(msg,url,'success');
},
scan(){
uni.scanCode({
success(res){
/**
* result 所扫码的内容
scanType 所扫码的类型 App、微信小程序、百度小程序、QQ小程序、京东小程序、支付宝小程序
charSet 所扫码的字符集 App、微信小程序、百度小程序(所扫码的字符集,仅支持 Android 系统)、QQ小程序、京东小程序
path 当所扫的码为当前应用的合法二维码时,会返回此字段,内容为二维码携带的 path。 微信小程序、QQ小程序、京东小程序
rawData 原始数据,base64 编码 微信小程序、QQ小程序、京东小程序、支付宝小程序
code 扫码所得数据 支付宝小程序
qrCode 扫描二维码时返回二维码数据 支付宝小程序
barCode 扫描条形码时返回条形码数据 支付宝小程序
imageChannel 来源 支付宝小程序
*/
if(res.result){
if(res.result.indexOf('blackcatp:/')){
uni.navigateTo({
url:res.result.substring(11)
})
}else{
this.success(res.result)
}
}
},
fail(res){
},
complete(res){
console.log(res)
}
});
},
"goto":_goto,
showToast,
error,
success,
scan,
copy(v){
let that = this;
uni.setClipboardData({
@@ -225,71 +308,5 @@ export default{
},
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();
}
}
}
}