yuang-framework-ui-common 1.0.15 → 1.0.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/.DS_Store +0 -0
- package/lib/config/gatewayConfig.ts +23 -0
- package/lib/config/httpConfig.ts +31 -25
- package/lib/utils/objectUtils.ts +6 -0
- package/lib/utils/ssoUtils.ts +7 -8
- package/lib/utils/storageUtils.ts +110 -0
- package/package.json +3 -1
- package/src/.DS_Store +0 -0
- package/src/App.vue +4 -4
- package/lib/utils/gatewayUtils.ts +0 -19
package/lib/.DS_Store
ADDED
Binary file
|
@@ -0,0 +1,23 @@
|
|
1
|
+
import { http } from '../config/httpConfig';
|
2
|
+
import { getLocalStorageItem, setLocalStorageItem, removeLocalStorageItem } from '../utils/storageUtils';
|
3
|
+
|
4
|
+
const initGateway = () => {
|
5
|
+
return new Promise<void>(async resolve => {
|
6
|
+
let gatewayAccessToken = getLocalStorageItem("gatewayAccessToken");
|
7
|
+
if (!gatewayAccessToken) {
|
8
|
+
let res = await http.get('/server/gateway/getGatewayConfig');
|
9
|
+
setLocalStorageItem("gatewayAccessToken", res.data.data.gatewayAccessToken, res.data.data.gatewayTimeout);
|
10
|
+
setLocalStorageItem("gatewayPublicKey", res.data.data.gatewayPublicKey, res.data.data.gatewayTimeout);
|
11
|
+
}
|
12
|
+
resolve();
|
13
|
+
})
|
14
|
+
}
|
15
|
+
|
16
|
+
const clearGateway = () => {
|
17
|
+
removeLocalStorageItem("gatewayAccessToken");
|
18
|
+
removeLocalStorageItem("gatewayPublicKey");
|
19
|
+
}
|
20
|
+
export {
|
21
|
+
initGateway,
|
22
|
+
clearGateway
|
23
|
+
}
|
package/lib/config/httpConfig.ts
CHANGED
@@ -1,14 +1,17 @@
|
|
1
1
|
import axios from "axios"
|
2
2
|
|
3
|
+
import { showErrorMessage } from '../utils/messageUtils';
|
4
|
+
import { alertMessageBox } from '../utils/messageBoxUtils';
|
3
5
|
|
4
|
-
import {
|
5
|
-
import {alertMessageBox} from '../utils/messageBoxUtils';
|
6
|
+
import { getSsoLoginUrl, clearSsoAccessToken } from '../utils/ssoUtils';
|
6
7
|
|
7
|
-
import {
|
8
|
+
import { clearGateway } from '../config/gatewayConfig';
|
9
|
+
|
10
|
+
import {getLocalStorageItem} from '../utils/storageUtils'
|
8
11
|
|
9
12
|
const messageMap = {
|
10
13
|
requestError: '请求异常',
|
11
|
-
responseSturctError: '响应结构错误',
|
14
|
+
responseSturctError: '响应结构错误',
|
12
15
|
}
|
13
16
|
|
14
17
|
const http = axios.create({
|
@@ -20,33 +23,32 @@ const http = axios.create({
|
|
20
23
|
|
21
24
|
/* 请求拦截 */
|
22
25
|
http.interceptors.request.use(config => {
|
23
|
-
|
26
|
+
config.headers["X-Requested-With"] = 'XMLHttpRequest';
|
24
27
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
28
|
+
let gatewayAccessToken = getLocalStorageItem("gatewayAccessToken") ?? '';
|
29
|
+
let gatewayPublicKey = getLocalStorageItem("gatewayPublicKey") ?? '';
|
30
|
+
let ssoAccessToken = getLocalStorageItem("ssoAccessToken") ?? '';
|
31
|
+
if (gatewayAccessToken) {
|
32
|
+
config.headers["Gateway-Access-Token"] = gatewayAccessToken;
|
33
|
+
}
|
34
|
+
if (gatewayPublicKey) {
|
35
|
+
config.headers["Gateway-Public-Key"] = gatewayPublicKey;
|
36
|
+
}
|
34
37
|
|
35
|
-
|
36
|
-
|
37
|
-
}
|
38
|
-
config.headers["Request-Id"] = 'test';
|
39
|
-
return config;
|
40
|
-
}, error => {
|
41
|
-
showErrorMessage(error?.message || messageMap.requestError);
|
42
|
-
return Promise.reject(error);
|
38
|
+
if (ssoAccessToken) {
|
39
|
+
config.headers["Sso-Access-Token"] = ssoAccessToken;
|
43
40
|
}
|
44
|
-
|
41
|
+
config.headers["Request-Id"] = 'test';
|
42
|
+
return config;
|
43
|
+
}, error => {
|
44
|
+
showErrorMessage(error?.message || messageMap.requestError);
|
45
|
+
return Promise.reject(error);
|
46
|
+
});
|
45
47
|
|
46
48
|
|
47
49
|
/* 响应拦截器 */
|
48
50
|
http.interceptors.response.use((res) => {
|
49
|
-
if(!res?.data?.statusCode){
|
51
|
+
if (!res?.data?.statusCode) {
|
50
52
|
showErrorMessage(messageMap.responseSturctError);
|
51
53
|
return Promise.reject(new Error(messageMap.responseSturctError));
|
52
54
|
}
|
@@ -65,6 +67,10 @@ http.interceptors.response.use((res) => {
|
|
65
67
|
}
|
66
68
|
});
|
67
69
|
return Promise.reject(new Error(res?.data?.message));
|
70
|
+
} else if (res?.data?.statusCode === 822001) {
|
71
|
+
clearGateway();
|
72
|
+
showErrorMessage(messageMap.requestError);
|
73
|
+
return Promise.reject(new Error(res?.data?.message));
|
68
74
|
}
|
69
75
|
showErrorMessage(res?.data?.message || messageMap.requestError);
|
70
76
|
return Promise.reject(new Error(res?.data?.message));
|
@@ -76,4 +82,4 @@ http.interceptors.response.use((res) => {
|
|
76
82
|
});
|
77
83
|
|
78
84
|
|
79
|
-
export {http}
|
85
|
+
export { http }
|
package/lib/utils/ssoUtils.ts
CHANGED
@@ -1,8 +1,7 @@
|
|
1
|
-
'use strict'
|
2
|
-
|
3
1
|
import Cookies from 'js-cookie'
|
4
2
|
import {getAesRandomKey, aesEncrypt} from './aesUtils';
|
5
3
|
import {rsaEncrypt} from './rsaUtils';
|
4
|
+
import {getLocalStorageItem, setLocalStorageItem} from './storageUtils';
|
6
5
|
|
7
6
|
|
8
7
|
const getSsoLoginUrl = (redirectUrl = '') => {
|
@@ -44,7 +43,7 @@ const getSsoLoginRoutePath = (redirectRoutePath = '') => {
|
|
44
43
|
* @param password
|
45
44
|
*/
|
46
45
|
const getSsoEncrypt = (password = '') => {
|
47
|
-
let gatewayPublicKey =
|
46
|
+
let gatewayPublicKey = getLocalStorageItem('gatewayPublicKey');
|
48
47
|
// 每次请求生成aeskey
|
49
48
|
let aesKey = getAesRandomKey();
|
50
49
|
// 用登陆后后端生成并返回给前端的的RSA密钥对的公钥将AES16位密钥进行加密
|
@@ -65,7 +64,7 @@ const getSsoAccessToken = () => {
|
|
65
64
|
let ssoAccessToken = Cookies.get(ssoAccessTokenCookieKey);
|
66
65
|
|
67
66
|
if (!ssoAccessToken) {
|
68
|
-
ssoAccessToken =
|
67
|
+
ssoAccessToken = getLocalStorageItem(ssoAccessTokenLocalKey) as string;
|
69
68
|
return ssoAccessToken
|
70
69
|
}
|
71
70
|
|
@@ -76,8 +75,8 @@ const getSsoAccessToken = () => {
|
|
76
75
|
const setSsoAccessToken = (param: any) => {
|
77
76
|
Cookies.set(ssoAccessTokenCookieKey, param.ssoAccessToken);
|
78
77
|
|
79
|
-
|
80
|
-
|
78
|
+
setLocalStorageItem(ssoAccessTokenLocalKey, param.ssoAccessToken, param.ssoExpiresIn);
|
79
|
+
setLocalStorageItem(ssoRefreshTokenLocalKey, param.ssoRefreshToken, param.ssoRefreshExpiresIn);
|
81
80
|
|
82
81
|
}
|
83
82
|
|
@@ -85,8 +84,8 @@ const setSsoAccessToken = (param: any) => {
|
|
85
84
|
const clearSsoAccessToken = () => {
|
86
85
|
Cookies.remove(ssoAccessTokenCookieKey);
|
87
86
|
|
88
|
-
|
89
|
-
|
87
|
+
removeLocalStorageItem(ssoAccessTokenLocalKey);
|
88
|
+
removeLocalStorageItem(ssoRefreshTokenLocalKey);
|
90
89
|
|
91
90
|
}
|
92
91
|
|
@@ -0,0 +1,110 @@
|
|
1
|
+
|
2
|
+
/**
|
3
|
+
* 判断session存储数据是否过期
|
4
|
+
* @param key
|
5
|
+
*/
|
6
|
+
const isSessionStorageExpired = (key) => {
|
7
|
+
const value = sessionStorage.getItem(key);
|
8
|
+
const data = JSON.parse(value);
|
9
|
+
if (Date.now() > data.expires) {
|
10
|
+
sessionStorage.removeItem(key);
|
11
|
+
return true; // 已过期
|
12
|
+
}
|
13
|
+
return false; // 未过期
|
14
|
+
}
|
15
|
+
|
16
|
+
/**
|
17
|
+
* 设置session存储数据
|
18
|
+
* @param key
|
19
|
+
* @param value
|
20
|
+
* @param expires 过期时间,单位:秒
|
21
|
+
*/
|
22
|
+
const setSessionStorageItem = (key, value, expires) => {
|
23
|
+
const data = {
|
24
|
+
value: value,
|
25
|
+
expires: Date.now() + expires * 1000
|
26
|
+
};
|
27
|
+
sessionStorage.setItem(key, JSON.stringify(data));
|
28
|
+
}
|
29
|
+
|
30
|
+
/**
|
31
|
+
* 获取session存储数据
|
32
|
+
* @param key
|
33
|
+
*/
|
34
|
+
const getSessionStorageItem = (key) => {
|
35
|
+
if (isSessionStorageExpired(key)) {
|
36
|
+
return null;
|
37
|
+
}
|
38
|
+
const value = sessionStorage.getItem(key);
|
39
|
+
const data = JSON.parse(value);
|
40
|
+
return data.value;
|
41
|
+
}
|
42
|
+
|
43
|
+
/**
|
44
|
+
* 删除session存储数据
|
45
|
+
* @param key
|
46
|
+
*/
|
47
|
+
const removeSessionStorageItem = (key) => {
|
48
|
+
sessionStorage.removeItem(key);
|
49
|
+
}
|
50
|
+
|
51
|
+
|
52
|
+
/**
|
53
|
+
* 判断local存储数据是否过期
|
54
|
+
* @param key
|
55
|
+
*/
|
56
|
+
const isLocalStorageExpired = (key) => {
|
57
|
+
const value = localStorage.getItem(key);
|
58
|
+
const data = JSON.parse(value);
|
59
|
+
if (Date.now() > data.expires) {
|
60
|
+
localStorage.removeItem(key);
|
61
|
+
return true; // 已过期
|
62
|
+
}
|
63
|
+
return false; // 未过期
|
64
|
+
}
|
65
|
+
|
66
|
+
/**
|
67
|
+
* 设置local存储数据
|
68
|
+
* @param key
|
69
|
+
* @param value
|
70
|
+
* @param expires 过期时间,单位:秒
|
71
|
+
*/
|
72
|
+
const setLocalStorageItem = (key, value, expires) => {
|
73
|
+
const data = {
|
74
|
+
value: value,
|
75
|
+
expires: Date.now() + expires * 1000
|
76
|
+
};
|
77
|
+
localStorage.setItem(key, JSON.stringify(data));
|
78
|
+
}
|
79
|
+
|
80
|
+
/**
|
81
|
+
* 获取local存储数据
|
82
|
+
* @param key
|
83
|
+
*/
|
84
|
+
const getLocalStorageItem = (key) => {
|
85
|
+
if (isLocalStorageExpired(key)) {
|
86
|
+
return null;
|
87
|
+
}
|
88
|
+
const value = localStorage.getItem(key);
|
89
|
+
const data = JSON.parse(value);
|
90
|
+
return data.value;
|
91
|
+
}
|
92
|
+
|
93
|
+
/**
|
94
|
+
* 删除local存储数据
|
95
|
+
* @param key
|
96
|
+
*/
|
97
|
+
const removeLocalStorageItem = (key) => {
|
98
|
+
localStorage.removeItem(key);
|
99
|
+
}
|
100
|
+
|
101
|
+
export {
|
102
|
+
setSessionStorageItem,
|
103
|
+
getSessionStorageItem,
|
104
|
+
removeSessionStorageItem,
|
105
|
+
|
106
|
+
setLocalStorageItem,
|
107
|
+
getLocalStorageItem,
|
108
|
+
removeLocalStorageItem,
|
109
|
+
|
110
|
+
}
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "yuang-framework-ui-common",
|
3
|
-
"version": "1.0.
|
3
|
+
"version": "1.0.16",
|
4
4
|
"private": false,
|
5
5
|
"type": "module",
|
6
6
|
"scripts": {
|
@@ -24,6 +24,7 @@
|
|
24
24
|
"author": "yuang",
|
25
25
|
"license": "ISC",
|
26
26
|
"dependencies": {
|
27
|
+
"@types/lodash": "^4.17.12",
|
27
28
|
"@wangeditor/editor": "^5.1.23",
|
28
29
|
"@wangeditor/editor-for-vue": "^5.1.12",
|
29
30
|
"axios": "^1.7.3",
|
@@ -31,6 +32,7 @@
|
|
31
32
|
"element-plus": "^2.7.8",
|
32
33
|
"js-cookie": "^3.0.5",
|
33
34
|
"jsencrypt": "^3.3.2",
|
35
|
+
"lodash": "^4.17.21",
|
34
36
|
"moment": "^2.30.1",
|
35
37
|
"pinia": "^2.1.7",
|
36
38
|
"vant": "^4.9.8",
|
package/src/.DS_Store
ADDED
Binary file
|
package/src/App.vue
CHANGED
@@ -2,13 +2,13 @@
|
|
2
2
|
<RouterView v-if="isShowRouterView"/>
|
3
3
|
</template>
|
4
4
|
<script setup lang="ts">
|
5
|
-
import {RouterView} from 'vue-router'
|
6
|
-
import {onBeforeMount, ref, getCurrentInstance} from 'vue';
|
7
|
-
import {initGateway} from '../lib/
|
5
|
+
import { RouterView } from 'vue-router'
|
6
|
+
import { onBeforeMount, ref, getCurrentInstance } from 'vue';
|
7
|
+
import { initGateway } from '../lib/config/gatewayConfig'
|
8
8
|
|
9
9
|
let isShowRouterView = ref(false);
|
10
10
|
|
11
|
-
const {proxy} = getCurrentInstance() as
|
11
|
+
const { proxy } = getCurrentInstance() as any;
|
12
12
|
|
13
13
|
onBeforeMount(async () => {
|
14
14
|
await initGateway();
|
@@ -1,19 +0,0 @@
|
|
1
|
-
|
2
|
-
import {http} from '../config/httpConfig';
|
3
|
-
|
4
|
-
const initGateway = () => {
|
5
|
-
return new Promise<void>(async resolve => {
|
6
|
-
let gatewayAccessToken = localStorage.getItem("gatewayAccessToken");
|
7
|
-
if (!gatewayAccessToken) {
|
8
|
-
let res = await http.get('/server/gateway/getGatewayConfig');
|
9
|
-
localStorage.setItem("gatewayAccessToken", res.data.data.gatewayAccessToken);
|
10
|
-
localStorage.setItem("gatewayPublicKey", res.data.data.gatewayPublicKey);
|
11
|
-
}
|
12
|
-
resolve();
|
13
|
-
})
|
14
|
-
|
15
|
-
}
|
16
|
-
|
17
|
-
export {
|
18
|
-
initGateway
|
19
|
-
}
|