123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209 |
- 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;
|