notificationHelper.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. var abp = abp || {};
  2. abp.userNotificationHelper = abp.userNotificationHelper || {};
  3. abp.userNotificationHelper.getUrl = function (userNotification) {
  4. var data = userNotification.notification.data;
  5. switch (userNotification.notification.notificationName) {
  6. case "CalendarNotification":
  7. return '/Home/Calendar';
  8. case "WorkflowAuditNotification":
  9. return `/Wf/Audit?rId=${data.recordId}`;
  10. default:
  11. return '/Bs/Notification';
  12. }
  13. };
  14. abp.userNotificationHelper.getUiIconBySeverity = function (severity) {
  15. switch (severity) {
  16. case abp.notifications.severity.SUCCESS:
  17. return '<span class="symbol-label bg-light-success"><span class="fas fa-check-circle text-success fs-4"></span></span>';
  18. case abp.notifications.severity.WARN:
  19. return '<span class="symbol-label bg-light-warning"><span class="fas fa-exclamation-circle text-warning fs-4"></span></span>';
  20. case abp.notifications.severity.ERROR:
  21. return '<span class="symbol-label bg-light-danger"><span class="fas fa-exclamation-triangle text-danger fs-4"></span></span>';
  22. case abp.notifications.severity.FATAL:
  23. return '<span class="symbol-label bg-light-danger"><span class="fas fa-bomb text-danger fs-4"></span></span>';
  24. case abp.notifications.severity.INFO:
  25. default:
  26. return '<span class="symbol-label bg-light-primary"><span class="fas fa-info-circle text-primary fs-4"></span></span>';
  27. }
  28. };
  29. abp.userNotificationHelper.format = function (userNotification, truncateText) {
  30. var formatted = {
  31. id: userNotification.id,
  32. userNotificationId: userNotification.id,
  33. name: abp.localization.VberZero("Notification_" + userNotification.notification.notificationName),
  34. text: abp.notifications.getFormattedMessageFromUserNotification(userNotification),
  35. time: moment(userNotification.notification.creationTime).format("YYYY-MM-DD HH:mm:ss"),
  36. icon: abp.userNotificationHelper.getUiIconBySeverity(userNotification.notification.severity),
  37. state: abp.notifications.getUserNotificationStateAsString(userNotification.state),
  38. notificationState: userNotification.state,
  39. data: userNotification.notification.data,
  40. url: abp.userNotificationHelper.getUrl(userNotification),
  41. isUnread: userNotification.state === abp.notifications.userNotificationState.UNREAD,
  42. timeAgo: moment(userNotification.notification.creationTime).fromNow()
  43. };
  44. truncateText = truncateText === undefined ? 100 : truncateText;
  45. if (truncateText) {
  46. formatted.text = abp.utils.truncateStringWithPostfix(formatted.text, truncateText);
  47. }
  48. return formatted;
  49. };
  50. abp.userNotificationHelper.show = function (userNotification) {
  51. //Application notification
  52. abp.notifications.showUiNotifyForUserNotification(userNotification, {
  53. 'onclick': function () {
  54. //Take action when user clicks to live toastr notification
  55. var url = abp.userNotificationHelper.getUrl(userNotification);
  56. if (url) {
  57. location.href = url;
  58. }
  59. }
  60. });
  61. //var text = abp.userNotificationHelper.format(userNotification,30).text;
  62. //Desktop notification
  63. //Push.create("系统通知", {
  64. // body: "你有一个新通知!",
  65. // icon: abp.appPath + 'img/logo.png',
  66. // timeout: 6000,
  67. // onClick: function () {
  68. // window.focus();
  69. // this.close();
  70. // }
  71. //});
  72. };
  73. abp.userNotificationHelper.setAllAsRead = function (callback) {
  74. $.vbAjax1({
  75. url: abp.appUrl + 'Notification/SetAllNotificationsAsRead',
  76. success: function () {
  77. abp.event.trigger('app.notifications.refresh');
  78. callback && callback();
  79. }
  80. });
  81. };
  82. abp.userNotificationHelper.setAsRead = function (userNotificationId, callback) {
  83. $.vbAjax4({
  84. url: abp.appUrl + 'Notification/SetNotificationAsRead',
  85. data: { id: userNotificationId },
  86. success: function () {
  87. abp.event.trigger('app.notifications.read', userNotificationId);
  88. callback && callback(userNotificationId);
  89. }
  90. });
  91. };
  92. abp.notifications.messageFormatters["VberZero.DomainService.Notifications.Data.CalendarNotificationData"] = function (userNotification) {
  93. var data = userNotification.notification.data;
  94. //return '<a href="/Home/Calendar">你有一个新日程:{0}</a>'.format(data.title);
  95. return '您有一个新日程:' + data.title;
  96. }
  97. abp.notifications.messageFormatters["VberZero.DomainService.Notifications.Data.WorkflowAuditNotificationData"] = function (userNotification) {
  98. var data = userNotification.notification.data;
  99. //return '<a href="/Home/Calendar">你有一个新日程:{0}</a>'.format(data.title);
  100. return `【${data.applyUserName}】提交的【${data.title}】需要您审批`;
  101. }
  102. $(function () {
  103. //Notification handler
  104. function bindNotificationEvents() {
  105. $('#SetAllNotificationsAsRead').click(function (e) {
  106. e.preventDefault();
  107. e.stopPropagation();
  108. abp.userNotificationHelper.setAllAsRead(function () {
  109. loadNotifications();
  110. });
  111. });
  112. $('.set-read').click(function (e) {
  113. e.preventDefault();
  114. e.stopPropagation();
  115. var notificationId = $(this).closest('.notification-item').data("notification-id");
  116. if (notificationId) {
  117. abp.userNotificationHelper.setAsRead(notificationId, function () {
  118. loadNotifications();
  119. });
  120. }
  121. });
  122. $('div.notification-url').click(function () {
  123. var url = $(this).data('url');
  124. var notificationId = $(this).closest('.notification-item').data("notification-id");
  125. if (notificationId) {
  126. abp.userNotificationHelper.setAsRead(notificationId, function () {
  127. document.location.href = url;
  128. });
  129. }
  130. });
  131. }
  132. function loadNotifications() {
  133. $.vbAjax4({
  134. url: abp.appUrl + 'Notification/getUserNotifications',
  135. data: { maxResultCount: 5 },
  136. success: function (result) {
  137. let $notification = $('[data-vb-nav-header="notification"]'),
  138. $nav = $notification.find('div').eq(0),
  139. $menu = $notification.find('div').eq(1),
  140. $unreadCount = $menu.find('[data-vb-notification="unread-count"]'),
  141. $items_box = $menu.find('[data-vb-notification="items"]');
  142. $nav.find('.bullet').remove();
  143. result.notifications = [];
  144. result.unreadMessageExists = result.unreadCount > 0;
  145. result.items.forEach((v) => {
  146. result.notifications.push(abp.userNotificationHelper.format(v, 0));
  147. });
  148. $items_box.html(notificationsFormatter(result.notifications));
  149. bindNotificationEvents();
  150. if (result.unreadCount > 0) {
  151. $nav.append(
  152. `<span class="bullet bullet-dot bg-warning h-6px w-6px position-absolute translate-middle top-0 start-50 animation-blink"></span>`);
  153. $unreadCount.html(result.unreadCount);
  154. } else {
  155. $unreadCount.html(0);
  156. }
  157. //if (result.unreadCount > 0) {
  158. // $.blinkTitle.stop();
  159. // $.blinkTitle.start();
  160. //}
  161. //else {
  162. // $.blinkTitle.stop();
  163. //}
  164. }
  165. });
  166. }
  167. function notificationsFormatter(data) {
  168. let str = "";
  169. if (data && data.length) {
  170. data.forEach((v) => {
  171. str += `<div class="d-flex flex-stack py-4 notification-item" data-notification-id="${v.id}">
  172. <div class="d-flex align-items-center">
  173. <div class="symbol symbol-35px me-4">${v.icon}</div>
  174. <div class="mb-0 me-2">
  175. <div class="notification-url" data-url="${v.url}"><a href="javascript:void(0)" class="fs-6 ${v.isUnread ? "text-gray-800 text-hover-primary" : "text-gray-500"} fw-bolder">${v.name}</a></div>
  176. <div class="text-gray-400 fs-7">${v.text}</div>
  177. </div>
  178. </div>
  179. <span class="badge badge-light fs-8">${v.timeAgo}</span>
  180. </div>`;
  181. });
  182. } else {
  183. str += `<span class="notification-empty-text p-3">${abp.localization.VberZero('ThereIsNoNotification')}</span>`;
  184. }
  185. return str;
  186. }
  187. //Notification handler
  188. abp.event.on('abp.notifications.received', function (userNotification) {
  189. $.vbAjax4({
  190. url: abp.appUrl + 'Notification/GetUserNotificationById?id=' + userNotification.id,
  191. success: function (res) {
  192. console.log("received", res);
  193. abp.userNotificationHelper.show(res);
  194. }
  195. });
  196. loadNotifications();
  197. });
  198. abp.event.on('app.notifications.refresh', function () {
  199. loadNotifications();
  200. });
  201. abp.event.on('app.notifications.read', function () {
  202. loadNotifications();
  203. });
  204. loadNotifications();
  205. });