解决小程序上传时出现的问题。

This commit is contained in:
cst61
2026-02-01 10:55:35 +08:00
parent 802ffbb2f3
commit eef85dce4a
22 changed files with 735 additions and 645 deletions

View File

@@ -54,41 +54,38 @@ function setupRequestInterceptor() {
// 重写 uni.request自动添加 tenantId 和 token
uni.request = function(options = {}) {
const { url, header = {}, method = 'GET', ...restOptions } = options
// 详细的拦截器日志
console.log('[Request Interceptor] 开始拦截请求:', {
url: url,
method: method.toUpperCase(),
isLoginApi: isLoginApi(url),
originalOptions: options
})
// 获取 tenantId
let tenantId = ''
try {
tenantId = uni.getStorageSync('backend-tenant-id') || ''
} catch (e) {
console.error('获取 tenantId 失败:', e)
console.error('[Request Interceptor] 获取 tenantId 失败:', e)
}
console.log('[Request Interceptor] tenantId状态:', {
tenantId: tenantId,
hasTenantId: !!tenantId
})
// 检查是否需要登录如果不是登录接口且租户ID为空
// 只在页面完全加载后才跳转,避免扫码进入时页面空白
// 在体验版中,不立即跳转到登录页面,而是让请求继续执行
// 由后端返回401状态码前端再处理登录跳转
if (!isLoginApi(url) && !tenantId) {
// 获取当前页面栈,检查是否页面已完全加载
const pages = getCurrentPages()
const hasLoadedPages = pages.length > 0 && pages[pages.length - 1].route
// 如果页面已加载,检查是否在首页或登录相关页面
if (hasLoadedPages) {
const currentPage = pages[pages.length - 1]
const currentRoute = '/' + currentPage.route
// 在首页或登录相关页面时,不立即跳转,让页面先加载
const isHomePage = currentRoute.includes('/pages/furniture_reception/furniture_reception')
const isLoginRelated = currentRoute.includes('/uni_modules/uni-id-pages/pages/login') ||
currentRoute.includes('/uni_modules/uni-id-pages/pages/register') ||
currentRoute.includes('/uni_modules/uni-id-pages/pages/retrieve')
if (!isHomePage && !isLoginRelated) {
console.warn('[Request Interceptor] 租户ID为空跳转到登录页面')
redirectToLogin()
// 返回一个被拒绝的Promise阻止请求继续执行
return Promise.reject(new Error('未登录,请先登录'))
}
}
// 如果页面还没加载完成,不立即跳转,避免扫码进入时空白
console.warn('[Request Interceptor] 租户ID为空但允许请求继续执行由后端处理认证')
// 不阻止请求让后端返回401前端再处理
} else if (isLoginApi(url)) {
console.log('[Request Interceptor] 这是登录接口跳过租户ID检查')
} else {
console.log('[Request Interceptor] 租户ID存在正常处理请求')
}
// 构建新的 header保留原有的 header避免覆盖
@@ -133,15 +130,35 @@ function setupRequestInterceptor() {
console.error('获取 roleName 或 scenario 失败:', e)
}
// 记录最终的请求参数
const finalRequestOptions = {
...restOptions,
url,
method,
header: newHeader
}
console.log('[Request Interceptor] 准备发送请求:', {
url: url,
method: method.toUpperCase(),
headers: newHeader,
hasData: !!restOptions.data
})
// 统一使用 Promise 方式,确保正确返回响应
// 这样无论调用方使用 await 还是回调,都能正常工作
return new Promise((resolve, reject) => {
console.log('[Request Interceptor] 开始调用原始 uni.request')
originalRequest.call(uni, {
...restOptions,
url,
method,
header: newHeader,
...finalRequestOptions,
success: (res) => {
console.log('[Request Interceptor] 请求成功:', {
url: url,
method: method.toUpperCase(),
statusCode: res.statusCode,
hasData: !!res.data
})
// 如果提供了 success 回调,先执行它
if (options.success) {
options.success(res)
@@ -153,7 +170,8 @@ function setupRequestInterceptor() {
console.error('[Request Interceptor] 请求失败:', {
url: url,
method: method.toUpperCase(),
error: err
error: err,
errorMessage: err.errMsg || '未知错误'
})
// 如果提供了 fail 回调,先执行它
if (options.fail) {
@@ -163,6 +181,11 @@ function setupRequestInterceptor() {
reject(err)
},
complete: (res) => {
console.log('[Request Interceptor] 请求完成:', {
url: url,
method: method.toUpperCase(),
hasResponse: !!res
})
// 如果提供了 complete 回调,执行它
if (options.complete) {
options.complete(res)

View File

@@ -1,516 +0,0 @@
/**
* 蓝牙录音设备管理工具类
* 支持蓝牙设备搜索、连接、数据接收等功能
*/
class BluetoothRecorder {
constructor() {
this.adapterState = false // 蓝牙适配器状态
this.isScanning = false // 是否正在扫描
this.connectedDeviceId = null // 已连接的设备ID
this.deviceList = [] // 发现的设备列表
this.services = [] // 蓝牙服务列表
this.characteristics = [] // 特征值列表
this.notifyCharacteristicId = null // 用于接收数据的特征值ID
this.writeCharacteristicId = null // 用于发送数据的特征值ID
// 音频数据缓冲区
this.audioBuffer = []
this.isReceiving = false
// 回调函数
this.onDeviceFound = null // 发现设备回调
this.onDeviceConnected = null // 设备连接回调
this.onDeviceDisconnected = null // 设备断开回调
this.onAudioDataReceived = null // 音频数据接收回调
this.onError = null // 错误回调
}
/**
* 初始化蓝牙适配器
*/
async initBluetoothAdapter() {
return new Promise((resolve, reject) => {
uni.openBluetoothAdapter({
success: (res) => {
console.log('蓝牙适配器初始化成功', res)
this.adapterState = true
this._listenAdapterState()
resolve(res)
},
fail: (err) => {
console.error('蓝牙适配器初始化失败', err)
this.adapterState = false
this._handleError('蓝牙适配器初始化失败', err)
reject(err)
}
})
})
}
/**
* 监听蓝牙适配器状态变化
*/
_listenAdapterState() {
uni.onBluetoothAdapterStateChange((res) => {
console.log('蓝牙适配器状态变化', res)
this.adapterState = res.available
if (!res.available) {
this.connectedDeviceId = null
this._handleError('蓝牙适配器不可用', res)
}
})
}
/**
* 开始搜索蓝牙设备
* @param {Object} options - 搜索选项
* @param {Array} options.services - 服务UUID列表可选
* @param {number} options.interval - 上报间隔(毫秒)
* @param {boolean} options.allowDuplicatesKey - 是否允许重复上报
*/
async startScan(options = {}) {
if (this.isScanning) {
console.warn('已经在扫描中')
return
}
if (!this.adapterState) {
await this.initBluetoothAdapter()
}
return new Promise((resolve, reject) => {
// 监听设备发现
uni.onBluetoothDeviceFound((res) => {
const devices = res.devices || []
devices.forEach(device => {
// 过滤已存在的设备
const exists = this.deviceList.find(d => d.deviceId === device.deviceId)
if (!exists) {
this.deviceList.push({
deviceId: device.deviceId,
name: device.name || '未知设备',
RSSI: device.RSSI,
advertisData: device.advertisData,
advertisServiceUUIDs: device.advertisServiceUUIDs,
localName: device.localName
})
// 触发设备发现回调
if (this.onDeviceFound) {
this.onDeviceFound(device)
}
}
})
})
// 开始搜索
uni.startBluetoothDevicesDiscovery({
services: options.services || [],
allowDuplicatesKey: options.allowDuplicatesKey || false,
interval: options.interval || 0,
success: (res) => {
console.log('开始搜索蓝牙设备', res)
this.isScanning = true
resolve(res)
},
fail: (err) => {
console.error('搜索蓝牙设备失败', err)
this.isScanning = false
this._handleError('搜索蓝牙设备失败', err)
reject(err)
}
})
})
}
/**
* 停止搜索蓝牙设备
*/
async stopScan() {
if (!this.isScanning) {
return
}
return new Promise((resolve, reject) => {
uni.stopBluetoothDevicesDiscovery({
success: (res) => {
console.log('停止搜索蓝牙设备', res)
this.isScanning = false
resolve(res)
},
fail: (err) => {
console.error('停止搜索失败', err)
this._handleError('停止搜索失败', err)
reject(err)
}
})
})
}
/**
* 连接蓝牙设备
* @param {string} deviceId - 设备ID
*/
async connectDevice(deviceId) {
if (this.connectedDeviceId === deviceId) {
console.warn('设备已连接')
return
}
return new Promise((resolve, reject) => {
uni.createBLEConnection({
deviceId: deviceId,
success: async (res) => {
console.log('蓝牙设备连接成功', res)
this.connectedDeviceId = deviceId
// 监听连接状态
this._listenConnectionState(deviceId)
try {
// 获取服务列表
await this.getServices(deviceId)
// 触发连接成功回调
if (this.onDeviceConnected) {
this.onDeviceConnected(deviceId)
}
resolve(res)
} catch (err) {
reject(err)
}
},
fail: (err) => {
console.error('蓝牙设备连接失败', err)
this._handleError('蓝牙设备连接失败', err)
reject(err)
}
})
})
}
/**
* 监听连接状态
*/
_listenConnectionState(deviceId) {
uni.onBLEConnectionStateChange((res) => {
console.log('蓝牙连接状态变化', res)
if (res.deviceId === deviceId) {
if (!res.connected) {
// 连接断开
this.connectedDeviceId = null
this.isReceiving = false
if (this.onDeviceDisconnected) {
this.onDeviceDisconnected(deviceId)
}
}
}
})
}
/**
* 断开蓝牙设备连接
*/
async disconnectDevice() {
if (!this.connectedDeviceId) {
return
}
return new Promise((resolve, reject) => {
uni.closeBLEConnection({
deviceId: this.connectedDeviceId,
success: (res) => {
console.log('蓝牙设备断开成功', res)
this.connectedDeviceId = null
this.isReceiving = false
this.audioBuffer = []
resolve(res)
},
fail: (err) => {
console.error('蓝牙设备断开失败', err)
this._handleError('蓝牙设备断开失败', err)
reject(err)
}
})
})
}
/**
* 获取蓝牙设备服务列表
* @param {string} deviceId - 设备ID
*/
async getServices(deviceId) {
return new Promise((resolve, reject) => {
uni.getBLEDeviceServices({
deviceId: deviceId,
success: async (res) => {
console.log('获取服务列表成功', res)
this.services = res.services || []
// 遍历服务,获取特征值
for (const service of this.services) {
try {
await this.getCharacteristics(deviceId, service.uuid)
} catch (err) {
console.warn('获取特征值失败', service.uuid, err)
}
}
resolve(res)
},
fail: (err) => {
console.error('获取服务列表失败', err)
this._handleError('获取服务列表失败', err)
reject(err)
}
})
})
}
/**
* 获取特征值列表
* @param {string} deviceId - 设备ID
* @param {string} serviceId - 服务UUID
*/
async getCharacteristics(deviceId, serviceId) {
return new Promise((resolve, reject) => {
uni.getBLEDeviceCharacteristics({
deviceId: deviceId,
serviceId: serviceId,
success: (res) => {
console.log('获取特征值成功', serviceId, res)
const chars = res.characteristics || []
this.characteristics.push(...chars)
// 查找可用于通知和写入的特征值
chars.forEach(char => {
// 查找可通知的特征值(用于接收音频数据)
if (char.properties.notify || char.properties.indicate) {
if (!this.notifyCharacteristicId) {
this.notifyCharacteristicId = {
deviceId: deviceId,
serviceId: serviceId,
characteristicId: char.uuid
}
// 启用通知
this.enableNotify(deviceId, serviceId, char.uuid)
}
}
// 查找可写入的特征值(用于发送命令)
if (char.properties.write || char.properties.writeNoResponse) {
if (!this.writeCharacteristicId) {
this.writeCharacteristicId = {
deviceId: deviceId,
serviceId: serviceId,
characteristicId: char.uuid
}
}
}
})
resolve(res)
},
fail: (err) => {
console.error('获取特征值失败', err)
reject(err)
}
})
})
}
/**
* 启用特征值通知(用于接收音频数据)
* @param {string} deviceId - 设备ID
* @param {string} serviceId - 服务UUID
* @param {string} characteristicId - 特征值UUID
*/
async enableNotify(deviceId, serviceId, characteristicId) {
return new Promise((resolve, reject) => {
uni.notifyBLECharacteristicValueChange({
deviceId: deviceId,
serviceId: serviceId,
characteristicId: characteristicId,
state: true, // 启用通知
success: (res) => {
console.log('启用通知成功', res)
// 监听特征值变化(接收音频数据)
uni.onBLECharacteristicValueChange((res) => {
this._handleAudioData(res.value)
})
this.isReceiving = true
resolve(res)
},
fail: (err) => {
console.error('启用通知失败', err)
this._handleError('启用通知失败', err)
reject(err)
}
})
})
}
/**
* 处理接收到的音频数据
* @param {ArrayBuffer} data - 音频数据
*/
_handleAudioData(data) {
// 将 ArrayBuffer 转换为 Base64 或保存到缓冲区
const base64 = uni.arrayBufferToBase64(data)
// 添加到缓冲区
this.audioBuffer.push({
data: base64,
timestamp: Date.now()
})
// 触发回调
if (this.onAudioDataReceived) {
this.onAudioDataReceived({
data: base64,
arrayBuffer: data,
timestamp: Date.now()
})
}
}
/**
* 读取特征值
* @param {string} deviceId - 设备ID
* @param {string} serviceId - 服务UUID
* @param {string} characteristicId - 特征值UUID
*/
async readCharacteristic(deviceId, serviceId, characteristicId) {
return new Promise((resolve, reject) => {
uni.readBLECharacteristicValue({
deviceId: deviceId,
serviceId: serviceId,
characteristicId: characteristicId,
success: (res) => {
console.log('读取特征值成功', res)
resolve(res)
},
fail: (err) => {
console.error('读取特征值失败', err)
this._handleError('读取特征值失败', err)
reject(err)
}
})
})
}
/**
* 写入特征值(发送命令到设备)
* @param {ArrayBuffer} value - 要写入的数据
*/
async writeCharacteristic(value) {
if (!this.writeCharacteristicId) {
throw new Error('未找到可写入的特征值')
}
const { deviceId, serviceId, characteristicId } = this.writeCharacteristicId
return new Promise((resolve, reject) => {
uni.writeBLECharacteristicValue({
deviceId: deviceId,
serviceId: serviceId,
characteristicId: characteristicId,
value: value,
success: (res) => {
console.log('写入特征值成功', res)
resolve(res)
},
fail: (err) => {
console.error('写入特征值失败', err)
this._handleError('写入特征值失败', err)
reject(err)
}
})
})
}
/**
* 获取已缓存的音频数据
*/
getAudioBuffer() {
return this.audioBuffer
}
/**
* 清空音频缓冲区
*/
clearAudioBuffer() {
this.audioBuffer = []
}
/**
* 合并音频数据为完整文件Base64格式
*/
mergeAudioData() {
return this.audioBuffer.map(item => item.data).join('')
}
/**
* 处理错误
*/
_handleError(message, error) {
console.error(`[BluetoothRecorder] ${message}`, error)
if (this.onError) {
this.onError({
message,
error
})
}
}
/**
* 关闭蓝牙适配器
*/
async closeBluetoothAdapter() {
// 先断开连接
if (this.connectedDeviceId) {
await this.disconnectDevice()
}
// 停止扫描
if (this.isScanning) {
await this.stopScan()
}
return new Promise((resolve, reject) => {
uni.closeBluetoothAdapter({
success: (res) => {
console.log('关闭蓝牙适配器成功', res)
this.adapterState = false
resolve(res)
},
fail: (err) => {
console.error('关闭蓝牙适配器失败', err)
reject(err)
}
})
})
}
/**
* 获取设备列表
*/
getDeviceList() {
return this.deviceList
}
/**
* 清空设备列表
*/
clearDeviceList() {
this.deviceList = []
}
}
// 导出单例
export default new BluetoothRecorder()

View File

@@ -113,9 +113,20 @@ export function request(options = {}) {
const fullUrl = url.startsWith('http') ? url : getApiUrl(url)
const apiEnv = typeof getApiEnv === 'function' ? getApiEnv() : ''
// 详细的请求日志
console.log('[Request] 开始处理请求:', {
originalUrl: url,
fullUrl: fullUrl,
method: method.toUpperCase(),
isLoginApi: isLoginApi(fullUrl),
apiEnv: apiEnv,
needTenantId,
needToken
})
// 每次请求打印完整 URL格式示例
// H5环境(local)直接使用完整URL: http://localhost:8090/api/audioManagement/list
console.log(`H5环境(${apiEnv})直接使用完整URL: ${fullUrl}`)
console.log(`[Request] 完整请求URL: ${fullUrl}`)
// 构建请求头
const requestHeader = {
@@ -169,7 +180,16 @@ export function request(options = {}) {
}
// 发起请求
console.log('[Request] 准备发起 uni.request:', {
url: fullUrl,
method: method.toUpperCase(),
hasData: !!requestData,
hasHeaders: !!requestHeader,
timeout: timeout
})
return new Promise((resolve, reject) => {
console.log('[Request] 调用 uni.request 开始执行')
uni.request({
url: fullUrl,
method: method.toUpperCase(),
@@ -178,13 +198,30 @@ export function request(options = {}) {
timeout,
...restOptions,
success: (res) => {
console.log('[Request] 收到响应:', {
url: fullUrl,
method: method.toUpperCase(),
statusCode: res.statusCode,
hasData: !!res.data,
dataType: typeof res.data
})
// 检查HTTP状态码如果是401未授权跳转到登录页面
if (res.statusCode === 401) {
console.warn('[Request] 收到401状态码跳转到登录页面')
redirectToLogin()
reject(new Error('登录已过期,请重新登录'))
return
}
resolve(res)
},
fail: (err) => {
console.error('[Request] 请求失败:', {
url: fullUrl,
method: method.toUpperCase(),
error: err
error: err,
errorMessage: err.errMsg || '未知错误',
errorCode: err.code || '无错误码'
})
reject(err)
}