1
0
forked from dyf/APP

提交100J代码

This commit is contained in:
微微一笑
2026-03-27 09:53:17 +08:00
parent b99ac04c88
commit 553e24886f
3 changed files with 365 additions and 428 deletions

View File

@ -422,117 +422,6 @@
hideLoading(these);
}, 1200);
},
// 无网络时保存到本地,供蓝牙直接发送(不依赖 OSS
// 当前部分机型 PUBLIC_DOWNLOADS 在读取阶段会出现 requestFileSystem/resolve 长时间无回调,
// 因此本地语音优先落到 _doc/100J_audio后续蓝牙下发走 PRIVATE_DOC 分支更稳定。
saveLocalForBle(filePath) {
const deviceId = these.Status.ID;
if (!deviceId) {
uni.showToast({ title: '缺少设备信息,请从语音列表进入录音页', icon: 'none', duration: 2500 });
return;
}
const warmCacheNow = (srcPath) => {
if (!srcPath) return;
// 像 HBY100 一样优先写 uni 存储:后续「使用」先读缓存,尽量不再依赖当场读文件
cache100JVoiceFileForBle(deviceId, 'local_' + these.cEdit.Id, srcPath);
};
const doSave = (persistentPath) => {
const item = {
...these.cEdit,
localPath: persistentPath,
fileUrl: '',
deviceId,
id: 'local_' + these.cEdit.Id,
_createTime: these.cEdit.createTime || Common.DateFormat(new Date(), "yyyy年MM月dd日"),
_isLocal: true
};
const key = `100J_local_audio_${deviceId}`;
let list = uni.getStorageSync(key) || [];
list.unshift(item);
uni.setStorageSync(key, list);
these.AudioData.tempFilePath = "";
these.Status.isRecord = false;
// 再用持久路径补写一遍缓存(若前面已成功则覆盖同 key
cache100JVoiceFileForBle(deviceId, item.id, persistentPath);
uni.navigateBack();
};
const toPlusUrl = (p) => {
if (!p) return p;
try {
if (plus.io.convertLocalFileSystemURL) {
const c = plus.io.convertLocalFileSystemURL(p);
if (c) return c;
}
} catch (e) {}
return p;
};
const copyToDocByReadWrite = (srcPath, fileName, onOk, onFail) => {
plus.io.resolveLocalFileSystemURL(toPlusUrl(srcPath), (srcEntry) => {
srcEntry.file((file) => {
const reader = new plus.io.FileReader();
reader.onloadend = (e) => {
const buf = e.target.result;
if (!buf || !(buf.byteLength > 0)) {
onFail(new Error('读取录音文件为空'));
return;
}
plus.io.requestFileSystem(plus.io.PRIVATE_DOC, (fs) => {
fs.root.getDirectory('100J_audio', { create: true }, (dirEntry) => {
dirEntry.getFile(fileName, { create: true }, (newEntry) => {
newEntry.createWriter((writer) => {
writer.onwrite = () => onOk();
writer.onerror = (ex) => onFail(ex || new Error('写入失败'));
try {
const blob = new Blob([buf], { type: 'application/octet-stream' });
writer.write(blob);
} catch (wex) {
writer.write(buf);
}
}, onFail);
}, onFail);
}, onFail);
}, onFail);
};
reader.onerror = () => onFail(new Error('读取录音失败'));
reader.readAsArrayBuffer(file);
}, onFail);
}, onFail);
};
const fallbackDocSubdir = () => {
if (typeof plus === 'undefined' || !plus.io) {
doSave(filePath);
return;
}
const fileName = 'audio_' + (these.cEdit.Id || Date.now()) + '.mp3';
const docRel = '_doc/100J_audio/' + fileName;
const tryRwFallback = (reason) => {
console.warn('[100J] saveLocalForBle 复制失败,尝试读写落盘:', reason || '');
copyToDocByReadWrite(filePath, fileName, () => doSave(docRel), () => doSave(filePath));
};
plus.io.resolveLocalFileSystemURL(toPlusUrl(filePath), (entry) => {
plus.io.resolveLocalFileSystemURL('_doc/', (docEntry) => {
docEntry.getDirectory('100J_audio', { create: true }, (dirEntry) => {
entry.copyTo(dirEntry, fileName, () => {
doSave(docRel);
}, () => { tryRwFallback('copyTo'); });
}, () => { tryRwFallback('getDirectory'); });
}, () => { tryRwFallback('resolve _doc'); });
}, () => { tryRwFallback('resolve src'); });
};
if (typeof plus === 'undefined' || !plus.io) {
doSave(filePath);
return;
}
const rawPath = (filePath && String(filePath).trim()) || '';
// 保存流程开始时即尝试缓存一次(通常是 _doc/uniapp_temp 源路径)
warmCacheNow(rawPath);
// 已是 _doc 路径时直接保存;其余路径统一转存到 _doc/100J_audio
if (rawPath.indexOf('_doc/') === 0) {
doSave(rawPath);
return;
}
fallbackDocSubdir();
},
// 保存录音并上传(已修复文件格式问题)
uploadLuYin() {
// 文件类型验证
@ -554,6 +443,12 @@
const startOssUpload = () => {
console.log("上传文件路径:", uploadFilePath);
// _downloads/_doc 相对路径上 resolve 部分机型会长期无回调,直接走 doUpload内会 convert 供 uni.uploadFile
const fp = String(uploadFilePath || '');
if (fp.indexOf('_downloads/') === 0 || fp.indexOf('_doc/') === 0) {
this.doUpload(uploadFilePath);
return;
}
plus.io.resolveLocalFileSystemURL(uploadFilePath, (entry) => {
entry.getMetadata((metadata) => {
console.log("文件大小:", metadata.size, "字节");
@ -569,15 +464,15 @@
});
};
// 无网络时不调 OSS直接落本地列表避免 uploadFile 白失败;列表里「使用」蓝牙下发
// 无网络不允许保存:无网本地项无法上传云端,列表里「使用」也无法可靠读本地蓝牙下发
uni.getNetworkType({
success: (res) => {
if (res.networkType === 'none') {
this.saveLocalForBle(uploadFilePath);
uni.showToast({
title: '无网络,已保存到本地。请在列表连接蓝牙后点「使用」下发;有网后可再保存上传云端',
icon: 'none',
duration: 4000
uni.showModal({
title: '无法保存',
content: '当前无网络,语音需上传云端后才能正常使用与蓝牙下发。请连接 WiFi 或移动网络后再点保存。',
showCancel: false,
confirmText: '知道了'
});
return;
}
@ -592,18 +487,27 @@
// 执行上传操作
doUpload(filePath) {
const key = `${Common.pcmStorageKey}_${this.cEdit.Id}`;
const store = uni.getStorageInfoSync();
if (store.keys.includes(key)) return;
// 勿因历史 pcmStorageKey_* 存在就静默 return否则用户点保存无反应、OSS 永不上传
const token = uni.getStorageSync('token');
const clientid = uni.getStorageSync('clientID');
const these = this;
let pathForUpload = filePath;
try {
if (typeof plus !== 'undefined' && plus.io && plus.io.convertLocalFileSystemURL) {
const fp = String(filePath || '');
if (fp.indexOf('_downloads/') === 0 || fp.indexOf('_doc/') === 0) {
const c = plus.io.convertLocalFileSystemURL(fp);
if (c) pathForUpload = c;
}
}
} catch (e) {}
showLoading(this, {
text: "文件上传中"
});
console.log("最终上传文件路径:", filePath);
console.log("最终上传文件路径:", pathForUpload);
uni.uploadFile({
url: baseURL + "/app/video/uploadAudioToOss",
filePath: filePath,
filePath: pathForUpload,
name: 'file',
header: {
"Authorization": `Bearer ${token}`,
@ -690,14 +594,13 @@
},
fail: (err) => {
console.error('上传文件失败:', err);
// 无网络时保存到本地,供蓝牙直接发送
these.saveLocalForBle(filePath);
uni.showToast({
title: '网络不可用,已保存到本地,可通过蓝牙发送',
icon: 'none',
duration: 3000
});
these.timeOutCloseLoad();
uni.showModal({
title: '保存失败',
content: '文件未能上传到服务器。请检查网络后重试;无网时无法保存语音。',
showCancel: false,
confirmText: '知道了'
});
},
complete: () => {
console.log('上传操作完成');