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(url, post) {
- this.baseUrl = url;
- this.params = post || {_t:new Date().getTime()};
- this.buld('get');
- }
-
- post(url, post) {
- this.baseUrl = url;
- this.setHeader('content-type', "application/x-www-form-urlencoded");
- this.params = post;
- this.buld('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) => {
-
- 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);
- }
- }
- }
- });
- }
-
-
- setHeader(key, value) {
- this.header[key] = value;
- return this;
- }
-
- setType(type) {
- this.type = type;
- }
-
- intercept() {
- if(this.baseCall._intercept != null) {
- this.baseCall._intercept.call(this);
- }
- }
- };
- export default Http;
|