import { RhHttpRequestSettingDto, RhTestEnvironmentSettingDto } from 'rh-base/model'; import { RhApiUrlsService, RhStorageService, IndexDBHelper } from 'rh-base/core'; import { RhSafeAny } from 'rh-base/model'; import { Component, OnInit, OnDestroy } from '@angular/core'; import { debounceTime, takeUntil } from 'rxjs/operators'; import { Subscription, Subject, noop } from 'rxjs'; import Ajv from 'ajv'; import { ENVIRONMENT_DATAS, DataResultSchema } from '../config-manage-data'; import { KeyValue } from '@angular/common'; import { ActivatedRoute } from '@angular/router'; import { INDEXDB_DB_NAME, INDEXDB_DB_SHEET_NAME, INDEXDB_PRIMARY_KEY, INDEXDB_VERSION } from '../../syc-data'; @Component({ selector: 'rh-interface-debug', templateUrl: './interface-debug.component.html', styleUrls: ['./interface-debug.component.less'] }) export class RhInterfaceDebugComponent implements OnInit, OnDestroy { // /** 服务器 */ // @Input() rhServer: string; // /** 端口号 */ // @Input() rhPort: number; // /** 控制器名称 */ // @Input() rhControllName: string; // /** http请求类型 */ // @Input() rhHttpType: 'get' | 'post'; // /** 接口名称 */ // @Input() rhInterfaceName: string; // /** 输入参数 */ // @Input() rhInParam: CodeClassModel; // /** get请求的输入参数 */ // @Input() rhGetParam: KeyValue[]; // /** 输出参数 */ // @Input() rhOutParam: CodeClassModel; /** get请求的输入参数 */ rhGetParam!: KeyValue[]; requestDto!: RhHttpRequestSettingDto | RhSafeAny; id!: string; selectedInputIndex = 0; dataloading = false; isShowSettingModal = false; isShowEditModal = false; settingDataset: RhTestEnvironmentSettingDto[] = []; selectedEnvSetting!: RhTestEnvironmentSettingDto; // /** 输入参数 */ // inputValue = null; destroy$ = new Subject(); reponseDto: RhSafeAny; httpRequestSubscription!: Subscription; validSuccessMessage = ''; validWarningMessage = ''; validErrorMessage = ''; isShowSaveUsecaseModal = false; timer: RhSafeAny; // baseUrlSnippet: string; constructor(private apiUrl: RhApiUrlsService, private storageSer: RhStorageService, private route: ActivatedRoute) {} async ngOnInit() { // if (this.rhPort === 20028) { // this.baseUrlSnippet = MesBaseUrlSnippet; // } else { // this.baseUrlSnippet = BaseUrlSnippet; // } const tempDatas: RhSafeAny[] = this.storageSer.get('SERVER_ENV_DATAS') || ENVIRONMENT_DATAS; tempDatas.forEach((ele) => (ele.editable = false)); this.settingDataset = tempDatas; tempDatas.forEach((ele) => (ele.editable = false)); this.settingDataset = tempDatas; this.requestDto = RhHttpRequestSettingDto.create(); this.id = await this.getId(); if (this.id) { // 赋值请求对象 IndexDBHelper.open(INDEXDB_DB_NAME, INDEXDB_VERSION, INDEXDB_DB_SHEET_NAME, INDEXDB_PRIMARY_KEY, (db) => { IndexDBHelper.read(db, INDEXDB_DB_SHEET_NAME, this.id, (data) => { if (data) { this.requestDto = data; this.selectedInputIndex = this.requestDto.type === 'post' ? 1 : 0; } }); }); } this.selectedEnvSetting = this.settingDataset.find((ele) => ele.server === this.requestDto.server) || this.settingDataset[0]; // this.inputValue = this.rhHttpType === 'post' ? await this.initInputValue() : null; // this.rhGetParam = this.rhGetParam || []; // } ngOnDestroy() { this.destroy$.next(); this.destroy$.complete(); } inputValueChange(value: string) { if (this.timer) { clearTimeout(this.timer); } this.timer = setTimeout(() => { this.requestDto.input = JSON.parse(value) ? JSON.parse(value) : noop(); }, 1000); } selectSettingChange(item: RhTestEnvironmentSettingDto) { // 更新服务器地址 this.updateRequestDto(item); } closeSettingModal() { this.isShowSettingModal = false; } openSettingModal() { this.isShowSettingModal = true; } closeEditModal() { this.isShowEditModal = false; } openEditModal() { this.isShowEditModal = true; } cancelHttpRequest() { if (this.httpRequestSubscription) { this.httpRequestSubscription.unsubscribe(); } this.dataloading = false; } openSaveUsecaseModal() { this.isShowSaveUsecaseModal = true; } closeSaveUsecaseModal() { this.isShowSaveUsecaseModal = false; } submitHttpRequest() { this.dataloading = true; this.validWarningMessage = this.validErrorMessage = this.validSuccessMessage = ''; if (this.httpRequestSubscription) { this.httpRequestSubscription.unsubscribe(); } if (this.requestDto.type === 'get') { // const options: HttpOpitons = {}; // if (this.rhGetParam?.length) { // const temp = {}; // this.rhGetParam.forEach((ele) => { // temp[ele.key] = ele.value; // }); // options.params = temp; // } else { // options = undefined; // } this.httpRequestSubscription = this.apiUrl .get(`${this.requestDto.prefix}${this.requestDto.url}`) .pipe(debounceTime(500), takeUntil(this.destroy$)) .subscribe( (result) => { this.reponseDto = result; this.dataloading = false; this.validateReponse(this.reponseDto); }, () => { this.reponseDto = null; this.dataloading = false; this.validErrorMessage = '请求接口失败'; } ); } else { this.httpRequestSubscription = this.apiUrl.post(`${this.requestDto.prefix}${this.requestDto.url}`, this.requestDto.input).subscribe( (result) => { this.reponseDto = result; this.dataloading = false; this.validateReponse(this.reponseDto); }, () => { this.reponseDto = null; this.dataloading = false; this.validErrorMessage = '请求接口失败'; } ); } } private updateRequestDto(item: RhTestEnvironmentSettingDto) { this.requestDto.server = item.server; if (item?.server) { this.requestDto.prefix = `http://${this.requestDto.server}:${this.requestDto.port}${this.requestDto.baseUrl}`; } } private validateReponse(data: RhSafeAny) { const ajv = new Ajv(); const schema = DataResultSchema; const validate = ajv.compile(schema); const valid = validate(data); if (valid) { this.validSuccessMessage = `Response 数据结构校验通过!`; this.validWarningMessage = this.validErrorMessage = ''; } else { this.validSuccessMessage = this.validErrorMessage = ''; this.validWarningMessage = `Response 数据结构校验失败!${validate.errors?.length ? validate.errors[0].message : ''}`; } } private getId(): Promise { return new Promise((resolve, reject) => { this.route.queryParamMap.subscribe( (para) => { const id = para.get('id') as string; resolve(id); }, () => { reject(null); } ); }); } }