order.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. "use strict";
  2. const common_vendor = require("../../common/vendor.js");
  3. const utils_auth = require("../../utils/auth.js");
  4. const utils_api = require("../../utils/api.js");
  5. const _sfc_main = {
  6. __name: "order",
  7. setup(__props) {
  8. const orders = common_vendor.ref([]);
  9. const loading = common_vendor.ref(false);
  10. const isLoggedIn = common_vendor.ref(false);
  11. const checkLogin = () => {
  12. isLoggedIn.value = utils_auth.isLoggedIn();
  13. };
  14. const handleBack = () => {
  15. const pages = getCurrentPages();
  16. pages.length > 1 ? common_vendor.index.navigateBack() : common_vendor.index.switchTab({ url: "/pages/mine/mine" });
  17. };
  18. const loadOrders = async () => {
  19. if (!isLoggedIn.value)
  20. return;
  21. loading.value = true;
  22. try {
  23. const res = await utils_api.getUserOrders();
  24. if (res.code === 200) {
  25. orders.value = res.data || [];
  26. }
  27. } catch (e) {
  28. console.error("加载订单记录失败:", e);
  29. } finally {
  30. loading.value = false;
  31. }
  32. };
  33. const pollOrderStatus = async (orderNo, maxRetries = 5, interval = 1e3) => {
  34. for (let i = 0; i < maxRetries; i++) {
  35. try {
  36. const res = await utils_api.queryOrder(orderNo);
  37. if (res.code === 200 && res.data && res.data.orderStatus === 1) {
  38. return true;
  39. }
  40. } catch (e) {
  41. console.log("查询订单状态失败:", e);
  42. }
  43. if (i < maxRetries - 1) {
  44. await new Promise((r) => setTimeout(r, interval));
  45. }
  46. }
  47. return false;
  48. };
  49. const handlePay = async (order) => {
  50. try {
  51. common_vendor.index.showLoading({ title: "正在支付..." });
  52. const res = await utils_api.repayOrder(order.orderNo);
  53. if (res.code !== 200) {
  54. throw new Error(res.message || "获取支付参数失败");
  55. }
  56. await utils_api.wxPay(res.data);
  57. common_vendor.index.showLoading({ title: "确认支付结果..." });
  58. const confirmed = await pollOrderStatus(order.orderNo);
  59. if (confirmed) {
  60. common_vendor.index.showToast({ title: "支付成功", icon: "success" });
  61. } else {
  62. common_vendor.index.showToast({ title: "支付处理中,请稍后查看", icon: "none" });
  63. }
  64. setTimeout(() => loadOrders(), 1500);
  65. } catch (e) {
  66. common_vendor.index.showToast({ title: e.message || "支付失败", icon: "none" });
  67. } finally {
  68. common_vendor.index.hideLoading();
  69. }
  70. };
  71. const getStatusClass = (status) => {
  72. switch (status) {
  73. case 0:
  74. return "status-pending";
  75. case 1:
  76. return "status-paid";
  77. case 2:
  78. return "status-cancelled";
  79. default:
  80. return "status-closed";
  81. }
  82. };
  83. common_vendor.onLoad(() => checkLogin());
  84. common_vendor.onMounted(() => {
  85. if (isLoggedIn.value)
  86. loadOrders();
  87. });
  88. common_vendor.onShow(() => {
  89. checkLogin();
  90. if (isLoggedIn.value)
  91. loadOrders();
  92. });
  93. return (_ctx, _cache) => {
  94. return common_vendor.e({
  95. a: common_vendor.o(handleBack),
  96. b: loading.value
  97. }, loading.value ? {} : orders.value.length === 0 ? {} : {
  98. d: common_vendor.f(orders.value, (order, index, i0) => {
  99. return common_vendor.e({
  100. a: common_vendor.t(order.poolType === 1 ? "⚡" : "📈"),
  101. b: common_vendor.t(order.poolName || (order.poolType === 1 ? "超短池" : "强势池")),
  102. c: common_vendor.n(order.poolType === 1 ? "tag-orange" : "tag-purple"),
  103. d: common_vendor.t(order.orderStatusName),
  104. e: common_vendor.n(getStatusClass(order.orderStatus)),
  105. f: common_vendor.t(order.orderNo),
  106. g: common_vendor.t(order.amount),
  107. h: order.expireTime
  108. }, order.expireTime ? {
  109. i: common_vendor.t(order.expireTime)
  110. } : {}, {
  111. j: common_vendor.t(order.createTime),
  112. k: order.orderStatus === 0
  113. }, order.orderStatus === 0 ? {
  114. l: common_vendor.o(($event) => handlePay(order), order.orderNo)
  115. } : {}, {
  116. m: order.orderNo,
  117. n: index === orders.value.length - 1 ? 1 : ""
  118. });
  119. })
  120. }, {
  121. c: orders.value.length === 0
  122. });
  123. };
  124. }
  125. };
  126. const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["__scopeId", "data-v-88bf5328"], ["__file", "D:/program/gupiao/gupiao-wx/src/pages/order/order.vue"]]);
  127. wx.createPage(MiniProgramPage);