Files
kangyuan-mobile/pages/home/liquidation.vue
T

381 lines
9.2 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<template>
<s-pages title="去变现">
<view class="liquidation">
<view class="top_bar">
<view class="record_link" @click="goRecord">变现记录</view>
</view>
<view class="container">
<view class="card balance-card">
<view class="balance-label">当前钱包余额</view>
<view class="balance-value">{{ $fen2yuan(walletBalanceCents || 0) }}</view>
</view>
<view class="section-row">
<view class="section-title">转出资产</view>
</view>
<view class="card input-card">
<view class="amount-row">
<view class="amount-prefix">¥</view>
<uni-easyinput
v-model="amountInput"
type="digit"
placeholder="00.00"
placeholder-style="font-size: 64rpx;color:#C6C6C6;"
:inputBorder="false"
:min="0"
:clearSize="50"
@blur="onAmountBlur"
@confirm="submit"
/>
</view>
<view class="tip-text">请输入去变现数量</view>
</view>
<view class="card code-card">
<view class="code-row">
<uni-easyinput
v-model="smsCode"
type="number"
placeholder="请输入验证码"
placeholder-style="font-size: 28rpx;color:#C6C6C6;"
:inputBorder="false"
:maxlength="6"
/>
<view class="code-btn" :class="{ disabled: smsCountdown > 0 }" @click="sendSmsCode">
{{ smsCountdown > 0 ? `${smsCountdown}s` : '获取验证码' }}
</view>
</view>
</view>
<view class="card tip-card">
<view class="tip-header">
<view class="tip-title">兑换提示</view>
<uni-icons type="info" color="#C6C6C6" size="22" />
</view>
<view class="tip-text">请慎重考虑,一经兑换,不可撤销</view>
</view>
</view>
<view class="bottom-bar">
<view class="confirm-btn" @click="submit">确定</view>
</view>
</view>
</s-pages>
</template>
<script setup>
import { computed, ref } from 'vue';
import { onShow } from '@dcloudio/uni-app';
import * as payApi from '@/sheep/api/home/pay';
import * as incrementApi from '@/sheep/api/increment';
import AuthUtil from '@/sheep/api/member/auth';
import sheep from '@/sheep';
const walletBalanceCents = ref(0);
const amount = ref('');
const smsCode = ref('');
const smsCountdown = ref(0);
const smsScene = 7;
let smsTimer = null;
const sanitizeAmountInput = (val) => {
let next = String(val ?? '').replace(/[^\d.]/g, '');
const firstDot = next.indexOf('.');
if (firstDot !== -1) {
next = next.slice(0, firstDot + 1) + next.slice(firstDot + 1).replace(/\./g, '');
const [a, b = ''] = next.split('.');
next = `${a}.${b.slice(0, 2)}`;
}
return next;
};
const amountInput = computed({
get: () => String(amount.value ?? ''),
set: (v) => {
amount.value = sanitizeAmountInput(v);
},
});
const amountNumber = computed(() => {
const n = Number(amount.value);
return Number.isFinite(n) ? n : 0;
});
const availableYuan = computed(() => Number(walletBalanceCents.value || 0) / 100);
const refreshWallet = async () => {
const res = await payApi.getWallet().catch(() => ({ code: -1 }));
if (res?.code === 0) walletBalanceCents.value = res?.data?.balance ?? 0;
};
const onAmountBlur = () => {
if (amountNumber.value < 0) {
sheep.$helper.toast('请输入正确金额');
amount.value = '0';
return;
}
if (amountNumber.value > availableYuan.value) {
sheep.$helper.toast('变现金额不能大于钱包余额');
amount.value = availableYuan.value > 0 ? String(availableYuan.value.toFixed(2)) : '0';
}
};
const submit = async () => {
const amt = amountNumber.value;
if (!amt) {
sheep.$helper.toast('请输入去变现数量');
return;
}
if (amt > availableYuan.value) {
sheep.$helper.toast('变现金额不能大于钱包余额');
amount.value = availableYuan.value > 0 ? String(availableYuan.value.toFixed(2)) : '0';
return;
}
if (!String(smsCode.value || '').trim()) {
sheep.$helper.toast('请输入验证码');
return;
}
const userStore = sheep.$store('user');
if (!userStore?.userInfo?.mobile) {
await userStore?.getInfo?.().catch(() => {});
}
const mobile = userStore?.userInfo?.mobile;
if (!mobile) {
sheep.$helper.toast('未绑定手机号');
return;
}
const money = Math.round(amt * 100);
const payload = { money, mobile, scene: smsScene, code: smsCode.value };
const res = await incrementApi.walletLiquidation(payload).catch(() => ({ code: -1 }));
if (res?.code === 0) {
sheep.$helper.toast('提交成功');
amount.value = '';
smsCode.value = '';
refreshWallet();
}
};
const sendSmsCode = async () => {
if (smsCountdown.value > 0) return;
const amt = amountNumber.value;
if (!amt) {
sheep.$helper.toast('请输入去变现数量');
return;
}
if (amt > availableYuan.value) {
sheep.$helper.toast('变现金额不能大于钱包余额');
return;
}
const userStore = sheep.$store('user');
if (!userStore?.userInfo?.mobile) {
await userStore?.getInfo?.().catch(() => {});
}
const mobile = userStore?.userInfo?.mobile;
if (!mobile) {
sheep.$helper.toast('未绑定手机号');
return;
}
const res = await AuthUtil.sendSmsCode({ mobile, scene: smsScene }).catch(() => ({ code: -1 }));
if (res?.code !== 0) return;
smsCountdown.value = 60;
if (smsTimer) clearInterval(smsTimer);
smsTimer = setInterval(() => {
smsCountdown.value -= 1;
if (smsCountdown.value <= 0) {
clearInterval(smsTimer);
smsTimer = null;
smsCountdown.value = 0;
}
}, 1000);
};
const goRecord = () => {
uni.navigateTo({
url: '/pages/home/liquidationRecord',
});
};
onShow(() => {
refreshWallet();
});
</script>
<style lang="scss" scoped>
.liquidation {
min-height: 100vh;
}
.top_bar {
padding: 22rpx 40rpx 0;
display: flex;
justify-content: flex-end;
}
.record_link {
height: 60rpx;
padding: 0 22rpx;
border-radius: 30rpx;
border: 2rpx solid rgba(0, 0, 0, 0.08);
background: rgba(255, 255, 255, 0.75);
color: #333333;
font-size: 26rpx;
display: flex;
align-items: center;
justify-content: center;
box-sizing: border-box;
}
.container {
padding: 32rpx 40rpx 200rpx;
}
.card {
background: #f4f8f2;
border-radius: 24rpx;
padding: 28rpx;
box-shadow: 0 10rpx 26rpx rgba(0, 0, 0, 0.08);
}
.balance-card {
margin-bottom: 28rpx;
}
.balance-label {
color: #333333;
font-size: 26rpx;
line-height: 36rpx;
}
.balance-value {
margin-top: 12rpx;
color: #111111;
font-size: 72rpx;
font-weight: 700;
line-height: 84rpx;
}
.section-row {
display: flex;
align-items: center;
justify-content: space-between;
padding: 4rpx 4rpx 18rpx;
}
.section-title {
font-size: 28rpx;
color: #111111;
font-weight: 600;
}
.input-card {
padding: 24rpx 28rpx;
}
.code-card {
padding: 18rpx 22rpx;
margin: 20rpx 0;
}
.code-row {
display: flex;
align-items: center;
}
.code-btn {
margin-left: 18rpx;
min-width: 168rpx;
height: 68rpx;
padding: 0 18rpx;
border-radius: 16rpx;
background: rgba(54, 110, 238, 0.12);
color: #366eee;
font-size: 26rpx;
font-weight: 600;
display: flex;
align-items: center;
justify-content: center;
box-sizing: border-box;
white-space: nowrap;
}
.code-btn.disabled {
opacity: 0.6;
}
.amount-row {
display: flex;
align-items: center;
}
.amount-prefix {
color: #111111;
font-size: 40rpx;
font-weight: 600;
margin-right: 14rpx;
}
:deep(.uni-easyinput__content) {
background: transparent !important;
border: none !important;
}
:deep(.uni-easyinput__content-input) {
height: 84rpx !important;
font-size: 64rpx !important;
color: #9a9a9a !important;
font-weight: 600;
}
:deep(.uni-input-input) {
color: #111111 !important;
font-weight: 700;
}
.tip-card {
margin-top: 20rpx;
padding: 28rpx;
}
.tip-header {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 14rpx;
}
.tip-title {
font-size: 26rpx;
color: #111111;
font-weight: 600;
}
.tip-text {
color: #8b8b8b;
font-size: 22rpx;
line-height: 32rpx;
}
.bottom-bar {
position: fixed;
left: 0;
right: 0;
bottom: 0;
padding: 22rpx 32rpx calc(22rpx + env(safe-area-inset-bottom));
background: #ffffff;
}
.confirm-btn {
height: 96rpx;
border-radius: 20rpx;
background: linear-gradient(90deg, #00a87a 0%, #02b06c 100%);
color: #ffffff;
font-size: 34rpx;
font-weight: 700;
display: flex;
align-items: center;
justify-content: center;
}
</style>