UNPKG

@zibu/common-mina

Version:

微信小程序通用逻辑功能模块。包含AjaxService、clone、dataFormat

93 lines (85 loc) 2.84 kB
/** * 微信小程序HttpRequest类定义 */ export default class WXHttpRequest { /** * 微信小程序HttpRequest类构造函数 */ constructor() { // 仿照axios结构构建钩子函数,以便能够统一调用 this.interceptors = { request: {}, response: {} }; this.defaults = { headers: { common: { 'Content-Type': 'application/json' } } }; this.initInterceptors(); this.initRequestMethods(); } /** * 初始化钩子函数 */ initInterceptors() { // 不能使用箭头函数,因为this会被绑定为HttpRequest对象 let use = function(success, fail) { this._hooks.push({ success, fail }); }; this.interceptors.request = { _hooks: [], use }; this.interceptors.response = { _hooks: [], use }; } /** * 初始化请求方法 */ initRequestMethods() { // 微信小程序不支持patch ['post', 'delete', 'put', 'get'].forEach(key => { this[key] = (url, param, config) => { config = Object.assign( { url, header: this.defaults.headers.common, data: param, method: key.toUpperCase() }, config ); const requestHooks = this.interceptors.request._hooks; // 因为Axios的request钩子函数是按照堆栈方式(先进后出)调用,response钩子函数按照队列方式(先进先出)调用,所以为了统一业务逻辑,这里也改成倒序调用 for (let index = requestHooks.length - 1; index > -1; index--) { const hook = requestHooks[index]; config = hook.success(config); } let promise = new Promise((resolve, reject) => { config.success = response => { this.interceptors.response._hooks.forEach(hook => { response = hook.success(response); }); resolve(response); }; config.fail = error => { this.interceptors.response._hooks.forEach(hook => { error = hook.fail(error); }); reject(error); }; wx.request(config); }); return promise; }; }); } }