Commit 05158aa59c56ad873dd9f76b03d1bd14cb4818db

Authored by xp.Huang
2 parents 1f2a7897 5aff3f67

Merge branch 'perf/env-var-parse' into 'main_dev'

perf: 优化环境变量解析&&新增变量控制GBT设备播放协议

See merge request yunteng/thingskit-view!259
@@ -12,3 +12,7 @@ VITE_GLOB_CONTENT_SECURITY_POLICY = false @@ -12,3 +12,7 @@ VITE_GLOB_CONTENT_SECURITY_POLICY = false
12 12
13 # 公共路径 13 # 公共路径
14 VITE_GLOB_PUBLIC_PATH = /large-designer/ 14 VITE_GLOB_PUBLIC_PATH = /large-designer/
  15 +
  16 +# Streaming media content security protocol
  17 +VITE_GLOB_STREAM_MEDIA_CONTENT_SECURITY_PROTOCOL = false
  18 +
@@ -11,3 +11,6 @@ VITE_GLOB_CONTENT_SECURITY_POLICY = false @@ -11,3 +11,6 @@ VITE_GLOB_CONTENT_SECURITY_POLICY = false
11 11
12 # 公共路径 12 # 公共路径
13 VITE_GLOB_PUBLIC_PATH = /large-designer/ 13 VITE_GLOB_PUBLIC_PATH = /large-designer/
  14 +
  15 +# Streaming media content security protocol
  16 +VITE_GLOB_STREAM_MEDIA_CONTENT_SECURITY_PROTOCOL = false
@@ -4,6 +4,31 @@ import { Plugin } from "vite"; @@ -4,6 +4,31 @@ import { Plugin } from "vite";
4 import { GLOB_CONFIG_FILE_NAME } from "./const"; 4 import { GLOB_CONFIG_FILE_NAME } from "./const";
5 import { getGlobalConfigName } from "./getGlobConfigName"; 5 import { getGlobalConfigName } from "./getGlobConfigName";
6 6
  7 +
  8 +const stringToJSONParse = (string: string) => {
  9 + try {
  10 + return JSON.parse(string);
  11 + } catch (error) {
  12 + return string;
  13 + }
  14 +};
  15 +
  16 +export function parseEnv(env: Record<string, string>) {
  17 + const res: Record<string, string> = {};
  18 +
  19 + Object.keys(env).forEach((key) => {
  20 + try {
  21 + const value = env[key];
  22 + res[key] = stringToJSONParse(value);
  23 + } catch (err) {
  24 + // eslint-disable-next-line no-console
  25 + console.log(`env variable ${key} can't serialization!`);
  26 + }
  27 + });
  28 +
  29 + return res;
  30 +}
  31 +
