Commit 3d5fdecb422399a8bcc59edb17c08255f3be773a

Authored by fengtao
1 parent a0fb092c

pref:优化服务端配置项

1 import { 1 import {
2 getTabbarHeight 2 getTabbarHeight
3 -} from '@/plugins/utils';  
4 -let baseUrl = "https://dev.thingskit.com/api";  
5 -// let baseUrl = "http://192.168.10.136:8080/api";  
6 -let baseDrawioUrl = "https://dev.thingskit.com/thingskit-scada" 3 +} from "@/plugins/utils";
  4 +/**
  5 + * 服务端配置项
  6 + * baseUrl 服务端 api地址
  7 + * baseDrawioUrl 组态地址 注意端口
  8 + * baseWebSocketUrl 服务端 websocket地址
  9 + * socketPrefix websocket前缀 ((https, wss),( http, ws))
  10 + */
  11 +
  12 +// const baseUrl = "http://47.108.221.67:8080/api";
  13 +// const baseDrawioUrl = "http://47.108.221.67:9527/thingskit-scada";
  14 +// const baseWebSocketUrl = "47.108.221.67:8080";
  15 +// const socketPrefix = "ws";
  16 +
  17 +const baseUrl = "http://222.180.200.114:48080/api";
  18 +const baseDrawioUrl = "http://222.180.200.114:48080/thingskit-scada";
  19 +const baseWebSocketUrl = "222.180.200.114:48080";
  20 +const socketPrefix = "ws";
7 21
8 let systemInfo = { 22 let systemInfo = {
9 ...getTabbarHeight(), 23 ...getTabbarHeight(),
10 // #ifdef MP-ALIPAY 24 // #ifdef MP-ALIPAY
11 - navBarH: uni.getSystemInfoSync().statusBarHeight + uni.getSystemInfoSync().titleBarHeight, //菜单栏总高度--单位px 25 + navBarH: uni.getSystemInfoSync().statusBarHeight +
  26 + uni.getSystemInfoSync().titleBarHeight, //菜单栏总高度--单位px
12 titleBarHeight: uni.getSystemInfoSync().titleBarHeight, //标题栏高度--单位px 27 titleBarHeight: uni.getSystemInfoSync().titleBarHeight, //标题栏高度--单位px
13 // #endif 28 // #endif
14 // #ifndef MP-ALIPAY 29 // #ifndef MP-ALIPAY
@@ -18,32 +33,27 @@ let systemInfo = { @@ -18,32 +33,27 @@ let systemInfo = {
18 }; 33 };
19 // 平台 34 // 平台
20 // #ifdef MP-WEIXIN 35 // #ifdef MP-WEIXIN
21 -systemInfo.platform = 'weixin' 36 +systemInfo.platform = "weixin";
22 // #endif 37 // #endif
23 // #ifdef MP-ALIPAY 38 // #ifdef MP-ALIPAY
24 -systemInfo.platform = 'alipay' 39 +systemInfo.platform = "alipay";
25 // #endif 40 // #endif
26 // #ifdef MP-TOUTIAO 41 // #ifdef MP-TOUTIAO
27 -systemInfo.platform = 'toutiao' 42 +systemInfo.platform = "toutiao";
28 // #endif 43 // #endif
29 // #ifdef APP-PLUS 44 // #ifdef APP-PLUS
30 -systemInfo.platform = 'plus' 45 +systemInfo.platform = "plus";
31 // #endif 46 // #endif
32 -console.log(systemInfo, 'systemInfo')  
33 const courtConfig = { 47 const courtConfig = {
34 - publicAppId: '', //公众号appId  
35 - baseUrl, //域名  
36 - baseDrawioUrl, 48 + publicAppId: "", //公众号appId
  49 + baseUrl, //服务端地址
  50 + baseDrawioUrl, //服务端组态地址
  51 + baseWebSocketUrl, //服务端websocket地址
  52 + socketPrefix, //websocket前缀
37 systemInfo: systemInfo, //系统信息 53 systemInfo: systemInfo, //系统信息
38 mapData: { 54 mapData: {
39 - key: '', //地图key  
40 - sk: '', 55 + key: "", //地图key
  56 + sk: "",
41 }, 57 },
42 - share: {  
43 - title: '基于uview2.0快速开发框架', //分享标题  
44 - desc: "基于uview2.0快速开发框架详情", //分享详情  
45 - link: "", // 分享链接  
46 - imgUrl: "", // 分享图  
47 - }  
48 }; 58 };
49 export default Object.assign({}, courtConfig); 59 export default Object.assign({}, courtConfig);
1 import base from "@/config/baseUrl"; 1 import base from "@/config/baseUrl";
2 import store from "@/store"; 2 import store from "@/store";
3 -import {  
4 - judgeLogin  
5 -} from "@/config/login"; 3 +import { judgeLogin } from "@/config/login";
6 4
7 // 初始化请求配置 5 // 初始化请求配置
8 uni.$u.http.setConfig((config) => { 6 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; 7 + const token =
  8 + store.state.userInfo.isToken ||
  9 + uni.getStorageSync("userInfo").isToken ||
  10 + undefined;
  11 + // #ifdef H5
  12 + window.sessionStorage.getItem("userInfo").isToken;
  13 + // #endif
  14 + /* config 为默认全局配置*/
  15 + // const getConfiguration = uni.getStorageSync('getConfiguration').isConfiguration
  16 + // console.log('getConfiguration', getConfiguration);
  17 + // config.baseURL = getConfiguration == true ? base.baseDrawioUrl : base.baseUrl /* 根域名 */
  18 + config.baseURL = base.baseUrl; /* 根域名 */
  19 + config.header = {
  20 + "Content-Type": "application/json",
  21 + "X-Authorization": "Bearer " + token,
  22 + };
  23 + config.custom = {
  24 + load: true, //是否显示加载动画
  25 + isFactory: true, //true:返回的数据成功只返回data false:返回response
  26 + catch: true, //默认数据返回不成功进入catch返回
  27 + auth: true, //token
  28 + };
  29 + return config;
32 }); 30 });
33 31
34 // 请求拦截 32 // 请求拦截
35 uni.$u.http.interceptors.request.use( 33 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 - } 34 + (config) => {
  35 + if (store.state.userInfo.isThirdLogin) {
  36 + uni.switchTab({
  37 + url: "../pages/personal/personal",
  38 + });
  39 + }
  40 + // 初始化请求拦截器时,会执行此方法,此时data为undefined,赋予默认{}
  41 + config.data = config.data || {};
  42 + // 根据custom参数中配置的是否需要token,添加对应的请求头
  43 + const getConfiguration =
  44 + uni.getStorageSync("getConfiguration").isConfiguration;
  45 + if (config?.custom?.auth) {
  46 + config.header["X-Authorization"] =
  47 + "Bearer " + store.state.userInfo.isToken ||
  48 + uni.getStorageSync("userInfo").isToken ||
  49 + undefined;
  50 + config.baseURL =
  51 + 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 ); 67 );
