/* eslint-disable @typescript-eslint/no-unused-vars */ import { FileTypeEnum, AssetTypeEnum, AssetFromTypeEnum, loopHandle, FileInfo, } from '@easytwin/core'; import { setRecoilExternalState } from '@/RecoilExternalStatePortal'; import { _globalLoadingInfo } from '@/pages/module'; import { AssetApi, initStateData } from '.'; export async function initData() { const isInited = localStorage.getItem('isInited'); if (isInited) return; setRecoilExternalState(_globalLoadingInfo, () => ({ loading: true, tip: '初始化数据正在准备...', })); await initAssets(); setRecoilExternalState(_globalLoadingInfo, () => ({ loading: false, tip: null, })); localStorage.setItem('isInited', '1'); } const initAssetsFileInfos: { dirname: string; type: AssetTypeEnum; files: { url: string; preview?: string }[]; }[] = [ { dirname: 'PoiIframe', type: AssetTypeEnum.POIIFRAME, files: [ { url: '/easytwin/system/assets/PoiIframe/iframe.png', }, ], }, { dirname: 'PoiPanner', type: AssetTypeEnum.POIPANNEL, files: [ { url: '/easytwin/system/assets/PoiPanner/信息面板.png', preview: '/easytwin/system/assets/PoiPanner/信息面板_preview.png', }, ], }, { dirname: 'Point', type: AssetTypeEnum.POINT, files: [ { url: '/easytwin/system/assets/Point/基础散点.png', }, { url: '/easytwin/system/assets/Point/枪机摄像头散点.png', }, { url: '/easytwin/system/assets/Point/火灾警告散点.png', }, { url: '/easytwin/system/assets/Point/球机摄像头散点.png', preview: '/easytwin/system/assets/Point/球机摄像头散点.png', }, ], }, { dirname: 'FlyLine', type: AssetTypeEnum.FLYLINE, files: [ { url: '/easytwin/system/assets/FlyLine/飞线.png', }, ], }, { dirname: 'PathLine', type: AssetTypeEnum.PATHLINE, files: [ { url: '/easytwin/system/assets/PathLine/路径.png', }, ], }, { dirname: 'Fence', type: AssetTypeEnum.FENCE, files: [ { url: '/easytwin/system/assets/Fence/围栏.png', }, ], }, ]; export async function initAssets() { await loopHandle(initAssetsFileInfos, (item) => initAsset(item.dirname, item.type, item.files)); } export async function initAsset( dirname: string, type: AssetTypeEnum, files: { url: string; preview?: string }[], ) { const filesLen = files.length; setRecoilExternalState(_globalLoadingInfo, () => ({ loading: true, tip: `初始化 ${dirname} 数字要素资产(0/${filesLen})`, })); await loopHandle<{ url: string; preview?: string }>(files, async (file, index) => { let { url, preview = '' } = file; url = `${import.meta.env.VITE_ASSETS_URL}${url}`; if (preview) { preview = `${import.meta.env.VITE_ASSETS_URL}${preview}`; } const fileName = file.url.split('/').slice(-1)[0]; const name = fileName.split('.')[0]; await AssetApi.create({ name, type, thumb: preview, config: { stateConfig: initStateData[type], }, description: '', // FIXME: 需要获取 categoryId categoryId: '', }); setRecoilExternalState(_globalLoadingInfo, () => ({ loading: true, tip: `初始化 ${dirname} 数字要素资产 ${index}/${filesLen})`, })); }); }