WXL
11 小时以前 05c363fdd7ab04e3bd9a753e2c5d5bfff04d681c
1
"use strict";const e=require("../common/vendor.js"),o=require("./config.js"),{baseURL:t}=o.getConfig();let n=!1;e.index.__isSSOHandling=!1;const r=o=>{var t;(null==(t=e.index.$u)?void 0:t.toast)?e.index.$u.toast(o):e.index.showToast({title:o,icon:"none"})};console.log(t,"baseURL");const i={baseURL:t,timeout:6e4,header:{"Content-Type":"application/json","X-Business-System":"medical-system"},loginPage:"/pages/login/Login",tokenExpiredCode:401,noPermissionCode:403,whiteList:["/login","/getToken","/GiLink/getCode"]},s=()=>{if(n||e.index.__isSSOHandling)return void console.log("正在跳转登录页或SSO处理中,跳过");n=!0;const o=getCurrentPages(),t=o[o.length-1];if(t&&t.route&&t.route.includes("login/Login"))return console.log("当前已在登录页,不跳转"),void(n=!1);console.log("跳转到登录页"),setTimeout(()=>{e.index.redirectTo({url:i.loginPage+"?redirect="+encodeURIComponent(l()),success:()=>{console.log("跳转登录页成功"),n=!1},fail:()=>{console.log("跳转登录页失败"),n=!1}})},100)},l=()=>{var e;const o=getCurrentPages();return(null==(e=o[o.length-1])?void 0:e.route)||""},a=o=>{const t=e.index.getStorageSync("token");if((e=>{const o=e.replace(i.baseURL,"").split("?")[0];return i.whiteList.some(e=>e.endsWith("/")?o.startsWith(e):o===e)})(o.url))return console.log("白名单接口,跳过token校验:",o.url),o;if(!t){const e=getCurrentPages(),t=e[e.length-1];if(t&&t.route&&(t.route.includes("login/Login")||t.route.includes("login/DingTalkLogin")))return console.log("当前是登录页,允许无token请求"),o;throw console.log("未登录且不在登录页,跳转到登录页"),s(),new Error("未登录")}return o.header={...o.header,Authorization:`Bearer ${t}`},o},d=e=>{const{statusCode:o,data:t}=e;return(e=>e===i.tokenExpiredCode&&(r("登录已过期,请重新登录"),s(),!0))(o)?Promise.reject(t):o===i.noPermissionCode?(r("无权限访问"),Promise.reject(t)):200!==o?(r(`请求失败: ${o}`),Promise.reject(t)):200!==(null==t?void 0:t.code)?(r((null==t?void 0:t.msg)||"操作失败"),Promise.reject(t)):t},u=async o=>{try{return o={...i,...o,url:i.baseURL+o.url,header:{...i.header,...o.header}},o=await a(o),new Promise((t,n)=>{e.index.request({...o,success:e=>t(d(e)),fail:e=>{r("网络错误,请重试"),n(e)}})})}catch(t){return Promise.reject(t)}},c={get:(e,o={},t={})=>u({url:e,data:o,method:"GET",...t}),post:(e,o={},t={})=>u({url:e,data:o,method:"POST",...t}),put:(e,o={},t={})=>u({url:e,data:o,method:"PUT",...t}),delete:(e,o={},t={})=>u({url:e,data:o,method:"DELETE",...t}),upload:(e,o,t="file",n={})=>u({url:e,method:"POST",filePath:o,name:t,formData:n,header:{"Content-Type":"multipart/form-data"}})};e.index.$uapi=c;