request.js 3.6 KB

1
  1. "use strict";const u=require("./vendor.js"),f=require("./config.js"),c=require("../utils/aes_util.js");class d{constructor(){this.config={baseUrl:f.baseUrl,business:"data"};this.interceptor={request:void 0,response:void 0,fail:void 0,complete:void 0};this._success=function(t,e,r,n,a){if(r.data.code=="401"){t._fail(t,e,r,n,a);return}let i=null;if(e.contentType=="file"?i=JSON.parse(r==null?void 0:r.data):i=JSON.parse(c.CryptoJS.AesDecrypt(r==null?void 0:r.data)),i.code=="200"||i.code==200)return console.log("解密--------->",i),e.success?e.success(i):n(i);console.log("---12---->公用方法放回数据",r),t._fail(t,e,r,n,a)};this._fail=function(t,e,r,n,a){let i=c.CryptoJS.AesDecrypt(r==null?void 0:r.data);if(console.log("----数据失败返回信息",i),i.errMsg!=="request:fail"){var s=i;t.interceptor.fail&&typeof t.interceptor.fail=="function"&&(s=t.interceptor.fail(i,e)),e.fail?e.fail(s):a(s)}};this._prepare=function(t,e,r={}){if(t.interceptor.prepare&&typeof t.interceptor.prepare=="function"){t.interceptor.prepare(e,r);return}r.startTime=Date.now(),e.loadingTip&&u.index.showLoading({title:e.loadingTip}),e.contentType==="file"&&((e.formData===void 0||e.formData===null)&&(e.formData=e.data,delete e.data),delete e.header["Content-Type"],delete e.header.Referer,e.method="POST")};this._complete=function(t,e,r,n={}){if(t.interceptor.complete&&typeof t.interceptor.complete=="function"){t.interceptor.complete(e,n,r);return}if(n.endTime=Date.now(),e.loadingTip){let a=n.endTime-n.startTime,i=e.loadingDuration||500;a<i?a=i-a:a=0,setTimeout(function(){u.index.hideLoading()},a)}e.complete&&e.complete(r)}}static posUrl(t){return/(http|https):\/\/([\w.]+\/?)\S*/.test(t)}static getUrl(t){let e=t.url||"",r=d.posUrl(e);return r||t.slashAbsoluteUrl&&(r=/^\/([\w.]+\/?)\S*/.test(e)),r?e:t.baseUrl+e}static getContentType(t){var e=t.contentType||"json",r=t.encoding||"UTF-8";if(e==="json")return"application/json;charset="+r;if(e==="form")return"application/x-www-form-urlencoded;charset="+r;if(e==="file")return"multipart/form-data;charset="+r;if(e==="text")return"text/plain;charset="+r;if(e==="html")return"text/html;charset="+r;throw new Error("unsupported content type : "+e)}setConfig(t){this.config=Object.assign(this.config,t)}request(t={}){var e=this;t.data===void 0&&(t.data={}),t.header===void 0&&(t.header={});let r=Object.assign({},this.config,t);if(r=Object.assign(t,r),r.url=d.getUrl(r),!r.header["Content-Type"]){r.header["Content-Type"]=d.getContentType(r);let s=u.index.getStorageSync("setUerInfo"),o=s?s.token:void 0;r.header.Authorization=`Bearer ${o}`}let n=r;e.interceptor.request&&typeof e.interceptor.request=="function"&&(n=e.interceptor.request(r));let a,i=new Promise((s,o)=>{let p={};e._prepare(e,n,p),n.contentType==="file"?(a=u.index.uploadFile({...n,success:l=>{e._success(e,n,l,s,o)},fail:l=>{e._fail(e,n,l,s,o)},complete:l=>{e._complete(e,n,l,p)}}),n.progress&&typeof n.progress=="function"&&a.onProgressUpdate(l=>{n.progress(l,a)})):(console.log(n),a=u.index.request({...n,success:l=>{e._success(e,n,l,s,o)},fail:l=>{console.log("----2222--->公用方法_config放回数据",l),e._fail(e,n,l,s,o)},complete:l=>{e._complete(e,n,l,p)}}))});return n.success||n.fail||n.complete?a:i}aa(){console.log("掉到了")}get(t={}){return t.method="GET",this.request(t)}post(t={}){t.method="POST";let e={data:c.CryptoJS.AesEncrypt(JSON.stringify(t.data))};return t.data=e,this.request(t)}put(t={}){return t.method="PUT",this.request(t)}delete(t={}){return t.method="DELETE",this.request(t)}upload(t={}){return t.method="POST",t.contentType="file",t.baseUrl=f.photoUrl,this.request(t)}}var m=new d;m.setConfig({baseUrl:f.baseUrl,debug:!0});exports.request=m;