<template>
  <view>
    <web-view :src="url"></web-view>
  </view>
</template>

<script>
import { go2, back } from '@/utils/hook.js'
import { makeSocket } from '@/utils/index.js'
export default {
  data() {
    return {
      url: '',
      socket: null,
      params: null
    }
  },
  onLoad(option) {
    if (option) {
      // this.url = decodeURIComponent(option.url)
      this.url = option.url
      this.params = option.params ? JSON.parse(option.params) : ''
      console.log('params', option.params)
    } else {
      uni.showToast({
        title: '参数错误',
        icon: 'none',
        complete: () => {
          setTimeout(() => {
            back()
          }, 1000)
        }
      })
    }
  },
  onShow() {
    this.initSocket()
  },
  onHide() {
    console.log('onHide')
    this.destroySocket()
  },
  onUnload() {
    console.log('onUnload')
    this.destroySocket()
  },
  methods: {
    initSocket() {
      makeSocket({ pageInfo: 'page-view', retry: true }).then((res) => {
        this.destroySocket()
        this.socket = res
        this.socket.onMessage(this.getMessage)
      })
    },
    /**
     * @param {Object} data {data:{},type:''}
     */
    getMessage(data) {
      // 供应商担保协议回调
      if (data.type === 'guaranteeSuccess') {
        go2('fs-credit', { ...this.params }, true)
      }
      // 订单合同签署成功
      if (data.type == 'zzgSupplierTCSignSuccess') {
        go2('order-detail', { orderId: data.data.orderId }, true)
      }
      // 签约成功
      if (data.type == 'signSuccess') {
        // go2('paper-mall-order-detail', { id: data.orderId }, true)
      }
      // 实名认证成功
      if (data.type == 'certificatedSuccess') {
        back()
      }
    },
    destroySocket() {
      if (this.socket) {
        this.socket.close()
        this.socket = null
      }
    }
  }
}
</script>
