iframe.vue 3.07 KB
<template>
  <div class="iframe-wrapper">
    <h2>客户端</h2>
    <el-form size="mini" :model="orderInfo" style="width:200px">
      <el-form-item label="申请人姓名">
        <el-input v-model="orderInfo.appUserName"></el-input>
      </el-form-item>
      <el-form-item label="申请人手机号">
        <el-input v-model="orderInfo.appUserPhone"></el-input>
      </el-form-item>
      <el-form-item label="订单号">
        <el-input v-model="orderInfo.orderId"></el-input>
      </el-form-item>
      <el-form-item label="业务员工号">
        <el-input v-model="orderInfo.webUserName"></el-input>
      </el-form-item>
    </el-form>
    <p>
      <el-button type="primary" @click="open">呼叫</el-button>
      <el-button type="primary" @click="cancelCall">取消呼叫</el-button>
    </p>
    <iframe
      v-if="isStart"
      ref="srtcIframe"
      title="srtcIframe"
      :src="target"
      frameborder="2"
      allow="microphone;camera;midi;encrypted-media;fullscreen;"
    ></iframe>
  </div>
</template>

<script>
export default {
  data() {
    return {
      baseUrl: 'https://pre-web-chery.situdata.com',
      isStart: false,
      target: '',
      strWindowFeatures: `
        left=2400,
        top=200
        height=500,
        width=700,
        menubar=1,
        toolbar=1,
        fullscreen=1
        `,
      orderInfo: {
        appUserName: '刘臻',
        appUserPhone: '16621043587',
        orderId: '9527',
        webUserName: 'lz'
      }
    }
  },
  beforeMount() {
    window.addEventListener('message', this.receiveMessage, false)
  },
  methods: {
    open() {
      this.isStart = true
      this.target = `${this.baseUrl}/#/srtc-iframe?path="https://localhost:8080"`
      this.$nextTick().then(() => {
        this.$refs.srtcIframe.onload = () => {
          this.sendMessage()
        }
      })
    },
    sendMessage() {
      const { appUserName, appUserPhone, orderId, webUserName } = this.orderInfo
      this.$refs.srtcIframe.contentWindow.postMessage(
        {
          type: 'startCall',
          data: {
            appUserName,
            appUserPhone,
            orderId,
            webUserName
          }
        },
        this.baseUrl
      )
    },
    cancelCall() {
      this.$refs.srtcIframe.contentWindow.postMessage(
        {
          type: 'cancelCall'
        },
        this.baseUrl
      )
    },
    receiveMessage(e) {
      if (e.origin !== this.baseUrl) {
        return
      }
      const {
        data: { data, type }
      } = e
      switch (type) {
        case 'appHangUp':
          this.isStart = false
          break
        case 'cancelCallCB':
          this.isStart = false
          break
        case 'webHangUp':
          this.isStart = false
          break
      }
      this.popMessage(`${type}---${data}`)
    },
    popMessage(data) {
      this.$message({
        type: 'error',
        message: data
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.iframe-wrapper {
  text-align: left;
  padding: 40px;
}
iframe {
  width: 500px;
  height: 600px;
  position: fixed;
  top: 0;
  right: 0;
}
</style>