feat: complete buy and sell function

This commit is contained in:
Rogee
2024-12-13 18:09:10 +08:00
parent b9df73191b
commit 8a48f202f2
3 changed files with 62 additions and 13 deletions

View File

@@ -22,7 +22,7 @@
</template>
<template v-else>
<van-row gutter="20">
<van-row gutter="20" v-if="!pageLoading">
<van-col span="12">
<van-button type="primary" plain round @click="play(item.hash, 'video')" size="large" block>看视频</van-button>
</van-col>
@@ -43,6 +43,7 @@ import { onBeforeUnmount, onMounted } from "vue";
const router = useRouter();
const route = useRoute();
const pageLoading = ref(true)
const item = ref({
title: "加载中...",
@@ -77,6 +78,53 @@ onMounted(() => {
});
});
const buy = () => {
// get use balance
request
.get("/users/info")
.then((res) => {
if (res.data.balance < item.value.price) {
showConfirmDialog({
title: '余额不足',
message: `您的账户余额为 ${res.data.balance} 点,不足以购买此视频。是否前往充值?`,
})
.then(() => {
router.replace({
name: "tab.user",
params: {
tenant: route.params.tenant,
},
});
})
.catch(() => {
return;
});
return;
}
showConfirmDialog({
title: '购买确认',
message: `您的账户余额为 ${res.data.balance} 点,购买此视频将花费 ${item.value.price} 点。是否确认购买?`,
})
.then(() => {
request
.patch(`/medias/${route.params.hash}/checkout`)
.then((res) => {
showSuccessToast('购买成功');
loadMedia(route.params.hash);
})
.catch((err) => {
showFailToast('购买失败: ' + err.response.data.message);
});
})
.catch(() => {
return;
});
});
};
const onClickLeft = () => {
if (route.meta.from?.name) {
router.back();
@@ -91,17 +139,18 @@ const onClickLeft = () => {
};
const loadMedia = (hash) => {
setTimeout(() => {
request
.get(`/medias/${hash}`)
.then((res) => {
console.log(res);
item.value = res.data;
})
.catch((err) => {
console.error("ERROR", err);
});
}, 1000);
request
.get(`/medias/${hash}`)
.then((res) => {
console.log(res);
item.value = res.data;
})
.catch((err) => {
console.error("ERROR", err);
})
.finally(() => {
pageLoading.value = false
});
};
const play = (hash, type) => {