This commit is contained in:
@@ -155,13 +155,17 @@
|
||||
</view>
|
||||
|
||||
<view class="payment-methods">
|
||||
<view class="method-item" :class="{ active: rechargePaymentMethod === 'alipay' }" @tap="selectRechargePaymentMethod('alipay')">
|
||||
<view v-for="method in paymentMethodOptions" :key="method.value" class="method-item"
|
||||
:class="{ active: rechargePaymentMethod === method.value }" @tap="selectRechargePaymentMethod(method.value)">
|
||||
<view class="method-left">
|
||||
<view class="method-icon method-badge method-badge-alipay">支</view>
|
||||
<text class="method-name">支付宝支付</text>
|
||||
<view v-if="method.value === 'alipay'" class="method-icon method-badge method-badge-alipay">支</view>
|
||||
<view v-else-if="method.value === 'wechat'" class="method-icon method-badge">微</view>
|
||||
<image v-else class="method-icon" src="/static/wallet.png" mode="aspectFit"></image>
|
||||
<text class="method-name">{{ method.label }}</text>
|
||||
</view>
|
||||
<view class="method-radio" :class="{ checked: rechargePaymentMethod === 'alipay' }"></view>
|
||||
<view class="method-radio" :class="{ checked: rechargePaymentMethod === method.value }"></view>
|
||||
</view>
|
||||
<view v-if="paymentMethodOptions.length === 0" class="method-empty">暂无可用支付方式</view>
|
||||
</view>
|
||||
|
||||
<view class="popup-footer">
|
||||
@@ -180,7 +184,7 @@
|
||||
<script setup>
|
||||
import { ref, reactive, onMounted, computed } from 'vue';
|
||||
import { onShow } from '@dcloudio/uni-app';
|
||||
import { assetApi, walletApi } from '@/api/index.js';
|
||||
import { walletApi } from '@/api/index.js';
|
||||
import { useUserStore } from '@/store/index.js';
|
||||
import {
|
||||
consumePendingPaymentRefresh,
|
||||
@@ -192,6 +196,8 @@
|
||||
showPaymentToast,
|
||||
wechatH5Pay
|
||||
} from '@/utils/payment.js';
|
||||
import { formatMoney } from '@/utils/display.js';
|
||||
import { getDefaultPaymentMethod, getPaymentMethodOptions, normalizePaymentMethods } from '@/utils/payment-methods.js';
|
||||
|
||||
const userStore = useUserStore();
|
||||
|
||||
@@ -221,11 +227,8 @@
|
||||
const rechargePaymentMethod = ref('alipay');
|
||||
const rechargeSubmitting = ref(false);
|
||||
const rechargeOrderSubmittingKey = ref(null);
|
||||
const isDeviceAsset = ref(true);
|
||||
let assetTypePromise = null;
|
||||
const paymentMethodOptions = computed(() => [
|
||||
{ label: '支付宝支付', value: 'alipay' }
|
||||
]);
|
||||
const rechargeAllowedPaymentMethods = ref([]);
|
||||
const paymentMethodOptions = computed(() => getPaymentMethodOptions(rechargeAllowedPaymentMethods.value));
|
||||
const rechargeAmounts = [
|
||||
{ value: 1000, label: '10' },
|
||||
{ value: 2000, label: '20' },
|
||||
@@ -243,11 +246,6 @@
|
||||
currentTab.value = e.detail.current;
|
||||
};
|
||||
|
||||
const formatMoney = (amount) => {
|
||||
if (!amount && amount !== 0) return '0.00';
|
||||
return (amount / 100).toFixed(2).replace(/\B(?=(\d{3})+(?!\d))/g, ',');
|
||||
};
|
||||
|
||||
const formatDisplayMoney = (amount) => {
|
||||
if (!amount && amount !== 0) return '0';
|
||||
return parseFloat(amount).toFixed(0).replace(/\B(?=(\d{3})+(?!\d))/g, ',');
|
||||
@@ -298,34 +296,13 @@
|
||||
return rechargeOrderSubmittingKey.value === getRechargeOrderSubmitKey(rechargeOrder);
|
||||
};
|
||||
|
||||
const getDefaultRechargePaymentMethod = () => 'alipay';
|
||||
|
||||
const normalizeRechargePaymentMethod = () => {
|
||||
const defaultMethod = getDefaultRechargePaymentMethod();
|
||||
if (rechargePaymentMethod.value !== defaultMethod) {
|
||||
const defaultMethod = getDefaultPaymentMethod(rechargeAllowedPaymentMethods.value);
|
||||
if (!paymentMethodOptions.value.some((item) => item.value === rechargePaymentMethod.value)) {
|
||||
rechargePaymentMethod.value = defaultMethod;
|
||||
}
|
||||
};
|
||||
|
||||
const loadAssetType = async () => {
|
||||
if (!userStore.state.identifier) return;
|
||||
if (assetTypePromise) return assetTypePromise;
|
||||
|
||||
assetTypePromise = assetApi.getInfo(userStore.state.identifier)
|
||||
.then((data) => {
|
||||
isDeviceAsset.value = data.asset_type === 'device';
|
||||
normalizeRechargePaymentMethod();
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error('加载资产类型失败', error);
|
||||
})
|
||||
.finally(() => {
|
||||
assetTypePromise = null;
|
||||
});
|
||||
|
||||
return assetTypePromise;
|
||||
};
|
||||
|
||||
const resetRechargeListAndLoad = () => {
|
||||
rechargePage.value = 1;
|
||||
rechargeNoMore.value = false;
|
||||
@@ -339,7 +316,6 @@
|
||||
};
|
||||
|
||||
const syncWalletStatus = () => {
|
||||
loadAssetType();
|
||||
loadWalletDetail();
|
||||
resetRechargeListAndLoad();
|
||||
resetTransactionListAndLoad();
|
||||
@@ -359,10 +335,30 @@
|
||||
}, 3000);
|
||||
};
|
||||
|
||||
const applyRechargeCheck = (data) => {
|
||||
if (Array.isArray(data?.allowed_payment_methods)) {
|
||||
rechargeAllowedPaymentMethods.value = normalizePaymentMethods(data.allowed_payment_methods);
|
||||
} else {
|
||||
rechargeAllowedPaymentMethods.value = [];
|
||||
}
|
||||
normalizeRechargePaymentMethod();
|
||||
};
|
||||
|
||||
const openRechargeModal = async () => {
|
||||
await loadAssetType();
|
||||
rechargePaymentMethod.value = getDefaultRechargePaymentMethod();
|
||||
showRechargeModal.value = true;
|
||||
try {
|
||||
const data = await walletApi.rechargeCheck(userStore.state.identifier);
|
||||
applyRechargeCheck(data);
|
||||
if (!paymentMethodOptions.value.length) {
|
||||
uni.showToast({ title: '暂无可用支付方式', icon: 'none' });
|
||||
return;
|
||||
}
|
||||
rechargePaymentMethod.value = getDefaultPaymentMethod(
|
||||
rechargeAllowedPaymentMethods.value
|
||||
);
|
||||
showRechargeModal.value = true;
|
||||
} catch (error) {
|
||||
console.error('充值前校验失败', error);
|
||||
}
|
||||
};
|
||||
|
||||
const selectAmount = (value) => {
|
||||
@@ -377,7 +373,7 @@
|
||||
};
|
||||
|
||||
const selectRechargePaymentMethod = (method) => {
|
||||
if (method !== getDefaultRechargePaymentMethod()) return;
|
||||
if (!paymentMethodOptions.value.some((item) => item.value === method)) return;
|
||||
rechargePaymentMethod.value = method;
|
||||
};
|
||||
|
||||
@@ -504,6 +500,12 @@
|
||||
};
|
||||
|
||||
const handleRechargeResult = async (rechargeData, paymentMethod) => {
|
||||
if (paymentMethod === 'wallet') {
|
||||
showPaymentToast(true, '充值成功');
|
||||
setTimeout(() => syncWalletStatus(), 1500);
|
||||
return;
|
||||
}
|
||||
|
||||
if (paymentMethod === 'wechat') {
|
||||
if (!isValidWechatPayConfig(rechargeData?.pay_config)) {
|
||||
uni.showToast({
|
||||
@@ -577,23 +579,31 @@
|
||||
|
||||
try {
|
||||
const checkData = await walletApi.rechargeCheck(userStore.state.identifier);
|
||||
applyRechargeCheck(checkData);
|
||||
if (!paymentMethodOptions.value.some((item) => item.value === rechargePaymentMethod.value)) {
|
||||
throw { msg: '当前支付方式不可用,请重新选择' };
|
||||
}
|
||||
|
||||
if (checkData.need_force_recharge && amount < checkData.force_recharge_amount) {
|
||||
const forceAmount = Number(checkData.force_recharge_amount || 0);
|
||||
const minAmount = Number(checkData.min_amount || 0);
|
||||
const maxAmount = Number(checkData.max_amount || Number.MAX_SAFE_INTEGER);
|
||||
|
||||
if (checkData.need_force_recharge && amount < forceAmount) {
|
||||
uni.hideLoading();
|
||||
uni.showModal({
|
||||
title: '提示',
|
||||
content: `${checkData.message || '当前需先充值最低金额'} ¥${(checkData.force_recharge_amount / 100).toFixed(2)}`,
|
||||
content: `${checkData.message || '当前需先充值最低金额'} ¥${formatMoney(forceAmount)}`,
|
||||
showCancel: false
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
if (amount < checkData.min_amount || amount > checkData.max_amount) {
|
||||
if (amount < minAmount || amount > maxAmount) {
|
||||
uni.hideLoading();
|
||||
showRechargeModal.value = false;
|
||||
setTimeout(() => {
|
||||
uni.showToast({
|
||||
title: `充值金额范围:¥${(checkData.min_amount / 100).toFixed(2)} - ¥${(checkData.max_amount / 100).toFixed(2)}`,
|
||||
title: `充值金额范围:¥${formatMoney(minAmount)} - ¥${formatMoney(maxAmount)}`,
|
||||
icon: 'none',
|
||||
duration: 2500
|
||||
});
|
||||
@@ -628,7 +638,11 @@
|
||||
|
||||
const showRechargePaymentMethods = async (rechargeOrder) => {
|
||||
if (rechargeOrderSubmittingKey.value !== null) return;
|
||||
await loadAssetType();
|
||||
try {
|
||||
applyRechargeCheck(await walletApi.rechargeCheck(userStore.state.identifier));
|
||||
} catch (error) {
|
||||
console.error('加载充值支付方式失败', error);
|
||||
}
|
||||
const options = paymentMethodOptions.value;
|
||||
|
||||
uni.showActionSheet({
|
||||
|
||||
Reference in New Issue
Block a user