From bbfc2266728b8b572fc4553d1c3500c6d81d68fc Mon Sep 17 00:00:00 2001 From: buqiyuan <1743369777@qq.com> Date: Fri, 20 Jan 2023 21:59:09 +0800 Subject: [PATCH] fix: import openAPI --- .../import-api/import-api.component.ts | 11 +++- .../extension-select/import-api/old2new.ts | 65 +++++++++++++++++++ .../storage/db/services/project.service.ts | 1 + .../src/app/shared/store/effect.service.ts | 1 + 4 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 src/workbench/browser/src/app/modules/extension-select/import-api/old2new.ts diff --git a/src/workbench/browser/src/app/modules/extension-select/import-api/import-api.component.ts b/src/workbench/browser/src/app/modules/extension-select/import-api/import-api.component.ts index 7e58f297b..4b569a6e8 100644 --- a/src/workbench/browser/src/app/modules/extension-select/import-api/import-api.component.ts +++ b/src/workbench/browser/src/app/modules/extension-select/import-api/import-api.component.ts @@ -1,6 +1,7 @@ import { Component, OnInit } from '@angular/core'; import { Router } from '@angular/router'; import { EoNgFeedbackMessageService } from 'eo-ng-feedback'; +import { old2new } from 'eo/workbench/browser/src/app/modules/extension-select/import-api/old2new'; import { FeatureInfo } from 'eo/workbench/browser/src/app/shared/models/extension-manager'; import { ExtensionService } from 'eo/workbench/browser/src/app/shared/services/extensions/extension.service'; import { StorageRes, StorageResStatus } from 'eo/workbench/browser/src/app/shared/services/storage/index.model'; @@ -93,7 +94,7 @@ export class ImportApiComponent implements OnInit { const feature = this.featureMap.get(this.currentExtension); const action = feature.action || null; const module = await this.extensionService.getExtensionPackage(this.currentExtension); - const { name, content } = this.uploadData; + let { name, content } = this.uploadData; try { const [data, err] = module[action](content); // console.log('import data', window.structuredClone?.(data)); @@ -124,6 +125,14 @@ export class ImportApiComponent implements OnInit { // return obj; // }; try { + const projectUuid = this.store.getCurrentProjectID; + const workSpaceUuid = this.store.getCurrentWorkspaceUuid; + console.log('content', content); + // TODO 兼容旧数据 + if (Reflect.has(data, 'collections') && Reflect.has(data, 'environments')) { + content = old2new(data, projectUuid, workSpaceUuid); + console.log('new content', content); + } if (this.store.isLocal) { await this.effectService.projectImport('local', content); } else { diff --git a/src/workbench/browser/src/app/modules/extension-select/import-api/old2new.ts b/src/workbench/browser/src/app/modules/extension-select/import-api/old2new.ts new file mode 100644 index 000000000..737230228 --- /dev/null +++ b/src/workbench/browser/src/app/modules/extension-select/import-api/old2new.ts @@ -0,0 +1,65 @@ +import type { ImportProjectDto } from 'eo/workbench/browser/src/app/shared/services/storage/db/dto/project.dto'; + +import { convertApiData } from './../../../shared/services/storage/db/dataSource/convert'; + +export const old2new = (params, projectUuid, workSpaceUuid): ImportProjectDto => { + const { collections = [], environments } = params; + + const environmentList = environments.map(n => ({ + name: n.name, + hostUri: n.hostUri, + parameters: n.parameters, + projectUuid, + workSpaceUuid + })); + + const genId = () => crypto.getRandomValues(new Uint32Array(1))[0]; + + const genGroup = (name, parentId?) => { + return { + name, + parentId, + id: genId(), + type: 1, + projectUuid, + workSpaceUuid, + children: [] + }; + }; + + const rootGroup = genGroup(collections[0].name); + + const apiList = []; + const groupList = [rootGroup]; + + const formatData = (collections = [], parentGroup) => { + collections.forEach(item => { + // API + if (item.uri) { + const newApiData = convertApiData(item); + newApiData.groupId = parentGroup.id; + apiList.push(newApiData); + } + // 分组 + else { + const group = genGroup(item.name, parentGroup.id); + + parentGroup.children.push(group); + + if (item.children?.length) { + formatData(item.children, group); + } + } + }); + }; + + collections[0].id = genId(); + formatData(collections[0]?.children, groupList[0]); + + return { + name: 'Default', + apiList, + groupList, + environmentList + }; +}; diff --git a/src/workbench/browser/src/app/shared/services/storage/db/services/project.service.ts b/src/workbench/browser/src/app/shared/services/storage/db/services/project.service.ts index 33640a7ae..0c6c0c14a 100644 --- a/src/workbench/browser/src/app/shared/services/storage/db/services/project.service.ts +++ b/src/workbench/browser/src/app/shared/services/storage/db/services/project.service.ts @@ -130,6 +130,7 @@ export class ProjectService extends BaseService { .filter(n => n.depth !== 0) .map(n => { const { id, children, ...rest } = n; + rest.parentId ??= rootGroup[0].id; return rest; }); let remoteGroups = rootGroup; diff --git a/src/workbench/browser/src/app/shared/store/effect.service.ts b/src/workbench/browser/src/app/shared/store/effect.service.ts index 2d79f8be4..0120941f0 100644 --- a/src/workbench/browser/src/app/shared/store/effect.service.ts +++ b/src/workbench/browser/src/app/shared/store/effect.service.ts @@ -400,6 +400,7 @@ export class EffectService { .filter(n => n.depth !== 0) .map(n => { const { id, children, ...rest } = n; + rest.parentId ??= rootGroup.id; return rest; }); const [remoteGroups] = groupFilters.length ? await this.remote.api_groupCreate(groupFilters) : [[rootGroup]];