help.ts
1.41 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
export enum ScadaModeEnum {
LIGHTBOX = 'lightbox',
DESIGN = 'design',
SHARE = 'share',
}
export enum Platform {
PHONE = 'phone',
PC = 'pc',
}
interface ScadaLinkParamsType {
configurationId: string;
organizationId: string;
mode: ScadaModeEnum;
platform: Platform;
publicId?: string;
}
const getRandomString = () => Number(Math.random().toString().substring(2)).toString(36);
export const encode = (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 createScadaPageLink = (
record: Partial<Record<'id' | 'organizationId' | 'platform' | 'publicId', string>>,
mode: ScadaModeEnum = ScadaModeEnum.DESIGN,
open = true
) => {
const params: ScadaLinkParamsType = {
configurationId: record.id!,
organizationId: record.organizationId!,
mode: mode,
platform: record.platform as Platform,
};
if (mode === ScadaModeEnum.SHARE) {
params.publicId = record.publicId;
}
const href = new URL(location.origin);
href.pathname = '/thingskit-scada';
href.hash = encode(params);
open && window.open(href.href);
return href.href;
};