orders.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. "use strict";
  2. const common_vendor = require("../../common/vendor.js");
  3. const common_assets = require("../../common/assets.js");
  4. const api_order = require("../../api/order.js");
  5. const api_message = require("../../api/message.js");
  6. const _sfc_main = {
  7. __name: "orders",
  8. setup(__props) {
  9. const activeTab = common_vendor.ref("all");
  10. const tabs = [
  11. { name: "全部", key: "all" },
  12. { name: "待支付", key: "unpaid" },
  13. { name: "已支付", key: "paid" }
  14. ];
  15. const loading = common_vendor.ref(false);
  16. const orders = common_vendor.ref([]);
  17. const showPayModal = common_vendor.ref(false);
  18. const currentOrder = common_vendor.ref(null);
  19. const payLoading = common_vendor.ref(false);
  20. common_vendor.onMounted(() => {
  21. fetchOrders();
  22. });
  23. common_vendor.onPullDownRefresh(async () => {
  24. await fetchOrders();
  25. common_vendor.index.stopPullDownRefresh();
  26. });
  27. const fetchOrders = async () => {
  28. loading.value = true;
  29. try {
  30. const userInfo = common_vendor.index.getStorageSync("userInfo");
  31. if (!userInfo || !userInfo.studentId) {
  32. common_vendor.index.showToast({ title: "请先登录", icon: "none" });
  33. return;
  34. }
  35. const res = await api_order.listOrder({
  36. buyerId: userInfo.studentId,
  37. buyerType: 2
  38. });
  39. if (res && res.rows) {
  40. orders.value = res.rows.map((item) => {
  41. const statusInfo = resolveOrderStatus(item.orderStatus, item.payStatus);
  42. return {
  43. ...item,
  44. id: item.id,
  45. orderNo: item.orderNo,
  46. statusKey: statusInfo.key,
  47. statusText: statusInfo.text,
  48. jobName: item.productName || item.remark || "测评服务",
  49. company: item.sellerName || "审计之家",
  50. price: item.totalAmount || "0.00",
  51. isDeposit: item.orderType === 2,
  52. createTime: formatTime(item.createTime)
  53. };
  54. });
  55. }
  56. } catch (err) {
  57. common_vendor.index.__f__("error", "at pages/my/orders.vue:148", "获取订单列表失败:", err);
  58. common_vendor.index.showToast({ title: "获取订单失败", icon: "none" });
  59. } finally {
  60. loading.value = false;
  61. }
  62. };
  63. const resolveOrderStatus = (orderStatus, payStatus) => {
  64. if (orderStatus === 4)
  65. return { key: "refunded", text: "已退款" };
  66. if (orderStatus === 2)
  67. return { key: "partial_refund", text: "部分退款" };
  68. if (payStatus === 1 || payStatus === 2) {
  69. if (orderStatus === 1)
  70. return { key: "paid", text: "已完成" };
  71. return { key: "paid", text: "已支付" };
  72. }
  73. if (orderStatus === 0 && (payStatus === 0 || payStatus === null)) {
  74. return { key: "unpaid", text: "待支付" };
  75. }
  76. return { key: "unknown", text: "未知状态" };
  77. };
  78. const filteredOrders = common_vendor.computed(() => {
  79. if (activeTab.value === "all")
  80. return orders.value;
  81. return orders.value.filter((o) => o.statusKey === activeTab.value);
  82. });
  83. const formatTime = (time) => {
  84. if (!time)
  85. return "--";
  86. const d = new Date(time);
  87. const year = d.getFullYear();
  88. const month = String(d.getMonth() + 1).padStart(2, "0");
  89. const day = String(d.getDate()).padStart(2, "0");
  90. const hour = String(d.getHours()).padStart(2, "0");
  91. const minute = String(d.getMinutes()).padStart(2, "0");
  92. return `${year}-${month}-${day} ${hour}:${minute}`;
  93. };
  94. const handlePay = (order) => {
  95. currentOrder.value = order;
  96. showPayModal.value = true;
  97. };
  98. const confirmPay = async () => {
  99. if (!currentOrder.value || payLoading.value)
  100. return;
  101. payLoading.value = true;
  102. try {
  103. const userInfo = common_vendor.index.getStorageSync("userInfo") || {};
  104. const userId = userInfo.studentId;
  105. if (!userId) {
  106. common_vendor.index.showToast({ title: "请先登录", icon: "none" });
  107. return;
  108. }
  109. const businessId = currentOrder.value.businessId;
  110. if (!businessId) {
  111. common_vendor.index.showToast({ title: "该订单暂不支持在线支付", icon: "none" });
  112. return;
  113. }
  114. const payRes = await api_message.createWxPayOrder(businessId, userId);
  115. if (!(payRes.code === 200 || payRes.code === 0)) {
  116. common_vendor.index.showToast({ title: payRes.msg || "创建支付订单失败", icon: "none" });
  117. return;
  118. }
  119. showPayModal.value = false;
  120. if (payRes.data && payRes.data.wechatPayParams) {
  121. const wxPayParams = payRes.data.wechatPayParams;
  122. common_vendor.index.showLoading({ title: "发起微信支付..." });
  123. common_vendor.index.requestPayment({
  124. provider: "wxpay",
  125. timeStamp: wxPayParams.timeStamp,
  126. nonceStr: wxPayParams.nonceStr,
  127. package: wxPayParams.package,
  128. signType: wxPayParams.signType || "RSA",
  129. paySign: wxPayParams.paySign,
  130. success: (res) => {
  131. common_vendor.index.hideLoading();
  132. common_vendor.index.showToast({ title: "支付成功", icon: "success" });
  133. setTimeout(() => {
  134. fetchOrders();
  135. }, 1e3);
  136. },
  137. fail: (err) => {
  138. common_vendor.index.hideLoading();
  139. if (err.errMsg && err.errMsg.includes("cancel")) {
  140. common_vendor.index.showToast({ title: "已取消支付", icon: "none" });
  141. } else {
  142. common_vendor.index.showToast({ title: "支付失败,请重试", icon: "none" });
  143. }
  144. }
  145. });
  146. } else {
  147. common_vendor.index.showToast({ title: "支付参数异常", icon: "none" });
  148. }
  149. } catch (err) {
  150. common_vendor.index.__f__("error", "at pages/my/orders.vue:263", "支付异常:", err);
  151. common_vendor.index.showToast({ title: "支付异常,请重试", icon: "none" });
  152. } finally {
  153. payLoading.value = false;
  154. }
  155. };
  156. return (_ctx, _cache) => {
  157. var _a, _b;
  158. return common_vendor.e({
  159. a: common_vendor.f(tabs, (tab, k0, i0) => {
  160. return {
  161. a: common_vendor.t(tab.name),
  162. b: tab.key,
  163. c: common_vendor.n(activeTab.value === tab.key ? "active" : ""),
  164. d: common_vendor.o(($event) => activeTab.value = tab.key, tab.key)
  165. };
  166. }),
  167. b: common_vendor.f(filteredOrders.value, (order, index, i0) => {
  168. return common_vendor.e({
  169. a: common_vendor.t(order.orderNo),
  170. b: common_vendor.t(order.statusText),
  171. c: common_vendor.n(order.statusKey),
  172. d: common_vendor.t(order.jobName),
  173. e: common_vendor.t(order.company),
  174. f: common_vendor.t(order.price),
  175. g: common_vendor.t(order.createTime),
  176. h: order.isDeposit
  177. }, order.isDeposit ? {} : {}, {
  178. i: order.statusKey === "unpaid"
  179. }, order.statusKey === "unpaid" ? {
  180. j: common_vendor.o(($event) => handlePay(order), index)
  181. } : order.statusKey === "paid" ? {} : {}, {
  182. k: order.statusKey === "paid",
  183. l: index
  184. });
  185. }),
  186. c: filteredOrders.value.length === 0 && !loading.value
  187. }, filteredOrders.value.length === 0 && !loading.value ? {
  188. d: common_assets._imports_0$1
  189. } : {}, {
  190. e: filteredOrders.value.length > 0
  191. }, filteredOrders.value.length > 0 ? {} : {}, {
  192. f: showPayModal.value
  193. }, showPayModal.value ? {
  194. g: common_vendor.t((_a = currentOrder.value) == null ? void 0 : _a.jobName),
  195. h: common_vendor.t((_b = currentOrder.value) == null ? void 0 : _b.price),
  196. i: common_vendor.o(($event) => showPayModal.value = false),
  197. j: common_vendor.o(confirmPay),
  198. k: payLoading.value,
  199. l: common_vendor.o(() => {
  200. })
  201. } : {});
  202. };
  203. }
  204. };
  205. const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["__scopeId", "data-v-09724fb8"]]);
  206. wx.createPage(MiniProgramPage);
  207. //# sourceMappingURL=../../../.sourcemap/mp-weixin/pages/my/orders.js.map