<template>
  <view class="wh-full">
    <web-view :src="payUrl"></web-view>
  </view>
</template>

<script lang="ts" setup>
import { onLoad } from "@dcloudio/uni-app";
import { useWebSocket } from '@/utils/webSocket/webSocket'
import { useUserStore } from "@/store/modules/user";
const { getUser } = useUserStore();

const payUrl = ref("");
const ws = useWebSocket(`/zhxy/webSocket/${getUser.userId}`, (type: string, res: any) => {
  console.log('收到WebSocket消息:', type, res.data);
  // 将data从字符串转为对象
  const dataObj = JSON.parse(res.data);
  if (dataObj.action === 'pay') {
    uni.showToast({
      title: '支付成功',
      icon: 'success',
    });
    ws.closeConnect();
    // 跳转到支付成功页面
    setTimeout(() => {
      uni.reLaunch({
        url: "/pages/base/course-selection/payment-success",
      });
    }, 1000)
  }
});
onLoad((options: any) => {
  if (options.payUrl) {
    payUrl.value = decodeURIComponent(options.payUrl);
    ws.reconnect();
  } else {
    uni.showToast({ title: '缺少支付地址', icon: 'none' })
    setTimeout(() => {
      uni.navigateBack()
    }, 1000)
  }
});

onBeforeUnmount(() => {
  ws.closeConnect();
});
</script>

<style lang="scss" scoped></style>
