100J蓝牙上传语音待验证

This commit is contained in:
微微一笑
2026-03-18 18:09:31 +08:00
parent 2c9197833b
commit a9848bd299
5 changed files with 310 additions and 85 deletions

View File

@ -231,25 +231,39 @@
}
},
methods: {
//语音管理列表
//语音管理列表(合并云端 + 本地无网络保存的语音)
getinitData(val, isLoadMore = false) {
let data = {
deviceId: this.device.deviceId
}
deviceVoliceList(data).then((res) => {
const deviceId = this.device.deviceId;
if (!deviceId) return;
const mergeLocal = (serverList) => {
const key = `100J_local_audio_${deviceId}`;
const localList = uni.getStorageSync(key) || [];
const localMapped = localList.map(item => ({
...item,
fileNameExt: item.name || '本地语音',
createTime: item._createTime || item.createTime || Common.DateFormat(new Date(), "yyyy年MM月dd日"),
fileUrl: item.fileUrl || item.localPath,
useStatus: 0,
_isLocal: true
}));
return [...localMapped, ...(serverList || [])];
};
deviceVoliceList({ deviceId }).then((res) => {
if (res.code == 200) {
this.total = res.total;
const list = res.data.map(item => ({
const list = (res.data || []).map(item => ({
...item,
createTime: item.createTime || Common.DateFormat(new Date(), "yyyy年MM月dd日")
}));
this.dataListA = list;
// 通知mescroll加载完成
if (this.mescroll) {
this.mescroll.endBySize(list.length, this.total);
}
this.dataListA = mergeLocal(list);
if (this.mescroll) this.mescroll.endBySize(this.dataListA.length, this.total + (this.dataListA.length - list.length));
}
})
}).catch(() => {
// 无网络时仅显示本地保存的语音
this.dataListA = mergeLocal([]);
this.total = this.dataListA.length;
if (this.mescroll) this.mescroll.endBySize(this.dataListA.length, this.total);
});
},
createAudioPlayer(localPath) {
if (innerAudioContext) {
@ -401,22 +415,24 @@
return;
}
let task = () => {
let data = {
fileId: item.fileId,
deviceId: this.device.deviceId
if (item._isLocal) {
// 本地项:从本地存储移除
const key = `100J_local_audio_${this.device.deviceId}`;
let list = uni.getStorageSync(key) || [];
list = list.filter(l => l.id !== item.id && l.Id !== item.Id);
uni.setStorageSync(key, list);
uni.showToast({ title: '已删除', icon: 'none', duration: 1000 });
this.getinitData();
this.$refs.swipeAction.closeAll();
return;
}
deviceDeleteAudioFile(data).then((res) => {
deviceDeleteAudioFile({ fileId: item.fileId, deviceId: this.device.deviceId }).then((res) => {
if (res.code == 200) {
uni.showToast({
title: res.msg,
icon: 'none',
duration: 1000
});
this.getinitData()
// 关闭所有滑动项
uni.showToast({ title: res.msg, icon: 'none', duration: 1000 });
this.getinitData();
this.$refs.swipeAction.closeAll();
}
})
});
}
this.showPop({
showPop: true, //是否显示弹窗
@ -468,71 +484,72 @@
}
},
Apply(item, index) {
this.mqttClient = new MqttClient();
let data = {
id: item.id
}
deviceUpdateVoice(data).then((RES) => {
console.log(RES,'RES');
if (RES.code == 200) {
this.updateProgress = 0;
this.isUpdating = true;
const data = {
id: item.id,
fileUrl: item._isLocal ? '' : (item.fileUrl || item.url),
localPath: item._isLocal ? item.localPath : '',
onProgress: (p) => { this.updateProgress = p; }
};
// 整体超时 60 秒仅影响蓝牙上传4G HTTP 很快返回)
const overallTimer = setTimeout(() => {
if (this.isUpdating) {
uni.showToast({ title: '操作超时', icon: 'none', duration: 2000 });
this.isUpdating = false;
this.updateProgress = 0;
this.isUpdating = true;
}
}, 60000);
deviceUpdateVoice(data).then((RES) => {
clearTimeout(overallTimer);
console.log(RES, 'RES');
if (RES.code == 200) {
// 蓝牙上传:进度已由 onProgress 更新,直接完成
if (RES._channel === 'ble') {
uni.showToast({ title: '升级完成!', icon: 'success', duration: 2000 });
this.isUpdating = false;
setTimeout(() => { uni.navigateBack(); }, 1500);
return;
}
// 4G订阅 MQTT 获取设备端进度6 秒超时
this.upgradeTimer = setTimeout(() => {
if (this.isUpdating) {
uni.showToast({ title: '升级进度同步超时', icon: 'none', duration: 2000 });
this.isUpdating = false;
this.updateProgress = 0;
}
}, 6000);
this.mqttClient = this.mqttClient || new MqttClient();
this.mqttClient.connect(() => {
// 订阅来自设备的状态更新
const statusTopic = `status/894078/HBY100/${this.device.deviceImei}`;
this.mqttClient.subscribe(statusTopic, (payload) => {
console.log(payload, 'payloadpayloadpayload');
try {
// 解析MQTT返回的payload
const payloadObj = typeof payload === 'string' ? JSON.parse(
payload) : payload;
// 取出进度值(用可选链避免字段不存在报错)
const payloadObj = typeof payload === 'string' ? JSON.parse(payload) : payload;
const progress = payloadObj.data?.progress;
if (progress !== undefined && !isNaN(progress) && progress >=
0 && progress <= 100) {
if (progress !== undefined && !isNaN(progress) && progress >= 0 && progress <= 100) {
this.updateProgress = progress;
console.log('当前升级进度:', progress + '%');
// 进度到100%时,触发升级完成逻辑
if (progress === 100) {
clearTimeout(this.upgradeTimer);
uni.showToast({
title: '升级完成!',
icon: 'success',
duration: 2000
});
uni.showToast({ title: '升级完成!', icon: 'success', duration: 2000 });
this.isUpdating = false;
setTimeout(() => {
uni.navigateBack();
}, 1500);
setTimeout(() => { uni.navigateBack(); }, 1500);
}
}
} catch (e) {
clearTimeout(this.upgradeTimer);
clearTimeout(this.upgradeTimer);
console.error('解析MQTT payload失败', e);
}
})
})
} else {
uni.showToast({
title: RES.msg,
icon: 'none',
duration: 1000
});
});
} else {
this.isUpdating = false;
uni.showToast({ title: RES.msg || '操作失败', icon: 'none', duration: 1000 });
}
})
this.upgradeTimer = setTimeout(() => {
// 超时后执行:隐藏进度条+提示超时+重置进度
uni.showToast({
title: '升级进度同步超时',
icon: 'none',
duration: 2000
});
this.isUpdating = false; // 关闭进度条
this.updateProgress = 0; // 重置进度值
// 可选如果需要取消MQTT订阅加这行根据需求选择
// this.mqttClient.unsubscribe(statusTopic);
}, 6000); // 6000ms = 6秒时间可直接修改
}).catch((err) => {
clearTimeout(overallTimer);
this.isUpdating = false;
uni.showToast({ title: err.message || '操作失败', icon: 'none', duration: 2000 });
});
},
closePop: function() {
this.Status.Pop.showPop = false;

View File

@ -421,6 +421,27 @@
hideLoading(these);
}, 1200);
},
// 无网络时保存到本地,供蓝牙直接发送(不依赖 OSS
saveLocalForBle(filePath) {
const deviceId = these.Status.ID;
if (!deviceId) return;
const item = {
...these.cEdit,
localPath: filePath,
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;
uni.navigateBack();
},
// 保存录音并上传(已修复文件格式问题)
uploadLuYin() {
// 文件类型验证
@ -541,8 +562,10 @@
},
fail: (err) => {
console.error('上传文件失败:', err);
// 无网络时保存到本地,供蓝牙直接发送
these.saveLocalForBle(filePath);
uni.showToast({
title: '上传失败,请检查网络',
title: '网络不可用,已保存到本地,可通过蓝牙发送',
icon: 'none',
duration: 3000
});