-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
Copy pathuni-id-mixin.ts
104 lines (96 loc) · 2.91 KB
/
uni-id-mixin.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
let realAtob: (b64Str: string) => string
const b64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='
const b64re =
/^(?:[A-Za-z\d+/]{4})*?(?:[A-Za-z\d+/]{2}(?:==)?|[A-Za-z\d+/]{3}=?)?$/
if (typeof atob !== 'function') {
realAtob = function (str: string) {
str = String(str).replace(/[\t\n\f\r ]+/g, '')
if (!b64re.test(str)) {
throw new Error(
"Failed to execute 'atob' on 'Window': The string to be decoded is not correctly encoded."
)
}
// Adding the padding if missing, for semplicity
str += '=='.slice(2 - (str.length & 3))
var bitmap
var result = ''
var r1
var r2
var i = 0
for (; i < str.length; ) {
bitmap =
(b64.indexOf(str.charAt(i++)) << 18) |
(b64.indexOf(str.charAt(i++)) << 12) |
((r1 = b64.indexOf(str.charAt(i++))) << 6) |
(r2 = b64.indexOf(str.charAt(i++)))
result +=
r1 === 64
? String.fromCharCode((bitmap >> 16) & 255)
: r2 === 64
? String.fromCharCode((bitmap >> 16) & 255, (bitmap >> 8) & 255)
: String.fromCharCode(
(bitmap >> 16) & 255,
(bitmap >> 8) & 255,
bitmap & 255
)
}
return result
}
} else {
// 注意atob只能在全局对象上调用,例如:`const Base64 = {atob};Base64.atob('xxxx')`是错误的用法
realAtob = atob
}
function b64DecodeUnicode(str: string): string {
return decodeURIComponent(
realAtob(str)
.split('')
.map(function (c) {
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2)
})
.join('')
)
}
interface UniIdUserInfo {
uid: null | string
role: Array<string>
permission: Array<string>
tokenExpired: number
}
function getCurrentUserInfo(): UniIdUserInfo {
const token = uni.getStorageSync('uni_id_token') || ''
const tokenArr = token.split('.')
if (!token || tokenArr.length !== 3) {
return {
uid: null,
role: [],
permission: [],
tokenExpired: 0,
}
}
let userInfo
try {
userInfo = JSON.parse(b64DecodeUnicode(tokenArr[1]))
} catch (error: any) {
throw new Error('获取当前用户信息出错,详细错误信息为:' + error.message)
}
userInfo.tokenExpired = userInfo.exp * 1000
delete userInfo.exp
delete userInfo.iat
return userInfo
}
export function uniIdMixin(globalProperties: Record<string, any>): void {
globalProperties.uniIDHasRole = function (roleId: string): boolean {
const { role } = getCurrentUserInfo()
return role.indexOf(roleId) > -1
}
globalProperties.uniIDHasPermission = function (
permissionId: string
): boolean {
const { permission } = getCurrentUserInfo()
return this.uniIDHasRole('admin') || permission.indexOf(permissionId) > -1
}
globalProperties.uniIDTokenValid = function (): boolean {
const { tokenExpired } = getCurrentUserInfo()
return tokenExpired > Date.now()
}
}