import { RhAppConfigService } from 'rh-base/core'; import { Component, Input, OnInit, OnDestroy, OnChanges, SimpleChanges } from '@angular/core'; import { RhMessageContentDto } from 'rh-itm/model'; import { Subscription } from 'rxjs'; import { RhSafeAny } from 'rh-base/model'; @Component({ selector: 'rh-basic-message', templateUrl: './basic-message.component.html', styleUrls: ['./basic-message.component.less'] }) export class RhBasicMessageComponent implements OnInit, OnChanges, OnDestroy { /** 消息内容,默认设置为显示五秒后隐藏 */ // @Input() rhMessage: RhMessageContentDto | TemplateRef; @Input() rhMessage!: RhMessageContentDto; /** 消息显示类型 */ @Input() rhMode: 'dto' | 'tpl' = 'dto'; /** 显示时间,单位毫秒,超出时间后自动异常,默认为`5000` */ @Input() rhDisplayTime = 5000; /** 是否保持消息显示 */ @Input() rhShow = false; messageVisible = false; messageVisibleTimer: RhSafeAny; /** 消息样式 */ messageStyle: { [propName: string]: string } = { color: 'whitesmoke', 'font-size': '24px', 'text-align': 'center' }; /** sucess消息 */ successStyle: { [propName: string]: string } = { color: '#49aa19', 'font-size': '24px', 'text-align': 'center' }; /** warning消息 */ warningStyle: { [propName: string]: string } = { color: '#d8bd14', 'font-size': '24px', 'text-align': 'center' }; /** error消息 */ errorStyle: { [propName: string]: string } = { color: '#d32029', 'font-size': '24px', 'text-align': 'center' }; msgSubscription!: Subscription; constructor(public appconfigSer: RhAppConfigService) { // } ngOnInit(): void { /** 订阅网络错误的提示 */ this.appconfigSer.connectionError$.subscribe((message) => { if (message?.trim()) { this.rhMessage = { Type: 'error', Value: message }; this.updateTimer(); } }); } ngOnChanges(changes: SimpleChanges) { const { rhMessage } = changes; if (rhMessage.currentValue) { this.updateTimer(); } } updateTimer() { if (!this.rhMessage) { return; } this.messageVisible = true; if (this.rhShow) { return; } this.messageVisibleTimer = setTimeout(() => { this.messageVisible = false; }, this.rhDisplayTime); } // ngOnChanges(changes: SimpleChanges) { // // // } ngOnDestroy() { if (this.msgSubscription) { this.msgSubscription.unsubscribe(); } if (this.messageVisibleTimer) { clearTimeout(this.messageVisibleTimer); } } }