help.js 1.21 KB
import config from '../../../../config/baseUrl.js'
import {
	atob,
	btoa
} from './weapp.atob.js'
const getRandomString = () => Number(Math.random().toString().substring(2)).toString(36);

export const ScadaModeEnum = {
	PRIVATE_VIEW: 'PRIVATE_VIEW',
	PUBLIC_VIEW: 'PUBLIC_VIEW',
}

export const encode = (record) => {
	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 = btoa(hash);
	hash = hash.substring(0, 6) + mixinString + hash.substring(6);
	hash = btoa(hash);
	return hash;
};


export const createScadaPageLink = (
	record,
	mode = ScadaModeEnum.PRIVATE_VIEW,
	open = false
) => {
	const userInfo = uni.getStorageSync('userInfo')
	const params = {
		configurationId: record?.id,
		organizationId: record?.organizationId,
		mode: record?.viewType === ScadaModeEnum.PRIVATE_VIEW ? 'lightbox' : 'share',
		platform: record?.platform,
		userId: userInfo.userId
	};

	if (record?.viewType === ScadaModeEnum.PUBLIC_VIEW) {
		params.publicId = record.publicId;
	}

	const hash = encode(params);

	const href = `${config.baseDrawioUrl}#${hash}`

	return href
};