68 68
69 // 响应拦截 69 // 响应拦截
70 uni.$u.http.interceptors.response.use( 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")  
99 - if (!store.state.userInfo.isThirdLogin) {  
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 - }  
122 -); 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 + },
  88 + (response) => {
  89 + // 关闭加载动画
  90 + store.commit("setLoadingShow", false);
  91 + let show = true;
  92 + let errorData = "请检查网络或服务器";
  93 + let message =
  94 + response.data?.message || response.data?.msg || response?.errMsg;
  95 + if (message == "request:fail url not in domain list") {
  96 + errorData = "检查请求域名是否添加了域名白名单";
  97 + } else if (message == "request:fail timeout") {
  98 + errorData = "请求超时:请检查网络";
  99 + } else if (response.data.status == 401) {
  100 + const isLoginFlag = uni.getStorageSync("userInfo");
  101 + if (!store.state.userInfo.isThirdLogin) {
  102 + const routers = getCurrentPages();
  103 + const currentRoute = routers[routers.length - 1].route;
  104 + const isLoginPage = currentRoute.includes(
  105 + "publicLoginSubPage/public/login"
  106 + );
  107 + !isLoginPage &&
  108 + uni.reLaunch({
  109 + url: "/publicLoginSubPage/public/login",
  110 + });
  111 + // 清空登录信息
  112 + store.commit("emptyUserInfo");
  113 + show = false;
  114 + } else {
  115 + uni.switchTab({
  116 + url: "../pages/personal/personal",
  117 + });
  118 + show = false;
  119 + }
  120 + } else if (message == "request:ok") {
  121 + show = false;
  122 + errorData = "";
  123 + } else {
  124 + errorData = message || "";
  125 + }
  126 + if (show) uni.$u.toast(errorData);
  127 + return Promise.reject(response);
  128 + }
  129 +);