diff --git a/packages/uni-app-plus/dist/uni-app-service.es.js b/packages/uni-app-plus/dist/uni-app-service.es.js index 0f1ef1e390133887e0cb0ab7bab93c3c23591739..7bdfd425b3c46f70239a2e7cedc2ab57e144fd47 100644 --- a/packages/uni-app-plus/dist/uni-app-service.es.js +++ b/packages/uni-app-plus/dist/uni-app-service.es.js @@ -6397,7 +6397,7 @@ var serviceContext = (function (vue) { var ZStream = zstream; - var toString$1 = Object.prototype.toString; + var toString$2 = Object.prototype.toString; /* Public constants ==========================================================*/ /* ===========================================================================*/ @@ -6561,7 +6561,7 @@ var serviceContext = (function (vue) { if (typeof opt.dictionary === 'string') { // If we need to compress text, change encoding to utf8. dict = strings.string2buf(opt.dictionary); - } else if (toString$1.call(opt.dictionary) === '[object ArrayBuffer]') { + } else if (toString$2.call(opt.dictionary) === '[object ArrayBuffer]') { dict = new Uint8Array(opt.dictionary); } else { dict = opt.dictionary; @@ -6619,7 +6619,7 @@ var serviceContext = (function (vue) { if (typeof data === 'string') { // If we need to compress text, change encoding to utf8. strm.input = strings.string2buf(data); - } else if (toString$1.call(data) === '[object ArrayBuffer]') { + } else if (toString$2.call(data) === '[object ArrayBuffer]') { strm.input = new Uint8Array(data); } else { strm.input = data; @@ -9181,7 +9181,7 @@ var serviceContext = (function (vue) { var GZheader = gzheader; - var toString = Object.prototype.toString; + var toString$1 = Object.prototype.toString; /** * class Inflate @@ -9322,7 +9322,7 @@ var serviceContext = (function (vue) { // Convert data if needed if (typeof opt.dictionary === 'string') { opt.dictionary = strings.string2buf(opt.dictionary); - } else if (toString.call(opt.dictionary) === '[object ArrayBuffer]') { + } else if (toString$1.call(opt.dictionary) === '[object ArrayBuffer]') { opt.dictionary = new Uint8Array(opt.dictionary); } if (opt.raw) { //In raw mode we need to set the dictionary early @@ -9380,7 +9380,7 @@ var serviceContext = (function (vue) { if (typeof data === 'string') { // Only binary strings can be decompressed on practice strm.input = strings.binstring2buf(data); - } else if (toString.call(data) === '[object ArrayBuffer]') { + } else if (toString$1.call(data) === '[object ArrayBuffer]') { strm.input = new Uint8Array(data); } else { strm.input = data; @@ -14920,20 +14920,24 @@ var serviceContext = (function (vue) { const options = { method, url: url.trim(), - // weex 官方文档有误,headers 类型实际 object,用 string 类型会无响应 headers, type: responseType === 'arraybuffer' ? 'base64' : 'text', - // weex 官方文档未说明实际支持 timeout,单位:ms timeout: timeout || 6e5, // 配置和weex模块内相反 sslVerify: !sslVerify, firstIpv4: firstIpv4, tls, }; + let withArrayBuffer = false; if (method !== 'GET') { - options.body = typeof data === 'string' ? data : JSON.stringify(data); + if (toString.call(data) === '[object ArrayBuffer]') { + withArrayBuffer = true; + } + else { + options.body = typeof data === 'string' ? data : JSON.stringify(data); + } } - stream.fetch(options, ({ ok, status, data, headers, errorMsg, }) => { + const callback = ({ ok, status, data, headers, errorMsg, }) => { if (aborted) { return; } @@ -14958,7 +14962,16 @@ var serviceContext = (function (vue) { } reject(errMsg); } - }); + }; + if (withArrayBuffer) { + stream.fetchWithArrayBuffer({ + '@type': 'binary', + base64: arrayBufferToBase64(data) + }, options, callback); + } + else { + stream.fetch(options, callback); + } return new RequestTask({ abort() { aborted = true; diff --git a/packages/uni-app-plus/src/service/api/network/request.ts b/packages/uni-app-plus/src/service/api/network/request.ts index 0140bfcfee6d2db569a0ee4d60ca5663cf08d727..d424cab4ec6ad8c17954856190ee351980033b28 100644 --- a/packages/uni-app-plus/src/service/api/network/request.ts +++ b/packages/uni-app-plus/src/service/api/network/request.ts @@ -11,17 +11,10 @@ import { ConfigMTLSOptions, ConfigMTLSProtocol, } from '@dcloudio/uni-api' -import { base64ToArrayBuffer } from '@dcloudio/uni-api' +import { base64ToArrayBuffer, arrayBufferToBase64 } from '@dcloudio/uni-api' import { requireNativePlugin } from '../plugin/requireNativePlugin' +import { Stream, FetchOptions, FetchCallback, FetchHeaders } from './stream' -type Type = 'base64' | 'text' -type Headers = Record -type Options = UniApp.RequestOptions & { - tls: any - headers: Headers - type: Type - body?: string | Data -} interface RequestTasks { abort: Function } @@ -139,8 +132,8 @@ export const request = defineTaskApi( data = JSON.stringify(data) } - const stream = requireNativePlugin('stream') - const headers: Headers = {} + const stream: Stream = requireNativePlugin('stream') + const headers: FetchHeaders = {} let abortTimeout: ReturnType let aborted: boolean let hasContentType = false @@ -183,70 +176,74 @@ export const request = defineTaskApi( }, timeout + 200) // TODO +200 发消息到原生层有时间开销,以后考虑由原生层回调超时 } - const options: Options = { + const options: FetchOptions = { method, url: url.trim(), - // weex 官方文档有误,headers 类型实际 object,用 string 类型会无响应 headers, type: responseType === 'arraybuffer' ? 'base64' : 'text', - // weex 官方文档未说明实际支持 timeout,单位:ms timeout: timeout || 6e5, // 配置和weex模块内相反 sslVerify: !sslVerify, firstIpv4: firstIpv4, tls, } - + let withArrayBuffer: boolean = false if (method !== 'GET') { - options.body = typeof data === 'string' ? data : JSON.stringify(data) + if (toString.call(data) === '[object ArrayBuffer]') { + withArrayBuffer = true + } else { + options.body = typeof data === 'string' ? data : JSON.stringify(data) + } } - - stream.fetch( - options, - ({ - ok, - status, - data, - headers, - errorMsg, - }: { - ok: boolean - status: number - data: string - headers: Headers - errorMsg: string - }) => { - if (aborted) { - return - } - if (abortTimeout) { - clearTimeout(abortTimeout) - } - const statusCode = status - if (statusCode > 0) { - resolve( - formatResponse( - { - data: - ok && responseType === 'arraybuffer' - ? base64ToArrayBuffer(data) - : data, - statusCode, - header: headers, - cookies: cookiesParse(headers), - }, - args - ) + const callback: FetchCallback = ({ + ok, + status, + data, + headers, + errorMsg, + }) => { + if (aborted) { + return + } + if (abortTimeout) { + clearTimeout(abortTimeout) + } + const statusCode = status + if (statusCode > 0) { + resolve( + formatResponse( + { + data: + ok && responseType === 'arraybuffer' + ? base64ToArrayBuffer(data) + : data, + statusCode, + header: headers, + cookies: cookiesParse(headers), + }, + args ) - } else { - let errMsg = 'abort statusCode:' + statusCode - if (errorMsg) { - errMsg = errMsg + ' ' + errorMsg - } - reject(errMsg) + ) + } else { + let errMsg = 'abort statusCode:' + statusCode + if (errorMsg) { + errMsg = errMsg + ' ' + errorMsg } + reject(errMsg) } - ) + } + if (withArrayBuffer) { + stream.fetchWithArrayBuffer( + { + '@type': 'binary', + base64: arrayBufferToBase64(data as ArrayBuffer), + }, + options, + callback + ) + } else { + stream.fetch(options, callback) + } return new RequestTask({ abort() { diff --git a/packages/uni-app-plus/src/service/api/network/stream.d.ts b/packages/uni-app-plus/src/service/api/network/stream.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..19e0efe3f7f668751061f6decb724415835e1723 --- /dev/null +++ b/packages/uni-app-plus/src/service/api/network/stream.d.ts @@ -0,0 +1,34 @@ +export type FetchHeaders = Record +export interface FetchOptions { + method?: string + url: string + // weex 官方文档有误,headers 类型实际 object,用 string 类型会无响应 + headers?: FetchHeaders + type?: 'base64' | 'text' + // weex 官方文档未说明实际支持 timeout,单位:ms + timeout?: number + sslVerify?: boolean + firstIpv4?: boolean + tls?: any + body?: string | Data +} +export interface FetchResult { + ok: boolean + status: number + data: string + headers: FetchHeaders + errorMsg: string +} +export type FetchCallback = (result: FetchResult) => void +export interface FetchArrayBuffer { + '@type': 'binary' + base64: string +} +export interface Stream { + fetch: (options: FetchOptions, callback: FetchCallback) => void + fetchWithArrayBuffer: ( + body: FetchArrayBuffer, + options: FetchOptions, + callback: FetchCallback + ) => void +}