-f
This commit is contained in:
@@ -60,7 +60,7 @@
|
|||||||
|
|
||||||
<!-- 支付宝支付 -->
|
<!-- 支付宝支付 -->
|
||||||
<van-cell
|
<van-cell
|
||||||
v-else
|
v-if="!isWeChat"
|
||||||
title="支付宝支付"
|
title="支付宝支付"
|
||||||
clickable
|
clickable
|
||||||
@click="selectedPaymentMethod = 'alipay'"
|
@click="selectedPaymentMethod = 'alipay'"
|
||||||
@@ -80,6 +80,29 @@
|
|||||||
/>
|
/>
|
||||||
</template>
|
</template>
|
||||||
</van-cell>
|
</van-cell>
|
||||||
|
|
||||||
|
<!-- 开发环境测试支付(仅开发环境显示,不跳转支付宝/微信,生成空报告) -->
|
||||||
|
<van-cell
|
||||||
|
v-if="isDev"
|
||||||
|
title="开发环境测试支付"
|
||||||
|
clickable
|
||||||
|
@click="selectedPaymentMethod = 'test'"
|
||||||
|
>
|
||||||
|
<template #icon>
|
||||||
|
<van-icon
|
||||||
|
size="24"
|
||||||
|
name="description"
|
||||||
|
color="#ff976a"
|
||||||
|
class="mr-2"
|
||||||
|
/>
|
||||||
|
</template>
|
||||||
|
<template #right-icon>
|
||||||
|
<van-radio
|
||||||
|
v-model="selectedPaymentMethod"
|
||||||
|
name="test"
|
||||||
|
/>
|
||||||
|
</template>
|
||||||
|
</van-cell>
|
||||||
</van-cell-group>
|
</van-cell-group>
|
||||||
</div>
|
</div>
|
||||||
<!-- 确认按钮 -->
|
<!-- 确认按钮 -->
|
||||||
@@ -92,8 +115,9 @@
|
|||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup>
|
<script setup>
|
||||||
import { ref, defineProps } from "vue";
|
import { ref, defineProps, watch } from "vue";
|
||||||
const { isWeChat } = useEnv();
|
const { isWeChat } = useEnv();
|
||||||
|
const isDev = import.meta.env.DEV;
|
||||||
|
|
||||||
const props = defineProps({
|
const props = defineProps({
|
||||||
data: {
|
data: {
|
||||||
@@ -111,23 +135,20 @@ const props = defineProps({
|
|||||||
});
|
});
|
||||||
const show = defineModel();
|
const show = defineModel();
|
||||||
const selectedPaymentMethod = ref(isWeChat.value ? "wechat" : "alipay");
|
const selectedPaymentMethod = ref(isWeChat.value ? "wechat" : "alipay");
|
||||||
onMounted(() => {
|
|
||||||
if (isWeChat.value) {
|
function setDefaultPaymentMethod() {
|
||||||
selectedPaymentMethod.value = "wechat";
|
selectedPaymentMethod.value = isWeChat.value ? "wechat" : "alipay";
|
||||||
} else {
|
}
|
||||||
selectedPaymentMethod.value = "alipay";
|
onMounted(setDefaultPaymentMethod);
|
||||||
}
|
|
||||||
|
// 每次打开弹窗时重置为支付宝/微信,避免上一次选的「开发环境测试支付」被沿用导致误走自动 paid
|
||||||
|
watch(show, (v) => {
|
||||||
|
if (v) setDefaultPaymentMethod();
|
||||||
});
|
});
|
||||||
|
|
||||||
const orderNo = ref("");
|
const orderNo = ref("");
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
const discountPrice = ref(false); // 是否应用折扣
|
const discountPrice = ref(false); // 是否应用折扣
|
||||||
onMounted(() => {
|
|
||||||
if (isWeChat.value) {
|
|
||||||
selectedPaymentMethod.value = "wechat";
|
|
||||||
} else {
|
|
||||||
selectedPaymentMethod.value = "alipay";
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
async function getPayment() {
|
async function getPayment() {
|
||||||
const { data, error } = await useApiFetch("/pay/payment")
|
const { data, error } = await useApiFetch("/pay/payment")
|
||||||
@@ -139,10 +160,28 @@ async function getPayment() {
|
|||||||
.json();
|
.json();
|
||||||
|
|
||||||
if (data.value && !error.value) {
|
if (data.value && !error.value) {
|
||||||
|
const prepayId = data.value.data.prepay_id;
|
||||||
|
const orderNoFromResp = data.value.data.order_no;
|
||||||
|
|
||||||
|
// 开发环境测试支付:仅当用户选择「开发环境测试支付」时后端才返回 test_payment_success
|
||||||
|
// 若选择支付宝/微信却收到此值,说明后端异常,不跳转、直接报错
|
||||||
|
if (prepayId === "test_payment_success") {
|
||||||
|
if (selectedPaymentMethod.value === "alipay" || selectedPaymentMethod.value === "wechat") {
|
||||||
|
showToast({ message: "支付参数异常,请重试", type: "fail" });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
show.value = false;
|
||||||
|
router.push({
|
||||||
|
path: "/payment/result",
|
||||||
|
query: { orderNo: orderNoFromResp },
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (selectedPaymentMethod.value === "alipay") {
|
if (selectedPaymentMethod.value === "alipay") {
|
||||||
orderNo.value = data.value.data.order_no;
|
orderNo.value = orderNoFromResp;
|
||||||
// 存储订单ID以便支付宝返回时获取
|
// 存储订单ID以便支付宝返回时获取
|
||||||
const prepayUrl = data.value.data.prepay_id;
|
const prepayUrl = prepayId;
|
||||||
const paymentForm = document.createElement("form");
|
const paymentForm = document.createElement("form");
|
||||||
paymentForm.method = "POST";
|
paymentForm.method = "POST";
|
||||||
paymentForm.action = prepayUrl;
|
paymentForm.action = prepayUrl;
|
||||||
@@ -159,7 +198,7 @@ async function getPayment() {
|
|||||||
// 支付成功,直接跳转到结果页面
|
// 支付成功,直接跳转到结果页面
|
||||||
router.push({
|
router.push({
|
||||||
path: "/payment/result",
|
path: "/payment/result",
|
||||||
query: { orderNo: data.value.data.order_no },
|
query: { orderNo: orderNoFromResp },
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user