notificationHelper.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. var abp = abp || {};
  2. abp.userNotificationHelper = abp.userNotificationHelper || {};
  3. abp.userNotificationHelper.getUrl = function (userNotification) {
  4. switch (userNotification.notification.notificationName) {
  5. case 'KeyPointAlarm':
  6. case 'KeyPointExpire':
  7. var contractNo = userNotification.notification.data.contractNo;
  8. return "/LegalCase/ContractDetail/" + contractNo;
  9. default:
  10. return '#';
  11. }
  12. //switch (userNotification.notification.notificationName) {
  13. // case 'App.NewUserRegistered':
  14. // return '/AppAreaName/users?filterText=' + userNotification.notification.data.properties.emailAddress;
  15. // case 'App.NewTenantRegistered':
  16. // return '/AppAreaName/tenants?filterText=' + userNotification.notification.data.properties.tenancyName;
  17. // case 'App.DownloadInvalidImportUsers':
  18. // return '/File/DownloadTempFile?fileToken=' + userNotification.notification.data.properties.fileToken +
  19. // '&fileType=' + userNotification.notification.data.properties.fileType +
  20. // '&fileName=' + userNotification.notification.data.properties.fileName;
  21. // //Add your custom notification names to navigate to a URL when user clicks to a notification.
  22. //}
  23. };
  24. abp.userNotificationHelper.getUiIconBySeverity = function (severity) {
  25. switch (severity) {
  26. case abp.notifications.severity.SUCCESS:
  27. return 'fa fa-check bg-success';
  28. case abp.notifications.severity.WARN:
  29. return 'fa fa-exclamation bg-warning';
  30. case abp.notifications.severity.ERROR:
  31. return 'fa fa-bolt bg-danger';
  32. case abp.notifications.severity.FATAL:
  33. return 'fa fa-bomb bg-danger';
  34. case abp.notifications.severity.INFO:
  35. default:
  36. return 'fa fa-info bg-primary';
  37. }
  38. };
  39. abp.userNotificationHelper.format = function (userNotification, truncateText) {
  40. moment.locale(lang);
  41. var formatted = {
  42. userNotificationId: userNotification.id,
  43. text: abp.notifications.getFormattedMessageFromUserNotification(userNotification),
  44. time: moment(userNotification.notification.creationTime).format("YYYY-MM-DD HH:mm:ss"),
  45. icon: abp.userNotificationHelper.getUiIconBySeverity(userNotification.notification.severity),
  46. state: abp.notifications.getUserNotificationStateAsString(userNotification.state),
  47. data: userNotification.notification.data,
  48. url: abp.userNotificationHelper.getUrl(userNotification),
  49. isUnread: userNotification.state === abp.notifications.userNotificationState.UNREAD,
  50. timeAgo: moment(userNotification.notification.creationTime).fromNow()
  51. };
  52. if (truncateText || truncateText === undefined) {
  53. formatted.text = abp.utils.truncateStringWithPostfix(formatted.text, 100);
  54. }
  55. return formatted;
  56. };
  57. abp.userNotificationHelper.show = function (userNotification) {
  58. //Application notification
  59. abp.notifications.showUiNotifyForUserNotification(userNotification, {
  60. 'onclick': function () {
  61. //Take action when user clicks to live toastr notification
  62. var url = abp.userNotificationHelper.getUrl(userNotification);
  63. if (url) {
  64. location.href = url;
  65. }
  66. }
  67. });
  68. var text = abp.notifications.getFormattedMessageFromUserNotification(userNotification);
  69. //Desktop notification
  70. Push.create("系统通知", {
  71. body: text,
  72. icon: abp.appPath + 'Content/Image/logo.png',
  73. timeout: 6000,
  74. onClick: function () {
  75. window.focus();
  76. this.close();
  77. }
  78. });
  79. };
  80. abp.userNotificationHelper.setAllAsRead = function (callback) {
  81. $.iwbAjax4({
  82. url: abp.appUrl + 'Notification/SetAllNotificationsAsRead',
  83. success: function () {
  84. abp.event.trigger('app.notifications.refresh');
  85. callback && callback();
  86. }
  87. });
  88. };
  89. abp.userNotificationHelper.setAsRead = function (userNotificationId, callback) {
  90. $.iwbAjax4({
  91. url: abp.appUrl + 'Notification/SetNotificationAsRead',
  92. data: { id: userNotificationId },
  93. success: function () {
  94. abp.event.trigger('app.notifications.read', userNotificationId);
  95. callback && callback(userNotificationId);
  96. }
  97. });
  98. };
  99. abp.notifications.messageFormatters["ContractService.CommonManager.Notifications.KeyPointNotificationData"] = function (userNotification) {
  100. var data = userNotification.notification.data;
  101. return '<strong class="ml-1 mr-1">[{0}]</strong>合同的<strong class="ml-1 mr-1">[{1}]</strong>关键点需要在<strong class="ml-1 mr-1">[{2}]</strong>处理 <span class="ml-2 {3}" data-url="{5}" data-id="{6}" onclick="abp.userNotificationHelper.setAsRead(\'{6}\', function () {document.location.href = \'{5}\';})">立即处理{4}</span>'
  102. .format(data.contractName,
  103. data.keyPointName,
  104. new Date(data.expireDate).format("yyyy-MM-dd"),
  105. data.isAlarm ? "text-iwb" : "text-danger",
  106. data.isAlarm ? '' : '<strong class="text-danger">(已过期)</strong>',
  107. abp.userNotificationHelper.getUrl(userNotification),
  108. userNotification.id
  109. );
  110. }
  111. $(function () {
  112. //Notification handler
  113. function bindNotificationEvents() {
  114. $('#SetAllNotificationsAsRead').click(function (e) {
  115. e.preventDefault();
  116. e.stopPropagation();
  117. abp.userNotificationHelper.setAllAsRead(function () {
  118. loadNotifications();
  119. });
  120. });
  121. $('.set-read').click(function (e) {
  122. e.preventDefault();
  123. e.stopPropagation();
  124. var notificationId = $(this).closest('.notification-item').data("notification-id");
  125. if (notificationId) {
  126. abp.userNotificationHelper.setAsRead(notificationId, function () {
  127. loadNotifications();
  128. });
  129. }
  130. });
  131. $('div.notification-url').click(function () {
  132. var url = $(this).data('url');
  133. var notificationId = $(this).closest('.notification-item').data("notification-id");
  134. if (notificationId) {
  135. abp.userNotificationHelper.setAsRead(notificationId, function () {
  136. document.location.href = url;
  137. });
  138. }
  139. });
  140. }
  141. function loadNotifications() {
  142. $.iwbAjax4({
  143. url: abp.appUrl + 'Notification/getUserNotifications',
  144. data: { maxResultCount: 5 },
  145. success: function (result) {
  146. result.notifications = [];
  147. result.unreadMessageExists = result.unreadCount > 0;
  148. $.each(result.items, function (index, item) {
  149. result.notifications.push(abp.userNotificationHelper.format(item,0));
  150. });
  151. var $li = $('#header_notification_bar');
  152. var rendered = NotificationsFormatter(result);
  153. $li.html(rendered);
  154. bindNotificationEvents();
  155. if (result.unreadCount > 0) {
  156. $.blinkTitle.stop();
  157. $.blinkTitle.start();
  158. } else {
  159. $.blinkTitle.stop();
  160. }
  161. }
  162. });
  163. }
  164. function NotificationsFormatter(data) {
  165. var str = '';
  166. str += '<a class="nav-link" data-toggle="dropdown" href="#"><i class="fa fa-bell text-warning"></i>{0}</a>'.format(
  167. data.unreadCount
  168. ? '<span class="bg-danger navbar-badge" style="top: 0px;border-radius: 50%;width: 15px;height: 15px;line-height: 10px;">{0}</span>'.format(data.unreadCount)
  169. : '');
  170. str += '<div class="dropdown-menu notification-menu dropdown-menu-right">';
  171. str += '<span class="dropdown-header">{0} {1}</span>'.format(
  172. data.unreadCount,
  173. abp.localization.iwbZero('NewNotifications'));
  174. if (data.notifications && data.notifications.length) {
  175. data.notifications.forEach(function(v) {
  176. str += NotificationFormatter(v);
  177. });
  178. str += '<div class="dropdown-divider"></div>';
  179. str += '<div class="row m-2">';
  180. if (data.unreadCount) {
  181. str += '<div class="text-center col-6"><a href = "" id = "SetAllNotificationsAsRead" >{0}</a ></div >'
  182. .format(
  183. abp.localization.iwbZero('SetAllAsRead'));
  184. }
  185. str += '<div class="text-center col-6"><div class="text-center"><a href="/BaseSystem/Notification">{0}</a ></div></div>'.format(
  186. abp.localization.iwbZero('SeeAllNotifications'));
  187. str += '</div>';
  188. } else {
  189. str += ' <span class="notification-empty-text p-3">{0}</span >'.format(
  190. abp.localization.iwbZero('ThereIsNoNotification'));
  191. }
  192. str += '</div>';
  193. return str;
  194. }
  195. function NotificationFormatter(data) {
  196. var str = '<div class="dropdown-divider" ></div>';
  197. str += '<div class="notification-item {1} {2}" data-url="{0}" data-notification-id="{3}">'.format(data.url,
  198. data.isUnread ? 'user-notification-item-unread' : 'read',
  199. data.url ? 'notification-url' : '');
  200. str += '<div class="item-icon"><i class="{0}" ></i></div><div class="item-text">{1}</div><div class="item-time text-muted ">{2}</div>'
  201. .format(data.icon, data.text, data.timeAgo);
  202. str += '</div>';
  203. return str;
  204. }
  205. abp.event.on('abp.notifications.received', function (userNotification) {
  206. abp.userNotificationHelper.show(userNotification);
  207. loadNotifications();
  208. });
  209. abp.event.on('app.notifications.refresh', function () {
  210. loadNotifications();
  211. });
  212. abp.event.on('app.notifications.read', function () {
  213. loadNotifications();
  214. });
  215. loadNotifications();
  216. });