加入ws 檢查優惠碼

h888
Wayne Hsu 3 years ago
parent fbc99361c0
commit cf10b86c77

@ -9,3 +9,5 @@ VITE_APP_SSO_URL = 'https://sso.h888.fun/api/v1'
VITE_APP_LINE_CLIENT_ID = '1661025693' VITE_APP_LINE_CLIENT_ID = '1661025693'
VITE_APP_LINE_CLIENT_SECRET = '914cc067cb11ffbd47640de8f3f4a00a' VITE_APP_LINE_CLIENT_SECRET = '914cc067cb11ffbd47640de8f3f4a00a'
VITE_APP_WS_URL = 'ws://108.61.223.162:9501'

@ -7,3 +7,5 @@ VITE_APP_SSO_URL = 'https://sso.h888.fun/api/v1'
VITE_APP_LINE_CLIENT_ID = '1657876696' VITE_APP_LINE_CLIENT_ID = '1657876696'
VITE_APP_LINE_CLIENT_SECRET = '2a7930d6143a00ff421812b942cde200' VITE_APP_LINE_CLIENT_SECRET = '2a7930d6143a00ff421812b942cde200'
VITE_APP_WS_URL = 'ws://108.61.223.162:9501'

@ -9,3 +9,5 @@ VITE_APP_SSO_URL = 'https://sso.slash1000.com/api/v1'
VITE_APP_LINE_CLIENT_ID = '1661025693' VITE_APP_LINE_CLIENT_ID = '1661025693'
VITE_APP_LINE_CLIENT_SECRET = '914cc067cb11ffbd47640de8f3f4a00a' VITE_APP_LINE_CLIENT_SECRET = '914cc067cb11ffbd47640de8f3f4a00a'
VITE_APP_WS_URL = 'ws://167.179.106.36:9501'

@ -208,8 +208,8 @@
</div> </div>
<div class="bonus-desc"> <div class="bonus-desc">
{{ bonus.name }} {{ bonus.name }}
<span v-if="bonus.number > 0" <span v-if="bonus.number > 1"
>數量: {{ bonus.number }} 剩下: {{ bonus.remain }} </span >數量: {{ bonus.number }} 剩下: <span v-html="remainDesc"></span> </span
> >
</div> </div>
</div> </div>
@ -268,7 +268,7 @@
<script setup> <script setup>
import _ from "lodash"; 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 { useCartStore } from "@/store/Cart";
import { useOrderStore } from "@/store/Order"; import { useOrderStore } from "@/store/Order";
import { storeToRefs } from "pinia"; 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 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 cartStore = useCartStore();
const orderStore = useOrderStore(); const orderStore = useOrderStore();
@ -328,12 +353,14 @@ onMounted(async () => {
} }
}); });
const bonus = reactive({ const bonusInit = {
name: "沒有優惠劵", name: "沒有優惠劵",
sn: "", sn: "",
number: 0, number: 0,
remain: 0, remain: 0,
}); }
const bonus = reactive({...bonusInit});
// //
const handleCheckBonusSn = async () => { const handleCheckBonusSn = async () => {
@ -351,11 +378,25 @@ const handleCheckBonusSn = async () => {
} else { } else {
orderStore.discount = res.data; orderStore.discount = res.data;
} }
if(bonus.number > 1){
ws.send(JSON.stringify({
"action":"init",
"payload":bonus.sn,
}));
}
} else { } else {
showToast(res.data); showToast(res.data);
} }
}; };
const remainDesc = computed(() => {
if (bonus.remain > 3) {
return bonus.remain;
} else {
return '<font color=red>'+bonus.remain+'</font>';
}
});
// //
const cartToOrder = () => { const cartToOrder = () => {
if (cartStore.cartItems.length === 0) { if (cartStore.cartItems.length === 0) {
@ -385,7 +426,6 @@ const selectShipping = async (value) => {
switch (value.shipping_code) { switch (value.shipping_code) {
case "shipping": case "shipping":
let resAddr = await getUserDefaultAddress(); let resAddr = await getUserDefaultAddress();
console.log(resAddr.data);
if (resAddr.code === 200) { if (resAddr.code === 200) {
orderStore.shipping = { orderStore.shipping = {
@ -501,6 +541,14 @@ const handleFinish = async () => {
let res = await addOrder(order); let res = await addOrder(order);
if (res.code === 200) { if (res.code === 200) {
if(bonus.sn.length > 0){
ws.send(JSON.stringify({
"action":"use",
"payload":bonus.sn,
}));
ws.close()
}
// //
cartStore.clearCart(); cartStore.clearCart();
orderStore.$reset(); orderStore.$reset();
@ -532,6 +580,10 @@ const detail = computed(() => {
let total_amount = orderStore.total_amount; let total_amount = orderStore.total_amount;
return { sum_amount, discount, shipping_fee, pay_fee, total_amount }; return { sum_amount, discount, shipping_fee, pay_fee, total_amount };
}); });
onUnmounted(()=>{
ws.close()
})
</script> </script>
<style lang="less" scoped> <style lang="less" scoped>

Loading…
Cancel
Save