ts源码: [代码]export namespace Hz.Http {[代码] [代码] [代码][代码]const ES6Promise = require([代码][代码]'es6-promise'[代码][代码]);[代码] [代码] [代码][代码]export interface DatasObject {[代码][代码] [代码][代码][data: string]: number | string | string[] | undefined;[代码][代码] [代码][代码]}[代码] [代码] [代码][代码]export interface HeadersObject {[代码][代码] [代码][代码][header: string]: number | string | string[] | undefined;[代码][代码] [代码][代码]}[代码] [代码] [代码][代码]export interface RequestParams {[代码][代码] [代码][代码]url: string;[代码][代码] [代码][代码]method?: string;[代码][代码] [代码][代码]dataType?: string;[代码][代码] [代码][代码]data?: string | DatasObject | ArrayBuffer;[代码][代码] [代码][代码]header?: HeadersObject;[代码][代码] [代码][代码]responseType?: string;[代码][代码] [代码][代码]}[代码] [代码] [代码][代码]export class HttpError extends Error {[代码] [代码] [代码][代码]public statusCode: number;[代码][代码] [代码][代码]public header: HeadersObject;[代码][代码] [代码][代码]public body: string | object | ArrayBuffer;[代码] [代码] [代码][代码]constructor(statusCode: number, header: HeadersObject, body: string | object | ArrayBuffer) {[代码][代码] [代码][代码]super[代码][代码](`response status code: ${statusCode}`);[代码][代码] [代码][代码]this[代码][代码].statusCode = statusCode;[代码][代码] [代码][代码]this[代码][代码].header = header;[代码][代码] [代码][代码]this[代码][代码].body = body;[代码][代码] [代码][代码]}[代码][代码] [代码][代码]}[代码] [代码] [代码][代码]export class ResponsePromise<T> extends ES6Promise<T> {[代码] [代码] [代码][代码]public statusCode: number;[代码][代码] [代码][代码]public header: HeadersObject;[代码][代码] [代码][代码]public body: string | object | ArrayBuffer;[代码] [代码] [代码][代码]public requestTask: WechatMiniprogram.RequestTask;[代码] [代码] [代码][代码]constructor(executor: (resolve: (value?: T | PromiseLike<T>) => void, reject: (reason?: any) => void) => void) {[代码][代码] [代码][代码]super[代码][代码](executor);[代码][代码] [代码][代码]}[代码] [代码] [代码][代码]public onHeadersReceived(callback: WechatMiniprogram.RequestTaskOnHeadersReceivedCallback) {[代码][代码] [代码][代码]this[代码][代码].requestTask.onHeadersReceived(callback);[代码][代码] [代码][代码]}[代码] [代码] [代码][代码]public offHeadersReceived(callback: WechatMiniprogram.RequestTaskOffHeadersReceivedCallback) {[代码][代码] [代码][代码]this[代码][代码].requestTask.offHeadersReceived(callback);[代码][代码] [代码][代码]}[代码] [代码] [代码][代码]public abort(): void {[代码][代码] [代码][代码]this[代码][代码].requestTask.abort();[代码][代码] [代码][代码]}[代码][代码] [代码][代码]}[代码] [代码] [代码][代码]export class Request {[代码] [代码] [代码][代码]public static ExecAsync<T>(pars: RequestParams): ResponsePromise<T> {[代码] [代码] [代码][代码]var[代码] [代码]requestTask: WechatMiniprogram.RequestTask;[代码] [代码] [代码][代码]var[代码] [代码]p = [代码][代码]new[代码] [代码]ResponsePromise<T>((resolve, reject) => {[代码] [代码] [代码][代码]try[代码] [代码]{[代码][代码] [代码][代码]requestTask = wx.request({[代码][代码] [代码][代码]url: pars.url,[代码][代码] [代码][代码]data: pars.data,[代码][代码] [代码][代码]header: pars.header,[代码][代码] [代码][代码]method: pars.method,[代码][代码] [代码][代码]dataType: pars.dataType,[代码][代码] [代码][代码]responseType: pars.responseType,[代码][代码] [代码][代码]success(res) {[代码] [代码] [代码][代码]if[代码] [代码](res.statusCode >= 200 && res.statusCode < 300) {[代码] [代码] [代码][代码]p.statusCode = res.statusCode;[代码][代码] [代码][代码]p.header = res.header;[代码][代码] [代码][代码]p.body = res.data;[代码] [代码] [代码][代码]resolve(res.data);[代码][代码] [代码][代码]}[代码][代码] [代码][代码]else[代码] [代码]{[代码][代码] [代码][代码]reject([代码][代码]new[代码] [代码]HttpError(res.statusCode, res.header, res.data));[代码][代码] [代码][代码]}[代码] [代码] [代码][代码]},[代码][代码] [代码][代码]fail(err) {[代码][代码] [代码][代码]reject([代码][代码]new[代码] [代码]Error(err.errMsg));[代码][代码] [代码][代码]}[代码][代码] [代码][代码]});[代码][代码] [代码][代码]}[代码][代码] [代码][代码]catch[代码] [代码](e) {[代码][代码] [代码][代码]reject(e);[代码][代码] [代码][代码]}[代码] [代码] [代码][代码]});[代码] [代码] [代码][代码]p.requestTask = requestTask;[代码][代码] [代码][代码]return[代码] [代码]p;[代码][代码] [代码][代码]}[代码] [代码] [代码][代码]public static getAsync<T>(url: string, data?: string | DatasObject | ArrayBuffer, header?: HeadersObject): ResponsePromise<T> {[代码][代码] [代码][代码]return[代码] [代码]Request.ExecAsync<T>({ url: url, data: data, header: header, method: [代码][代码]"GET"[代码] [代码]});[代码][代码] [代码][代码]}[代码] [代码] [代码][代码]public static postAsync<T>(url: string, data?: string | DatasObject | ArrayBuffer, header?: HeadersObject): ResponsePromise<T> {[代码][代码] [代码][代码]return[代码] [代码]Request.ExecAsync<T>({ url: url, data: data, header: header, method: [代码][代码]"POST"[代码] [代码]});[代码][代码] [代码][代码]}[代码] [代码] [代码][代码]public static deleteAsync<T>(url: string, data?: string | DatasObject | ArrayBuffer, header?: HeadersObject): ResponsePromise<T> {[代码][代码] [代码][代码]return[代码] [代码]Request.ExecAsync<T>({ url: url, data: data, header: header, method: [代码][代码]"DELETE"[代码] [代码]});[代码][代码] [代码][代码]}[代码][代码] [代码][代码]}[代码][代码]}[代码]
wx.request 经 Promise 封装后,如何拿到requestTask大家会用 promise 将 wx.request 包装一层。但经过这么一层包装后,就拿到不到 requestTask,从而调用不了 abort 方法。大家都是如何解决的? 代码来自:https://www.kancloud.cn/xiaoyulive/wechat/526990 [代码]class Request {[代码][代码] [代码][代码]constructor (parms) {[代码][代码] [代码][代码]this[代码][代码].withBaseURL = parms.withBaseURL[代码][代码] [代码][代码]this[代码][代码].baseURL = parms.baseURL[代码][代码] [代码][代码]}[代码][代码] [代码][代码]get (url, data) {[代码][代码] [代码][代码]return[代码] [代码]this[代码][代码].request([代码][代码]'GET'[代码][代码], url, data)[代码][代码] [代码][代码]}[代码][代码] [代码][代码]post (url, data) {[代码][代码] [代码][代码]return[代码] [代码]this[代码][代码].request([代码][代码]'POST'[代码][代码], url, data)[代码][代码] [代码][代码]}[代码][代码] [代码][代码]put (url, data) {[代码][代码] [代码][代码]return[代码] [代码]this[代码][代码].request([代码][代码]'PUT'[代码][代码], url, data)[代码][代码] [代码][代码]}[代码][代码] [代码][代码]request (method, url, data) {[代码][代码] [代码][代码]const vm = [代码][代码]this[代码][代码] [代码][代码]return[代码] [代码]new[代码] [代码]Promise((resolve, reject) => {[代码][代码] [代码][代码]wx.request({[代码][代码] [代码][代码]url: vm.withBaseURL ? vm.baseURL + url : url,[代码][代码] [代码][代码]data,[代码][代码] [代码][代码]method,[代码][代码] [代码][代码]success (res) {[代码][代码] [代码][代码]resolve(res)[代码][代码] [代码][代码]},[代码][代码] [代码][代码]fail () {[代码][代码] [代码][代码]reject({[代码][代码] [代码][代码]msg: [代码][代码]'请求失败'[代码][代码],[代码][代码] [代码][代码]url: vm.withBaseURL ? vm.baseURL + url : url,[代码][代码] [代码][代码]method,[代码][代码] [代码][代码]data[代码][代码] [代码][代码]})[代码][代码] [代码][代码]}[代码][代码] [代码][代码]})[代码][代码] [代码][代码]})[代码][代码] [代码][代码]}[代码][代码]}[代码] [代码]const request = [代码][代码]new[代码] [代码]Request({[代码][代码] [代码][代码]baseURL: [代码][代码]'http://test'[代码][代码],[代码][代码] [代码][代码]withBaseURL: [代码][代码]true[代码][代码]})[代码] [代码]module.exports = request[代码]
2019-12-10