小程序聊天修正

This commit is contained in:
2026-01-12 10:47:44 +08:00
parent 0972d504ab
commit 6921ac7e66
2 changed files with 146 additions and 101 deletions

View File

@@ -22,6 +22,51 @@ declare const uni: {
request: (options: any) => any request: (options: any) => any
} }
/**
* ArrayBuffer 转字符串(兼容微信小程序真机环境)
* 微信小程序真机不支持 TextDecoder需要手动解码 UTF-8
*/
function arrayBufferToString(buffer: ArrayBuffer): string {
// 优先使用 TextDecoder开发者工具和支持的环境
if (typeof TextDecoder !== 'undefined') {
return new TextDecoder('utf-8').decode(new Uint8Array(buffer))
}
// 微信小程序真机兼容方案:手动解码 UTF-8
const bytes = new Uint8Array(buffer)
let result = ''
let i = 0
while (i < bytes.length) {
const byte1 = bytes[i++]
if (byte1 < 0x80) {
// 单字节字符 (0xxxxxxx)
result += String.fromCharCode(byte1)
} else if ((byte1 & 0xE0) === 0xC0) {
// 双字节字符 (110xxxxx 10xxxxxx)
const byte2 = bytes[i++] & 0x3F
result += String.fromCharCode(((byte1 & 0x1F) << 6) | byte2)
} else if ((byte1 & 0xF0) === 0xE0) {
// 三字节字符 (1110xxxx 10xxxxxx 10xxxxxx) - 中文常用
const byte2 = bytes[i++] & 0x3F
const byte3 = bytes[i++] & 0x3F
result += String.fromCharCode(((byte1 & 0x0F) << 12) | (byte2 << 6) | byte3)
} else if ((byte1 & 0xF8) === 0xF0) {
// 四字节字符 (11110xxx 10xxxxxx 10xxxxxx 10xxxxxx) - emoji等
const byte2 = bytes[i++] & 0x3F
const byte3 = bytes[i++] & 0x3F
const byte4 = bytes[i++] & 0x3F
const codePoint = ((byte1 & 0x07) << 18) | (byte2 << 12) | (byte3 << 6) | byte4
// 转换为 UTF-16 代理对
const surrogate = codePoint - 0x10000
result += String.fromCharCode(0xD800 + (surrogate >> 10), 0xDC00 + (surrogate & 0x3FF))
}
}
return result
}
/** /**
* @description AI对话相关接口直接调用ai模块 * @description AI对话相关接口直接调用ai模块
* @filename aiChat.ts * @filename aiChat.ts
@@ -128,8 +173,8 @@ export const aiChatAPI = {
// 监听分块数据 // 监听分块数据
requestTask.onChunkReceived((res: any) => { requestTask.onChunkReceived((res: any) => {
try { try {
const decoder = new TextDecoder('utf-8') // 兼容微信小程序真机环境(不支持 TextDecoder
const text = decoder.decode(new Uint8Array(res.data)) const text = arrayBufferToString(res.data)
const lines = text.split('\n') const lines = text.split('\n')
for (const line of lines) { for (const line of lines) {

View File

@@ -1,100 +1,100 @@
{ {
"name": "泰豪小电", "name" : "泰豪小电",
"appid": "__UNI__69FD573", "appid" : "__UNI__69FD573",
"description": "泰豪小电智能工单系统", "description" : "泰豪小电智能工单系统",
"versionName": "1.0.0", "versionName" : "1.0.0",
"versionCode": "100", "versionCode" : "100",
"uni-app-x": {}, "uni-app-x" : {},
"quickapp": {}, "quickapp" : {},
"mp-weixin": { "mp-weixin" : {
"appid": "wx3708f41b1dc31f52", "appid" : "wx3708f41b1dc31f52",
"setting": { "setting" : {
"urlCheck": true, "urlCheck" : true,
"postcss": true, "postcss" : true,
"minified": true "minified" : true
}, },
"usingComponents": true, "usingComponents" : true,
"permission": { "permission" : {
"scope.camera": { "scope.camera" : {
"desc": "用于视频会议时开启摄像头" "desc" : "用于视频会议时开启摄像头"
}, },
"scope.record": { "scope.record" : {
"desc": "用于视频会议时开启麦克风" "desc" : "用于视频会议时开启麦克风"
} }
}, },
"requiredPrivateInfos": [] "requiredPrivateInfos" : []
}, },
"mp-alipay": { "mp-alipay" : {
"usingComponents": true "usingComponents" : true
}, },
"mp-baidu": { "mp-baidu" : {
"usingComponents": true "usingComponents" : true
}, },
"mp-toutiao": { "mp-toutiao" : {
"usingComponents": true "usingComponents" : true
}, },
"uniStatistics": { "uniStatistics" : {
"enable": false "enable" : false
}, },
"vueVersion": "3", "vueVersion" : "3",
"app": { "app" : {
"distribute": { "distribute" : {
"icons": { "icons" : {
"android": { "android" : {
"hdpi": "", "hdpi" : "",
"xhdpi": "", "xhdpi" : "",
"xxhdpi": "", "xxhdpi" : "",
"xxxhdpi": "" "xxxhdpi" : ""
} }
}, },
"android": { "android" : {
"permissions": [ "permissions" : [
"<uses-permission android:name=\"android.permission.CAMERA\"/>", "<uses-permission android:name=\"android.permission.CAMERA\"/>",
"<uses-permission android:name=\"android.permission.RECORD_AUDIO\"/>", "<uses-permission android:name=\"android.permission.RECORD_AUDIO\"/>",
"<uses-permission android:name=\"android.permission.MODIFY_AUDIO_SETTINGS\"/>" "<uses-permission android:name=\"android.permission.MODIFY_AUDIO_SETTINGS\"/>"
] ]
}, },
"ios": { "ios" : {
"privacyDescription": { "privacyDescription" : {
"NSCameraUsageDescription": "用于视频会议时开启摄像头", "NSCameraUsageDescription" : "用于视频会议时开启摄像头",
"NSMicrophoneUsageDescription": "用于视频会议时开启麦克风" "NSMicrophoneUsageDescription" : "用于视频会议时开启麦克风"
} }
} }
} }
}, },
"app-android": { "app-android" : {
"distribute": { "distribute" : {
"permissions": [ "permissions" : [
"<uses-permission android:name=\"android.permission.CAMERA\"/>", "<uses-permission android:name=\"android.permission.CAMERA\"/>",
"<uses-permission android:name=\"android.permission.RECORD_AUDIO\"/>", "<uses-permission android:name=\"android.permission.RECORD_AUDIO\"/>",
"<uses-permission android:name=\"android.permission.MODIFY_AUDIO_SETTINGS\"/>" "<uses-permission android:name=\"android.permission.MODIFY_AUDIO_SETTINGS\"/>"
], ],
"modules": {}, "modules" : {},
"icons": { "icons" : {
"hdpi": "", "hdpi" : "",
"xhdpi": "", "xhdpi" : "",
"xxhdpi": "", "xxhdpi" : "",
"xxxhdpi": "" "xxxhdpi" : ""
}, },
"splashScreens": { "splashScreens" : {
"default": {} "default" : {}
} }
} }
}, },
"app-ios": { "app-ios" : {
"distribute": { "distribute" : {
"privacyDescription": { "privacyDescription" : {
"NSCameraUsageDescription": "用于视频会议时开启摄像头", "NSCameraUsageDescription" : "用于视频会议时开启摄像头",
"NSMicrophoneUsageDescription": "用于视频会议时开启麦克风" "NSMicrophoneUsageDescription" : "用于视频会议时开启麦克风"
}, },
"modules": {}, "modules" : {},
"icons": {}, "icons" : {},
"splashScreens": {} "splashScreens" : {}
} }
}, },
"web": { "web" : {
"router": { "router" : {
"mode": "" "mode" : ""
} }
} }
} }