Merge branch 'new-20250827' of http://47.107.152.87:3000/liubiao/APP into new-20250827

# Conflicts:
#	pages/common/index/index.vue
This commit is contained in:
liub
2026-03-30 14:01:07 +08:00
12 changed files with 1066 additions and 280 deletions

View File

@ -64,7 +64,7 @@
<view class="info-value status-running">
{{ deviceInfo && deviceInfo.longitude ? Number(deviceInfo.longitude).toFixed(4) : '' }}
{{ deviceInfo && deviceInfo.latitude ? Number(deviceInfo.latitude).toFixed(4) : '' }}
</view>
</view>
<view class="info-value status-running locationGPS">
<uni-icons type="location" size="17" color="rgba(255, 255, 255, 0.8)"
style="vertical-align: bottom;" />
@ -469,23 +469,41 @@
const eventChannel = this.getOpenerEventChannel();
var these = this;
// 低电量提示同一百分比不重复弹MQTT/蓝牙反复上报时避免刷屏);恢复高于 20% 后再次降低可再提示
// 低电量:语音上传/蓝牙分包时电量字段易抖动,防抖 + 上传中不弹,避免「发送中频繁低电量」误报
this._lastBatteryLowToastPct = null;
this.$watch("deviceInfo.batteryPercentage", (newVal) => {
const n = Number(newVal);
this._batteryLowDebounceTimer = null;
this.$watch("deviceInfo.batteryPercentage", () => {
if (bleTool.isVoiceUploading && bleTool.isVoiceUploading()) {
if (this._batteryLowDebounceTimer) {
clearTimeout(this._batteryLowDebounceTimer);
this._batteryLowDebounceTimer = null;
}
return;
}
const n = Math.round(Number(this.deviceInfo.batteryPercentage));
if (!Number.isFinite(n)) return;
if (n > 20) {
this._lastBatteryLowToastPct = null;
if (this._batteryLowDebounceTimer) {
clearTimeout(this._batteryLowDebounceTimer);
this._batteryLowDebounceTimer = null;
}
return;
}
if (n <= 20 && this._lastBatteryLowToastPct !== n) {
this._lastBatteryLowToastPct = n;
if (this._batteryLowDebounceTimer) clearTimeout(this._batteryLowDebounceTimer);
this._batteryLowDebounceTimer = setTimeout(() => {
this._batteryLowDebounceTimer = null;
if (bleTool.isVoiceUploading && bleTool.isVoiceUploading()) return;
const cur = Math.round(Number(this.deviceInfo.batteryPercentage));
if (!Number.isFinite(cur) || cur > 20) return;
if (this._lastBatteryLowToastPct === cur) return;
this._lastBatteryLowToastPct = cur;
uni.showToast({
title: '设备电量低',
icon: 'none',
duration: 2000
});
}
}, 800);
});
eventChannel.on('detailData', function(data) {
var device = data.data;
@ -552,13 +570,12 @@
// 报警模式,选中,首次上报值,或者切换设备按键上报值
const enable = siren_alarm.enable ?? 0; // 报警开关1=开0=关
const mode = siren_alarm.mode ?? 0; // 模式0/1/2/3/4/5/6
if (these.formData.sta_VoiceType != 7) {
if (String(these.formData.sta_VoiceType) !== '7') {
if (enable === 1) {
// 开启状态
these.formData.sta_VoiceType = mode + '';
} else {
// 关闭状态:赋值-1表示关闭
these.formData.sta_VoiceType = mode + '';
const m = Number(mode);
these.formData.sta_VoiceType = m === 7 ? '-1' : m + '';
}
} else {
// 播放语音,上报消息
@ -582,18 +599,15 @@
}
// 强制报警按键解除报警状态,app同步
} else if (funcType === '12') {
const enable_alarm = data.voice_strobe_alarm ??
0; // 报警开关1=开0=关
const mode_alarm = data.mode ?? 0; // 模式0/1/2/3/4/5/6
const enable_alarm = data.voice_strobe_alarm ?? 0;
const mode_alarm = data.mode ?? 0;
if (enable_alarm === 1) {
// 开启状
these.deviceInfo.voiceStrobeAlarm = 1;
these.formData.sta_VoiceType = mode_alarm + ''
these.formData.sta_VoiceType = mode_alarm + '';
} else {
// 关闭状态:赋值-1表示关闭
these.deviceInfo.voiceStrobeAlarm = -1;
// 模式还是选中的,模式,解除也是
these.formData.sta_VoiceType = mode_alarm + ''
const m = Number(mode_alarm);
these.formData.sta_VoiceType = m === 7 ? '-1' : m + '';
}
}
// 警示灯模式选中切换
@ -656,6 +670,10 @@
this.Status.pageHide = true;
},
onUnload() {
if (this._batteryLowDebounceTimer) {
clearTimeout(this._batteryLowDebounceTimer);
this._batteryLowDebounceTimer = null;
}
if (this._hby100jBleAdapterHandler && typeof uni.offBluetoothAdapterStateChange === 'function') {
uni.offBluetoothAdapterStateChange(this._hby100jBleAdapterHandler);
this._hby100jBleAdapterHandler = null;
@ -749,7 +767,9 @@
this.formData.bleStatu = 'connecting';
bleTool.LinkBlue(f.deviceId, f.writeServiceId, f.wirteCharactId, f.notifyCharactId).then(() => {
this.formData.bleStatu = true;
this.bleStateRecovry({ deviceId: f.deviceId });
this.bleStateRecovry({
deviceId: f.deviceId
});
}).catch(() => {
this.formData.bleStatu = 'err';
});
@ -787,10 +807,12 @@
// 关闭状态
that.formData.sta_LightType = '-1';
}
if (this.formData.sta_VoiceType === '7' || this.formData.sta_VoiceType === 7) {
this.formData.sta_VoiceType = (res.data.voiceStrobeAlarm ?? 0) + '';
const alarmOnDetail = res.data.voiceStrobeAlarm === 1;
const amDetail = res.data.alarmMode != null ? Number(res.data.alarmMode) : 0;
if (alarmOnDetail) {
this.formData.sta_VoiceType = amDetail + '';
} else {
this.formData.sta_VoiceType = res.data.alarmMode + ''
this.formData.sta_VoiceType = amDetail === 7 ? '-1' : amDetail + '';
}
}
})
@ -1006,18 +1028,18 @@
item = this.dic.sta_VoiceType[index];
}
let val = item.key;
const prevVoiceType = this.formData.sta_VoiceType;
if (this.formData.sta_VoiceType === val) {
val = '-1';
}
this.formData.sta_VoiceType = val;
// 模式类型为7时才去判断
console.log(val, 'valllll');
const isVoiceOperate = val === '7' || val === '-1'; // 标记是否是语音开启/关闭操作
if (this.deviceInfo.voiceStrobeAlarm == 1) {
// 如果强制报警已经开启了,那么切换下面的模式需要时,需要触发报警指令
// 仅「播放语音」7 的开关走播报接口;-1 只有从 7 取消时才视为关播报,避免取消内置音色选中误调播报接口
const isVoiceOperate = val === '7' || (val === '-1' && prevVoiceType === '7');
if (this.deviceInfo.voiceStrobeAlarm === 1) {
// 强制报警已开启:切换下方模式需带报警下发
const data = {
deviceIds: [this.deviceInfo.deviceId],
// 声光报警开关开启传1
voiceStrobeAlarm: 1,
mode: this.formData.sta_VoiceType
};
@ -1033,13 +1055,14 @@
icon: 'none'
});
}
})
});
} else if (isVoiceOperate) {
console.log('我是谁');
let data = {
const data = {
deviceId: this.deviceInfo.deviceId,
voiceBroadcast: Number(this.formData.sta_VoiceType) === -1 ? 0 : 1
}
voiceBroadcast: Number(this.formData.sta_VoiceType) === -1 ? 0 : 1,
mode: this.formData.sta_VoiceType,
voiceStrobeAlarm: this.deviceInfo.voiceStrobeAlarm
};
deviceVoiceBroadcast(data).then((res) => {
if (res.code == 200) {
uni.showToast({
@ -1052,14 +1075,55 @@
icon: 'none'
});
}
})
}).catch(() => {
uni.showToast({
title: '下发失败,请检查蓝牙或网络',
icon: 'none'
});
});
} else if (prevVoiceType === '7' && val !== '7' && val !== '-1') {
// 从「播放语音」切到其它内置音色:先关播报;报警未开启时不走 forceAlarm仅 UI 预选音色
const data = {
deviceId: this.deviceInfo.deviceId,
voiceBroadcast: 0,
mode: val,
voiceStrobeAlarm: this.deviceInfo.voiceStrobeAlarm
};
deviceVoiceBroadcast(data).then((res) => {
if (res.code == 200) {
uni.showToast({
title: res.msg || '已切换',
icon: 'none'
});
} else {
uni.showToast({
title: res.msg || '操作失败',
icon: 'none'
});
}
}).catch(() => {
uni.showToast({
title: '下发失败,请检查蓝牙或网络',
icon: 'none'
});
});
}
// 未开启强制报警时,在 06 内置音色间切换或取消选中:只改按钮选中,不下发
},
// 报警模式
sosSetting(item) {
console.log(this.deviceInfo, '44444');
console.log(item, 'tent');
const isClose = item === 0;
// 与「已解除不再重复关报警」对称:已在报警中不再弹窗重复下发「开启」,未报警时不再重复「解除」
if (!isClose && this.deviceInfo.voiceStrobeAlarm === 1) {
uni.showToast({ title: '当前已在报警中', icon: 'none' });
return;
}
if (isClose && this.deviceInfo.voiceStrobeAlarm !== 1) {
uni.showToast({ title: '当前未在报警中', icon: 'none' });
return;
}
if (!this.Status) this.Status = {};
if (!this.Status.Pop) this.Status.Pop = {
showPop: false
@ -1089,7 +1153,8 @@
title: isClose ? '声光报警已解除' : '强制报警已开启',
icon: 'none'
});
if (isClose && this.formData.sta_VoiceType === '7') {
// 解除后「播放语音」行与 mode7 绑定,需退出高亮(兼容 sta_VoiceType 为数字 7
if (isClose && String(this.formData.sta_VoiceType) === '7') {
this.formData.sta_VoiceType = '-1';
}
} else {
@ -1240,7 +1305,8 @@
const last6 = target.slice(-6);
const item = bleTool.data.LinkedList.find((v) => {
const m = macNorm(v.macAddress || '');
return v.deviceId === res.deviceId && (m === target || (m.length >= 6 && m.slice(-6) === last6));
return v.deviceId === res.deviceId && (m === target || (m.length >= 6 && m.slice(-6) ===
last6));
});
return !!item;
},
@ -1314,7 +1380,9 @@
this.formData.sta_VoiceType = (parsedData.alarmMode ?? 0) + '';
} else {
this.$set(this.deviceInfo, 'voiceStrobeAlarm', -1);
this.formData.sta_VoiceType = (parsedData.alarmMode ?? 0) + '';
const am = Number(parsedData.alarmMode ?? 0);
// 报警已关:设备仍可能带 mode7避免「播放语音」仍显示为开启
this.formData.sta_VoiceType = am === 7 ? '-1' : am + '';
}
}
// 0x0A 爆闪模式:警示灯开关/模式
@ -1340,7 +1408,8 @@
this.formData.sta_VoiceType = (parsedData.alarmMode ?? 0) + '';
} else {
this.$set(this.deviceInfo, 'voiceStrobeAlarm', -1);
this.formData.sta_VoiceType = (parsedData.alarmMode ?? 0) + '';
const am = Number(parsedData.alarmMode ?? 0);
this.formData.sta_VoiceType = am === 7 ? '-1' : am + '';
}
}
if (parsedData.voiceBroadcast !== undefined) {

View File

@ -29,8 +29,8 @@
</view>
<view class="itemRight ">
<view class="btn" @click.stop="Apply(item, index)"
:class="{ 'active': item.useStatus, 'btn-default': !item.useStatus }">
{{ item.useStatus == 1 ? '使用中' : '使用' }}
:class="{ 'active': isVoiceInUse(item), 'btn-default': !isVoiceInUse(item) }">
{{ isVoiceInUse(item) ? '使用中' : '使用' }}
</view>
</view>
<view class="clear"></view>
@ -105,7 +105,9 @@
videRenameAudioFile,
deviceDeleteAudioFile,
deviceUpdateVoice,
updateBleStatus
updateBleStatus,
sync100JBleProtocolFromHelper,
remove100JVoiceBleCache
} from '@/api/100J/HBY100-J.js'
import { baseURL } from '@/utils/request.js'
import {
@ -231,12 +233,37 @@
console.log("页面返回")
},
onUnload() {
// 页面卸载时断开MQTT连接
this.clearVoiceApplyTimers();
if (this.mqttClient) {
this.mqttClient.disconnect();
}
},
methods: {
/** 清除「使用」语音相关的全部定时器,避免返回上一页后仍触发 toast / 二次 navigateBack */
clearVoiceApplyTimers() {
if (this._applyOverallTimer) {
clearTimeout(this._applyOverallTimer);
this._applyOverallTimer = null;
}
if (this.upgradeTimer) {
clearTimeout(this.upgradeTimer);
this.upgradeTimer = null;
}
if (this._applyNavigateTimer) {
clearTimeout(this._applyNavigateTimer);
this._applyNavigateTimer = null;
}
},
scheduleNavigateBackAfterVoice(delayMs = 1500) {
if (this._applyNavigateTimer) {
clearTimeout(this._applyNavigateTimer);
this._applyNavigateTimer = null;
}
this._applyNavigateTimer = setTimeout(() => {
this._applyNavigateTimer = null;
uni.navigateBack();
}, delayMs);
},
//语音管理列表(合并云端 + 本地无网络保存的语音)
getinitData(val, isLoadMore = false) {
const deviceId = this.device.deviceId;
@ -266,7 +293,8 @@
this.total = res.total;
const list = (res.data || []).map(item => ({
...item,
createTime: item.createTime || Common.DateFormat(new Date(), "yyyy年MM月dd日")
createTime: item.createTime || Common.DateFormat(new Date(), "yyyy年MM月dd日"),
useStatus: Number(item.useStatus) === 1 ? 1 : 0
}));
this.dataListA = mergeLocal(list);
if (this.mescroll) this.mescroll.endBySize(this.dataListA.length, this.total + (this.dataListA.length - list.length));
@ -430,7 +458,10 @@
let task = () => {
if (item._isLocal) {
// 本地项:从本地存储移除
const key = `100J_local_audio_${this.device.deviceId}`;
const devId = this.device.deviceId;
const vid = (item.id != null && item.id !== '') ? item.id : item.fileId;
remove100JVoiceBleCache(devId, vid);
const key = `100J_local_audio_${devId}`;
let list = uni.getStorageSync(key) || [];
list = list.filter(l => l.id !== item.id && l.Id !== item.Id);
uni.setStorageSync(key, list);
@ -496,9 +527,31 @@
this.checkList.push(item.Id);
}
},
/** 与后端约定:仅 1 为使用中(避免字符串 "0" 在 class 里仍为 truthy */
isVoiceInUse(item) {
return Number(item && item.useStatus) === 1;
},
/** 切换「使用」后同步整表:仅当前项为 1其余为 0避免第一项永远显示使用中 */
syncVoiceListUseStatus(activeItem) {
const pickId = (o) => {
if (!o) return '';
const v = o.id ?? o.fileId ?? o.Id;
return v != null && v !== '' ? String(v) : '';
};
const aid = pickId(activeItem);
if (!aid) return;
this.dataListA.forEach((row, i) => {
const rid = pickId(row);
const use = rid === aid ? 1 : 0;
if (Number(row.useStatus) !== use) {
this.$set(this.dataListA, i, { ...row, useStatus: use });
}
});
},
Apply(item, index) {
this.updateProgress = 0;
this.isUpdating = true;
this.clearVoiceApplyTimers();
// 本地项在无网时禁止下发仅弹窗isUpdating 在确认可执行后再置 true
// 本地项优先用 localPath云端项用 fileUrl兼容多种字段名相对路径补全 baseURL
let fileUrl = '';
let localPath = (item.localPath && typeof item.localPath === 'string') ? item.localPath : '';
@ -506,73 +559,144 @@
const raw = item.fileUrl || item.url || item.filePath || item.audioUrl || item.ossUrl || '';
fileUrl = (typeof raw === 'string' && raw) ? (raw.startsWith('/') ? (baseURL + raw) : raw) : '';
} else {
// 本地项localPath 优先,无则用 fileUrlmergeLocal 可能只有 fileUrl路径
if (!localPath && item.fileUrl) localPath = item.fileUrl;
// 本地项localPath 优先mergeLocal 可能把路径放在 fileUrl,但勿把 http 当成本地路径
if (!localPath && item.fileUrl) {
const cand = String(item.fileUrl).trim();
if (cand && !/^https?:\/\//i.test(cand)) localPath = cand;
}
}
const data = {
id: item.id,
fileUrl,
id: (item.id != null && item.id !== '') ? item.id : item.fileId,
// 本地合并项 mergeLocal 会把路径写在 fileUrl需带给接口层做 effectiveLocal 兜底
fileUrl: item._isLocal ? (typeof item.fileUrl === 'string' ? item.fileUrl : '') : fileUrl,
localPath,
onProgress: (p) => { this.updateProgress = p; },
onWaiting: () => { uni.showToast({ title: '等待蓝牙连接中...', icon: 'none', duration: 2000 }); }
onProgress: (p) => {
const n = Math.min(100, Math.max(0, Math.round(Number(p) || 0)));
const cur = Number(this.updateProgress) || 0;
this.updateProgress = Math.max(cur, n);
},
// 不传「蓝牙连接中」类提示:关蓝牙走 4G 时易误导;进度条 + 必要时全局请稍候即可
onWaiting: () => {}
};
// 整体超时 60 秒仅影响蓝牙上传4G HTTP 很快返回)
const overallTimer = setTimeout(() => {
if (this.isUpdating) {
uni.showToast({ title: '操作超时', icon: 'none', duration: 2000 });
const runDeviceUpdate = () => {
// 大文件蓝牙分片耗时可远超 2 分钟,整体超时放宽到 10 分钟(挂到实例上,便于 onUnload / 成功时清除)
const OVERALL_MS = 600000;
if (this._applyOverallTimer) {
clearTimeout(this._applyOverallTimer);
this._applyOverallTimer = null;
}
this._applyOverallTimer = setTimeout(() => {
this._applyOverallTimer = null;
if (this.isUpdating) {
uni.showToast({ title: '操作时间过长已中断,请重试或检查蓝牙连接', icon: 'none', duration: 2500 });
this.isUpdating = false;
this.updateProgress = 0;
}
}, OVERALL_MS);
// 进入列表时的蓝牙快照可能过期;与 HBY100 详情页一致,从 BleHelper 按 MAC 再对齐一次
sync100JBleProtocolFromHelper(this.device).then(() => deviceUpdateVoice(data)).then((RES) => {
if (this._applyOverallTimer) {
clearTimeout(this._applyOverallTimer);
this._applyOverallTimer = null;
}
if (RES.code == 200) {
// 蓝牙上传:进度已由 onProgress 更新,直接完成
if (RES._channel === 'ble') {
if (this.upgradeTimer) {
clearTimeout(this.upgradeTimer);
this.upgradeTimer = null;
}
const title = RES._updateVoiceAfterBleFailed
? '蓝牙已下发,云端同步失败可稍后重试'
: '音频上传成功';
this.syncVoiceListUseStatus(item);
uni.showToast({ title, icon: RES._updateVoiceAfterBleFailed ? 'none' : 'success', duration: 2000 });
this.isUpdating = false;
this.scheduleNavigateBackAfterVoice(1500);
return;
}
// 4GMQTT 进度可能数十秒才上报,用「自上次进度起」滑动超时,避免误报
const MQTT_IDLE_MS = 120000;
const armMqttIdle = () => {
if (this.upgradeTimer) clearTimeout(this.upgradeTimer);
this.upgradeTimer = setTimeout(() => {
if (!this.isUpdating) return;
uni.showToast({
title: '长时间未收到设备进度,若语音已生效可返回查看',
icon: 'none',
duration: 3500
});
this.isUpdating = false;
this.updateProgress = 0;
}, MQTT_IDLE_MS);
};
armMqttIdle();
this.mqttClient = this.mqttClient || new MqttClient();
this.mqttClient.connect(() => {
const statusTopic = `status/894078/HBY100/${this.device.deviceImei}`;
this.mqttClient.subscribe(statusTopic, (payload) => {
try {
const payloadObj = typeof payload === 'string' ? JSON.parse(payload) : payload;
const progress = payloadObj.data != null && payloadObj.data.progress !== undefined
? payloadObj.data.progress
: payloadObj.progress;
if (progress !== undefined && !isNaN(progress) && progress >= 0 && progress <= 100) {
armMqttIdle();
const cur = Number(this.updateProgress) || 0;
this.updateProgress = Math.max(cur, Math.round(progress));
if (Number(progress) === 100) {
if (this.upgradeTimer) clearTimeout(this.upgradeTimer);
this.upgradeTimer = null;
this.syncVoiceListUseStatus(item);
uni.showToast({ title: '音频上传成功', icon: 'success', duration: 2000 });
this.isUpdating = false;
this.scheduleNavigateBackAfterVoice(1500);
}
}
} catch (e) {
console.error('解析MQTT payload失败', e);
armMqttIdle();
}
});
});
} else {
this.isUpdating = false;
uni.showToast({ title: RES.msg || '操作失败', icon: 'none', duration: 1000 });
}
}).catch((err) => {
if (this._applyOverallTimer) {
clearTimeout(this._applyOverallTimer);
this._applyOverallTimer = null;
}
this.isUpdating = false;
this.updateProgress = 0;
}
}, 60000);
deviceUpdateVoice(data).then((RES) => {
clearTimeout(overallTimer);
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;
uni.showToast({ title: err.message || '操作失败', icon: 'none', duration: 2500 });
});
};
if (item._isLocal) {
uni.getNetworkType({
success: (net) => {
if (net.networkType === 'none') {
uni.showModal({
title: '无法使用',
content: '无网保存的本地语音无法通过蓝牙下发。请先连接 WiFi 或移动网络后,重新录制并保存(上传云端),再点「使用」。',
showCancel: false,
confirmText: '知道了'
});
return;
}
}, 6000);
this.mqttClient = this.mqttClient || new MqttClient();
this.mqttClient.connect(() => {
const statusTopic = `status/894078/HBY100/${this.device.deviceImei}`;
this.mqttClient.subscribe(statusTopic, (payload) => {
try {
const payloadObj = typeof payload === 'string' ? JSON.parse(payload) : payload;
const progress = payloadObj.data?.progress;
if (progress !== undefined && !isNaN(progress) && progress >= 0 && progress <= 100) {
this.updateProgress = progress;
if (progress === 100) {
clearTimeout(this.upgradeTimer);
uni.showToast({ title: '音频上传成功', icon: 'success', duration: 2000 });
this.isUpdating = false;
setTimeout(() => { uni.navigateBack(); }, 1500);
}
}
} catch (e) {
clearTimeout(this.upgradeTimer);
console.error('解析MQTT payload失败', e);
}
});
});
} else {
this.isUpdating = false;
uni.showToast({ title: RES.msg || '操作失败', icon: 'none', duration: 1000 });
}
}).catch((err) => {
clearTimeout(overallTimer);
this.isUpdating = false;
uni.showToast({ title: err.message || '操作失败', icon: 'none', duration: 2000 });
});
this.isUpdating = true;
runDeviceUpdate();
},
fail: () => {
this.isUpdating = true;
runDeviceUpdate();
}
});
return;
}
this.isUpdating = true;
runDeviceUpdate();
},
closePop: function() {
this.Status.Pop.showPop = false;

View File

@ -137,8 +137,9 @@
updateLoading
} from '@/utils/loading.js';
import Common from '@/utils/Common.js';
import {
cache100JVoiceFileForBle
} from '@/api/100J/HBY100-J.js';
export default {
data() {
@ -421,44 +422,6 @@
hideLoading(these);
}, 1200);
},
// 无网络时保存到本地,供蓝牙直接发送(不依赖 OSS
// 将临时文件复制到持久化目录 _doc/100J_audio/,避免被系统清理
saveLocalForBle(filePath) {
const deviceId = these.Status.ID;
if (!deviceId) return;
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;
uni.navigateBack();
};
if (typeof plus !== 'undefined' && plus.io) {
const fileName = 'audio_' + (these.cEdit.Id || Date.now()) + '.mp3';
plus.io.resolveLocalFileSystemURL(filePath, (entry) => {
plus.io.resolveLocalFileSystemURL('_doc/', (docEntry) => {
docEntry.getDirectory('100J_audio', { create: true }, (dirEntry) => {
entry.copyTo(dirEntry, fileName, (newEntry) => {
doSave(newEntry.fullPath);
}, () => { doSave(filePath); });
}, () => { doSave(filePath); });
}, () => { doSave(filePath); });
}, () => { doSave(filePath); });
} else {
doSave(filePath);
}
},
// 保存录音并上传(已修复文件格式问题)
uploadLuYin() {
// 文件类型验证
@ -478,37 +441,73 @@
console.log("自动添加.mp3扩展名新路径:", uploadFilePath);
}
console.log("上传文件路径:", uploadFilePath);
plus.io.resolveLocalFileSystemURL(uploadFilePath, (entry) => {
entry.getMetadata((metadata) => {
console.log("文件大小:", metadata.size, "字节");
console.log("文件类型验证通过");
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, "字节");
console.log("文件类型验证通过");
this.doUpload(uploadFilePath);
}, (err) => {
console.error("获取文件元数据失败:", err);
this.doUpload(this.AudioData.tempFilePath);
});
}, (err) => {
console.error("获取文件元数据失败:", err);
console.error("文件不存在或路径错误:", err);
this.doUpload(this.AudioData.tempFilePath);
});
}, (err) => {
console.error("文件不存在或路径错误:", err);
this.doUpload(this.AudioData.tempFilePath);
};
// 无网络不允许保存:无网本地项无法上传云端,列表里「使用」也无法可靠读本地蓝牙下发
uni.getNetworkType({
success: (res) => {
if (res.networkType === 'none') {
uni.showModal({
title: '无法保存',
content: '当前无网络,语音需上传云端后才能正常使用与蓝牙下发。请连接 WiFi 或移动网络后再点保存。',
showCancel: false,
confirmText: '知道了'
});
return;
}
startOssUpload();
},
fail: () => {
startOssUpload();
}
});
},
// 执行上传操作
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}`,
@ -535,6 +534,9 @@
if (fileUrl) cache[fileUrl] = filePath;
if (d && typeof d === 'object' && d.id) cache[d.id] = filePath;
uni.setStorageSync(cacheKey, cache);
if (d && typeof d === 'object' && d.id) {
cache100JVoiceFileForBle(deviceId, d.id, filePath);
}
}
}
// 合并两个存储操作
@ -592,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('上传操作完成');