// utils/upload.ts /** * 获取平台特定的上传方法 */ const getPlatformUploadAPI = (): any => { // #ifdef H5 || APP-PLUS return uni.uploadFile // #endif // #ifdef MP-WEIXIN return wx.uploadFile // #endif // #ifdef MP-ALIPAY return my.uploadFile || my.httpRequest // #endif // #ifdef MP-BAIDU return swan.uploadFile // #endif // #ifdef MP-TOUTIAO return tt.uploadFile // #endif // #ifdef MP-KUAISHOU return ks.uploadFile // #endif // 默认返回 uni.uploadFile return uni.uploadFile } /** * 获取平台特定的请求头处理 */ const getPlatformHeaders = (headers: any, platform: any): any => { // 微信小程序需要特殊处理 if (platform === 'wx') { // 微信小程序中,header 字段名是小写的 const wxHeaders: any = {} Object.keys(headers).forEach(key => { wxHeaders[key.toLowerCase()] = headers[key] }) return wxHeaders } return headers } /** * 获取平台特定的请求配置 */ const getPlatformConfig = (config: any, platform: any): any => { const baseConfig = { url: config.url, filePath: config.filePath, name: config.name || 'file', formData: config.formData || {}, timeout: config.timeout || 60000, complete: config.complete, success: config.success, fail: config.fail } // 平台特定的配置调整 switch (platform) { case 'wx': // 微信小程序 return { ...baseConfig, header: getPlatformHeaders(config.header || {}, 'wx') } case 'alipay': // 支付宝小程序 return { ...baseConfig, headers: config.header || {}, // 支付宝使用 headers fileType: 'image' // 支付宝可能需要指定文件类型 } case 'baidu': // 百度小程序 return { ...baseConfig, header: config.header || {} } case 'tt': // 字节跳动小程序 return { ...baseConfig, header: config.header || {} } default: // uni-app 和其他平台 return { ...baseConfig, header: config.header || {}, withCredentials: config.withCredentials || false } } } /** * 获取当前平台标识 */ const getCurrentPlatform = (): any => { // #ifdef MP-WEIXIN return 'wx' // #endif // #ifdef MP-ALIPAY return 'alipay' // #endif // #ifdef MP-BAIDU return 'baidu' // #endif // #ifdef MP-TOUTIAO return 'tt' // #endif // #ifdef MP-KUAISHOU return 'ks' // #endif // #ifdef H5 return 'h5' // #endif // #ifdef APP-PLUS return 'app' // #endif return 'uni' } /** * 多平台兼容的文件上传方法 */ export const universalUploadFile = (config: any): any => { const platform = getCurrentPlatform() const platformConfig = getPlatformConfig(config, platform) const uploadAPI = getPlatformUploadAPI() return new Promise((resolve: any, reject: any) => { // 平台特定的成功回调处理 const successCallback = (res: any) => { try { // 统一处理返回数据格式 let data = res.data || res if (typeof data === 'string') { try { data = JSON.parse(data) } catch (e) { // 如果不是 JSON,保持原样 } } // 构建统一的响应格式 const unifiedResponse = { statusCode: res.statusCode || 200, data, errMsg: res.errMsg || 'uploadFile:ok' } // 调用原始 success 回调 if (config.success) { config.success(unifiedResponse) } resolve(unifiedResponse) } catch (error) { reject(error) } } // 平台特定的失败回调处理 const failCallback = (err: any) => { const error = { errMsg: err.errMsg || 'uploadFile:fail', detail: err } if (config.fail) { config.fail(error) } reject(error) } // 执行上传 try { if (platform === 'alipay' && my.uploadFile) { // 支付宝小程序特殊处理 my.uploadFile({ url: platformConfig.url, fileType: 'image', fileName: 'file', filePath: platformConfig.filePath, formData: platformConfig.formData, headers: platformConfig.headers, success: successCallback, fail: failCallback, complete: platformConfig.complete }) } else { // 其他平台 uploadAPI({ ...platformConfig, success: successCallback, fail: failCallback }) } } catch (error) { reject({ errMsg: 'uploadFile:fail', detail: error }) } }) }