import { Component, OnInit, ViewChild } from '@angular/core'; import { DataResultT, RhSelectableDto, RhBaseDto, CodeClassModel, RhDynamicTableFieldDto, RhTableQueryOptionDto, RhTableButtonInfoDto, RhSafeAny, RhHttpRequestSettingDto } from 'rh-base/model'; import { UntypedFormGroup, UntypedFormBuilder } from '@angular/forms'; import { MsgHelper, guid, RhNavigatorService, IndexDBHelper } from 'rh-base/core'; import { DynamicFormComponent } from 'rh-base/shared'; import { WebApiInvokeLog } from './../../syc-models'; import { INDEXDB_DB_NAME, INDEXDB_DB_SHEET_NAME, INDEXDB_PRIMARY_KEY, INDEXDB_VERSION } from '../../syc-data'; @Component({ selector: 'rh-web-api-invoke-log', templateUrl: './web-api-invoke-log.component.html', styleUrls: ['./web-api-invoke-log.component.less'] }) export class RhWebApiInvokeLogComponent implements OnInit { @ViewChild('queryForm', { static: false }) queryFormCmt!: DynamicFormComponent; selectedIndex = 0; editDto: RhBaseDto = {}; dataLoading = false; dataresult!: DataResultT; selectedItem: WebApiInvokeLog | null = null; isShowEditModal = false; detailsForm!: UntypedFormGroup; defaultFieldDataset: RhDynamicTableFieldDto[] = []; constructor(private fb: UntypedFormBuilder, public navigator: RhNavigatorService) { // } ngOnInit(): void {} editButtonDataset: RhTableButtonInfoDto[] = [ new RhTableButtonInfoDto( 'showDetailsView', '查看详情', 'default', false, 'eye', false, null, (item) => this.openDetailsTab(item as WebApiInvokeLog), true, false, '#1890ff' ) ]; setDatas(dataset: DataResultT) { this.dataresult = dataset; if (!this.dataresult?.Success) { MsgHelper.ShowWarningMessage(`搜索无数据!${this.dataresult?.Message}`); } } dataLoadingChange(status: boolean) { this.dataLoading = status; } setInitFieldDatset(item: CodeClassModel) { const tempDatas: RhDynamicTableFieldDto[] = []; item.CodeProperties.forEach((ele) => { const temp = RhDynamicTableFieldDto.create(); temp.name = ele.PropertyName; temp.displayName = ele.Descirption; tempDatas.push(temp); }); this.defaultFieldDataset = [...tempDatas]; } refreshDatas(option: RhTableQueryOptionDto) { this.getDatas(option); } openDetailsTab(item: WebApiInvokeLog) { this.selectedItem = item; this.createDetailsForm(item); this.selectedIndex = 1; } closeDetailsTab() { this.selectedIndex = 0; } createDetailsForm(dto: WebApiInvokeLog) { this.detailsForm = this.fb.group({}); } /** 转到接口调试界面 */ navigateToInterfaceDebug() { // 写入信息到indexDB,获取guid。然后跳转到接口调试界面 MsgHelper.ShowGlobalLoadingMessage('正在存储接口数据,请稍候'); const requestDto = this.initRequestDto(this.selectedItem as WebApiInvokeLog); const dbName = INDEXDB_DB_NAME, sheetName = INDEXDB_DB_SHEET_NAME, primaryKey = INDEXDB_PRIMARY_KEY, version = INDEXDB_VERSION; IndexDBHelper.open( dbName, version, sheetName, primaryKey, (db) => { IndexDBHelper.add( db, sheetName, requestDto, () => { MsgHelper.CloseGlobalLoadingMessage(); this.closeDetailsTab(); setTimeout(() => { // 导航至接口调试页面 this.navigator.navigatorTrigger('RH_InterfaceDebug', { id: requestDto.InterfaceId }); }, 100); }, () => MsgHelper.ShowWarningMessage(`接口数据存储失败!`) ); }, () => { MsgHelper.ShowErrorMessage(`保存接口数据失败,无法打开调试页面!`); MsgHelper.CloseGlobalLoadingMessage(); } ); } //调用子组件中的接口 private getDatas(option?: RhTableQueryOptionDto) { if (this.queryFormCmt) { this.queryFormCmt.submitDefaultForm(null, option); } } private initRequestDto(data: WebApiInvokeLog): RhHttpRequestSettingDto { const temp = RhHttpRequestSettingDto.create(); temp.InterfaceId = guid(); temp.controllerName = data.ControllerName; temp.interfaceName = data.MethodName; temp.input = JSON.parse(data.CallPara as string); temp.fullUrl = data.ApiUrl; temp.type = data.HttpMethod ? (data.HttpMethod.toLocaleLowerCase() as RhSafeAny) : 'post'; try { if (data.ApiUrl) { const tempDatas = data.ApiUrl.split('/'); temp.prefix = tempDatas.slice(0, -2).join('/'); temp.url = `/${tempDatas.slice(-2).join('/')}`; temp.baseUrl = `/${tempDatas.slice(3, 5).join('/')}`; const serverPort = tempDatas[2]; temp.server = serverPort.split(':')[0]; temp.port = serverPort.split(':')[1]; } } catch (error) { return temp; } return temp; } }