Commit 70766f5c76a459a9a2b6211586880a52fc34daff

Authored by xp.Huang
2 parents 71001bdf d4193965

Merge branch 'ww' into 'main'

perf: login fail will reload login page && perf /homepage/app api

See merge request huang/thingskit-app!87
1 import { 1 import {
2 getTabbarHeight 2 getTabbarHeight
3 } from '@/plugins/utils'; 3 } from '@/plugins/utils';
4 -let baseUrl = "https://dev.thingskit.com/api"; 4 +let baseUrl = "https://dev.thingskit.com/api";
  5 +// let baseUrl = "http://192.168.10.136:8080/api";
5 let baseDrawioUrl = "https://dev.thingskit.com/thingskit-scada" 6 let baseDrawioUrl = "https://dev.thingskit.com/thingskit-scada"
6 7
7 let systemInfo = { 8 let systemInfo = {
1 -import base from "@/config/baseUrl";  
2 -import store from "@/store";  
3 -import {  
4 - judgeLogin  
5 -} from "@/config/login";  
6 -  
7 -// 初始化请求配置  
8 -uni.$u.http.setConfig((config) => {  
9 - const token =  
10 - store.state.userInfo.isToken ||  
11 - uni.getStorageSync("userInfo").isToken ||  
12 - undefined;  
13 - // #ifdef H5  
14 - window.sessionStorage.getItem("userInfo").isToken;  
15 - // #endif  
16 - /* config 为默认全局配置*/  
17 - // const getConfiguration = uni.getStorageSync('getConfiguration').isConfiguration  
18 - // console.log('getConfiguration', getConfiguration);  
19 - // config.baseURL = getConfiguration == true ? base.baseDrawioUrl : base.baseUrl /* 根域名 */  
20 - config.baseURL = base.baseUrl /* 根域名 */  
21 - config.header = {  
22 - "Content-Type": "application/json",  
23 - 'X-Authorization': "Bearer " + token,  
24 - };  
25 - config.custom = {  
26 - load: true, //是否显示加载动画  
27 - isFactory: true, //true:返回的数据成功只返回data false:返回response  
28 - catch: true, //默认数据返回不成功进入catch返回  
29 - auth: true, //token  
30 - };  
31 - return config;  
32 -});  
33 -  
34 -// 请求拦截  
35 -uni.$u.http.interceptors.request.use(  
36 - (config) => {  
37 - if (store.state.userInfo.isThirdLogin) {  
38 - uni.switchTab({  
39 - url: "../pages/personal/personal",  
40 - });  
41 - }  
42 - // 初始化请求拦截器时,会执行此方法,此时data为undefined,赋予默认{}  
43 - config.data = config.data || {};  
44 - // 根据custom参数中配置的是否需要token,添加对应的请求头  
45 - const getConfiguration = uni.getStorageSync('getConfiguration').isConfiguration  
46 - if (config?.custom?.auth) {  
47 - config.header['X-Authorization'] =  
48 - "Bearer " + store.state.userInfo.isToken ||  
49 - uni.getStorageSync("userInfo").isToken ||  
50 - undefined;  
51 - config.baseURL = getConfiguration == true ? base.baseDrawioUrl : base.baseUrl  
52 - }  
53 - if (config?.custom?.load) {  
54 - //打开加载动画  
55 - store.commit("setLoadingShow", true);  
56 - }  
57 - if (getConfiguration) {  
58 - uni.setStorageSync('config', config)  
59 - } else {  
60 - uni.removeStorageSync('config')  
61 - }  
62 - return config;  
63 - },  
64 - (config) => {  
65 - return Promise.reject(config);  
66 - }  
67 -);  
68 -  
69 -// 响应拦截  
70 -uni.$u.http.interceptors.response.use(  
71 - (response) => {  
72 - // 关闭加载动画  
73 - store.commit("setLoadingShow", false);  
74 - const data = response.data;  
75 - const custom = response.config?.custom;  
76 - // code: 200、请求成功 其他,没有更多参数 401、被迫下线重新登录、  
77 - if (response.statusCode == 200) {  
78 - return Promise.resolve(data);  
79 - } else if (response.statusCode == 401) {  
80 - // 清空登录信息  
81 - store.commit("emptyUserInfo");  
82 - // 20秒节流,弹窗登录  
83 - uni.$u.throttle(judgeLogin(), 20000);  
84 - return new Promise(() => {});  
85 - } else {}  
86 - },  
87 - (response) => {  
88 - // 关闭加载动画  
89 - store.commit("setLoadingShow", false);  
90 - let show = true;  
91 - let errorData = '请检查网络或服务器'  
92 - let message = response.data?.message || response.data?.msg || response?.errMsg  
93 - if (message == "request:fail url not in domain list") {  
94 - errorData = "检查请求域名是否添加了域名白名单";  
95 - } else if (message == "request:fail timeout") {  
96 - errorData = "请求超时:请检查网络";  
97 - } else if (response.data.status == 401) { 1 +import base from "@/config/baseUrl";
  2 +import store from "@/store";
  3 +import {
  4 + judgeLogin
  5 +} from "@/config/login";
  6 +
  7 +// 初始化请求配置
  8 +uni.$u.http.setConfig((config) => {
  9 + const token =
  10 + store.state.userInfo.isToken ||
  11 + uni.getStorageSync("userInfo").isToken ||
  12 + undefined;
  13 + // #ifdef H5
  14 + window.sessionStorage.getItem("userInfo").isToken;
  15 + // #endif
  16 + /* config 为默认全局配置*/
  17 + // const getConfiguration = uni.getStorageSync('getConfiguration').isConfiguration
  18 + // console.log('getConfiguration', getConfiguration);
  19 + // config.baseURL = getConfiguration == true ? base.baseDrawioUrl : base.baseUrl /* 根域名 */
  20 + config.baseURL = base.baseUrl /* 根域名 */
  21 + config.header = {
  22 + "Content-Type": "application/json",
  23 + 'X-Authorization': "Bearer " + token,
  24 + };
  25 + config.custom = {
  26 + load: true, //是否显示加载动画
  27 + isFactory: true, //true:返回的数据成功只返回data false:返回response
  28 + catch: true, //默认数据返回不成功进入catch返回
  29 + auth: true, //token
  30 + };
  31 + return config;
  32 +});
  33 +
  34 +// 请求拦截
  35 +uni.$u.http.interceptors.request.use(
  36 + (config) => {
  37 + if (store.state.userInfo.isThirdLogin) {
  38 + uni.switchTab({
  39 + url: "../pages/personal/personal",
  40 + });
  41 + }
  42 + // 初始化请求拦截器时,会执行此方法,此时data为undefined,赋予默认{}
  43 + config.data = config.data || {};
  44 + // 根据custom参数中配置的是否需要token,添加对应的请求头
  45 + const getConfiguration = uni.getStorageSync('getConfiguration').isConfiguration
  46 + if (config?.custom?.auth) {
  47 + config.header['X-Authorization'] =
  48 + "Bearer " + store.state.userInfo.isToken ||
  49 + uni.getStorageSync("userInfo").isToken ||
  50 + undefined;
  51 + config.baseURL = getConfiguration == true ? base.baseDrawioUrl : base.baseUrl
  52 + }
  53 + if (config?.custom?.load) {
  54 + //打开加载动画
  55 + store.commit("setLoadingShow", true);
  56 + }
  57 + if (getConfiguration) {
  58 + uni.setStorageSync('config', config)
  59 + } else {
  60 + uni.removeStorageSync('config')
  61 + }
  62 + return config;
  63 + },
  64 + (config) => {
  65 + return Promise.reject(config);
  66 + }
  67 +);
  68 +
  69 +// 响应拦截
  70 +uni.$u.http.interceptors.response.use(
  71 + (response) => {
  72 + // 关闭加载动画
  73 + store.commit("setLoadingShow", false);
  74 + const data = response.data;
  75 + const custom = response.config?.custom;
  76 + // code: 200、请求成功 其他,没有更多参数 401、被迫下线重新登录、
  77 + if (response.statusCode == 200) {
  78 + return Promise.resolve(data);
  79 + } else if (response.statusCode == 401) {
  80 + // 清空登录信息
  81 + store.commit("emptyUserInfo");
  82 + // 20秒节流,弹窗登录
  83 + uni.$u.throttle(judgeLogin(), 20000);
  84 + return new Promise(() => {});
  85 + } else {}
  86 + },
  87 + (response) => {
  88 + // 关闭加载动画
  89 + store.commit("setLoadingShow", false);
  90 + let show = true;
  91 + let errorData = '请检查网络或服务器'
  92 + let message = response.data?.message || response.data?.msg || response?.errMsg
  93 + if (message == "request:fail url not in domain list") {
  94 + errorData = "检查请求域名是否添加了域名白名单";
  95 + } else if (message == "request:fail timeout") {
  96 + errorData = "请求超时:请检查网络";
  97 + } else if (response.data.status == 401) {
  98 + const isLoginFlag = uni.getStorageSync("userInfo")
98 if (!store.state.userInfo.isThirdLogin) { 99 if (!store.state.userInfo.isThirdLogin) {
99 - uni.reLaunch({  
100 - url: "/publicLoginSubPage/public/login",  
101 - });  
102 - // 清空登录信息  
103 - store.commit("emptyUserInfo");  
104 - show = false;  
105 - } else {  
106 - uni.switchTab({  
107 - url: "../pages/personal/personal",  
108 - });  
109 - show = false;  
110 - }  
111 - } else {  
112 - errorData = message || "";  
113 - }  
114 - if (show) uni.$u.toast(errorData);  
115 - return Promise.reject(response);  
116 - } 100 + const routers = getCurrentPages()
  101 + const currentRoute = routers[routers.length - 1].route
  102 + const isLoginPage = currentRoute.includes('publicLoginSubPage/public/login')
  103 + !isLoginPage && uni.reLaunch({
  104 + url: "/publicLoginSubPage/public/login",
  105 + });
  106 + // 清空登录信息
  107 + store.commit("emptyUserInfo");
  108 + show = false;
  109 + } else {
  110 + uni.switchTab({
  111 + url: "../pages/personal/personal",
  112 + });
  113 + show = false;
  114 + }
  115 +
  116 + } else {
  117 + errorData = message || "";
  118 + }
  119 + if (show) uni.$u.toast(errorData);
  120 + return Promise.reject(response);
  121 + }
117 ); 122 );
@@ -129,7 +129,7 @@ export default { @@ -129,7 +129,7 @@ export default {
129 methods: { 129 methods: {
130 ...mapActions(['updateBadgeTotal']), 130 ...mapActions(['updateBadgeTotal']),
131 getDeviceTotalData() { 131 getDeviceTotalData() {
132 - uni.$u.http.get('/yt/homepage/app').then(res => { 132 + uni.$u.http.get('/yt/homepage/app?login=false').then(res => {
133 if (res) { 133 if (res) {
134 this.deviceData.onLine = res.totalDevice?.onLine; 134 this.deviceData.onLine = res.totalDevice?.onLine;
135 this.deviceData.unLine = res.totalDevice?.offLine; 135 this.deviceData.unLine = res.totalDevice?.offLine;
@@ -155,7 +155,7 @@ export default { @@ -155,7 +155,7 @@ export default {
155 }); 155 });
156 }, 156 },
157 async getAlarmTotalData() { 157 async getAlarmTotalData() {
158 - const res = await uni.$u.http.get('/yt/homepage/app'); 158 + const res = await uni.$u.http.get('/yt/homepage/app?login=true');
159 if (res) { 159 if (res) {
160 //异步实时更新告警徽标数 160 //异步实时更新告警徽标数
161 this.updateBadgeTotal(res.totalAlarm?.activedAlarm); 161 this.updateBadgeTotal(res.totalAlarm?.activedAlarm);
@@ -196,7 +196,7 @@ export default { @@ -196,7 +196,7 @@ export default {
196 }); 196 });
197 }, 197 },
198 async getAlarmTotalData() { 198 async getAlarmTotalData() {
199 - const res = await uni.$u.http.get('/yt/homepage/app'); 199 + const res = await uni.$u.http.get('/yt/homepage/app?login=true');
200 if (res) { 200 if (res) {
201 //异步实时更新告警徽标数 201 //异步实时更新告警徽标数
202 this.updateBadgeTotal(res.totalAlarm?.activedAlarm); 202 this.updateBadgeTotal(res.totalAlarm?.activedAlarm);