7 export function GenerateBuildConfig(viteEnv: Record<string, any>) { 32 export function GenerateBuildConfig(viteEnv: Record<string, any>) {
8 return { 33 return {
9 name: 'vite-plugin-generate-global-config', 34 name: 'vite-plugin-generate-global-config',
@@ -15,7 +40,7 @@ export function GenerateBuildConfig(viteEnv: Record<string, any>) { @@ -15,7 +40,7 @@ export function GenerateBuildConfig(viteEnv: Record<string, any>) {
15 try { 40 try {
16 const windowConf = `window.${configName}`; 41 const windowConf = `window.${configName}`;
17 // Ensure that the variable will not be modified 42 // Ensure that the variable will not be modified
18 - const configStr = `${windowConf}=${JSON.stringify(config)}; 43 + const configStr = `${windowConf}=${JSON.stringify(parseEnv(config))};
19 Object.freeze(${windowConf}); 44 Object.freeze(${windowConf});
20 Object.defineProperty(window, "${configName}", { 45 Object.defineProperty(window, "${configName}", {
21 configurable: false, 46 configurable: false,
@@ -10,7 +10,8 @@ export const useGlobSetting = (): Readonly<GlobConfig> => { @@ -10,7 +10,8 @@ export const useGlobSetting = (): Readonly<GlobConfig> => {
10 VITE_GLOB_UPLOAD_URL, 10 VITE_GLOB_UPLOAD_URL,
11 VITE_GLOB_WEB_SOCKET, 11 VITE_GLOB_WEB_SOCKET,
12 VITE_GLOB_CONTENT_SECURITY_POLICY, 12 VITE_GLOB_CONTENT_SECURITY_POLICY,
13 - VITE_GLOB_PUBLIC_PATH 13 + VITE_GLOB_PUBLIC_PATH,
  14 + VITE_GLOB_STREAM_MEDIA_CONTENT_SECURITY_PROTOCOL
14 } = getAppEnvConfig(); 15 } = getAppEnvConfig();
15 16
16 if (!/[a-zA-Z_]*/.test(VITE_GLOB_APP_SHORT_NAME)) { 17 if (!/[a-zA-Z_]*/.test(VITE_GLOB_APP_SHORT_NAME)) {
@@ -28,7 +29,8 @@ export const useGlobSetting = (): Readonly<GlobConfig> => { @@ -28,7 +29,8 @@ export const useGlobSetting = (): Readonly<GlobConfig> => {
28 uploadUrl: VITE_GLOB_UPLOAD_URL, 29 uploadUrl: VITE_GLOB_UPLOAD_URL,
29 socketUrl: VITE_GLOB_WEB_SOCKET, 30 socketUrl: VITE_GLOB_WEB_SOCKET,
30 securityPolicy: VITE_GLOB_CONTENT_SECURITY_POLICY, 31 securityPolicy: VITE_GLOB_CONTENT_SECURITY_POLICY,
31 - publicPath: VITE_GLOB_PUBLIC_PATH 32 + publicPath: VITE_GLOB_PUBLIC_PATH,
  33 + streamMediaContentSecurityProtocol: VITE_GLOB_STREAM_MEDIA_CONTENT_SECURITY_PROTOCOL
32 }; 34 };
33 35
34 return glob as Readonly<GlobConfig>; 36 return glob as Readonly<GlobConfig>;
@@ -6,8 +6,7 @@ let initialFlag = false; @@ -6,8 +6,7 @@ let initialFlag = false;
6 export function useWebSecurityPolicy() { 6 export function useWebSecurityPolicy() {
7 if (window && window.document && window.document.documentElement) { 7 if (window && window.document && window.document.documentElement) {
8 const { securityPolicy } = useGlobSetting(); 8 const { securityPolicy } = useGlobSetting();
9 - const flag = isBoolean(securityPolicy) ? securityPolicy : securityPolicy === 'true'  
10 - if (flag && !initialFlag) { 9 + if (securityPolicy && !initialFlag) {
11 const meta = document.createElement('meta'); 10 const meta = document.createElement('meta');
12 meta.setAttribute('http-equiv', 'Content-Security-Policy'); 11 meta.setAttribute('http-equiv', 'Content-Security-Policy');
13 meta.setAttribute('content', 'upgrade-insecure-requests'); 12 meta.setAttribute('content', 'upgrade-insecure-requests');
@@ -2,11 +2,12 @@ import { PublicConfigClass } from '@/packages/public' @@ -2,11 +2,12 @@ import { PublicConfigClass } from '@/packages/public'
2 import { CreateComponentType } from '@/packages/index.d' 2 import { CreateComponentType } from '@/packages/index.d'
3 import { SingleCameraConfig } from './index' 3 import { SingleCameraConfig } from './index'
4 import cloneDeep from 'lodash/cloneDeep' 4 import cloneDeep from 'lodash/cloneDeep'
5 -import {StreamType} from "@/components/Video/src/types";  
6 -import {useFingerprint} from "@/utils/external/useFingerprint";  
7 -import {getOpenFlvPlayUrl, getVideoControlStart} from "@/api/external/flvPlay";  
8 -import {getVideoUrl} from "@/api/external/common";  
9 -import {CameraRecord} from "@/api/external/common/model"; 5 +import { StreamType } from "@/components/Video/src/types";
  6 +import { useFingerprint } from "@/utils/external/useFingerprint";
  7 +import { getOpenFlvPlayUrl, getVideoControlStart } from "@/api/external/flvPlay";
  8 +import { getVideoUrl } from "@/api/external/common";
  9 +import { CameraRecord } from "@/api/external/common/model";
  10 +import { useGlobSetting } from '@/hooks/external/setting';
10 11
11 export enum sourceTypeEnum { 12 export enum sourceTypeEnum {
12 CUSTOM = 'custom', 13 CUSTOM = 'custom',
@@ -89,7 +90,7 @@ export const isRtspProtocol = (url: string) => { @@ -89,7 +90,7 @@ export const isRtspProtocol = (url: string) => {
89 }; 90 };
90 91
91 export async function getPlayUrl( 92 export async function getPlayUrl(
92 - params: CameraRecord 93 + params: CameraRecord
93 ): Promise<{ url: string; type: StreamType } | undefined> { 94 ): Promise<{ url: string; type: StreamType } | undefined> {
94 const { accessMode } = params; 95 const { accessMode } = params;
95 if (accessMode === AccessMode.ManuallyEnter) { 96 if (accessMode === AccessMode.ManuallyEnter) {
@@ -106,18 +107,20 @@ export async function getPlayUrl( @@ -106,18 +107,20 @@ export async function getPlayUrl(
106 } 107 }
107 } 108 }
108 } else if (accessMode === AccessMode.GBT28181) { 109 } else if (accessMode === AccessMode.GBT28181) {
  110 + const { streamMediaContentSecurityProtocol } = useGlobSetting()
  111 + console.log(useGlobSetting())
109 const { deviceId, channelNo } = params?.params || {}; 112 const { deviceId, channelNo } = params?.params || {};
110 const result = await getVideoControlStart({ channelId: channelNo!, deviceId: deviceId! }); 113 const result = await getVideoControlStart({ channelId: channelNo!, deviceId: deviceId! });
111 - return { url: result.data.flv, type: 'flv' }; 114 + return { url: streamMediaContentSecurityProtocol ? result.data.https_flv : result.data.flv, type: 'flv' };
112 } else { 115 } else {
113 const { id, playProtocol } = params; 116 const { id, playProtocol } = params;
114 const result = await getVideoUrl(id); 117 const result = await getVideoUrl(id);
115 const type: StreamType = 118 const type: StreamType =
116 - playProtocol === FluoriteMideaProtocolEnum.FLV  
117 - ? 'flv'  
118 - : playProtocol === FluoriteMideaProtocolEnum.HLS  
119 - ? 'hls'  
120 - : 'auto'; 119 + playProtocol === FluoriteMideaProtocolEnum.FLV
  120 + ? 'flv'
  121 + : playProtocol === FluoriteMideaProtocolEnum.HLS
  122 + ? 'hls'
  123 + : 'auto';
121 return { url: result.data.url, type }; 124 return { url: result.data.url, type };
122 } 125 }
123 } 126 }
@@ -4,6 +4,30 @@ import type { GlobEnvConfig } from '/#/external/config'; @@ -4,6 +4,30 @@ import type { GlobEnvConfig } from '/#/external/config';
4 import { getGlobalConfigName } from '../../../../build/external/vite/plugins/globConfig/getGlobConfigName'; 4 import { getGlobalConfigName } from '../../../../build/external/vite/plugins/globConfig/getGlobConfigName';
5 import { ProjectRuntimeEnvEnum } from '@/enums/external/envEnum'; 5 import { ProjectRuntimeEnvEnum } from '@/enums/external/envEnum';
6 6
  7 +const stringToJSONParse = (string: string) => {
  8 + try {
  9 + return JSON.parse(string);
  10 + } catch (error) {
  11 + return string;
  12 + }
  13 +};
  14 +
  15 +export function parseEnv(env: Record<string, string>) {
  16 + const res: Record<string, string> = {};
  17 +
  18 + Object.keys(env).forEach((key) => {
  19 + try {
  20 + const value = env[key];
  21 + res[key] = stringToJSONParse(value);
  22 + } catch (err) {
  23 + // eslint-disable-next-line no-console
  24 + console.log(`env variable ${key} can't serialization!`);
  25 + }
  26 + });
  27 +
  28 + return res;
  29 +}
  30 +
7 31
8 export function getCommonStoragePrefix() { 32 export function getCommonStoragePrefix() {
9 // const { VITE_GLOB_APP_SHORT_NAME } = getAppEnvConfig(); 33 // const { VITE_GLOB_APP_SHORT_NAME } = getAppEnvConfig();
@@ -23,7 +47,7 @@ export function getAppEnvConfig() { @@ -23,7 +47,7 @@ export function getAppEnvConfig() {
23 47
24 const ENV = (import.meta.env.DEV 48 const ENV = (import.meta.env.DEV
25 ? // Get the global configuration (the configuration will be extracted independently when packaging) 49 ? // Get the global configuration (the configuration will be extracted independently when packaging)
26 - (import.meta.env as unknown as GlobEnvConfig) 50 + (parseEnv(import.meta.env) as unknown as GlobEnvConfig)
27 : window[ENV_NAME as any]) as unknown as GlobEnvConfig; 51 : window[ENV_NAME as any]) as unknown as GlobEnvConfig;
28 52
29 const { 53 const {
@@ -35,7 +59,8 @@ export function getAppEnvConfig() { @@ -35,7 +59,8 @@ export function getAppEnvConfig() {
35 VITE_GLOB_WEB_SOCKET, 59 VITE_GLOB_WEB_SOCKET,
36 VITE_GLOB_ALARM_NOTIFY_DURATION, 60 VITE_GLOB_ALARM_NOTIFY_DURATION,
37 VITE_GLOB_CONTENT_SECURITY_POLICY, 61 VITE_GLOB_CONTENT_SECURITY_POLICY,
38 - VITE_GLOB_PUBLIC_PATH 62 + VITE_GLOB_PUBLIC_PATH,
  63 + VITE_GLOB_STREAM_MEDIA_CONTENT_SECURITY_PROTOCOL
39 } = ENV; 64 } = ENV;
40 65
41 if (!/^[a-zA-Z\_]*$/.test(VITE_GLOB_APP_SHORT_NAME)) { 66 if (!/^[a-zA-Z\_]*$/.test(VITE_GLOB_APP_SHORT_NAME)) {
@@ -53,7 +78,8 @@ export function getAppEnvConfig() { @@ -53,7 +78,8 @@ export function getAppEnvConfig() {
53 VITE_GLOB_WEB_SOCKET, 78 VITE_GLOB_WEB_SOCKET,
54 VITE_GLOB_ALARM_NOTIFY_DURATION, 79 VITE_GLOB_ALARM_NOTIFY_DURATION,
55 VITE_GLOB_CONTENT_SECURITY_POLICY, 80 VITE_GLOB_CONTENT_SECURITY_POLICY,
56 - VITE_GLOB_PUBLIC_PATH 81 + VITE_GLOB_PUBLIC_PATH,
  82 + VITE_GLOB_STREAM_MEDIA_CONTENT_SECURITY_PROTOCOL
57 }; 83 };
58 } 84 }
59 85
@@ -152,6 +152,8 @@ export interface GlobConfig { @@ -152,6 +152,8 @@ export interface GlobConfig {
152 securityPolicy: string; 152 securityPolicy: string;
153 // assets file prefix 153 // assets file prefix
154 publicPath: string 154 publicPath: string
  155 + // Streaming media content security protocol
  156 + streamMediaContentSecurityProtocol: boolean
155 } 157 }
156 export interface GlobEnvConfig { 158 export interface GlobEnvConfig {
157 // Site title 159 // Site title
@@ -172,4 +174,6 @@ export interface GlobEnvConfig { @@ -172,4 +174,6 @@ export interface GlobEnvConfig {
172 VITE_GLOB_CONTENT_SECURITY_POLICY: string 174 VITE_GLOB_CONTENT_SECURITY_POLICY: string
173 // public assets file prefix 175 // public assets file prefix
174 VITE_GLOB_PUBLIC_PATH: string 176 VITE_GLOB_PUBLIC_PATH: string
  177 + // Streaming media content security protocol
  178 + VITE_GLOB_STREAM_MEDIA_CONTENT_SECURITY_PROTOCOL: boolean
175 } 179 }
@@ -11,6 +11,10 @@ interface ImportMetaEnv { @@ -11,6 +11,10 @@ interface ImportMetaEnv {
11 VITE_GLOB_CONTENT_SECURITY_POLICY: boolean 11 VITE_GLOB_CONTENT_SECURITY_POLICY: boolean
12 12
13 VITE_GLOB_APP_SHORT_NAME: string 13 VITE_GLOB_APP_SHORT_NAME: string
  14 +
  15 + // 流媒体播放协议
  16 + VITE_GLOB_STREAM_MEDIA_CONTENT_SECURITY_PROTOCOL: boolean
  17 +
14 } 18 }
15 19
16 20