2
0

notify.component.ts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. * Copyright [2022] [MaxKey of copyright http://www.maxkey.top]
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. import { ChangeDetectionStrategy, ChangeDetectorRef, Component } from '@angular/core';
  17. import { NoticeIconList, NoticeIconSelect, NoticeItem } from '@delon/abc/notice-icon';
  18. import { add, formatDistanceToNow, parse } from 'date-fns';
  19. import { NzI18nService } from 'ng-zorro-antd/i18n';
  20. import { NzMessageService } from 'ng-zorro-antd/message';
  21. @Component({
  22. selector: 'header-notify',
  23. template: `
  24. <notice-icon
  25. [data]="data"
  26. [count]="count"
  27. [loading]="loading"
  28. btnClass="alain-default__nav-item"
  29. btnIconClass="alain-default__nav-item-icon"
  30. (select)="select($event)"
  31. (clear)="clear($event)"
  32. (popoverVisibleChange)="loadData()"
  33. ></notice-icon>
  34. `,
  35. changeDetection: ChangeDetectionStrategy.OnPush
  36. })
  37. export class HeaderNotifyComponent {
  38. data: NoticeItem[] = [
  39. {
  40. title: '通知',
  41. list: [],
  42. emptyText: '你已查看所有通知',
  43. emptyImage: 'https://gw.alipayobjects.com/zos/rmsportal/wAhyIChODzsoKIOBHcBk.svg',
  44. clearText: '清空通知'
  45. },
  46. {
  47. title: '消息',
  48. list: [],
  49. emptyText: '您已读完所有消息',
  50. emptyImage: 'https://gw.alipayobjects.com/zos/rmsportal/sAuJeJzSKbUmHfBQRzmZ.svg',
  51. clearText: '清空消息'
  52. },
  53. {
  54. title: '待办',
  55. list: [],
  56. emptyText: '你已完成所有待办',
  57. emptyImage: 'https://gw.alipayobjects.com/zos/rmsportal/HsIsxMZiWKrNUavQUXqx.svg',
  58. clearText: '清空待办'
  59. }
  60. ];
  61. count = 5;
  62. loading = false;
  63. constructor(private msg: NzMessageService, private nzI18n: NzI18nService, private cdr: ChangeDetectorRef) {}
  64. private updateNoticeData(notices: NoticeIconList[]): NoticeItem[] {
  65. const data = this.data.slice();
  66. data.forEach(i => (i.list = []));
  67. notices.forEach(item => {
  68. const newItem = { ...item } as NoticeIconList;
  69. if (typeof newItem.datetime === 'string') {
  70. newItem.datetime = parse(newItem.datetime, 'yyyy-MM-dd', new Date());
  71. }
  72. if (newItem.datetime) {
  73. newItem.datetime = formatDistanceToNow(newItem.datetime as Date, { locale: this.nzI18n.getDateLocale() });
  74. }
  75. if (newItem.extra && newItem['status']) {
  76. newItem['color'] = (
  77. {
  78. todo: undefined,
  79. processing: 'blue',
  80. urgent: 'red',
  81. doing: 'gold'
  82. } as { [key: string]: string | undefined }
  83. )[newItem['status']];
  84. }
  85. data.find(w => w.title === newItem['type'])!.list.push(newItem);
  86. });
  87. return data;
  88. }
  89. loadData(): void {
  90. if (this.loading) {
  91. return;
  92. }
  93. this.loading = true;
  94. setTimeout(() => {
  95. const now = new Date();
  96. this.data = this.updateNoticeData([
  97. {
  98. id: '000000001',
  99. avatar: 'https://gw.alipayobjects.com/zos/rmsportal/ThXAXghbEsBCCSDihZxY.png',
  100. title: '你收到了 14 份新周报',
  101. datetime: add(now, { days: 10 }),
  102. type: '通知'
  103. },
  104. {
  105. id: '000000002',
  106. avatar: 'https://gw.alipayobjects.com/zos/rmsportal/OKJXDXrmkNshAMvwtvhu.png',
  107. title: '你推荐的 曲妮妮 已通过第三轮面试',
  108. datetime: add(now, { days: -3 }),
  109. type: '通知'
  110. },
  111. {
  112. id: '000000003',
  113. avatar: 'https://gw.alipayobjects.com/zos/rmsportal/kISTdvpyTAhtGxpovNWd.png',
  114. title: '这种模板可以区分多种通知类型',
  115. datetime: add(now, { months: -3 }),
  116. read: true,
  117. type: '通知'
  118. },
  119. {
  120. id: '000000004',
  121. avatar: 'https://gw.alipayobjects.com/zos/rmsportal/GvqBnKhFgObvnSGkDsje.png',
  122. title: '左侧图标用于区分不同的类型',
  123. datetime: add(now, { years: -1 }),
  124. type: '通知'
  125. },
  126. {
  127. id: '000000005',
  128. avatar: 'https://gw.alipayobjects.com/zos/rmsportal/ThXAXghbEsBCCSDihZxY.png',
  129. title: '内容不要超过两行字,超出时自动截断',
  130. datetime: '2017-08-07',
  131. type: '通知'
  132. },
  133. {
  134. id: '000000006',
  135. avatar: 'https://gw.alipayobjects.com/zos/rmsportal/fcHMVNCjPOsbUGdEduuv.jpeg',
  136. title: '曲丽丽 评论了你',
  137. description: '描述信息描述信息描述信息',
  138. datetime: '2017-08-07',
  139. type: '消息'
  140. },
  141. {
  142. id: '000000007',
  143. avatar: 'https://gw.alipayobjects.com/zos/rmsportal/fcHMVNCjPOsbUGdEduuv.jpeg',
  144. title: '朱偏右 回复了你',
  145. description: '这种模板用于提醒谁与你发生了互动,左侧放『谁』的头像',
  146. datetime: '2017-08-07',
  147. type: '消息'
  148. },
  149. {
  150. id: '000000008',
  151. avatar: 'https://gw.alipayobjects.com/zos/rmsportal/fcHMVNCjPOsbUGdEduuv.jpeg',
  152. title: '标题',
  153. description: '这种模板用于提醒谁与你发生了互动,左侧放『谁』的头像',
  154. datetime: '2017-08-07',
  155. type: '消息'
  156. },
  157. {
  158. id: '000000009',
  159. title: '任务名称',
  160. description: '任务需要在 2017-01-12 20:00 前启动',
  161. extra: '未开始',
  162. status: 'todo',
  163. type: '待办'
  164. },
  165. {
  166. id: '000000010',
  167. title: '第三方紧急代码变更',
  168. description: '冠霖提交于 2017-01-06,需在 2017-01-07 前完成代码变更任务',
  169. extra: '马上到期',
  170. status: 'urgent',
  171. type: '待办'
  172. },
  173. {
  174. id: '000000011',
  175. title: '信息安全考试',
  176. description: '指派竹尔于 2017-01-09 前完成更新并发布',
  177. extra: '已耗时 8 天',
  178. status: 'doing',
  179. type: '待办'
  180. },
  181. {
  182. id: '000000012',
  183. title: 'ABCD 版本发布',
  184. description: '冠霖提交于 2017-01-06,需在 2017-01-07 前完成代码变更任务',
  185. extra: '进行中',
  186. status: 'processing',
  187. type: '待办'
  188. }
  189. ]);
  190. this.loading = false;
  191. this.cdr.detectChanges();
  192. }, 500);
  193. }
  194. clear(type: string): void {
  195. this.msg.success(`清空了 ${type}`);
  196. }
  197. select(res: NoticeIconSelect): void {
  198. this.msg.success(`点击了 ${res.title} 的 ${res.item.title}`);
  199. }
  200. }