browsing-history.js 4.3 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. historyList: [],
  8. userInfo: null,
  9. isLoading: false
  10. };
  11. },
  12. onLoad() {
  13. try {
  14. const userInfo = common_vendor.index.getStorageSync("userInfo");
  15. if (userInfo && userInfo.id) {
  16. this.userInfo = userInfo;
  17. this.loadBrowsingHistory();
  18. } else {
  19. common_vendor.index.showToast({
  20. title: "请先登录",
  21. icon: "none"
  22. });
  23. setTimeout(() => {
  24. common_vendor.index.navigateBack();
  25. }, 1500);
  26. }
  27. } catch (e) {
  28. common_vendor.index.__f__("error", "at pages/browsing-history/browsing-history.vue:67", "获取用户信息失败", e);
  29. }
  30. },
  31. methods: {
  32. goBack() {
  33. common_vendor.index.navigateBack();
  34. },
  35. async loadBrowsingHistory() {
  36. if (!this.userInfo || !this.userInfo.id) {
  37. return;
  38. }
  39. try {
  40. this.isLoading = true;
  41. const res = await utils_api.getBrowsingHistory(this.userInfo.id);
  42. if (res && res.code === 200 && res.data) {
  43. this.historyList = res.data.map((item) => ({
  44. id: item.bookId,
  45. title: item.title || "未知书籍",
  46. image: item.image || "https://via.placeholder.com/200x300?text=No+Image",
  47. time: item.time || ""
  48. }));
  49. } else {
  50. this.historyList = [];
  51. }
  52. } catch (e) {
  53. common_vendor.index.__f__("error", "at pages/browsing-history/browsing-history.vue:94", "加载浏览记录失败", e);
  54. this.historyList = [];
  55. } finally {
  56. this.isLoading = false;
  57. }
  58. },
  59. handleClear() {
  60. if (!this.userInfo || !this.userInfo.id) {
  61. return;
  62. }
  63. common_vendor.index.showModal({
  64. title: "提示",
  65. content: "确定要清空所有浏览记录吗?",
  66. success: async (res) => {
  67. if (res.confirm) {
  68. try {
  69. common_vendor.index.showLoading({
  70. title: "清空中...",
  71. mask: true
  72. });
  73. const result = await utils_api.clearBrowsingHistory(this.userInfo.id);
  74. common_vendor.index.hideLoading();
  75. if (result && result.code === 200) {
  76. this.historyList = [];
  77. common_vendor.index.showToast({
  78. title: "已清空",
  79. icon: "success"
  80. });
  81. } else {
  82. common_vendor.index.showToast({
  83. title: result.message || "清空失败",
  84. icon: "none"
  85. });
  86. }
  87. } catch (e) {
  88. common_vendor.index.hideLoading();
  89. common_vendor.index.__f__("error", "at pages/browsing-history/browsing-history.vue:133", "清空浏览记录失败", e);
  90. common_vendor.index.showToast({
  91. title: "清空失败,请重试",
  92. icon: "none"
  93. });
  94. }
  95. }
  96. }
  97. });
  98. },
  99. goToBookDetail(item) {
  100. if (!item || !item.id) {
  101. common_vendor.index.showToast({
  102. title: "书籍信息不完整",
  103. icon: "none"
  104. });
  105. return;
  106. }
  107. common_vendor.index.navigateTo({
  108. url: `/pages/book-detail/book-detail?bookId=${item.id}`
  109. });
  110. }
  111. }
  112. };
  113. function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  114. return common_vendor.e({
  115. a: common_vendor.o((...args) => $options.goBack && $options.goBack(...args)),
  116. b: common_vendor.o((...args) => $options.handleClear && $options.handleClear(...args)),
  117. c: common_vendor.f($data.historyList, (item, index, i0) => {
  118. return {
  119. a: item.image,
  120. b: common_vendor.t(item.title),
  121. c: common_vendor.t(item.time),
  122. d: index,
  123. e: common_vendor.o(($event) => $options.goToBookDetail(item), index)
  124. };
  125. }),
  126. d: $data.historyList.length === 0
  127. }, $data.historyList.length === 0 ? {} : {});
  128. }
  129. const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render], ["__scopeId", "data-v-3dbaa36d"]]);
  130. wx.createPage(MiniProgramPage);
  131. //# sourceMappingURL=../../../.sourcemap/mp-weixin/pages/browsing-history/browsing-history.js.map