class Http { //头部文件 header = {} //远程地址 baseUrl = "" params = {} //函数存放口 baseCall = { before: [], //请求之前 after: [], //请求之后 success: [], //请求成功 fail: [], //请求失败 complete: [] ,//请求完毕 _intercept:null,//拦截器专用 } //传递格式 type = "json" //监听器 interceptors = { //请求之前 before: (call) => { this.baseCall.before.push(call); }, //请求之后 after: (call) => { this.baseCall.after.push(call); }, //请求成功 success: (call) => { this.baseCall.success.push(call); }, //请求失败 fail: (call) => { this.baseCall.fail.push(call); }, //请求完毕 complete: (call) => { this.baseCall.complete.push(call); }, //被拦截 _intercept:(call)=>{ this.baseCall._intercept = call; } }; constructor() { } setParams(key,value) { this.params[key] = value; } /** * get提交 * @param {Object} url * @param {Object} post */ get(url, post) { this.baseUrl = url; this.params = post || {_t:new Date().getTime()}; this.buld('get'); } /** * post提交 * @param {Object} url * @param {Object} post */ post(url, post) { this.baseUrl = url; this.setHeader('content-type', "application/x-www-form-urlencoded"); this.params = post; this.buld('post'); } /** * 上传文件 * @param {Object} url * @param {Object} post */ uploadFile(url,post,img,name) { this.baseUrl = url; this.params = post; this.upFile(img,name); } /** * 提交数据 */ buld(method) { //提交之前 for (var i in this.baseCall.before) { var b = this.baseCall.before[i].call(this); //进行拦截 if(b != null && b === false) { this.intercept(); return; } } uni.request({ url: this.baseUrl, method: method, data: this.params, header: this.header, dataType: this.type, success: res => { //提交之后 for (var i in this.baseCall.after) { var b = this.baseCall.after[i].call(this,res); //进行拦截 if(b != null && b === false) { this.intercept(); return; } } if (res.statusCode == 200) { for (var i in this.baseCall.success) { this.baseCall.success[i].call(this,res.data); } } else { for (var i in this.baseCall.fail) { this.baseCall.fail[i].call(this,res.data); } } }, fail: (res) => { /** uni.showModal({ title:"错误提示", content:JSON.stringify(res) }); console.log(res); **/ for (var i in this.baseCall.fail) { this.baseCall.fail[i].call(this,res.errMsg); } }, complete: (res) => { if(res.errMsg == 'request:ok') { for (var i in this.baseCall.complete) { this.baseCall.complete[i].call(this,res); } } else { console.log(res); for (var i in this.baseCall.fail) { this.baseCall.fail[i].call(this,res.errMsg); } } } }); } upFile(img,name) { var that = this; //提交之前 for (var i in this.baseCall.before) { var b = this.baseCall.before[i].call(this); //进行拦截 if(b != null && b === false) { this.intercept(); return; } } uni.uploadFile({ url : this.baseUrl, filePath : img, name : name, formData: this.params, header:this.header, success : (res)=>{ try{ res = JSON.parse(res.data); for (var i in this.baseCall.success) { this.baseCall.success[i].call(this,res); } }catch(e) { for (var i in this.baseCall.fail) { this.baseCall.fail[i].call(this,e); } } } }); } /** * 提交头部数据 * @param {Object} key * @param {Object} value */ setHeader(key, value) { this.header[key] = value; return this; } /** * 设置类型 * @param {Object} type */ setType(type) { this.type = type; } /** * 告诉监听者,被拦截了,可以进行反应操作 */ intercept() { if(this.baseCall._intercept != null) { this.baseCall._intercept.call(this); } } }; export default Http;