Compare commits
2 Commits
5bf94a6223
...
34cad2543d
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
34cad2543d | ||
|
|
134c900946 |
@@ -8,7 +8,6 @@ import {
|
|||||||
onHide,
|
onHide,
|
||||||
onUnload
|
onUnload
|
||||||
} from '@dcloudio/uni-app'
|
} from '@dcloudio/uni-app'
|
||||||
import WavDecoder from '@/lib/wav-decoder@1.3.0.js'
|
|
||||||
|
|
||||||
export function useTTSPlayer(httpUrl) {
|
export function useTTSPlayer(httpUrl) {
|
||||||
const isSpeaking = ref(false)
|
const isSpeaking = ref(false)
|
||||||
@@ -23,22 +22,79 @@ export function useTTSPlayer(httpUrl) {
|
|||||||
|
|
||||||
// #ifdef MP-WEIXIN
|
// #ifdef MP-WEIXIN
|
||||||
const audioContext = null // 微信小程序不支持 AudioContext
|
const audioContext = null // 微信小程序不支持 AudioContext
|
||||||
|
let innerAudioContext = null // 微信小程序音频上下文
|
||||||
|
let backgroundAudioManager = null // 微信小程序背景音频管理器
|
||||||
// #endif
|
// #endif
|
||||||
|
|
||||||
let currentAudioBuffer = null
|
let currentAudioBuffer = null
|
||||||
let currentSource = null
|
let currentSource = null
|
||||||
let playTimeOffset = 0
|
let playTimeOffset = 0
|
||||||
|
|
||||||
const speak = async (text) => {
|
// 初始化微信小程序音频上下文
|
||||||
if (!audioContext) {
|
// #ifdef MP-WEIXIN
|
||||||
console.warn('⚠️ TTS not supported in current environment');
|
const initAudioManager = () => {
|
||||||
return;
|
// 优先使用BackgroundAudioManager,更适合真机环境
|
||||||
|
try {
|
||||||
|
console.log('<27> 微信小程序:创建背景音频管理器')
|
||||||
|
backgroundAudioManager = uni.getBackgroundAudioManager()
|
||||||
|
|
||||||
|
// 设置默认配置
|
||||||
|
backgroundAudioManager.title = 'AI语音播报'
|
||||||
|
backgroundAudioManager.singer = 'KS AI'
|
||||||
|
backgroundAudioManager.coverImgUrl = '/static/icon/logo.png'
|
||||||
|
backgroundAudioManager.volume = 1.0
|
||||||
|
|
||||||
|
backgroundAudioManager.onPlay(() => {
|
||||||
|
console.log('🎵 微信小程序背景音频播放开始')
|
||||||
|
isSpeaking.value = true
|
||||||
|
isPaused.value = false
|
||||||
|
})
|
||||||
|
|
||||||
|
backgroundAudioManager.onPause(() => {
|
||||||
|
console.log('⏸️ 微信小程序背景音频播放暂停')
|
||||||
|
isPaused.value = true
|
||||||
|
})
|
||||||
|
|
||||||
|
backgroundAudioManager.onStop(() => {
|
||||||
|
console.log('⏹️ 微信小程序背景音频播放停止')
|
||||||
|
isSpeaking.value = false
|
||||||
|
isComplete.value = true
|
||||||
|
})
|
||||||
|
|
||||||
|
backgroundAudioManager.onEnded(() => {
|
||||||
|
console.log('🎵 微信小程序背景音频播放结束')
|
||||||
|
isSpeaking.value = false
|
||||||
|
isComplete.value = true
|
||||||
|
})
|
||||||
|
|
||||||
|
backgroundAudioManager.onError((res) => {
|
||||||
|
console.error('❌ 微信小程序背景音频播放错误:', res.errMsg, '错误码:', res.errCode)
|
||||||
|
isSpeaking.value = false
|
||||||
|
isComplete.value = false
|
||||||
|
})
|
||||||
|
|
||||||
|
backgroundAudioManager.onCanplay(() => {
|
||||||
|
console.log('🎵 微信小程序背景音频可以播放了')
|
||||||
|
})
|
||||||
|
|
||||||
|
backgroundAudioManager.onWaiting(() => {
|
||||||
|
console.log('⏳ 微信小程序背景音频加载中...')
|
||||||
|
})
|
||||||
|
|
||||||
|
backgroundAudioManager.onTimeUpdate(() => {
|
||||||
|
console.log('⏱️ 微信小程序背景音频播放进度:', backgroundAudioManager.currentTime)
|
||||||
|
})
|
||||||
|
|
||||||
|
console.log('✅ 微信小程序背景音频管理器初始化成功')
|
||||||
|
return true
|
||||||
|
} catch (e) {
|
||||||
|
console.error('❌ 微信小程序背景音频管理器初始化失败:', e)
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
|
}
|
||||||
console.log('🎤 TTS speak function called');
|
// #endif
|
||||||
console.log('📝 Text to synthesize:', text ? text.substring(0, 100) + '...' : 'No text');
|
|
||||||
console.log('🔗 HTTP URL:', httpUrl);
|
const speak = async (text) => {
|
||||||
|
|
||||||
// 停止当前播放
|
// 停止当前播放
|
||||||
stop()
|
stop()
|
||||||
|
|
||||||
@@ -51,22 +107,93 @@ export function useTTSPlayer(httpUrl) {
|
|||||||
const url = `${httpUrl}?text=${encodeURIComponent(speechText)}`
|
const url = `${httpUrl}?text=${encodeURIComponent(speechText)}`
|
||||||
console.log('🔗 Final GET URL:', url);
|
console.log('🔗 Final GET URL:', url);
|
||||||
|
|
||||||
// 发送GET请求获取语音数据
|
// #ifdef MP-WEIXIN
|
||||||
const response = await fetch(url)
|
// 微信小程序环境,使用背景音频管理器
|
||||||
if (!response.ok) {
|
const isBackgroundAudioAvailable = initAudioManager()
|
||||||
throw new Error(`HTTP error! status: ${response.status}`)
|
|
||||||
|
// 重置音频状态
|
||||||
|
isSpeaking.value = true
|
||||||
|
isPaused.value = false
|
||||||
|
isComplete.value = false
|
||||||
|
|
||||||
|
if (isBackgroundAudioAvailable && backgroundAudioManager) {
|
||||||
|
console.log('🎵 微信小程序:使用背景音频管理器播放,URL:', url);
|
||||||
|
|
||||||
|
// 设置背景音频参数
|
||||||
|
backgroundAudioManager.title = 'AI语音播报'
|
||||||
|
backgroundAudioManager.singer = 'KS AI'
|
||||||
|
backgroundAudioManager.coverImgUrl = '/static/icon/logo.png'
|
||||||
|
|
||||||
|
// 直接设置src并播放
|
||||||
|
backgroundAudioManager.src = url
|
||||||
|
console.log('🎵 微信小程序背景音频开始播放');
|
||||||
|
} else {
|
||||||
|
// 降级方案:使用InnerAudioContext
|
||||||
|
console.log('🔄 微信小程序:背景音频不可用,降级使用InnerAudioContext');
|
||||||
|
|
||||||
|
// 如果已有音频上下文,先销毁再重新创建
|
||||||
|
if (innerAudioContext) {
|
||||||
|
innerAudioContext.destroy()
|
||||||
|
innerAudioContext = null
|
||||||
|
}
|
||||||
|
|
||||||
|
innerAudioContext = uni.createInnerAudioContext()
|
||||||
|
innerAudioContext.autoplay = false
|
||||||
|
innerAudioContext.obeyMuteSwitch = false
|
||||||
|
innerAudioContext.volume = 1.0
|
||||||
|
|
||||||
|
innerAudioContext.onPlay(() => {
|
||||||
|
console.log('🎵 微信小程序InnerAudioContext播放开始')
|
||||||
|
isSpeaking.value = true
|
||||||
|
isPaused.value = false
|
||||||
|
})
|
||||||
|
|
||||||
|
innerAudioContext.onEnded(() => {
|
||||||
|
console.log('🎵 微信小程序InnerAudioContext播放结束')
|
||||||
|
isSpeaking.value = false
|
||||||
|
isComplete.value = true
|
||||||
|
})
|
||||||
|
|
||||||
|
innerAudioContext.onError((res) => {
|
||||||
|
console.error('❌ 微信小程序InnerAudioContext错误:', res.errMsg, '错误码:', res.errCode)
|
||||||
|
isSpeaking.value = false
|
||||||
|
isComplete.value = false
|
||||||
|
})
|
||||||
|
|
||||||
|
innerAudioContext.src = url
|
||||||
|
innerAudioContext.play()
|
||||||
|
console.log('🎵 微信小程序InnerAudioContext开始播放');
|
||||||
}
|
}
|
||||||
|
// #endif
|
||||||
|
|
||||||
// 获取二进制数据
|
// #ifdef H5
|
||||||
const arrayBuffer = await response.arrayBuffer()
|
// H5环境,使用 AudioContext
|
||||||
console.log('✅ Received audio data, size:', arrayBuffer.byteLength + ' bytes');
|
if (audioContext) {
|
||||||
|
// 发送GET请求获取语音数据
|
||||||
|
const response = await fetch(url)
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error(`HTTP error! status: ${response.status}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取二进制数据
|
||||||
|
const arrayBuffer = await response.arrayBuffer()
|
||||||
|
console.log('✅ Received audio data, size:', arrayBuffer.byteLength + ' bytes');
|
||||||
|
|
||||||
|
try {
|
||||||
|
// 直接使用 audioContext.decodeAudioData 解码,不依赖外部库
|
||||||
|
const decoded = await audioContext.decodeAudioData(arrayBuffer)
|
||||||
|
console.log('✅ Audio decoded, sampleRate:', decoded.sampleRate, 'channels:', decoded.numberOfChannels);
|
||||||
|
|
||||||
|
// 播放音频
|
||||||
|
playDecodedAudio(decoded)
|
||||||
|
} catch (decodeError) {
|
||||||
|
console.error('❌ AudioContext decodeAudioData failed:', decodeError);
|
||||||
|
// 降级处理:创建一个简单的音频缓冲区
|
||||||
|
createFallbackAudio(arrayBuffer)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// #endif
|
||||||
|
|
||||||
// 解码音频数据
|
|
||||||
const decoded = await WavDecoder.decode(arrayBuffer)
|
|
||||||
console.log('✅ Audio decoded, sampleRate:', decoded.sampleRate, 'channels:', decoded.channelData.length);
|
|
||||||
|
|
||||||
// 播放音频
|
|
||||||
playDecodedAudio(decoded)
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('❌ TTS synthesis failed:', error);
|
console.error('❌ TTS synthesis failed:', error);
|
||||||
isSpeaking.value = false
|
isSpeaking.value = false
|
||||||
@@ -74,18 +201,13 @@ export function useTTSPlayer(httpUrl) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// #ifdef H5
|
||||||
const playDecodedAudio = (decoded) => {
|
const playDecodedAudio = (decoded) => {
|
||||||
if (!audioContext) return;
|
if (!audioContext) return;
|
||||||
|
|
||||||
// 使用第一个声道的数据
|
|
||||||
const audioBuffer = audioContext.createBuffer(1, decoded.channelData[0].length, decoded.sampleRate)
|
|
||||||
audioBuffer.copyToChannel(decoded.channelData[0], 0)
|
|
||||||
|
|
||||||
currentAudioBuffer = audioBuffer
|
|
||||||
|
|
||||||
// 创建音频源
|
// 创建音频源
|
||||||
currentSource = audioContext.createBufferSource()
|
currentSource = audioContext.createBufferSource()
|
||||||
currentSource.buffer = audioBuffer
|
currentSource.buffer = decoded
|
||||||
currentSource.connect(audioContext.destination)
|
currentSource.connect(audioContext.destination)
|
||||||
|
|
||||||
// 监听播放结束
|
// 监听播放结束
|
||||||
@@ -100,39 +222,113 @@ export function useTTSPlayer(httpUrl) {
|
|||||||
isSpeaking.value = true
|
isSpeaking.value = true
|
||||||
isPaused.value = false
|
isPaused.value = false
|
||||||
isComplete.value = false
|
isComplete.value = false
|
||||||
console.log('<EFBFBD> Audio playback started');
|
console.log('🎵 Audio playback started');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 降级处理:创建一个简单的音频缓冲区
|
||||||
|
const createFallbackAudio = (arrayBuffer) => {
|
||||||
|
console.log('🔄 使用降级方案创建音频');
|
||||||
|
|
||||||
|
// 创建一个简单的音频缓冲区,生成提示音
|
||||||
|
const sampleRate = 44100
|
||||||
|
const duration = 1 // 1秒
|
||||||
|
const frameCount = sampleRate * duration
|
||||||
|
|
||||||
|
const audioBuffer = audioContext.createBuffer(1, frameCount, sampleRate)
|
||||||
|
const channelData = audioBuffer.getChannelData(0)
|
||||||
|
|
||||||
|
// 生成一个简单的提示音(正弦波)
|
||||||
|
for (let i = 0; i < frameCount; i++) {
|
||||||
|
const t = i / sampleRate
|
||||||
|
channelData[i] = Math.sin(2 * Math.PI * 440 * t) * 0.1 // 440Hz正弦波,音量0.1
|
||||||
|
}
|
||||||
|
|
||||||
|
playDecodedAudio(audioBuffer)
|
||||||
|
}
|
||||||
|
// #endif
|
||||||
|
|
||||||
const pause = () => {
|
const pause = () => {
|
||||||
if (!audioContext || !isSpeaking.value || isPaused.value) {
|
console.log('⏸️ TTS pause called');
|
||||||
|
|
||||||
|
// #ifdef MP-WEIXIN
|
||||||
|
// 优先使用背景音频管理器
|
||||||
|
if (backgroundAudioManager) {
|
||||||
|
try {
|
||||||
|
backgroundAudioManager.pause()
|
||||||
|
console.log('⏸️ 微信小程序背景音频暂停');
|
||||||
|
return
|
||||||
|
} catch (e) {
|
||||||
|
console.error('❌ 微信小程序背景音频暂停失败:', e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 降级使用InnerAudioContext
|
||||||
|
if (innerAudioContext && isSpeaking.value && !isPaused.value) {
|
||||||
|
try {
|
||||||
|
innerAudioContext.pause()
|
||||||
|
console.log('⏸️ 微信小程序InnerAudioContext暂停');
|
||||||
|
return
|
||||||
|
} catch (e) {
|
||||||
|
console.error('❌ 微信小程序InnerAudioContext暂停失败:', e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// #endif
|
||||||
|
|
||||||
|
// #ifdef H5
|
||||||
|
if (audioContext && !isSpeaking.value || isPaused.value) {
|
||||||
console.warn('⚠️ Cannot pause TTS playback');
|
console.warn('⚠️ Cannot pause TTS playback');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log('⏸️ TTS pause called');
|
|
||||||
|
|
||||||
if (audioContext.state === 'running') {
|
if (audioContext.state === 'running') {
|
||||||
audioContext.suspend()
|
audioContext.suspend()
|
||||||
isPaused.value = true
|
isPaused.value = true
|
||||||
// 保存当前播放位置
|
// 保存当前播放位置
|
||||||
playTimeOffset = audioContext.currentTime
|
playTimeOffset = audioContext.currentTime
|
||||||
console.log('✅ Audio paused successfully');
|
console.log('✅ H5 Audio paused successfully');
|
||||||
}
|
}
|
||||||
|
// #endif
|
||||||
}
|
}
|
||||||
|
|
||||||
const resume = () => {
|
const resume = () => {
|
||||||
if (!audioContext || !isSpeaking.value || !isPaused.value) {
|
console.log('▶️ TTS resume called');
|
||||||
|
|
||||||
|
// #ifdef MP-WEIXIN
|
||||||
|
// 优先使用背景音频管理器
|
||||||
|
if (backgroundAudioManager) {
|
||||||
|
try {
|
||||||
|
backgroundAudioManager.play()
|
||||||
|
console.log('▶️ 微信小程序背景音频恢复播放');
|
||||||
|
return
|
||||||
|
} catch (e) {
|
||||||
|
console.error('❌ 微信小程序背景音频恢复失败:', e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 降级使用InnerAudioContext
|
||||||
|
if (innerAudioContext && isSpeaking.value && isPaused.value) {
|
||||||
|
try {
|
||||||
|
innerAudioContext.play()
|
||||||
|
console.log('▶️ 微信小程序InnerAudioContext恢复播放');
|
||||||
|
return
|
||||||
|
} catch (e) {
|
||||||
|
console.error('❌ 微信小程序InnerAudioContext恢复失败:', e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// #endif
|
||||||
|
|
||||||
|
// #ifdef H5
|
||||||
|
if (audioContext && !isSpeaking.value || !isPaused.value) {
|
||||||
console.warn('⚠️ Cannot resume TTS playback');
|
console.warn('⚠️ Cannot resume TTS playback');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log('▶️ TTS resume called');
|
|
||||||
|
|
||||||
if (audioContext.state === 'suspended') {
|
if (audioContext.state === 'suspended') {
|
||||||
audioContext.resume()
|
audioContext.resume()
|
||||||
isPaused.value = false
|
isPaused.value = false
|
||||||
console.log('✅ Audio resumed successfully');
|
console.log('✅ H5 Audio resumed successfully');
|
||||||
}
|
}
|
||||||
|
// #endif
|
||||||
}
|
}
|
||||||
|
|
||||||
const cancelAudio = () => {
|
const cancelAudio = () => {
|
||||||
@@ -142,12 +338,37 @@ export function useTTSPlayer(httpUrl) {
|
|||||||
const stop = () => {
|
const stop = () => {
|
||||||
console.log('⏹️ TTS stop called');
|
console.log('⏹️ TTS stop called');
|
||||||
|
|
||||||
|
// #ifdef MP-WEIXIN
|
||||||
|
// 优先使用背景音频管理器
|
||||||
|
if (backgroundAudioManager) {
|
||||||
|
try {
|
||||||
|
backgroundAudioManager.stop()
|
||||||
|
console.log('✅ 微信小程序背景音频停止');
|
||||||
|
} catch (e) {
|
||||||
|
console.error('❌ 微信小程序背景音频停止失败:', e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 降级使用InnerAudioContext
|
||||||
|
if (innerAudioContext) {
|
||||||
|
try {
|
||||||
|
innerAudioContext.stop()
|
||||||
|
console.log('✅ 微信小程序InnerAudioContext停止');
|
||||||
|
innerAudioContext.destroy()
|
||||||
|
innerAudioContext = null
|
||||||
|
} catch (e) {
|
||||||
|
console.error('❌ 微信小程序InnerAudioContext停止错误:', e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// #endif
|
||||||
|
|
||||||
|
// #ifdef H5
|
||||||
if (currentSource) {
|
if (currentSource) {
|
||||||
try {
|
try {
|
||||||
currentSource.stop()
|
currentSource.stop()
|
||||||
currentSource.disconnect()
|
currentSource.disconnect()
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error('❌ Error stopping audio source:', e);
|
console.error('❌ Error stopping H5 audio source:', e);
|
||||||
}
|
}
|
||||||
currentSource = null
|
currentSource = null
|
||||||
}
|
}
|
||||||
@@ -156,9 +377,10 @@ export function useTTSPlayer(httpUrl) {
|
|||||||
try {
|
try {
|
||||||
audioContext.suspend()
|
audioContext.suspend()
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error('❌ Error suspending audio context:', e);
|
console.error('❌ Error suspending H5 audio context:', e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// #endif
|
||||||
|
|
||||||
isSpeaking.value = false
|
isSpeaking.value = false
|
||||||
isPaused.value = false
|
isPaused.value = false
|
||||||
|
|||||||
@@ -283,7 +283,7 @@ import {
|
|||||||
getCurrentInstance,
|
getCurrentInstance,
|
||||||
} from 'vue';
|
} from 'vue';
|
||||||
import { storeToRefs } from 'pinia';
|
import { storeToRefs } from 'pinia';
|
||||||
// import config from '@/config.js';
|
// 移除重复导入,使用从globalFunction注入的config
|
||||||
import useChatGroupDBStore from '@/stores/userChatGroupStore';
|
import useChatGroupDBStore from '@/stores/userChatGroupStore';
|
||||||
import MdRender from '@/components/md-render/md-render.vue';
|
import MdRender from '@/components/md-render/md-render.vue';
|
||||||
import CollapseTransition from '@/components/CollapseTransition/CollapseTransition.vue';
|
import CollapseTransition from '@/components/CollapseTransition/CollapseTransition.vue';
|
||||||
|
|||||||
@@ -51,101 +51,101 @@ function StreamRequestMiniProgram(url, data = {}, onDataReceived, onError, onCom
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// UTF-8解码函数,用于微信小程序真机环境
|
// UTF-8解码函数,用于微信小程序真机环境
|
||||||
function utf8Decode(uint8Array) {
|
function utf8Decode(uint8Array) {
|
||||||
let result = '';
|
let result = '';
|
||||||
let i = 0;
|
let i = 0;
|
||||||
const len = uint8Array.length;
|
const len = uint8Array.length;
|
||||||
|
|
||||||
while (i < len) {
|
while (i < len) {
|
||||||
const byte1 = uint8Array[i];
|
const byte1 = uint8Array[i];
|
||||||
|
|
||||||
// 1字节字符 (0xxxxxxx)
|
// 1字节字符 (0xxxxxxx)
|
||||||
if (byte1 < 0x80) {
|
if (byte1 < 0x80) {
|
||||||
result += String.fromCharCode(byte1);
|
result += String.fromCharCode(byte1);
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
// 2字节字符 (110xxxxx 10xxxxxx)
|
// 2字节字符 (110xxxxx 10xxxxxx)
|
||||||
else if (byte1 >= 0xC0 && byte1 < 0xE0) {
|
else if (byte1 >= 0xC0 && byte1 < 0xE0) {
|
||||||
if (i + 1 < len) {
|
if (i + 1 < len) {
|
||||||
const byte2 = uint8Array[i + 1];
|
const byte2 = uint8Array[i + 1];
|
||||||
if (byte2 >= 0x80 && byte2 < 0xC0) {
|
if (byte2 >= 0x80 && byte2 < 0xC0) {
|
||||||
const codePoint = ((byte1 & 0x1F) << 6) | (byte2 & 0x3F);
|
const codePoint = ((byte1 & 0x1F) << 6) | (byte2 & 0x3F);
|
||||||
result += String.fromCharCode(codePoint);
|
result += String.fromCharCode(codePoint);
|
||||||
i += 2;
|
i += 2;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// 无效的UTF-8序列,跳过
|
// 无效的UTF-8序列,跳过
|
||||||
result += '<27>';
|
result += '<27>';
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
// 3字节字符 (1110xxxx 10xxxxxx 10xxxxxx)
|
// 3字节字符 (1110xxxx 10xxxxxx 10xxxxxx)
|
||||||
else if (byte1 >= 0xE0 && byte1 < 0xF0) {
|
else if (byte1 >= 0xE0 && byte1 < 0xF0) {
|
||||||
if (i + 2 < len) {
|
if (i + 2 < len) {
|
||||||
const byte2 = uint8Array[i + 1];
|
const byte2 = uint8Array[i + 1];
|
||||||
const byte3 = uint8Array[i + 2];
|
const byte3 = uint8Array[i + 2];
|
||||||
if ((byte2 >= 0x80 && byte2 < 0xC0) && (byte3 >= 0x80 && byte3 < 0xC0)) {
|
if ((byte2 >= 0x80 && byte2 < 0xC0) && (byte3 >= 0x80 && byte3 < 0xC0)) {
|
||||||
const codePoint = ((byte1 & 0x0F) << 12) | ((byte2 & 0x3F) << 6) | (byte3 & 0x3F);
|
const codePoint = ((byte1 & 0x0F) << 12) | ((byte2 & 0x3F) << 6) | (byte3 & 0x3F);
|
||||||
result += String.fromCharCode(codePoint);
|
result += String.fromCharCode(codePoint);
|
||||||
i += 3;
|
i += 3;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// 无效的UTF-8序列,跳过
|
// 无效的UTF-8序列,跳过
|
||||||
result += '<27>';
|
result += '<27>';
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
// 4字节字符 (11110xxx 10xxxxxx 10xxxxxx 10xxxxxx)
|
// 4字节字符 (11110xxx 10xxxxxx 10xxxxxx 10xxxxxx)
|
||||||
else if (byte1 >= 0xF0 && byte1 < 0xF8) {
|
else if (byte1 >= 0xF0 && byte1 < 0xF8) {
|
||||||
if (i + 3 < len) {
|
if (i + 3 < len) {
|
||||||
const byte2 = uint8Array[i + 1];
|
const byte2 = uint8Array[i + 1];
|
||||||
const byte3 = uint8Array[i + 2];
|
const byte3 = uint8Array[i + 2];
|
||||||
const byte4 = uint8Array[i + 3];
|
const byte4 = uint8Array[i + 3];
|
||||||
if ((byte2 >= 0x80 && byte2 < 0xC0) && (byte3 >= 0x80 && byte3 < 0xC0) && (byte4 >= 0x80 && byte4 < 0xC0)) {
|
if ((byte2 >= 0x80 && byte2 < 0xC0) && (byte3 >= 0x80 && byte3 < 0xC0) && (byte4 >= 0x80 && byte4 < 0xC0)) {
|
||||||
let codePoint = ((byte1 & 0x07) << 18) | ((byte2 & 0x3F) << 12) | ((byte3 & 0x3F) << 6) | (byte4 & 0x3F);
|
let codePoint = ((byte1 & 0x07) << 18) | ((byte2 & 0x3F) << 12) | ((byte3 & 0x3F) << 6) | (byte4 & 0x3F);
|
||||||
// 处理UTF-16代理对
|
// 处理UTF-16代理对
|
||||||
if (codePoint >= 0x10000) {
|
if (codePoint >= 0x10000) {
|
||||||
codePoint -= 0x10000;
|
codePoint -= 0x10000;
|
||||||
const highSurrogate = (codePoint >> 10) + 0xD800;
|
const highSurrogate = (codePoint >> 10) + 0xD800;
|
||||||
const lowSurrogate = (codePoint & 0x3FF) + 0xDC00;
|
const lowSurrogate = (codePoint & 0x3FF) + 0xDC00;
|
||||||
result += String.fromCharCode(highSurrogate, lowSurrogate);
|
result += String.fromCharCode(highSurrogate, lowSurrogate);
|
||||||
} else {
|
} else {
|
||||||
result += String.fromCharCode(codePoint);
|
result += String.fromCharCode(codePoint);
|
||||||
}
|
}
|
||||||
i += 4;
|
i += 4;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// 无效的UTF-8序列,跳过
|
// 无效的UTF-8序列,跳过
|
||||||
result += '<27>';
|
result += '<27>';
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
// 无效的UTF-8序列,跳过
|
// 无效的UTF-8序列,跳过
|
||||||
else {
|
else {
|
||||||
result += '<27>';
|
result += '<27>';
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 监听分块数据
|
// 监听分块数据
|
||||||
requestTask.onChunkReceived((res) => {
|
requestTask.onChunkReceived((res) => {
|
||||||
try {
|
try {
|
||||||
// 微信小程序兼容处理:微信小程序不支持TextDecoder,使用自定义UTF-8解码
|
// 微信小程序兼容处理:微信小程序不支持TextDecoder,使用自定义UTF-8解码
|
||||||
let chunk = '';
|
let chunk = '';
|
||||||
if (typeof TextDecoder !== 'undefined') {
|
if (typeof TextDecoder !== 'undefined') {
|
||||||
// 支持TextDecoder的环境(如开发者工具)
|
// 支持TextDecoder的环境(如开发者工具)
|
||||||
const decoder = new TextDecoder('utf-8');
|
const decoder = new TextDecoder('utf-8');
|
||||||
chunk = decoder.decode(new Uint8Array(res.data));
|
chunk = decoder.decode(new Uint8Array(res.data));
|
||||||
} else {
|
} else {
|
||||||
// 微信小程序真机环境,使用自定义UTF-8解码函数
|
// 微信小程序真机环境,使用自定义UTF-8解码函数
|
||||||
const uint8Array = new Uint8Array(res.data);
|
const uint8Array = new Uint8Array(res.data);
|
||||||
chunk = utf8Decode(uint8Array);
|
chunk = utf8Decode(uint8Array);
|
||||||
}
|
}
|
||||||
console.log('📦 收到分块数据:', chunk);
|
console.log('📦 收到分块数据:', chunk);
|
||||||
buffer += chunk;
|
buffer += chunk;
|
||||||
|
|
||||||
let lines = buffer.split("\n");
|
let lines = buffer.split("\n");
|
||||||
|
|||||||
Reference in New Issue
Block a user