yuang-framework-ui-common 1.0.97 → 1.0.99
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/utils/dataUtils.ts +17 -0
- package/lib/utils/dateUtils.ts +167 -17
- package/lib/utils/ssoUtils.ts +21 -21
- package/package.json +1 -1
- package/src/router/index.ts +0 -7
- package/src/views/sso/client/sso-auth/auth-success.vue +0 -46
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 设置数字补零
|
|
3
|
+
* @param num 数字
|
|
4
|
+
* @param length 位数
|
|
5
|
+
* @returns {string}
|
|
6
|
+
*/
|
|
7
|
+
const setNumAddZero = (num, length = 2) => {
|
|
8
|
+
let str = '';
|
|
9
|
+
num = String(num);
|
|
10
|
+
for (let i = num.length; i < length; i++) str += '0';
|
|
11
|
+
return num < Math.pow(10, length) ? str + (num | 0) : num;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export {
|
|
15
|
+
setNumAddZero,
|
|
16
|
+
|
|
17
|
+
}
|
package/lib/utils/dateUtils.ts
CHANGED
|
@@ -1,3 +1,139 @@
|
|
|
1
|
+
import { setNumAddZero } from './dataUtils';
|
|
2
|
+
|
|
3
|
+
import moment, { type MomentInput } from 'moment';
|
|
4
|
+
// 可选:引入中文语言包(按需开启)
|
|
5
|
+
import 'moment/locale/zh-cn';
|
|
6
|
+
|
|
7
|
+
moment.locale('zh-cn');
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* 初始化日期对象(内部工具函数,不对外导出)
|
|
11
|
+
* @param {string|number|Date|moment.Moment} date - 日期入参(不传则为当前时间)
|
|
12
|
+
* @returns {moment.Moment} 格式化后的moment对象(无效则返回当前时间)
|
|
13
|
+
*/
|
|
14
|
+
const initDate = (date?: MomentInput): moment.Moment => {
|
|
15
|
+
if (!date) return moment();
|
|
16
|
+
const momentDate = moment(date);
|
|
17
|
+
return momentDate.isValid() ? momentDate : moment();
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* 日期格式化(默认格式:YYYY-MM-DD HH:mm:ss)
|
|
22
|
+
* @param {string|number|Date|moment.Moment} date - 要格式化的日期
|
|
23
|
+
* @param {string} format - 自定义格式
|
|
24
|
+
* @returns {string} 格式化后的日期字符串
|
|
25
|
+
*/
|
|
26
|
+
const formatDate = (date, format = 'YYYY-MM-DD HH:mm:ss') => {
|
|
27
|
+
return initDate(date).format(format);
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* 获取当前时间(快捷方法)
|
|
32
|
+
* @param {string} format - 自定义格式
|
|
33
|
+
* @returns {string} 当前时间字符串
|
|
34
|
+
*/
|
|
35
|
+
const getNow = (format = 'YYYY-MM-DD HH:mm:ss') => {
|
|
36
|
+
return moment().format(format);
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* 日期加减计算
|
|
41
|
+
* @param {string|number|Date|moment.Moment} date - 基准日期
|
|
42
|
+
* @param {number} num - 加减的数值(正数加,负数减)
|
|
43
|
+
* @param {string} unit - 单位(year/month/day/hour/minute/second/week)
|
|
44
|
+
* @param {string} format - 输出格式
|
|
45
|
+
* @returns {string} 计算后的日期字符串
|
|
46
|
+
*/
|
|
47
|
+
const addSubtractDate = (date, num, unit, format = 'YYYY-MM-DD HH:mm:ss') => {
|
|
48
|
+
const momentDate = initDate(date);
|
|
49
|
+
return momentDate.add(num, unit).format(format);
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* 比较两个日期的大小
|
|
54
|
+
* @param {string|number|Date|moment.Moment} date1 - 日期1
|
|
55
|
+
* @param {string|number|Date|moment.Moment} date2 - 日期2
|
|
56
|
+
* @returns {number} 1: date1 > date2;-1: date1 < date2;0: 相等
|
|
57
|
+
*/
|
|
58
|
+
const compareDate = (date1, date2) => {
|
|
59
|
+
const m1 = initDate(date1);
|
|
60
|
+
const m2 = initDate(date2);
|
|
61
|
+
if (m1.isAfter(m2)) return 1;
|
|
62
|
+
if (m1.isBefore(m2)) return -1;
|
|
63
|
+
return 0;
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* 计算两个日期的差值
|
|
68
|
+
* @param {string|number|Date|moment.Moment} date1 - 日期1
|
|
69
|
+
* @param {string|number|Date|moment.Moment} date2 - 日期2
|
|
70
|
+
* @param {string} unit - 差值单位(days/hours/minutes/seconds/months/years)
|
|
71
|
+
* @returns {number} 差值(正数,date1 - date2 的绝对值)
|
|
72
|
+
*/
|
|
73
|
+
const diffDate = (date1: MomentInput, date2: MomentInput, unit = 'days'): number => {
|
|
74
|
+
const m1 = initDate(date1);
|
|
75
|
+
const m2 = initDate(date2);
|
|
76
|
+
// @ts-ignore
|
|
77
|
+
return Math.abs(m1.diff(m2, unit));
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* 验证日期是否有效
|
|
82
|
+
* @param {string|number|Date|moment.Moment} date - 要验证的日期
|
|
83
|
+
* @returns {boolean} true: 有效;false: 无效
|
|
84
|
+
*/
|
|
85
|
+
const isValidDate = (date) => {
|
|
86
|
+
return moment(date).isValid();
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* 获取日期的指定部分(年/月/日/时/分/秒)
|
|
91
|
+
* @param {string|number|Date|moment.Moment} date - 基准日期
|
|
92
|
+
* @param {string} unit - 要获取的单位(year/month/day/hour/minute/second/weekday)
|
|
93
|
+
* @returns {number} 对应的值(month 从 1 开始,weekday 周日为 0)
|
|
94
|
+
*/
|
|
95
|
+
const getDatePart = (date, unit) => {
|
|
96
|
+
const momentDate = initDate(date);
|
|
97
|
+
if (unit === 'month') {
|
|
98
|
+
return momentDate.month() + 1; // moment的month是0开始,转为1开始
|
|
99
|
+
}
|
|
100
|
+
return momentDate[unit]();
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* 转换为时间戳
|
|
105
|
+
* @param {string|number|Date|moment.Moment} date - 基准日期
|
|
106
|
+
* @param {boolean} isMillisecond - 是否返回毫秒级(默认false:秒级)
|
|
107
|
+
* @returns {number} 时间戳
|
|
108
|
+
*/
|
|
109
|
+
const toDateTimestamp = (date, isMillisecond = false) => {
|
|
110
|
+
const momentDate = initDate(date);
|
|
111
|
+
return isMillisecond ? momentDate.valueOf() : momentDate.unix();
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* 格式化相对时间(如:几秒前、几小时前)
|
|
116
|
+
* @param {string|number|Date|moment.Moment} date - 基准日期
|
|
117
|
+
* @returns {string} 相对时间字符串(中文)
|
|
118
|
+
*/
|
|
119
|
+
const formatFromNow = (date) => {
|
|
120
|
+
return initDate(date).fromNow();
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* 获取当月的第一天/最后一天
|
|
125
|
+
* @param {string|number|Date|moment.Moment} date - 基准日期
|
|
126
|
+
* @param {boolean} isFirst - true: 第一天;false: 最后一天
|
|
127
|
+
* @param {string} format - 输出格式
|
|
128
|
+
* @returns {string} 对应日期
|
|
129
|
+
*/
|
|
130
|
+
const getMonthEdgeDate = (date, isFirst = true, format = 'YYYY-MM-DD') => {
|
|
131
|
+
const momentDate = initDate(date);
|
|
132
|
+
return isFirst
|
|
133
|
+
? momentDate.startOf('month').format(format)
|
|
134
|
+
: momentDate.endOf('month').format(format);
|
|
135
|
+
};
|
|
136
|
+
|
|
1
137
|
/**
|
|
2
138
|
* 某个时间在当前时间的多久前
|
|
3
139
|
* @param time 需要语义化的时间
|
|
@@ -7,28 +143,30 @@
|
|
|
7
143
|
const getTimeAgoString = (time, onlyDate) => {
|
|
8
144
|
if (!time) return '';
|
|
9
145
|
if (typeof time === 'string') time = time.replace(/-/g, '/');
|
|
10
|
-
let arr = [[], []]
|
|
146
|
+
let arr = [ [], [] ];
|
|
147
|
+
let timestamp = new Date().getTime() - new Date(time).getTime();
|
|
11
148
|
// 30天以上返回具体日期
|
|
12
|
-
if (
|
|
13
|
-
|
|
14
|
-
arr[0][0] =
|
|
15
|
-
arr[0][1] =
|
|
16
|
-
arr[0][2] =
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
arr[1][
|
|
20
|
-
arr[1][
|
|
149
|
+
if (timestamp > 1000 * 60 * 60 * 24 * 31) {
|
|
150
|
+
let date = new Date(time);
|
|
151
|
+
arr[0][0] = setNumAddZero(date.getFullYear(), 4);
|
|
152
|
+
arr[0][1] = setNumAddZero(date.getMonth() + 1);
|
|
153
|
+
arr[0][2] = setNumAddZero(date.getDate());
|
|
154
|
+
// 是否输出时间
|
|
155
|
+
if (!onlyDate) {
|
|
156
|
+
arr[1][0] = setNumAddZero(date.getHours());
|
|
157
|
+
arr[1][1] = setNumAddZero(date.getMinutes());
|
|
158
|
+
arr[1][2] = setNumAddZero(date.getSeconds());
|
|
21
159
|
}
|
|
22
160
|
return arr[0].join('-') + ' ' + arr[1].join(':');
|
|
23
161
|
}
|
|
24
162
|
// 30天以内,返回“多久前”
|
|
25
|
-
if (
|
|
26
|
-
return ((
|
|
27
|
-
} else if (
|
|
28
|
-
return ((
|
|
29
|
-
} else if (
|
|
30
|
-
return ((
|
|
31
|
-
} else if (
|
|
163
|
+
if (timestamp >= 1000 * 60 * 60 * 24) {
|
|
164
|
+
return ((timestamp / 1000 / 60 / 60 / 24) | 0) + '天前';
|
|
165
|
+
} else if (timestamp >= 1000 * 60 * 60) {
|
|
166
|
+
return ((timestamp / 1000 / 60 / 60) | 0) + '小时前';
|
|
167
|
+
} else if (timestamp >= 1000 * 60 * 3) { // 3分钟以内为:刚刚
|
|
168
|
+
return ((timestamp / 1000 / 60) | 0) + '分钟前';
|
|
169
|
+
} else if (timestamp < 0) {
|
|
32
170
|
return '未来';
|
|
33
171
|
} else {
|
|
34
172
|
return '刚刚';
|
|
@@ -36,6 +174,18 @@ const getTimeAgoString = (time, onlyDate) => {
|
|
|
36
174
|
}
|
|
37
175
|
|
|
38
176
|
export {
|
|
177
|
+
formatDate,
|
|
178
|
+
getNow,
|
|
179
|
+
addSubtractDate,
|
|
180
|
+
compareDate,
|
|
181
|
+
diffDate,
|
|
182
|
+
isValidDate,
|
|
183
|
+
getDatePart,
|
|
184
|
+
toDateTimestamp,
|
|
185
|
+
formatFromNow,
|
|
186
|
+
getMonthEdgeDate,
|
|
187
|
+
|
|
188
|
+
|
|
39
189
|
getTimeAgoString,
|
|
40
190
|
|
|
41
191
|
}
|
package/lib/utils/ssoUtils.ts
CHANGED
|
@@ -79,26 +79,6 @@ const getSsoLogoutUrl = (ssoRedirectUrl = '') => {
|
|
|
79
79
|
}
|
|
80
80
|
return ssoLogoutUrl;
|
|
81
81
|
}
|
|
82
|
-
//
|
|
83
|
-
// /**
|
|
84
|
-
// * 获取sso认证路由路径
|
|
85
|
-
// * @param ssoRedirectRoutePath
|
|
86
|
-
// */
|
|
87
|
-
// const getSsoAuthRoutePath = (ssoRedirectRoutePath = '') => {
|
|
88
|
-
// if(getIsDebug()) {
|
|
89
|
-
// debugger;
|
|
90
|
-
// }
|
|
91
|
-
// let ssoAuthRoutePath = '/sso/client/sso-auth/index';
|
|
92
|
-
// if (!ssoRedirectRoutePath) {
|
|
93
|
-
// ssoAuthRoutePath += `?ssoRedirectRoutePath=${encodeURIComponent('/sso/client/sso-auth/auth-success')}`;
|
|
94
|
-
// } else {
|
|
95
|
-
// ssoAuthRoutePath += `?ssoRedirectRoutePath=${encodeURIComponent(ssoRedirectRoutePath)}`;
|
|
96
|
-
// }
|
|
97
|
-
// if(getIsDebug()) {
|
|
98
|
-
// console.log('ssoAuthRoutePath', ssoAuthRoutePath);
|
|
99
|
-
// }
|
|
100
|
-
// return ssoAuthRoutePath;
|
|
101
|
-
// }
|
|
102
82
|
|
|
103
83
|
/**
|
|
104
84
|
* 处理sso认证
|
|
@@ -247,6 +227,24 @@ const clearSsoAccessToken = () => {
|
|
|
247
227
|
|
|
248
228
|
}
|
|
249
229
|
|
|
230
|
+
const setSsoIdentity = (ssoIdentity) => {
|
|
231
|
+
if(!ssoIdentity) {
|
|
232
|
+
console.error('参数[ssoIdentity]为null');
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
localStorage.setItem('ssoIdentity', JSON.stringify(ssoIdentity));
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
const getSsoIdentity = () => {
|
|
239
|
+
let object = localStorage.getItem('ssoIdentity');
|
|
240
|
+
if(!object){
|
|
241
|
+
return null;
|
|
242
|
+
}
|
|
243
|
+
let ssoIdentity = JSON.parse(object);
|
|
244
|
+
return ssoIdentity;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
|
|
250
248
|
const logoutSso = () => {
|
|
251
249
|
clearSsoAccessToken();
|
|
252
250
|
window.location.href = getSsoLogoutUrl() || '';
|
|
@@ -257,7 +255,6 @@ export {
|
|
|
257
255
|
getSsoLoginUrl,
|
|
258
256
|
getSsoAuthUrl,
|
|
259
257
|
getSsoLogoutUrl,
|
|
260
|
-
// getSsoAuthRoutePath,
|
|
261
258
|
handleSsoAuth,
|
|
262
259
|
|
|
263
260
|
getSsoEncrypt,
|
|
@@ -271,5 +268,8 @@ export {
|
|
|
271
268
|
|
|
272
269
|
clearSsoAccessToken,
|
|
273
270
|
|
|
271
|
+
setSsoIdentity,
|
|
272
|
+
getSsoIdentity,
|
|
273
|
+
|
|
274
274
|
logoutSso,
|
|
275
275
|
}
|
package/package.json
CHANGED
package/src/router/index.ts
CHANGED
|
@@ -7,13 +7,6 @@ import { handleSsoAuth } from '../../lib/utils/ssoUtils';
|
|
|
7
7
|
const router = createRouter({
|
|
8
8
|
history: createWebHistory(import.meta.env.BASE_URL),
|
|
9
9
|
routes: [
|
|
10
|
-
{
|
|
11
|
-
path: '/sso/client/sso-auth/auth-success',
|
|
12
|
-
component: () => import('@/views/sso/client/sso-auth/auth-success.vue'),
|
|
13
|
-
meta: {
|
|
14
|
-
title: '认证成功',
|
|
15
|
-
}
|
|
16
|
-
},
|
|
17
10
|
{
|
|
18
11
|
path: '/gateway/gateway-config',
|
|
19
12
|
component: () => import('@/views/gateway/gateway-config.vue')
|
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
<template>
|
|
2
|
-
<h1>认证成功! {{ ssoData?.ssoTokenUser?.account }}</h1>
|
|
3
|
-
|
|
4
|
-
<h3 @click="getSsoTokenUser">获取ssoTokenUser</h3>
|
|
5
|
-
<h3 @click="getSsoIdentity">获取SsoIdentity</h3>
|
|
6
|
-
|
|
7
|
-
<a @click="logout">注销登录</a>
|
|
8
|
-
</template>
|
|
9
|
-
|
|
10
|
-
<script setup lang="ts">
|
|
11
|
-
import { onMounted, reactive } from 'vue';
|
|
12
|
-
import { ElMessage } from 'element-plus/es';
|
|
13
|
-
|
|
14
|
-
import { getSsoAccessToken, logoutSso } from '../../../../lib/utils/ssoUtils';
|
|
15
|
-
import { http } from '../../../../lib/config/httpConfig';
|
|
16
|
-
|
|
17
|
-
let ssoData = reactive({
|
|
18
|
-
ssoTokenUser: {},
|
|
19
|
-
ssoIdentity: {}
|
|
20
|
-
});
|
|
21
|
-
onMounted(() => {
|
|
22
|
-
let ssoAccessToken = getSsoAccessToken();
|
|
23
|
-
console.log('ssoAccessToken', ssoAccessToken);
|
|
24
|
-
getSsoTokenUser();
|
|
25
|
-
});
|
|
26
|
-
const getSsoTokenUser = () => {
|
|
27
|
-
http.get(`/sso-api/client/sso-user/getSsoTokenUser`).then((res: any) => {
|
|
28
|
-
ssoData.ssoTokenUser = res.data.data;
|
|
29
|
-
console.log('ssoTokenUser', ssoData.ssoTokenUser);
|
|
30
|
-
ElMessage.success('获取成功');
|
|
31
|
-
});
|
|
32
|
-
};
|
|
33
|
-
const getSsoIdentity = () => {
|
|
34
|
-
http.get(`/sso-api/client/sso-user/getSsoIdentity`).then((res: any) => {
|
|
35
|
-
ssoData.ssoIdentity = res.data.data;
|
|
36
|
-
console.log('ssoIdentity', ssoData.ssoIdentity);
|
|
37
|
-
ElMessage.success('获取成功');
|
|
38
|
-
});
|
|
39
|
-
};
|
|
40
|
-
|
|
41
|
-
const logout = () => {
|
|
42
|
-
logoutSso();
|
|
43
|
-
};
|
|
44
|
-
</script>
|
|
45
|
-
|
|
46
|
-
<style scoped></style>
|