const.js 2.55 KB
const isShareMode = (() => {
	const urlParams = Array.from(new URLSearchParams(window.location.href)).reduce((prev, [key, value]) => ({ ...prev, [key]: value }), {})
	return urlParams.share && urlParams.share === 'SCADA'
})()

const ORGANIZATION_ID = (() => {
	var result = new Object();
	var params = window.location.search.slice(1).split('&');

	for (var i = 0; i < params.length; i++) {
		var idx = params[i].indexOf('=');

		if (idx > 0) {
			result[params[i].substring(0, idx)] = params[i].substring(idx + 1);
		}
	}
	
	return result.organizationId
})()

const GLOBAL_STORAGE_KEY = (() => {
	const isDEV = location.href.includes('dev=1')
	const DEVELOPMENT = 'DEVELOPMENT'
	const PRODUCTION = 'PRODUCTION'
	return `UNDEFINED__${isDEV ? DEVELOPMENT : PRODUCTION}__2.7.1__COMMON__LOCAL__KEY__`
})()

const GLOBAL_PLATFORM_INFO_KEY = (() => {
	const isDEV = location.href.includes('dev=1')
	const DEVELOPMENT = 'DEVELOPMENT'
	const PRODUCTION = 'PRODUCTION'
	return `UNDEFINED__${isDEV ? DEVELOPMENT : PRODUCTION}__2.7.1__PLATFORMINFO`
})()

const GLOBAL_TOKEN = (() => {
	const ls = createStorage({ storage: localStorage })
	/**
	 * @description user info
	 * @type {{JWT_TOKEN: {value: string}}}
	 */
	const common = ls.get(GLOBAL_STORAGE_KEY)

	return isShareMode
		? {
			token: common && common.SHARE_JWT_TOKEN && common.SHARE_JWT_TOKEN.value,
			refreshToken: common && common.SHARE_REFRESH_TOKEN && common.SHARE_REFRESH_TOKEN.value,
		}
		: {
			token: common && common.JWT_TOKEN && common.JWT_TOKEN.value,
			refreshToken: common && common.REFRESH_TOKEN && common.REFRESH_TOKEN.value,
		}
})()


const GLOBAL_PLATFORM_INFO = (() => {
	const ls = createStorage({ storage: localStorage })
	/**
	 * @description user info
	 * @type {{JWT_TOKEN: {value: string}}}
	 */
	const common = ls.get(GLOBAL_PLATFORM_INFO_KEY) || {}
	return common
})()

/**
 * @description 写权限 key
 */
const CAN_WRITE = 'api:yt:configuration:center:get_configuration_info:design'

const USER_PERMISSION = {
	permission: []
}

const IS_CUSTOMER_USER = (() => {
	const ls = createStorage({ storage: localStorage })
	const common = ls.get(GLOBAL_STORAGE_KEY) || { USER__INFO__: {} }
	const userInfo = ((common || {}).USER__INFO__ || {}).value || {}
	return (((userInfo || {})).roles || [])[0] === 'CUSTOMER_USER'
})()

const hasSavePermission = () => {
	return USER_PERMISSION.permission.includes(CAN_WRITE) && !IS_CUSTOMER_USER
}

const GLOBAL_WS_URL = () => {
	const { host, href } = location
	const reg = /^https/
	return `${reg.test(href) ? 'wss' : 'ws'}://${host}/api/ws/plugins/telemetry?token=${GLOBAL_TOKEN.token}`
}