2021-11-28 14:58:20 +08:00
|
|
|
import { ChangeDetectionStrategy, ChangeDetectorRef, Component } from '@angular/core';
|
|
|
|
|
import { NoticeIconList, NoticeIconSelect, NoticeItem } from '@delon/abc/notice-icon';
|
|
|
|
|
import { add, formatDistanceToNow, parse } from 'date-fns';
|
|
|
|
|
import { NzI18nService } from 'ng-zorro-antd/i18n';
|
|
|
|
|
import { NzMessageService } from 'ng-zorro-antd/message';
|
|
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
|
selector: 'header-notify',
|
|
|
|
|
template: `
|
|
|
|
|
<notice-icon
|
|
|
|
|
[data]="data"
|
|
|
|
|
[count]="count"
|
|
|
|
|
[loading]="loading"
|
|
|
|
|
btnClass="alain-default__nav-item"
|
|
|
|
|
btnIconClass="alain-default__nav-item-icon"
|
|
|
|
|
(select)="select($event)"
|
|
|
|
|
(clear)="clear($event)"
|
|
|
|
|
(popoverVisibleChange)="loadData()"
|
|
|
|
|
></notice-icon>
|
|
|
|
|
`,
|
|
|
|
|
changeDetection: ChangeDetectionStrategy.OnPush
|
|
|
|
|
})
|
|
|
|
|
export class HeaderNotifyComponent {
|
|
|
|
|
data: NoticeItem[] = [
|
|
|
|
|
{
|
2021-12-03 16:46:10 +08:00
|
|
|
title: '告警通知',
|
2021-11-28 14:58:20 +08:00
|
|
|
list: [],
|
2021-12-03 16:46:10 +08:00
|
|
|
emptyText: '暂无告警通知',
|
2021-11-28 14:58:20 +08:00
|
|
|
emptyImage: 'https://gw.alipayobjects.com/zos/rmsportal/wAhyIChODzsoKIOBHcBk.svg',
|
2021-12-03 16:46:10 +08:00
|
|
|
clearText: '清空告警'
|
2021-11-28 14:58:20 +08:00
|
|
|
}
|
|
|
|
|
];
|
|
|
|
|
count = 5;
|
|
|
|
|
loading = false;
|
|
|
|
|
|
|
|
|
|
constructor(private msg: NzMessageService, private nzI18n: NzI18nService, private cdr: ChangeDetectorRef) {}
|
|
|
|
|
|
|
|
|
|
private updateNoticeData(notices: NoticeIconList[]): NoticeItem[] {
|
|
|
|
|
const data = this.data.slice();
|
|
|
|
|
data.forEach(i => (i.list = []));
|
|
|
|
|
|
|
|
|
|
notices.forEach(item => {
|
|
|
|
|
const newItem = { ...item } as NoticeIconList;
|
|
|
|
|
if (typeof newItem.datetime === 'string') {
|
|
|
|
|
newItem.datetime = parse(newItem.datetime, 'yyyy-MM-dd', new Date());
|
|
|
|
|
}
|
|
|
|
|
if (newItem.datetime) {
|
|
|
|
|
newItem.datetime = formatDistanceToNow(newItem.datetime as Date, { locale: this.nzI18n.getDateLocale() });
|
|
|
|
|
}
|
|
|
|
|
if (newItem.extra && newItem.status) {
|
|
|
|
|
newItem.color = (
|
|
|
|
|
{
|
|
|
|
|
todo: undefined,
|
|
|
|
|
processing: 'blue',
|
|
|
|
|
urgent: 'red',
|
|
|
|
|
doing: 'gold'
|
|
|
|
|
} as { [key: string]: string | undefined }
|
|
|
|
|
)[newItem.status];
|
|
|
|
|
}
|
|
|
|
|
data.find(w => w.title === newItem.type)!.list.push(newItem);
|
|
|
|
|
});
|
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
loadData(): void {
|
|
|
|
|
if (this.loading) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
this.loading = true;
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
const now = new Date();
|
|
|
|
|
this.data = this.updateNoticeData([
|
|
|
|
|
{
|
|
|
|
|
id: '000000001',
|
|
|
|
|
avatar: 'https://gw.alipayobjects.com/zos/rmsportal/ThXAXghbEsBCCSDihZxY.png',
|
2021-12-03 16:46:10 +08:00
|
|
|
title: '监控-MYSQL_192.135.34.2-发出2级告警',
|
2021-11-28 14:58:20 +08:00
|
|
|
datetime: add(now, { days: 10 }),
|
2021-12-03 16:46:10 +08:00
|
|
|
type: '告警通知'
|
2021-11-28 14:58:20 +08:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
id: '000000002',
|
2021-12-03 16:46:10 +08:00
|
|
|
avatar: 'https://gw.alipayobjects.com/zos/rmsportal/ThXAXghbEsBCCSDihZxY.png',
|
|
|
|
|
title: '监控-MYSQL_192.135.44.2-发出1级告警',
|
2021-11-28 14:58:20 +08:00
|
|
|
datetime: add(now, { days: -3 }),
|
2021-12-03 16:46:10 +08:00
|
|
|
type: '告警通知'
|
2021-11-28 14:58:20 +08:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
id: '000000003',
|
2021-12-03 16:46:10 +08:00
|
|
|
avatar: 'https://gw.alipayobjects.com/zos/rmsportal/ThXAXghbEsBCCSDihZxY.png',
|
|
|
|
|
title: '监控-WEBSITE_www.baidu.com-发出4级告警',
|
2021-11-28 14:58:20 +08:00
|
|
|
datetime: add(now, { months: -3 }),
|
|
|
|
|
read: true,
|
2021-12-03 16:46:10 +08:00
|
|
|
type: '告警通知'
|
2021-11-28 14:58:20 +08:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
id: '000000004',
|
|
|
|
|
avatar: 'https://gw.alipayobjects.com/zos/rmsportal/GvqBnKhFgObvnSGkDsje.png',
|
2021-12-03 16:46:10 +08:00
|
|
|
title: '监控-REDIS_192.34.55.3-发出4级告警',
|
2021-11-28 14:58:20 +08:00
|
|
|
datetime: add(now, { years: -1 }),
|
2021-12-03 16:46:10 +08:00
|
|
|
type: '告警通知'
|
2021-11-28 14:58:20 +08:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
id: '000000005',
|
|
|
|
|
avatar: 'https://gw.alipayobjects.com/zos/rmsportal/ThXAXghbEsBCCSDihZxY.png',
|
2021-12-03 16:46:10 +08:00
|
|
|
title: '监控-REDIS_192.34.55.6-发出4级告警',
|
|
|
|
|
datetime: '2020-08-07',
|
|
|
|
|
type: '告警通知'
|
2021-11-28 14:58:20 +08:00
|
|
|
}
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
this.loading = false;
|
|
|
|
|
this.cdr.detectChanges();
|
|
|
|
|
}, 500);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
clear(type: string): void {
|
2021-12-03 16:46:10 +08:00
|
|
|
this.loading = true;
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
this.data = this.updateNoticeData([]);
|
|
|
|
|
this.loading = false;
|
|
|
|
|
this.cdr.detectChanges();
|
|
|
|
|
}, 500);
|
|
|
|
|
this.msg.success(`已清空 ${type}`);
|
2021-11-28 14:58:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
select(res: NoticeIconSelect): void {
|
|
|
|
|
this.msg.success(`点击了 ${res.title} 的 ${res.item.title}`);
|
|
|
|
|
}
|
|
|
|
|
}
|