payment.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. paymentAmount: 30,
  8. selectedMethod: 0,
  9. planName: "",
  10. planType: "month",
  11. planId: 0,
  12. isProcessing: false,
  13. paymentMethods: [
  14. {
  15. name: "微信支付",
  16. icon: "💬",
  17. iconBg: "#07C160",
  18. type: "wechat"
  19. },
  20. {
  21. name: "支付宝支付",
  22. icon: "💰",
  23. iconBg: "#1677FF",
  24. type: "alipay"
  25. },
  26. {
  27. name: "其他支付",
  28. icon: "💳",
  29. iconBg: "#999999",
  30. type: "other"
  31. }
  32. ]
  33. };
  34. },
  35. onLoad(options) {
  36. if (options.price) {
  37. this.paymentAmount = parseFloat(options.price);
  38. }
  39. if (options.planName) {
  40. this.planName = decodeURIComponent(options.planName);
  41. }
  42. if (options.planType) {
  43. this.planType = options.planType;
  44. }
  45. if (options.planId) {
  46. this.planId = parseInt(options.planId);
  47. }
  48. },
  49. methods: {
  50. goBack() {
  51. common_vendor.index.navigateBack();
  52. },
  53. selectPaymentMethod(index) {
  54. this.selectedMethod = index;
  55. },
  56. async confirmPayment() {
  57. const userInfo2 = common_vendor.index.getStorageSync("userInfo");
  58. if (!userInfo2 || !userInfo2.id) {
  59. common_vendor.index.showToast({
  60. title: "请先登录",
  61. icon: "none"
  62. });
  63. setTimeout(() => {
  64. common_vendor.index.navigateTo({
  65. url: "/pages/login/login"
  66. });
  67. }, 1500);
  68. return;
  69. }
  70. if (this.isProcessing) {
  71. return;
  72. }
  73. const method = this.paymentMethods[this.selectedMethod];
  74. common_vendor.index.showModal({
  75. title: "确认支付",
  76. content: `使用${method.name}支付 ¥${this.paymentAmount.toFixed(2)}?`,
  77. success: async (res) => {
  78. if (res.confirm) {
  79. await this.processPayment(method, userInfo2.id);
  80. }
  81. }
  82. });
  83. },
  84. async processPayment(method, userId) {
  85. this.isProcessing = true;
  86. common_vendor.index.showLoading({
  87. title: "支付中...",
  88. mask: true
  89. });
  90. try {
  91. const purchaseData = {
  92. userId,
  93. vipType: this.planType,
  94. vipName: this.planName,
  95. price: this.paymentAmount,
  96. paymentMethod: method.type
  97. };
  98. const res = await utils_api.purchaseVip(purchaseData);
  99. common_vendor.index.hideLoading();
  100. this.isProcessing = false;
  101. if (res && res.code === 200) {
  102. common_vendor.index.showToast({
  103. title: "支付成功",
  104. icon: "success"
  105. });
  106. if (userInfo) {
  107. userInfo.isVip = true;
  108. if (res.data && res.data.expireTime) {
  109. }
  110. common_vendor.index.setStorageSync("userInfo", userInfo);
  111. }
  112. setTimeout(() => {
  113. common_vendor.index.navigateBack({
  114. delta: 2
  115. // 返回VIP页面再返回上一页
  116. });
  117. }, 1500);
  118. } else {
  119. common_vendor.index.showToast({
  120. title: res.message || "支付失败,请重试",
  121. icon: "none"
  122. });
  123. }
  124. } catch (error) {
  125. common_vendor.index.hideLoading();
  126. this.isProcessing = false;
  127. common_vendor.index.__f__("error", "at pages/payment/payment.vue:186", "支付失败:", error);
  128. common_vendor.index.showToast({
  129. title: error.message || "支付失败,请重试",
  130. icon: "none"
  131. });
  132. }
  133. }
  134. }
  135. };
  136. function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  137. return {
  138. a: common_vendor.o((...args) => $options.goBack && $options.goBack(...args)),
  139. b: common_vendor.t($data.paymentAmount.toFixed(2)),
  140. c: common_vendor.f($data.paymentMethods, (method, index, i0) => {
  141. return common_vendor.e({
  142. a: common_vendor.t(method.icon),
  143. b: method.iconBg,
  144. c: common_vendor.t(method.name),
  145. d: $data.selectedMethod === index
  146. }, $data.selectedMethod === index ? {} : {}, {
  147. e: $data.selectedMethod === index ? 1 : "",
  148. f: index,
  149. g: common_vendor.o(($event) => $options.selectPaymentMethod(index), index)
  150. });
  151. }),
  152. d: common_vendor.o((...args) => $options.confirmPayment && $options.confirmPayment(...args))
  153. };
  154. }
  155. const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render], ["__scopeId", "data-v-eade9ab2"]]);
  156. wx.createPage(MiniProgramPage);
  157. //# sourceMappingURL=../../../.sourcemap/mp-weixin/pages/payment/payment.js.map