|
|
|
|
@ -208,8 +208,8 @@
|
|
|
|
|
</div>
|
|
|
|
|
<div class="bonus-desc">
|
|
|
|
|
{{ bonus.name }}
|
|
|
|
|
<span v-if="bonus.number > 0"
|
|
|
|
|
>數量: {{ bonus.number }} 剩下: {{ bonus.remain }} 張</span
|
|
|
|
|
<span v-if="bonus.number > 1"
|
|
|
|
|
>數量: {{ bonus.number }} 剩下: <span v-html="remainDesc"></span> 張</span
|
|
|
|
|
>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
@ -268,7 +268,7 @@
|
|
|
|
|
|
|
|
|
|
<script setup>
|
|
|
|
|
import _ from "lodash";
|
|
|
|
|
import { ref, reactive, onMounted, computed, watch } from "vue";
|
|
|
|
|
import { ref, reactive, onMounted,onUnmounted, computed, watch } from "vue";
|
|
|
|
|
import { useCartStore } from "@/store/Cart";
|
|
|
|
|
import { useOrderStore } from "@/store/Order";
|
|
|
|
|
import { storeToRefs } from "pinia";
|
|
|
|
|
@ -290,6 +290,31 @@ import mastericon from "/src/assets/icons/png/mastercard1.png";
|
|
|
|
|
|
|
|
|
|
const img_url = import.meta.env.VITE_APP_IMG_URL
|
|
|
|
|
|
|
|
|
|
// 建構websocket
|
|
|
|
|
const ws = new WebSocket(import.meta.env.VITE_APP_WS_URL);
|
|
|
|
|
|
|
|
|
|
ws.onopen = function(evt) {
|
|
|
|
|
console.log("Connection open ...");
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ws.onmessage = function(evt) {
|
|
|
|
|
console.log( "Received Message: " + evt.data);
|
|
|
|
|
let data = JSON.parse(evt.data);
|
|
|
|
|
if(data.action === 'use'){
|
|
|
|
|
console.log('use')
|
|
|
|
|
bonus.remain -= 1;
|
|
|
|
|
if(bonus.remain === 0){
|
|
|
|
|
showToast('優惠券已被搶完');
|
|
|
|
|
Object.assign(bonus, bonusInit);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ws.onclose = function(evt) {
|
|
|
|
|
console.log("Connection closed.");
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const cartStore = useCartStore();
|
|
|
|
|
const orderStore = useOrderStore();
|
|
|
|
|
|
|
|
|
|
@ -328,12 +353,14 @@ onMounted(async () => {
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const bonus = reactive({
|
|
|
|
|
const bonusInit = {
|
|
|
|
|
name: "沒有優惠劵",
|
|
|
|
|
sn: "",
|
|
|
|
|
number: 0,
|
|
|
|
|
remain: 0,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const bonus = reactive({...bonusInit});
|
|
|
|
|
//檢查優惠代碼
|
|
|
|
|
|
|
|
|
|
const handleCheckBonusSn = async () => {
|
|
|
|
|
@ -351,11 +378,25 @@ const handleCheckBonusSn = async () => {
|
|
|
|
|
} else {
|
|
|
|
|
orderStore.discount = res.data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(bonus.number > 1){
|
|
|
|
|
ws.send(JSON.stringify({
|
|
|
|
|
"action":"init",
|
|
|
|
|
"payload":bonus.sn,
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
showToast(res.data);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const remainDesc = computed(() => {
|
|
|
|
|
if (bonus.remain > 3) {
|
|
|
|
|
return bonus.remain;
|
|
|
|
|
} else {
|
|
|
|
|
return '<font color=red>'+bonus.remain+'</font>';
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
//將購物車商品資料轉換成訂單商品資料
|
|
|
|
|
const cartToOrder = () => {
|
|
|
|
|
if (cartStore.cartItems.length === 0) {
|
|
|
|
|
@ -385,7 +426,6 @@ const selectShipping = async (value) => {
|
|
|
|
|
switch (value.shipping_code) {
|
|
|
|
|
case "shipping":
|
|
|
|
|
let resAddr = await getUserDefaultAddress();
|
|
|
|
|
console.log(resAddr.data);
|
|
|
|
|
|
|
|
|
|
if (resAddr.code === 200) {
|
|
|
|
|
orderStore.shipping = {
|
|
|
|
|
@ -501,6 +541,14 @@ const handleFinish = async () => {
|
|
|
|
|
let res = await addOrder(order);
|
|
|
|
|
|
|
|
|
|
if (res.code === 200) {
|
|
|
|
|
if(bonus.sn.length > 0){
|
|
|
|
|
ws.send(JSON.stringify({
|
|
|
|
|
"action":"use",
|
|
|
|
|
"payload":bonus.sn,
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
ws.close()
|
|
|
|
|
}
|
|
|
|
|
// 清空購物車
|
|
|
|
|
cartStore.clearCart();
|
|
|
|
|
orderStore.$reset();
|
|
|
|
|
@ -532,6 +580,10 @@ const detail = computed(() => {
|
|
|
|
|
let total_amount = orderStore.total_amount;
|
|
|
|
|
return { sum_amount, discount, shipping_fee, pay_fee, total_amount };
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
onUnmounted(()=>{
|
|
|
|
|
ws.close()
|
|
|
|
|
})
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<style lang="less" scoped>
|
|
|
|
|
|