_plugin-vue_export-helper.DB5wsZxt.js 2.3 KB

1
  1. var e=Object.defineProperty,t=(t,o,a)=>(((t,o,a)=>{o in t?e(t,o,{enumerable:!0,configurable:!0,writable:!0,value:a}):t[o]=a})(t,"symbol"!=typeof o?o+"":o,a),a);import{v as o,a,h as r,s,x as n,y as c}from"./index-4s5eHE7i.js";const i="https://api.shop.huaml.com";class d{constructor(){t(this,"config",{baseUrl:i,header:{"Content-Type":"application/x-www-form-urlencoded;charset=UTF-8"},data:{},method:"GET",dataType:"json",responseType:"text",success(){},fail(){},complete(){}}),t(this,"interceptor",{request:e=>{e&&"function"==typeof e&&(this.requestBeforeFun=e)},response:e=>{e&&"function"==typeof e&&(this.requestComFun=e)}})}static posUrl(e){return/(http|https):\/\/([\w.]+\/?)\S*/.test(e)}requestBeforeFun(e,t){let r="";try{const e=o("token");e&&(r=e)}catch(n){console.error(n,"获取token异常")}e.header={...e.header},r&&(e.header.token=r),e.header.appVersion=2,e.headers={"Content-Type":"application/x-www-form-urlencoded;charset=UTF-8"};const s=o("account")||null;s&&(e.header.account=s);{const t=o("shopId")||0;t&&(e.header.shopId=t)}return e.config.loading&&a({title:"",mask:!0}),e}requestComFun(e){return e.config.config.loading&&r(),e}setConfig(e){this.config=e(this.config)}request(e={},t){return e.baseUrl=e.baseUrl||this.config.baseUrl,e.dataType=e.dataType||this.config.dataType,e.url=d.posUrl(e.url)?e.url:e.baseUrl+e.url,e.data=e.data||this.config.data,e.header=e.header||this.config.header,e.method=e.method||this.config.method,new Promise(((o,a)=>{let r=!0,i=null;e.complete=e=>{const r=e.statusCode;e.config=i,e.config.config=t,e=this.requestComFun(e),t&&t.hideError?o(e.data):200===r?1===e.data.code?o(e.data):0==e.data.code?(t.hideError||s({title:e.data.msg,icon:"none",mask:!1,duration:2e3}),a(e.data)):e.data.code&&-1==e.data.code||(2==e.data.code?(n("token"),n("shopUser"),n("account")):(t.hideError||s({title:e.data.msg,icon:"none",mask:!1}),a(e.data))):(t.hideError||s({title:e.data.message||"请求失败",icon:"none"}),a(e))};const d={...this.config,...e,config:t};i={...d,...this.requestBeforeFun(d,((e="handle cancel")=>{a({errMsg:e,config:d}),r=!1}))},r&&c(i)}))}get(e,t={},o={},a={}){return this.request({url:e,data:t,method:"GET",...o},a)}post(e,t={},o={},a={}){return this.request({url:e,data:t,method:"POST",...o},a)}}const h=new d,u=(e,t)=>{const o=e.__vccOpts||e;for(const[a,r]of t)o[a]=r;return o};export{u as _,h};