uni.requset
1 uni.request({ 2 url:'', // 地址 3 data:{}, // 请求body 4 header:{}, //请求header 5 method: '', // 请求方式 'GET'/'POST'/'PUT'/'DELETE' 6 success: (res)=>{}, // 请求成功回调 7 fail: (res)=>{}, // 请求失败回调 8 complete: (res)=>{}, // 请求完成回调 9 })
请求拦截
request(options = {}) { return new Promise((resolve, reject) => { // 拦截处理1:header共通处理 const _config = this.requestBeforeFun(options) if (!next) return const requestTask = uni.request({ url: _config.url, data: _config.data, header: _config.header, method: _config.method, complete: (response) => { if (this.validateStatus(response.statusCode)) { // 成功 // 拦截处理2:请求成功,数据的共通处理 response = this.requestComFun(response) resolve(response) } else { // 拦截处理3:请求失败,错误码的共通处理 response = this.requestComFail(response) reject(response) } } }) }) }
设置拦截器
interceptor = { request: (cb) => { if (cb) { this.requestBeforeFun = cb } }, response: (cb, ecb) => { if (cb) { this.requestComFun = cb } if (ecb) { this.requestComFail = ecb } } }
标签:拦截器,请求,cb,request,header,uni,config,response From: https://www.cnblogs.com/txiaoyuan/p/17276017.html