messages.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. "use strict";
  2. const common_vendor = require("../../common/vendor.js");
  3. const utils_api = require("../../utils/api.js");
  4. const _sfc_main = {
  5. data() {
  6. return {
  7. messageList: [],
  8. loading: false,
  9. page: 1,
  10. size: 20,
  11. hasMore: true
  12. };
  13. },
  14. onLoad() {
  15. this.loadMessages();
  16. },
  17. onPullDownRefresh() {
  18. this.page = 1;
  19. this.messageList = [];
  20. this.hasMore = true;
  21. this.loadMessages().finally(() => {
  22. common_vendor.index.stopPullDownRefresh();
  23. });
  24. },
  25. onReachBottom() {
  26. if (this.hasMore && !this.loading) {
  27. this.page++;
  28. this.loadMessages();
  29. }
  30. },
  31. methods: {
  32. async loadMessages() {
  33. const userInfo = common_vendor.index.getStorageSync("userInfo");
  34. if (!userInfo || !userInfo.id) {
  35. common_vendor.index.showToast({
  36. title: "请先登录",
  37. icon: "none"
  38. });
  39. setTimeout(() => {
  40. common_vendor.index.navigateTo({
  41. url: "/pages/login/login"
  42. });
  43. }, 1500);
  44. return;
  45. }
  46. if (this.loading)
  47. return;
  48. this.loading = true;
  49. try {
  50. const res = await utils_api.getMessages(userInfo.id, null, null, this.page, this.size);
  51. if (res && res.code === 200 && res.data) {
  52. const newMessages = res.data.list || [];
  53. if (this.page === 1) {
  54. this.messageList = newMessages;
  55. } else {
  56. this.messageList = this.messageList.concat(newMessages);
  57. }
  58. this.hasMore = newMessages.length >= this.size;
  59. this.messageList.forEach((msg) => {
  60. if (!msg.avatar) {
  61. msg.avatar = "https://picsum.photos/seed/default/200/200";
  62. }
  63. });
  64. } else {
  65. if (this.page === 1) {
  66. this.messageList = [];
  67. }
  68. this.hasMore = false;
  69. }
  70. } catch (error) {
  71. common_vendor.index.__f__("error", "at pages/messages/messages.vue:121", "加载消息失败:", error);
  72. if (this.page === 1) {
  73. this.messageList = [];
  74. }
  75. if (this.page > 1) {
  76. this.page--;
  77. }
  78. } finally {
  79. this.loading = false;
  80. }
  81. },
  82. goBack() {
  83. common_vendor.index.navigateBack();
  84. },
  85. async handleMessageClick(message) {
  86. if (message.isRead === 0) {
  87. try {
  88. await utils_api.markMessageAsRead(message.id);
  89. message.isRead = 1;
  90. } catch (error) {
  91. common_vendor.index.__f__("error", "at pages/messages/messages.vue:142", "标记消息已读失败:", error);
  92. }
  93. }
  94. if (message.type === "like" || message.type === "reply") {
  95. common_vendor.index.showToast({
  96. title: "查看评论详情",
  97. icon: "none"
  98. });
  99. } else if (message.type === "comment") {
  100. common_vendor.index.showToast({
  101. title: "查看书籍详情",
  102. icon: "none"
  103. });
  104. }
  105. }
  106. }
  107. };
  108. function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  109. return common_vendor.e({
  110. a: common_vendor.o((...args) => $options.goBack && $options.goBack(...args)),
  111. b: common_vendor.f($data.messageList, (message, index, i0) => {
  112. return common_vendor.e({
  113. a: message.avatar,
  114. b: common_vendor.t(message.sender),
  115. c: common_vendor.t(message.time),
  116. d: common_vendor.t(message.actionIcon),
  117. e: common_vendor.t(message.actionText),
  118. f: message.comment
  119. }, message.comment ? {
  120. g: common_vendor.t(message.comment)
  121. } : {}, {
  122. h: index,
  123. i: common_vendor.o(($event) => $options.handleMessageClick(message), index)
  124. });
  125. }),
  126. c: $data.messageList.length === 0
  127. }, $data.messageList.length === 0 ? {} : {});
  128. }
  129. const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render], ["__scopeId", "data-v-ecc172b4"]]);
  130. wx.createPage(MiniProgramPage);
  131. //# sourceMappingURL=../../../.sourcemap/mp-weixin/pages/messages/messages.js.map