export type Request = (param: RequestOptions) => RequestTask; /** * 网络请求参数 */ export type RequestOptions = { /** * 开发者服务器接口地址 */ url: string, /** * 请求的参数 Object|String类型 * @type {RequestDataOptions} * @defaultValue null */ data?: any | null, /** * 设置请求的 header,header 中不能设置 Referer * @defaultValue null */ header?: UTSJSONObject, /** * 请求类型 默认值GET * GET|POST|PUT|DELETE|HEAD|OPTIONS * @type {RequestMethod} * @defaultValue "GET" */ method?: "GET" | "POST" | "PUT" | "PATCH" | "DELETE" | "HEAD" | "OPTIONS"| null, /** * 超时时间,单位 ms * @defaultValue 60000 */ timeout?: number | null, /** * 如果设为 json,会对返回的数据进行一次 JSON.parse,非 json 不会进行 JSON.parse * @defaultValue "json" * @deprecated 不支持 * @autodoc false */ dataType?: string | null, /** * 设置响应的数据类型。 * * @deprecated 不支持 * @autodoc false */ responseType?: string | null, /** * 验证 ssl 证书 * @deprecated 不支持 * @autodoc false */ sslVerify?: boolean | null, /** * 跨域请求时是否携带凭证(cookies) * * @uniPlatform { * "app": { * "android": { * "osVer": "4.4", * "uniVer": "√", * "unixVer": "x" * }, * "ios": { * "osVer": "9.0", * "uniVer": "√", * "unixVer": "x" * } * } * } */ withCredentials?: boolean | null, /** * DNS解析时优先使用ipv4 * @defaultValue false */ firstIpv4?: boolean | null, /** * 网络请求成功回调。 * @defaultValue null */ success?: RequestSuccessCallback | null, /** * 网络请求失败回调。 * @defaultValue null */ fail?: RequestFailCallback | null, /** * 网络请求完成回调,成功或者失败都会调用。 * @defaultValue null */ complete?: RequestCompleteCallback | null } export type RequestSuccess = { /** * 开发者服务器返回的数据 * @type {RequestDataOptions} */ data: any | null, /** * 开发者服务器返回的 HTTP 状态码 */ statusCode: number, /** * 开发者服务器返回的 HTTP Response Header */ header: any, /** * 开发者服务器返回的 cookies,格式为字符串数组 */ cookies: Array } export type RequestFail = UniError; export type RequestSuccessCallback = (option: RequestSuccess) => void; export type RequestFailCallback = (option: RequestFail) => void; export type RequestCompleteCallback = (option: any) => void; export interface RequestTask { /** * abort() * @description * 中断网络请求。 * @param {void} * @return {void} * @tutorial https://uniapp.dcloud.net.cn/api/request/request.html#request * @uniPlatform { * "app": { * "android": { * "osVer": "4.4", * "uniVer": "√", * "unixVer": "3.9+" * }, * "ios": { * "osVer": "9.0", * "uniVer": "√", * "unixVer": "3.9+" * } * } * } * @example ```typescript var requestTask = uni.request({ url: 'http://192.168.12.106:8080/postHalo', //仅为示例,并非真实接口地址。 complete: ()=> {} }); requestTask.abort(); ``` */ abort(): void } //===============================上传================================== export type UploadFile = (options: UploadFileOptions) => UploadTask; export type UploadFileOptionFiles = { /** * multipart 提交时,表单的项目名,默认为 file,如果 name 不填或填的值相同,可能导致服务端读取文件时只能读取到一个文件。 */ name: string | null, /** * 要上传文件资源的路径 */ uri: string, /** * 要上传的文件对象,仅H5(2.6.15+)支持 * * @uniPlatform { * "app": { * "android": { * "osVer": "4.4", * "uniVer": "√", * "unixVer": "x" * }, * "ios": { * "osVer": "9.0", * "uniVer": "√", * "unixVer": "x" * } * } * } */ file: any | null }; export type UploadFileSuccess = { /** * 开发者服务器返回的数据 */ data: string, /** * 开发者服务器返回的 HTTP 状态码 */ statusCode: number }; export type UploadFileSuccessCallback = (result: UploadFileSuccess) => void; export type UploadFileFail = UniError; export type UploadFileFailCallback = (result: UploadFileFail) => void; export type UploadFileCompleteCallback = (result: any) => void; export type UploadFileOptions = { /** * 开发者服务器 url */ url: string, /** * 要上传文件资源的路径 */ filePath?: string | null, /** * 文件对应的 key , 开发者在服务器端通过这个 key 可以获取到文件二进制内容 */ name?: string | null, /** * 需要上传的文件列表。 */ files?: (UploadFileOptionFiles[]) | null, /** * HTTP 请求 Header, header 中不能设置 Referer */ header?: UTSJSONObject | null, /** * HTTP 请求中其他额外的 form data */ formData?: UTSJSONObject | null, /** * 超时时间,单位 ms */ timeout?: number | null, /** * 成功返回的回调函数 */ success?: UploadFileSuccessCallback | null, /** * 失败的回调函数 */ fail?: UploadFileFailCallback | null, /** * 结束的回调函数(调用成功、失败都会执行) */ complete?: UploadFileCompleteCallback | null }; export type OnProgressUpdateResult = { /** * 上传进度百分比 */ progress: number, /** * 已经上传的数据长度,单位 Bytes */ totalBytesSent: number, /** * 预期需要上传的数据总长度,单位 Bytes */ totalBytesExpectedToSend: number }; export type UploadFileProgressUpdateCallback = (result: OnProgressUpdateResult) => void export interface UploadTask { /** * abort() * @description * 中断上传任务。 * @param {void} * @return {void} * @tutorial https://uniapp.dcloud.net.cn/api/request/network-file.html#uploadfile * @uniPlatform { * "app": { * "android": { * "osVer": "4.4", * "uniVer": "√", * "unixVer": "3.9+" * }, * "ios": { * "osVer": "9.0", * "uniVer": "√", * "unixVer": "x" * } * } * } * @example ```typescript var uploadTask = uni.uploadFile({ url: 'http://192.168.12.106:8080/uploadFile', //仅为示例,并非真实接口地址。 complete: ()=> {} }); uploadTask.abort(); ``` */ abort(): void, /** * onProgressUpdate() * @description * 监听上传进度变化。 * @param {UploadFileProgressUpdateCallback} callback * @return {void} * @tutorial https://uniapp.dcloud.net.cn/api/request/network-file.html#uploadfile * @uniPlatform { * "app": { * "android": { * "osVer": "4.4", * "uniVer": "√", * "unixVer": "3.9+" * }, * "ios": { * "osVer": "9.0", * "uniVer": "√", * "unixVer": "x" * } * } * } * @example ```typescript uploadTask.onProgressUpdate((res) => { console.log('上传进度' + res.progress); console.log('已经上传的数据长度' + res.totalBytesSent); console.log('预期需要上传的数据总长度' + res.totalBytesExpectedToSend); }); ``` */ onProgressUpdate(callback: UploadFileProgressUpdateCallback): void, }; //===============================下载================================== export type DownloadFile = (options: DownloadFileOptions) => DownloadTask; export type DownloadFileSuccess = { /** * 临时文件路径,下载后的文件会存储到一个临时文件 */ tempFilePath: string, /** * 开发者服务器返回的 HTTP 状态码 */ statusCode: number }; export type DownloadFileSuccessCallback = (result: DownloadFileSuccess) => void; export type DownloadFileFail = UniError; export type DownloadFileFailCallback = (result: DownloadFileFail) => void; export type DownloadFileComplete = any; export type DownloadFileCompleteCallback = (result: DownloadFileComplete) => void; export type DownloadFileOptions = { /** * 下载资源的 url */ url: string, /** * HTTP 请求 Header,header 中不能设置 Referer */ header?: UTSJSONObject | null, /** * 超时时间,单位 ms */ timeout?: number | null, /** * 下载成功后以 tempFilePath 的形式传给页面,res = {tempFilePath: '文件的临时路径'} */ success?: DownloadFileSuccessCallback | null, /** * 失败的回调函数 */ fail?: DownloadFileFailCallback | null, /** * 结束的回调函数(调用成功、失败都会执行) */ complete?: DownloadFileCompleteCallback | null }; export type OnProgressDownloadResult = { /** * 下载进度百分比 */ progress: number, /** * 已经下载的数据长度,单位 Bytes */ totalBytesWritten: number, /** * 预期需要下载的数据总长度,单位 Bytes */ totalBytesExpectedToWrite: number }; export type DownloadFileProgressUpdateCallback = (result: OnProgressDownloadResult) => void; export interface DownloadTask { /** * abort() * @description * 中断下载任务。 * @param {void} * @return {void} * @tutorial https://uniapp.dcloud.net.cn/api/request/network-file.html#downloadfile * @uniPlatform { * "app": { * "android": { * "osVer": "4.4", * "uniVer": "√", * "unixVer": "3.9+" * }, * "ios": { * "osVer": "9.0", * "uniVer": "√", * "unixVer": "x" * } * } * } * @example ```typescript var downloadTask = uni.downloadFile({ url: 'https://www.example.com/file/test', //仅为示例,并非真实接口地址。 complete: ()=> {} }); downloadTask.abort(); ``` */ abort(): void, /** * onProgressUpdate() * @description * 监听下载进度变化。 * @param {DownloadFileProgressUpdateCallback} callback * @return {void} * @tutorial https://uniapp.dcloud.net.cn/api/request/network-file.html#downloadfile * @uniPlatform { * "app": { * "android": { * "osVer": "4.4", * "uniVer": "√", * "unixVer": "3.9+" * }, * "ios": { * "osVer": "9.0", * "uniVer": "√", * "unixVer": "x" * } * } * } * @example ```typescript downloadTask.onProgressUpdate((res) => { console.log('下载进度' + res.progress); console.log('已经下载的数据长度' + res.totalBytesWritten); console.log('预期需要下载的数据总长度' + res.totalBytesExpectedToWrite); }); ``` */ onProgressUpdate(callback: DownloadFileProgressUpdateCallback): void, }; export interface Uni { /** * Request() * @description * 发起网络请求。 * @param {RequestOptions} options * @return {RequestTask} * @tutorial https://uniapp.dcloud.net.cn/api/request/request.html * @uniPlatform { * "app": { * "android": { * "osVer": "4.4", * "uniVer": "√", * "unixVer": "3.9+" * }, * "ios": { * "osVer": "9.0", * "uniVer": "√", * "unixVer": "3.9+" * } * } * } * @example ```typescript uni.request({ url: "http://192.168.12.106:8080/postHalo", dataType: "json", responseType: "json", method: "POST", data: { platform: "ios", }, // header: { // "Content-Type": "application/json", // }, timeout: 6000, sslVerify: false, withCredentials: false, firstIpv4: false, success(res) { console.log("success :", res.data); }, fail(e) { console.log(e); }, complete(res) { console.log("complete :", res); }, }); ``` */ request: Request, /** * UploadFile() * @description * 将本地资源上传到开发者服务器。 * @param {UploadFileOptions} options * @return {UploadTask} * @tutorial https://uniapp.dcloud.net.cn/api/request/network-file.html#uploadfile * @uniPlatform { * "app": { * "android": { * "osVer": "4.4", * "uniVer": "√", * "unixVer": "3.9+" * }, * "ios": { * "osVer": "9.0", * "uniVer": "√", * "unixVer": "x" * } * } * } * @example ```typescript uni.uploadFile({ url: 'http://192.168.12.106:8080/uploadFile', //仅为示例,非真实的接口地址 filePath: "/static/logo.png", name: 'file', formData: { 'user': 'test' }, success: (uploadFileRes) => { console.log(uploadFileRes.data); } }); ``` */ uploadFile: UploadFile, /** * DownloadFile() * @description * 下载文件资源到本地,客户端直接发起一个 HTTP GET 请求,返回文件的本地临时路径。 * @param {DownloadFileOptions} options * @return {DownloadTask} * @tutorial https://uniapp.dcloud.net.cn/api/request/network-file.html#downloadfile * @uniPlatform { * "app": { * "android": { * "osVer": "4.4", * "uniVer": "√", * "unixVer": "3.9+" * }, * "ios": { * "osVer": "9.0", * "uniVer": "√", * "unixVer": "x" * } * } * } * @example ```typescript uni.downloadFile({ url: "http://192.168.12.106:8080/downloadfile", success(e) { console.log("success111 :", e); } }); ``` */ downloadFile: DownloadFile }