message.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. "use strict";
  2. const common_vendor = require("../../common/vendor.js");
  3. const common_assets = require("../../common/assets.js");
  4. const store_message = require("../../store/message.js");
  5. const api_message = require("../../api/message.js");
  6. if (!Math) {
  7. CustomTabbar();
  8. }
  9. const CustomTabbar = () => "../../components/custom-tabbar/custom-tabbar.js";
  10. const _sfc_main = {
  11. __name: "message",
  12. setup(__props) {
  13. const activeTab = common_vendor.ref("all");
  14. const filterTabs = [
  15. { name: "全部", key: "all" },
  16. { name: "未读", key: "unread" },
  17. { name: "已读", key: "read" }
  18. ];
  19. const messages = common_vendor.ref([]);
  20. const loading = common_vendor.ref(false);
  21. const fetchMessages = async () => {
  22. loading.value = true;
  23. try {
  24. const res = await api_message.getMessageList();
  25. if (res.code === 200) {
  26. let list = [];
  27. if (Array.isArray(res.data)) {
  28. list = res.data;
  29. } else if (res.rows) {
  30. list = res.rows;
  31. } else if (res.data && res.data.list) {
  32. list = res.data.list;
  33. }
  34. messages.value = list.map((item) => ({
  35. id: item.id,
  36. title: item.title || "系统通知",
  37. position: item.positionName || "系统消息",
  38. company: item.companyName || "审计之家",
  39. status: item.status,
  40. desc: item.content,
  41. time: item.createTime ? item.createTime.substring(5, 16) : "",
  42. unread: item.isRead === 0
  43. }));
  44. updateGlobalUnread();
  45. }
  46. } catch (e) {
  47. common_vendor.index.__f__("error", "at pages/message/message.vue:113", "获取消息列表失败", e);
  48. } finally {
  49. loading.value = false;
  50. }
  51. };
  52. const updateGlobalUnread = () => {
  53. const count = messages.value.filter((m) => m.unread).length;
  54. store_message.messageStore.setUnreadCount(count);
  55. };
  56. common_vendor.onMounted(() => {
  57. fetchMessages();
  58. });
  59. common_vendor.onShow(() => {
  60. fetchMessages();
  61. });
  62. common_vendor.onPullDownRefresh(async () => {
  63. await fetchMessages();
  64. common_vendor.index.stopPullDownRefresh();
  65. });
  66. const onTabChange = (key) => {
  67. activeTab.value = key;
  68. };
  69. const getCount = (key) => {
  70. if (key === "unread")
  71. return store_message.messageStore.unreadCount;
  72. if (key === "all")
  73. return messages.value.length;
  74. if (key === "read")
  75. return messages.value.filter((m) => !m.unread).length;
  76. return 0;
  77. };
  78. const filteredMessages = common_vendor.computed(() => {
  79. if (activeTab.value === "unread")
  80. return messages.value.filter((m) => m.unread);
  81. if (activeTab.value === "read")
  82. return messages.value.filter((m) => !m.unread);
  83. return messages.value;
  84. });
  85. const markAllAsRead = async () => {
  86. try {
  87. const res = await api_message.readAllMessages();
  88. if (res.code === 200) {
  89. messages.value.forEach((m) => m.unread = false);
  90. updateGlobalUnread();
  91. common_vendor.index.showToast({ title: "已全部设为已读", icon: "none" });
  92. }
  93. } catch (e) {
  94. common_vendor.index.__f__("error", "at pages/message/message.vue:167", "标记已读失败", e);
  95. }
  96. };
  97. const goToDetail = async (msg) => {
  98. if (msg.unread) {
  99. try {
  100. await api_message.readMessage(msg.id);
  101. msg.unread = false;
  102. store_message.messageStore.setUnreadCount(Math.max(0, store_message.messageStore.unreadCount - 1));
  103. } catch (e) {
  104. common_vendor.index.__f__("error", "at pages/message/message.vue:179", "标记单条已读失败", e);
  105. }
  106. }
  107. common_vendor.index.navigateTo({
  108. url: `/pages/message/msgdetail?data=${encodeURIComponent(JSON.stringify(msg))}`
  109. });
  110. };
  111. return (_ctx, _cache) => {
  112. return common_vendor.e({
  113. a: common_vendor.f(filterTabs, (tab, k0, i0) => {
  114. return common_vendor.e({
  115. a: common_vendor.t(tab.name),
  116. b: getCount(tab.key) > 0
  117. }, getCount(tab.key) > 0 ? {
  118. c: common_vendor.t(getCount(tab.key))
  119. } : {}, {
  120. d: tab.key,
  121. e: common_vendor.n(activeTab.value === tab.key ? "active" : ""),
  122. f: common_vendor.o(($event) => onTabChange(tab.key), tab.key)
  123. });
  124. }),
  125. b: common_vendor.unref(store_message.messageStore).unreadCount > 0
  126. }, common_vendor.unref(store_message.messageStore).unreadCount > 0 ? {
  127. c: common_vendor.o(markAllAsRead)
  128. } : {}, {
  129. d: common_vendor.f(filteredMessages.value, (msg, index, i0) => {
  130. return common_vendor.e({
  131. a: common_vendor.t(msg.title),
  132. b: msg.unread
  133. }, msg.unread ? {} : {
  134. c: common_vendor.t(msg.time)
  135. }, {
  136. d: common_vendor.t(msg.position),
  137. e: common_vendor.t(msg.company),
  138. f: msg.unread
  139. }, msg.unread ? {
  140. g: common_vendor.t(msg.time)
  141. } : {}, {
  142. h: index,
  143. i: common_vendor.o(($event) => goToDetail(msg), index)
  144. });
  145. }),
  146. e: common_assets._imports_8,
  147. f: filteredMessages.value.length === 0
  148. }, filteredMessages.value.length === 0 ? {} : {}, {
  149. g: common_vendor.p({
  150. activeIndex: 2
  151. })
  152. });
  153. };
  154. }
  155. };
  156. const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["__scopeId", "data-v-4c1b26cf"]]);
  157. wx.createPage(MiniProgramPage);
  158. //# sourceMappingURL=../../../.sourcemap/mp-weixin/pages/message/message.js.map