Merge branch 'main' of github.com:eolinker/eoapi

This commit is contained in:
夜鹰 2022-06-17 17:38:12 +08:00
commit 5cd80ab96a
9 changed files with 58 additions and 68 deletions

View File

@ -42,6 +42,8 @@ function createWindow(): BrowserWindow {
win = new BrowserWindow({ win = new BrowserWindow({
width: Math.round(size.width * 0.85), width: Math.round(size.width * 0.85),
height: Math.round(size.height * 0.85), height: Math.round(size.height * 0.85),
minWidth: 1280,
minHeight: 720,
useContentSize: true, // 这个要设置,不然计算显示区域尺寸不准 useContentSize: true, // 这个要设置,不然计算显示区域尺寸不准
frame: os.type() === 'Darwin' ? true : false, //mac use default frame frame: os.type() === 'Darwin' ? true : false, //mac use default frame
webPreferences: { webPreferences: {

View File

@ -160,30 +160,11 @@ export class ApiGroupTreeComponent implements OnInit, OnDestroy {
...(getExpandGroupByKey(this.apiGroup, this.route.snapshot.queryParams.uuid) || []), ...(getExpandGroupByKey(this.apiGroup, this.route.snapshot.queryParams.uuid) || []),
]; ];
} }
// 重新构建整个group async createGroup({ name, projectID, content }) {
async rebuildGroupTree(result) { const groupID = await this.storageInstance.group.add({ name, projectID });
this.storageInstance.apiData.clear(); const result = content.apiData.map((it, index) => ({ ...it, groupID, uuid: Date.now() + index }));
this.storageInstance.group.clear();
await this.storageInstance.apiData.bulkAdd(result); await this.storageInstance.apiData.bulkAdd(result);
const apiItems = {}; this.buildGroupTreeData();
this.treeItems = [];
result.forEach((item: ApiData) => {
delete item.updatedAt;
apiItems[item.uuid] = item;
this.treeItems.push({
title: item.name,
key: item.uuid.toString(),
weight: item.weight || 0,
parentID: item.groupID ? `group-${item.groupID}` : '0',
method: item.method,
isLeaf: true,
});
});
this.apiDataItems = apiItems;
this.messageService.send({ type: 'loadApi', data: this.apiDataItems });
this.setSelectedKeys();
this.generateGroupTreeData();
this.restoreExpandStatus();
} }
/** /**
@ -205,8 +186,7 @@ export class ApiGroupTreeComponent implements OnInit, OnDestroy {
break; break;
} }
case 'importSuccess': { case 'importSuccess': {
const { apiData } = JSON.parse(inArg.data); this.createGroup({ projectID: 1, ...inArg.data });
this.rebuildGroupTree(apiData);
} }
} }
}); });
@ -349,7 +329,7 @@ export class ApiGroupTreeComponent implements OnInit, OnDestroy {
'groupBulkUpdate', 'groupBulkUpdate',
[ [
data.group.map((val) => { data.group.map((val) => {
return { ...val, uuid: val.uuid.replace('group-',''), parentID: val.parentID.replace('group-','') }; return { ...val, uuid: val.uuid.replace('group-', ''), parentID: val.parentID.replace('group-', '') };
}), }),
], ],
(result: StorageRes) => {} (result: StorageRes) => {}

View File

@ -35,11 +35,12 @@
</div> </div>
<div class="mt-4" *ngIf="allowDrag"> <div class="mt-4" *ngIf="allowDrag">
<nz-upload *ngIf="extensionList.length" nzType="drag" [nzBeforeUpload]="parserFile" [nzShowUploadList]="false"> <nz-upload *ngIf="extensionList.length" nzType="drag" [nzBeforeUpload]="parserFile">
<p class="ant-upload-drag-icon"> <p class="ant-upload-drag-icon">
<i nz-icon nzType="inbox"></i> <i nz-icon nzType="inbox"></i>
</p> </p>
<p class="ant-upload-text">点击或直接拖拽文件至此区域</p> <p class="ant-upload-text">点击或直接拖拽文件至此区域</p>
<p class="ant-upload-hint">仅支持JSON格式的单个文件导入</p> <p class="ant-upload-hint">仅支持JSON格式的单个文件导入</p>
</nz-upload> </nz-upload>
<div class="text h-4 my-2">{{ filename }}</div>
</div> </div>

View File

@ -20,6 +20,7 @@ export class ExtensionSelectComponent {
@Output() extensionChange = new EventEmitter<string>(); @Output() extensionChange = new EventEmitter<string>();
@Output() currentOptionChange = new EventEmitter<string>(); @Output() currentOptionChange = new EventEmitter<string>();
@Output() uploadChange = new EventEmitter<any>(); @Output() uploadChange = new EventEmitter<any>();
filename = '';
selectExtension({ key, properties }) { selectExtension({ key, properties }) {
this.extensionChange.emit(key); this.extensionChange.emit(key);
@ -36,7 +37,8 @@ export class ExtensionSelectComponent {
parserFile = (file) => parserFile = (file) =>
new Observable((observer: Observer<boolean>) => { new Observable((observer: Observer<boolean>) => {
parserJsonFile(file).then((result) => { parserJsonFile(file).then((result: { name: string }) => {
this.filename = result.name;
this.uploadChange.emit(result); this.uploadChange.emit(result);
observer.complete(); observer.complete();
}); });

View File

@ -1,17 +1,42 @@
import { Component, OnInit } from '@angular/core'; import { Component, OnInit } from '@angular/core';
import { StorageRes, StorageResStatus } from 'eo/workbench/browser/src/app/shared/services/storage/index.model';
import { EoMessageService } from 'eo/workbench/browser/src/app/eoui/message/eo-message.service';
import { StorageService } from '../../../shared/services/storage';
import { FeatureType } from '../../types'; import { FeatureType } from '../../types';
import { parserProperties, getDefaultValue } from '../../../utils';
import { MessageService } from 'eo/workbench/browser/src/app/shared/services/message/message.service'; import { MessageService } from 'eo/workbench/browser/src/app/shared/services/message/message.service';
// const optionList = [
// {
// value: 'import',
// type: 'string',
// default: '',
// label: '直接导入',
// description: '直接导入',
// },
// {
// value: 'add',
// type: 'string',
// default: true,
// label: '增量更新[推荐]',
// description: '增量更新',
// },
// {
// value: 'all',
// type: 'string',
// default: '',
// label: '全量更新[慎用]',
// description: '全量更新',
// },
// {
// value: 'new',
// type: 'string',
// default: '',
// label: '仅添加新 API',
// description: '仅添加新 API',
// },
// ];
@Component({ @Component({
selector: 'eo-import-api', selector: 'eo-import-api',
template: `<extension-select template: `<extension-select
[allowDrag]="true" [allowDrag]="true"
[optionList]="optionList"
[(currentOption)]="currentOption"
[(extension)]="currentExtension" [(extension)]="currentExtension"
[extensionList]="supportList" [extensionList]="supportList"
(uploadChange)="uploadChange($event)" (uploadChange)="uploadChange($event)"
@ -20,15 +45,9 @@ import { MessageService } from 'eo/workbench/browser/src/app/shared/services/mes
export class ImportApiComponent implements OnInit { export class ImportApiComponent implements OnInit {
supportList: Array<FeatureType> = []; supportList: Array<FeatureType> = [];
currentExtension = ''; currentExtension = '';
currentOption = '';
optionList = [];
uploadData = null; uploadData = null;
featureMap = window.eo.getFeature('apimanage.import'); featureMap = window.eo.getFeature('apimanage.import');
constructor( constructor(private messageService: MessageService) {}
private storage: StorageService,
private message: EoMessageService,
private messageService: MessageService
) {}
ngOnInit(): void { ngOnInit(): void {
this.featureMap?.forEach((data: FeatureType, key: string) => { this.featureMap?.forEach((data: FeatureType, key: string) => {
this.supportList.push({ this.supportList.push({
@ -37,39 +56,25 @@ export class ImportApiComponent implements OnInit {
}); });
}); });
{ {
const { key, properties } = this.supportList.at(0); const { key } = this.supportList.at(0);
this.currentExtension = key || ''; this.currentExtension = key || '';
this.optionList = parserProperties(properties || '');
this.currentOption = getDefaultValue(this.optionList, 'value');
} }
} }
getEoapiData() {
return new Promise((resolve) => {
this.storage.run('projectExport', [], (result: StorageRes) => {
const isOk = result.status === StorageResStatus.success;
resolve(isOk ? [result.data, null] : [null, true]);
});
});
}
uploadChange(data) { uploadChange(data) {
this.uploadData = data; this.uploadData = data;
} }
async submit(callback) { async submit(callback) {
const [eoapiData, err]: any = await this.getEoapiData();
if (err) {
this.message.error('获取本地数据失败');
return;
}
// * this.currentExtension is extension's key, like 'eoapi-import-openapi' // * this.currentExtension is extension's key, like 'eoapi-import-openapi'
const feature = this.featureMap.get(this.currentExtension); const feature = this.featureMap.get(this.currentExtension);
const action = feature.action || null; const action = feature.action || null;
const module = window.eo.loadFeatureModule(this.currentExtension); const module = window.eo.loadFeatureModule(this.currentExtension);
const data = module[action](eoapiData, this.uploadData, this.currentOption); const { name, content } = this.uploadData;
const data = module[action](content);
// console.log(JSON.stringify(data, null, 2));
this.messageService.send({ this.messageService.send({
type: 'importSuccess', type: 'importSuccess',
data: JSON.stringify(data), data: { name, content: data },
}); });
// console.log(JSON.stringify(data));
callback(true); callback(true);
} }
} }

View File

@ -322,11 +322,11 @@ export class IndexedDBStorage extends Dexie implements StorageInterface {
} }
apiDataLoadAllByGroupID(groupID: number | string): Observable<object> { apiDataLoadAllByGroupID(groupID: number | string): Observable<object> {
return this.loadAllByConditions(this.apiData, { groupID: groupID }); return this.loadAllByConditions(this.apiData, { groupID });
} }
apiDataLoadAllByProjectID(projectID: number | string): Observable<object> { apiDataLoadAllByProjectID(projectID: number | string): Observable<object> {
return this.loadAllByConditions(this.apiData, { projectID: projectID }); return this.loadAllByConditions(this.apiData, { projectID });
} }
/** /**
@ -335,7 +335,7 @@ export class IndexedDBStorage extends Dexie implements StorageInterface {
* @param groupID * @param groupID
*/ */
apiDataLoadAllByProjectIDAndGroupID(projectID: number | string, groupID: number | string): Observable<object> { apiDataLoadAllByProjectIDAndGroupID(projectID: number | string, groupID: number | string): Observable<object> {
return this.loadAllByConditions(this.apiData, { projectID: projectID, groupID: groupID }); return this.loadAllByConditions(this.apiData, { projectID, groupID });
} }
/** /**

View File

@ -54,6 +54,7 @@ export class HttpStorage implements StorageInterface {
projectLoad: (uuid: number | string) => Observable<object>; projectLoad: (uuid: number | string) => Observable<object>;
projectBulkLoad: (uuids: Array<number | string>) => Observable<object>; projectBulkLoad: (uuids: Array<number | string>) => Observable<object>;
projectExport() { projectExport() {
console.log('lo', new Error());
return this.http.get(`/project/export`) as Observable<object>; return this.http.get(`/project/export`) as Observable<object>;
} }
// Environment // Environment

View File

@ -26,7 +26,8 @@ export class StorageService {
* *
* @param args * @param args
*/ */
run = (action: string, params: Array<any>, callback) => { run(action: string, params: Array<any>, callback): void {
console.log('koko');
const handleResult = { const handleResult = {
status: StorageResStatus.invalid, status: StorageResStatus.invalid,
data: undefined, data: undefined,
@ -47,7 +48,7 @@ export class StorageService {
callback(handleResult); callback(handleResult);
} }
); );
}; }
setStorage = (type: DataSourceType = 'local', options = {}) => { setStorage = (type: DataSourceType = 'local', options = {}) => {
switch (type) { switch (type) {
case 'local': { case 'local': {

View File

@ -82,7 +82,7 @@ export const parserJsonFile = (file, type = 'UTF-8') =>
reader.onload = (ev) => { reader.onload = (ev) => {
const fileString: string = ev.target.result as string; const fileString: string = ev.target.result as string;
const json = JSON.parse(fileString); const json = JSON.parse(fileString);
resolve(json); resolve({ name: file.name, content: json });
}; };
}); });
@ -93,5 +93,3 @@ export const getDefaultValue = (list: any[], key) => {
const [target] = list.filter((it) => it.default); const [target] = list.filter((it) => it.default);
return target[key] || ''; return target[key] || '';
}; };
export const parserProperties = (properties) => Object.keys(properties).map((it) => ({ value: it, ...properties[it] }));