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 12
13 13 # 公共路径
14 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 11
12 12 # 公共路径
13 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 4 import { GLOB_CONFIG_FILE_NAME } from "./const";
5 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 32 export function GenerateBuildConfig(viteEnv: Record<string, any>) {
8 33 return {
9 34 name: 'vite-plugin-generate-global-config',
... ... @@ -15,7 +40,7 @@ export function GenerateBuildConfig(viteEnv: Record<string, any>) {
15 40 try {
16 41 const windowConf = `window.${configName}`;
17 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 44 Object.freeze(${windowConf});
20 45 Object.defineProperty(window, "${configName}", {
21 46 configurable: false,
... ...
... ... @@ -10,7 +10,8 @@ export const useGlobSetting = (): Readonly<GlobConfig> => {
10 10 VITE_GLOB_UPLOAD_URL,
11 11 VITE_GLOB_WEB_SOCKET,
12 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 15 } = getAppEnvConfig();
15 16
16 17 if (!/[a-zA-Z_]*/.test(VITE_GLOB_APP_SHORT_NAME)) {
... ... @@ -28,7 +29,8 @@ export const useGlobSetting = (): Readonly<GlobConfig> => {
28 29 uploadUrl: VITE_GLOB_UPLOAD_URL,
29 30 socketUrl: VITE_GLOB_WEB_SOCKET,
30 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 36 return glob as Readonly<GlobConfig>;
... ...
... ... @@ -6,8 +6,7 @@ let initialFlag = false;
6 6 export function useWebSecurityPolicy() {
7 7 if (window && window.document && window.document.documentElement) {
8 8 const { securityPolicy } = useGlobSetting();
9   - const flag = isBoolean(securityPolicy) ? securityPolicy : securityPolicy === 'true'
10   - if (flag && !initialFlag) {
  9 + if (securityPolicy && !initialFlag) {
11 10 const meta = document.createElement('meta');
12 11 meta.setAttribute('http-equiv', 'Content-Security-Policy');
13 12 meta.setAttribute('content', 'upgrade-insecure-requests');
... ...
... ... @@ -2,11 +2,12 @@ import { PublicConfigClass } from '@/packages/public'
2 2 import { CreateComponentType } from '@/packages/index.d'
3 3 import { SingleCameraConfig } from './index'
4 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 12 export enum sourceTypeEnum {
12 13 CUSTOM = 'custom',
... ... @@ -89,7 +90,7 @@ export const isRtspProtocol = (url: string) => {
89 90 };
90 91
91 92 export async function getPlayUrl(
92   - params: CameraRecord
  93 + params: CameraRecord
93 94 ): Promise<{ url: string; type: StreamType } | undefined> {
94 95 const { accessMode } = params;
95 96 if (accessMode === AccessMode.ManuallyEnter) {
... ... @@ -106,18 +107,20 @@ export async function getPlayUrl(
106 107 }
107 108 }
108 109 } else if (accessMode === AccessMode.GBT28181) {
  110 + const { streamMediaContentSecurityProtocol } = useGlobSetting()
  111 + console.log(useGlobSetting())
109 112 const { deviceId, channelNo } = params?.params || {};
110 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 115 } else {
113 116 const { id, playProtocol } = params;
114 117 const result = await getVideoUrl(id);
115 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 124 return { url: result.data.url, type };
122 125 }
123 126 }
... ...
... ... @@ -4,6 +4,30 @@ import type { GlobEnvConfig } from '/#/external/config';
4 4 import { getGlobalConfigName } from '../../../../build/external/vite/plugins/globConfig/getGlobConfigName';
5 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 32 export function getCommonStoragePrefix() {
9 33 // const { VITE_GLOB_APP_SHORT_NAME } = getAppEnvConfig();
... ... @@ -23,7 +47,7 @@ export function getAppEnvConfig() {
23 47
24 48 const ENV = (import.meta.env.DEV
25 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 51 : window[ENV_NAME as any]) as unknown as GlobEnvConfig;
28 52
29 53 const {
... ... @@ -35,7 +59,8 @@ export function getAppEnvConfig() {
35 59 VITE_GLOB_WEB_SOCKET,
36 60 VITE_GLOB_ALARM_NOTIFY_DURATION,
37 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 64 } = ENV;
40 65
41 66 if (!/^[a-zA-Z\_]*$/.test(VITE_GLOB_APP_SHORT_NAME)) {
... ... @@ -53,7 +78,8 @@ export function getAppEnvConfig() {
53 78 VITE_GLOB_WEB_SOCKET,
54 79 VITE_GLOB_ALARM_NOTIFY_DURATION,
55 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 152 securityPolicy: string;
153 153 // assets file prefix
154 154 publicPath: string
  155 + // Streaming media content security protocol
  156 + streamMediaContentSecurityProtocol: boolean
155 157 }
156 158 export interface GlobEnvConfig {
157 159 // Site title
... ... @@ -172,4 +174,6 @@ export interface GlobEnvConfig {
172 174 VITE_GLOB_CONTENT_SECURITY_POLICY: string
173 175 // public assets file prefix
174 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 11 VITE_GLOB_CONTENT_SECURITY_POLICY: boolean
12 12
13 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
... ...