"use strict";const e=require("../../common/vendor.js"),o=require("../config/index.js"),r=require("../store/index.js"),t=require("../platform/index.js"),s=require("../hooks/useModal.js"),a=require("../api/member/auth.js");let n={target:null,count:0};function c(){n.count>0&&n.count--,0===n.count&&e.index.hideLoading()}const i=new e.Request({baseURL:o.baseUrl+o.apiPath,timeout:8e3,method:"GET",header:{Accept:"text/json","Content-Type":"application/json;charset=UTF-8",platform:t._platform.name},custom:{showSuccess:!1,successMsg:"",showError:!0,errorMsg:"",showLoading:!0,loadingMsg:"加载中",auth:!1}});i.interceptors.request.use((o=>{if(o.custom.auth&&!r.$store("user").isLogin)return s.showAuthModal(),Promise.reject();o.custom.showLoading&&(n.count++,1===n.count&&e.index.showLoading({title:o.custom.loadingMsg,mask:!0,fail:()=>{e.index.hideLoading()}}));const t=l();return t&&(o.header.Authorization=t),o.header.Accept="*/*",o.header["tenant-id"]="1",o.header.terminal="20",o}),(e=>Promise.reject(e))),i.interceptors.response.use((o=>{var t,a;if(o.config.url.indexOf("/member/auth/")>=0&&(null==(a=null==(t=o.data)?void 0:t.data)?void 0:a.accessToken)&&r.$store("user").setToken(o.data.data.accessToken,o.data.data.refreshToken),o.config.custom.showLoading&&c(),0!==o.data.code){if(401===o.data.code)return s.cancelAutoSign(),h(o.config);o.config.custom.showError&&(s.cancelAutoSign(),e.index.showToast({title:o.data.msg||"服务器开小差啦,请稍后再试~",icon:"none",mask:!0}))}return o.config.custom.showSuccess&&""!==o.config.custom.successMsg&&0===o.data.code&&e.index.showToast({title:o.config.custom.successMsg,icon:"none"}),Promise.resolve(o.data)}),(o=>{var t;console.log("服务器开小差"),s.cancelAutoSign();const a=r.$store("user").isLogin;let n="网络请求出错";if(void 0!==o){switch(o.statusCode){case 400:n="请求错误";break;case 401:n=a?"您的登陆已过期":"请登录";break;case 403:n="拒绝访问";break;case 404:n="请求出错";break;case 408:n="请求超时";break;case 429:n="请求频繁, 请稍后再访问";break;case 500:n="服务器开小差啦,请稍后再试~";break;case 501:n="服务未实现";break;case 502:n="网络错误";break;case 503:n="服务不可用";break;case 504:n="网络超时";break;case 505:n="HTTP 版本不受支持"}o.errMsg.includes("timeout")&&(n="请求超时")}return o&&o.config&&(!1===o.config.custom.showError&&e.index.showToast({title:(null==(t=o.data)?void 0:t.msg)||n,icon:"none",mask:!0}),o.config.custom.showLoading&&c()),!1}));let u=[],d=!1;const h=async e=>{if(e.url.indexOf("/member/auth/refresh-token")>=0)return Promise.reject("error");if(d)return new Promise((o=>{u.push((()=>{e.header.Authorization="Bearer "+l(),o(f(e))}))}));{d=!0;const r=m();if(!r)return g();try{if(0!==(await a.AuthUtil.refreshToken(r)).code)throw new Error("刷新令牌失败");return e.header.Authorization="Bearer "+l(),u.forEach((e=>{e()})),u=[],f(e)}catch(o){return u.forEach((e=>{e()})),g()}finally{u=[],d=!1}}},g=()=>{const e=r.$store("user");return e.logout(!0),s.showAuthModal(),Promise.reject({code:401,msg:e.isLogin?"您的登陆已过期":"请登录"})},l=()=>e.index.getStorageSync("token"),m=()=>e.index.getStorageSync("refresh-token"),f=e=>i.middleware(e),k=f;exports.request=k;
