profile.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. "use strict";
  2. const common_vendor = require("../../common/vendor.js");
  3. const utils_api = require("../../utils/api.js");
  4. const defaultAvatar = "https://picsum.photos/seed/avatar/200/200";
  5. const _sfc_main = {
  6. data() {
  7. return {
  8. userInfo: null,
  9. menuItems: [
  10. {
  11. name: "消息",
  12. icon: "💬",
  13. action: "messages"
  14. },
  15. {
  16. name: "笔记",
  17. icon: "📝",
  18. action: "notes"
  19. },
  20. {
  21. name: "阅读排行",
  22. icon: "🏆",
  23. action: "readingRank"
  24. },
  25. {
  26. name: "浏览记录",
  27. icon: "🕐",
  28. action: "browsingHistory"
  29. },
  30. {
  31. name: "开通VIP记录",
  32. icon: "💰",
  33. action: "vipRecords"
  34. },
  35. {
  36. name: "意见反馈",
  37. icon: "❓",
  38. action: "feedback"
  39. }
  40. ]
  41. };
  42. },
  43. computed: {
  44. displayAvatar() {
  45. if (this.userInfo && this.userInfo.avatar) {
  46. return this.userInfo.avatar;
  47. }
  48. return defaultAvatar;
  49. }
  50. },
  51. onLoad() {
  52. this.loadUserInfo();
  53. },
  54. onShow() {
  55. this.loadUserInfo();
  56. },
  57. methods: {
  58. loadUserInfo() {
  59. try {
  60. const storedUser = common_vendor.index.getStorageSync("userInfo");
  61. const isLogin = common_vendor.index.getStorageSync("isLogin");
  62. if (!storedUser || !storedUser.id || !isLogin) {
  63. this.userInfo = null;
  64. return;
  65. }
  66. this.userInfo = storedUser;
  67. this.fetchRemoteUser(storedUser.id);
  68. } catch (e) {
  69. common_vendor.index.__f__("error", "at pages/profile/profile.vue:108", "读取用户信息失败", e);
  70. this.userInfo = null;
  71. }
  72. },
  73. async fetchRemoteUser(userId) {
  74. try {
  75. const res = await utils_api.getUserProfile(userId);
  76. if (res && res.code === 200 && res.data) {
  77. this.userInfo = {
  78. ...this.userInfo,
  79. ...res.data
  80. };
  81. common_vendor.index.setStorageSync("userInfo", this.userInfo);
  82. }
  83. } catch (e) {
  84. common_vendor.index.__f__("error", "at pages/profile/profile.vue:123", "刷新用户信息失败", e);
  85. }
  86. },
  87. editProfile() {
  88. common_vendor.index.navigateTo({
  89. url: "/pages/edit-profile/edit-profile"
  90. });
  91. },
  92. goToLogin() {
  93. common_vendor.index.navigateTo({
  94. url: "/pages/login/login"
  95. });
  96. },
  97. goToSettings() {
  98. common_vendor.index.navigateTo({
  99. url: "/pages/settings/settings"
  100. });
  101. },
  102. goToVip() {
  103. common_vendor.index.navigateTo({
  104. url: "/pages/vip/vip"
  105. });
  106. },
  107. handleMenuClick(item) {
  108. switch (item.action) {
  109. case "messages":
  110. common_vendor.index.navigateTo({
  111. url: "/pages/messages/messages"
  112. });
  113. break;
  114. case "notes":
  115. common_vendor.index.navigateTo({
  116. url: "/pages/notes/notes"
  117. });
  118. break;
  119. case "readingRank":
  120. common_vendor.index.navigateTo({
  121. url: "/pages/reading-rank/reading-rank"
  122. });
  123. break;
  124. case "browsingHistory":
  125. common_vendor.index.navigateTo({
  126. url: "/pages/browsing-history/browsing-history"
  127. });
  128. break;
  129. case "vipRecords":
  130. common_vendor.index.navigateTo({
  131. url: "/pages/vip-records/vip-records"
  132. });
  133. break;
  134. case "feedback":
  135. common_vendor.index.navigateTo({
  136. url: "/pages/feedback/feedback"
  137. });
  138. break;
  139. }
  140. }
  141. }
  142. };
  143. function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  144. return common_vendor.e({
  145. a: $options.displayAvatar,
  146. b: common_vendor.t($data.userInfo ? $data.userInfo.nickname || $data.userInfo.username : "未登录"),
  147. c: $data.userInfo
  148. }, $data.userInfo ? {
  149. d: common_vendor.o((...args) => $options.editProfile && $options.editProfile(...args))
  150. } : {
  151. e: common_vendor.o((...args) => $options.goToLogin && $options.goToLogin(...args))
  152. }, {
  153. f: common_vendor.o((...args) => $options.goToSettings && $options.goToSettings(...args)),
  154. g: common_vendor.o((...args) => $options.goToVip && $options.goToVip(...args)),
  155. h: $data.userInfo
  156. }, $data.userInfo ? {
  157. i: common_vendor.f($data.menuItems, (item, index, i0) => {
  158. return {
  159. a: common_vendor.t(item.icon),
  160. b: common_vendor.t(item.name),
  161. c: index,
  162. d: common_vendor.o(($event) => $options.handleMenuClick(item), index)
  163. };
  164. })
  165. } : {});
  166. }
  167. const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render], ["__scopeId", "data-v-dd383ca2"]]);
  168. wx.createPage(MiniProgramPage);
  169. //# sourceMappingURL=../../../.sourcemap/mp-weixin/pages/profile/profile.js.map