2 lines
3.4 KiB
JavaScript
2 lines
3.4 KiB
JavaScript
"use strict";const e=require("../../../../common/vendor.js"),r=require("../config/index.js"),o=require("../store/index.js"),t=require("../platform/index.js"),s=require("../hooks/useModal.js"),n=require("../api/member/auth.js"),a=require("../util/const.js"),i={target:null,count:0};function c(){i.count>0&&i.count--,0===i.count&&e.index.hideLoading()}const u=new e.Request({baseURL:r.baseUrl+r.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}});u.interceptors.request.use((t=>{if(t.custom.auth&&!o.$store("user").isLogin)return s.showAuthModal(),Promise.reject();t.custom.showLoading&&(i.count++,1===i.count&&e.index.showLoading({title:t.custom.loadingMsg,mask:!0,fail:()=>{e.index.hideLoading()}}));const n=g();return n&&(t.header.Authorization=n),t.header.terminal=a.getTerminal(),t.header.Accept="*/*",t.header["tenant-id"]=r.tenantId,t}),(e=>Promise.reject(e))),u.interceptors.response.use((r=>{var t,s;if(r.config.url.indexOf("/member/auth/")>=0&&(null==(s=null==(t=r.data)?void 0:t.data)?void 0:s.accessToken)&&o.$store("user").setToken(r.data.data.accessToken,r.data.data.refreshToken),r.config.custom.showLoading&&c(),0!==r.data.code){if(401===r.data.code)return l(r.config);r.config.custom.showError&&e.index.showToast({title:r.data.msg||"服务器开小差啦,请稍后再试~",icon:"none",mask:!0})}return r.config.custom.showSuccess&&""!==r.config.custom.successMsg&&0===r.data.code&&e.index.showToast({title:r.config.custom.successMsg,icon:"none"}),Promise.resolve(r.data)}),(r=>{var t;const s=o.$store("user").isLogin;let n="网络请求出错";if(void 0!==r){switch(r.statusCode){case 400:n="请求错误";break;case 401:n=s?"您的登陆已过期":"请先登录";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 版本不受支持"}r.errMsg.includes("timeout")&&(n="请求超时")}return r&&r.config&&(!1===r.config.custom.showError&&e.index.showToast({title:(null==(t=r.data)?void 0:t.msg)||n,icon:"none",mask:!0}),r.config.custom.showLoading&&c()),!1}));let d=[],h=!1;const l=e=>{return r=exports,o=null,t=function*(){if(e.url.indexOf("/member/auth/refresh-token")>=0)return Promise.reject("error");if(h)return new Promise((r=>{d.push((()=>{e.header.Authorization="Bearer "+g(),r(k(e))}))}));{h=!0;const o=f();if(!o)return m();try{if(0!==(yield n.AuthUtil.refreshToken(o)).code)throw new Error("刷新令牌失败");return e.header.Authorization="Bearer "+g(),d.forEach((e=>{e()})),d=[],k(e)}catch(r){return d.forEach((e=>{e()})),m()}finally{d=[],h=!1}}},new Promise(((e,s)=>{var n=e=>{try{i(t.next(e))}catch(r){s(r)}},a=e=>{try{i(t.throw(e))}catch(r){s(r)}},i=r=>r.done?e(r.value):Promise.resolve(r.value).then(n,a);i((t=t.apply(r,o)).next())}));var r,o,t},m=()=>{const e=o.$store("user");return e.logout(!0),s.showAuthModal(),Promise.reject({code:401,msg:e.isLogin?"您的登陆已过期":"请先登录"})},g=()=>e.index.getStorageSync("token"),f=()=>e.index.getStorageSync("refresh-token"),k=e=>u.middleware(e),w=k;exports.request=w;
|