You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2 lines
2.4 KiB

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