ipink-util
Version:
util.js
2 lines (1 loc) • 2.11 kB
JavaScript
;Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("./config.js"),t=require("./toast.js"),o=require("./cache.js");function genRequestUrl(t,o="",n=""){return t=t||e.o.t,n=n||e.o.i,o.startsWith("http://")||o.startsWith("https://")?o:n&&(o.includes(n)||t.includes(n))?t+o:t+n+o}const addHeaderToken=t=>{if(!t.u){let n="";n=e.o.l?e.o.l():o.Storage.get(e.o.p),n&&(t.u=n?`${e.o.h}${e.o.h?" ":""}${n}`:"")}};exports.addHeaderToken=addHeaderToken,exports.genRequestUrl=genRequestUrl,exports.request=(o,n,i)=>{"string"==typeof o&&(o={q:o}),"object"==typeof o&&o.data&&(n={...o.data,...n||{}});let u={...o,...i},{q:s,url:f,method:r=e.o.m,R:c,j:d=c,T:l=e.o.T,loading:p=e.o.loading,v:a,L:x,U:y=x,$:g,k:h=e.o.k,S:q,prefix:m=e.o.i,timeout:w=e.o.timeout,A:R=!1}=u;void 0===y&&(y=e.o.B),void 0===d&&(d=e.o.I);let j=genRequestUrl(f||e.o.t,s,m),T=e.M();return d&&("function"==typeof p?p(a||""):T&&("undefined"!=typeof uni?uni.j({title:a||"",icon:"none"}):wx.j({title:a||"",icon:"none"}))),new Promise((o=>{if(R&&n&&n.id){let e=""+n.id;delete n.id,j=j+"/"+e}let i={url:j,method:r||e.o.m,timeout:w,data:n,S:{O:e.o.P,...q}};addHeaderToken(i.S),i=e.C("BeforeRequest",i);const u={...i,D:n=>{n.data=e.C("AfterRequest",n.data||{});let i=!1,u="";200===n.statusCode?(i=n.data[e.o.F]==e.o.G,i||(u=n.data?.H||n.data?.message||n.data?.J||g||"参数错误")):401===n.statusCode?(u="您的登录已过期,请重新登录!",e.C("401",n)):403===n.statusCode?(u="您没有访问的权限!",e.C("403",n)):404===n.statusCode?(u="您访问的资源不存在!",e.C("404",n)):(u="您的访问出现了一点问题,请稍后再做尝试!",e.C(""+n.statusCode,n)),d&&("function"==typeof l?l():T&&("undefined"!=typeof uni?uni.hideLoading():wx.hideLoading())),y&&("function"==typeof h?h(u):t.k(u)),o({ok:i,...n.data||{}})},K:e=>{d&&("function"==typeof l?l():T&&("undefined"!=typeof uni?uni.hideLoading():wx.hideLoading()));let n=e&&e.N||g;y&&n&&("function"==typeof h?h(n):t.k(n)),o({ok:!1,V:e})},complete:()=>{}},s="undefined"!=typeof uni?uni.request(u):wx.request(u);e.C("ExecRequest",s)}))};