rank.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. "use strict";
  2. const common_vendor = require("../../common/vendor.js");
  3. const utils_api = require("../../utils/api.js");
  4. const utils_auth = require("../../utils/auth.js");
  5. const _sfc_main = {
  6. __name: "rank",
  7. setup(__props) {
  8. const isLoggedIn = common_vendor.ref(false);
  9. const portfolio = common_vendor.ref({
  10. balance: 1e5,
  11. profit: 0,
  12. profitRate: 0
  13. });
  14. const myRank = common_vendor.ref({
  15. rank: 5,
  16. rate: 0
  17. });
  18. const mockLeaderboard = [
  19. { rank: 1, name: "量化王者 ***", rate: 35.2 },
  20. { rank: 2, name: "短线猎手 ***", rate: 28.9 },
  21. { rank: 3, name: "趋势追踪者 ***", rate: 22.1 },
  22. { rank: 4, name: "稳健投资 ***", rate: 18.5 },
  23. { rank: 5, name: "价值发现 ***", rate: 15.3 }
  24. ];
  25. const leaderboard = common_vendor.ref(mockLeaderboard);
  26. const formatAmount = (amount) => {
  27. return amount.toLocaleString("zh-CN");
  28. };
  29. const formatProfit = (profit) => {
  30. return Math.abs(profit).toLocaleString("zh-CN");
  31. };
  32. const formatRate = (rate) => {
  33. return rate.toFixed(2);
  34. };
  35. const getRankClass = (rank) => {
  36. if (rank === 1)
  37. return "rank-first";
  38. if (rank === 2)
  39. return "rank-second";
  40. if (rank === 3)
  41. return "rank-third";
  42. return "";
  43. };
  44. const onGetPhoneNumber = async (e) => {
  45. console.log("[模拟排名] 获取手机号回调:", e.detail);
  46. if (e.detail.errMsg === "getPhoneNumber:ok") {
  47. const phoneCode = e.detail.code;
  48. console.log("[模拟排名] phoneCode:", phoneCode);
  49. common_vendor.index.showLoading({
  50. title: "登录中...",
  51. mask: true
  52. });
  53. try {
  54. const loginRes = await common_vendor.index.login();
  55. console.log("[模拟排名] uni.login完整响应:", loginRes);
  56. console.log("[模拟排名] 微信登录code:", loginRes.code);
  57. if (!loginRes.code) {
  58. throw new Error("获取微信登录code失败");
  59. }
  60. const result = await utils_auth.wxAuthLogin(loginRes.code, phoneCode);
  61. common_vendor.index.hideLoading();
  62. if (result) {
  63. checkLoginAndLoadData();
  64. }
  65. } catch (error) {
  66. common_vendor.index.hideLoading();
  67. console.error("[模拟排名] 登录失败:", error);
  68. }
  69. } else {
  70. common_vendor.index.showToast({
  71. title: "需要授权手机号才能登录",
  72. icon: "none",
  73. duration: 2e3
  74. });
  75. }
  76. };
  77. const checkLoginAndLoadData = () => {
  78. isLoggedIn.value = utils_auth.isLoggedIn();
  79. console.log("[模拟排名] 登录状态:", isLoggedIn.value);
  80. if (isLoggedIn.value) {
  81. loadRealData();
  82. } else {
  83. leaderboard.value = mockLeaderboard;
  84. }
  85. };
  86. const loadRealData = async () => {
  87. try {
  88. const portfolioRes = await utils_api.getUserPortfolio();
  89. if (portfolioRes.code === 0 && portfolioRes.data) {
  90. portfolio.value = portfolioRes.data;
  91. }
  92. } catch (err) {
  93. console.error("获取资产数据失败:", err);
  94. }
  95. try {
  96. const leaderboardRes = await utils_api.getLeaderboard();
  97. if (leaderboardRes.code === 0 && leaderboardRes.data) {
  98. leaderboard.value = leaderboardRes.data.list || [];
  99. myRank.value = leaderboardRes.data.myRank || { rank: 5, rate: 0 };
  100. }
  101. } catch (err) {
  102. console.error("获取排行榜数据失败:", err);
  103. }
  104. };
  105. common_vendor.onMounted(() => {
  106. checkLoginAndLoadData();
  107. });
  108. return (_ctx, _cache) => {
  109. return common_vendor.e({
  110. a: common_vendor.t(formatAmount(portfolio.value.balance)),
  111. b: common_vendor.t(formatProfit(portfolio.value.profit)),
  112. c: common_vendor.t(formatRate(portfolio.value.profitRate)),
  113. d: common_vendor.n(portfolio.value.profitRate >= 0 ? "profit-positive" : "profit-negative"),
  114. e: common_vendor.t(myRank.value.rank),
  115. f: common_vendor.t(myRank.value.rate >= 0 ? "+" : ""),
  116. g: common_vendor.t(myRank.value.rate),
  117. h: common_vendor.n(myRank.value.rate >= 0 ? "rate-positive" : "rate-negative"),
  118. i: common_vendor.f(leaderboard.value, (item, index, i0) => {
  119. return {
  120. a: common_vendor.t(item.rank),
  121. b: common_vendor.n(getRankClass(item.rank)),
  122. c: common_vendor.t(item.name),
  123. d: common_vendor.t(item.rate >= 0 ? "+" : ""),
  124. e: common_vendor.t(item.rate),
  125. f: common_vendor.n(item.rate >= 0 ? "rate-positive" : "rate-negative"),
  126. g: index
  127. };
  128. }),
  129. j: !isLoggedIn.value ? 1 : "",
  130. k: !isLoggedIn.value
  131. }, !isLoggedIn.value ? {
  132. l: common_vendor.o(onGetPhoneNumber)
  133. } : {});
  134. };
  135. }
  136. };
  137. const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["__file", "D:/program/gupiao-wx/src/pages/rank/rank.vue"]]);
  138. wx.createPage(MiniProgramPage);