useParseParams.ts
1.61 KB
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
import type { PlatformEnum } from '@/enums/modeEnum'
import { PageModeEnum } from '@/enums/modeEnum'
type FromType = 'edge'
interface HashParams {
configurationId: string
organizationId: string
mode: PageModeEnum
platform?: PlatformEnum
publicId?: string
userId?: string
parentPageIsPublic?: boolean
locale?: string
from?: FromType
}
export function useParseParams(): HashParams {
try {
const { hash } = window.location
let string = atob(hash.substring(1))
string = atob(string.substring(0, 6) + string.substring(16))
return JSON.parse(string)
}
catch (error) {
throw new Error('错误')
}
}
const getRandomString = () => Number(Math.random().toString().substring(2)).toString(36)
export const useEncodeParams = (record: Recordable) => {
let hash = JSON.stringify(record)
const mixinString = getRandomString()
.slice(0, 10)
.padEnd(10, getRandomString())
.split('')
.map(item => (Math.random() > 0.5 ? item.toUpperCase() : item))
.join('')
hash = window.btoa(hash)
hash = hash.substring(0, 6) + mixinString + hash.substring(6)
hash = window.btoa(hash)
return hash
}
export const switchLightboxMode = () => {
const params = useParseParams?.()
params.mode = PageModeEnum.LIGHTBOX
const { origin, pathname } = location
const url = new URL(origin)
url.pathname = pathname
url.hash = useEncodeParams?.(params)
window.open(url.href)
}
export function isFromEdge() {
const { from } = useParseParams()
return from === 'edge'
}
window.useEncodeParams = useEncodeParams
window.useParseParams = useParseParams
window.switchLightboxMode = switchLightboxMode