// var agent, password, mode, phoneId //登录所需参数 // window.onload = function () { // init(); // }; //初始化电话条 function init() { var iframe = document.createElement('iframe'); iframe.name = "uinCall"; iframe.id = "uinCall"; iframe.ref = "uinCall"; iframe.allow = "microphone"; iframe.src = "https://ykf.uincall.com/telephonebarplugin/index.html"; // iframe.src = "https://ceshi.uincall.com/telephonebarplugin/index.html"; var telephoneBar = document.getElementById("telephoneBar"); if (telephoneBar) { telephoneBar.appendChild(iframe); var uinCall = document.getElementById("uinCall"); console.log('@@@-', uinCall); iframe.style.width = telephoneBar.getAttribute("data-width"); iframe.style.height = telephoneBar.getAttribute("data-height"); iframe.style.border = 0; } } //打印来自电话条内的抛出事件 window.addEventListener('message', function (e) { // console.log('@EchoData','message>>',e) console.info(e); if (e.data) { try { var resData = JSON.parse(e.data); // 动态修改iframe的高度 if (resData && resData.key && resData.key === 'changeCallHistoryState') { var telephoneBar = document.getElementById("telephoneBar"); var uinCall = document.getElementById("uinCall"); var frameHeight = telephoneBar.getAttribute("data-height"); if (resData.data) { // 显示 var heightConfig = parseInt(frameHeight); uinCall.style.height = (!heightConfig || heightConfig < 310) ? '310px' : heightConfig + 'px'; } else { uinCall.style.height = frameHeight; } } } catch (err) { console.info(e); } } }); //高级话务 function telephoneFunction(data) { let uincall = document.getElementById('uinCall'); if (uincall) { uincall.contentWindow.postMessage(data, '*'); } else { init(); uincall = document.getElementById('uinCall'); if (uincall) { setTimeout(() => { uincall.contentWindow.postMessage(data, '*'); }, 500); } } } 用vue优化代码编程
时间: 2024-04-22 12:25:17 浏览: 17
你可以使用Vue来优化你的代码编程。首先,你需要将你的代码包装在一个Vue实例中。你可以在Vue的`created`生命周期钩子中初始化电话条,并在`message`事件监听器中处理来自电话条的抛出事件。然后,你可以在Vue实例的方法中调用`telephoneFunction`来发送消息给电话条。
下面是一个示例代码:
```javascript
new Vue({
el: '#app',
created() {
this.init();
},
methods: {
init() {
var iframe = document.createElement('iframe');
iframe.name = "uinCall";
iframe.id = "uinCall";
iframe.ref = "uinCall";
iframe.allow = "microphone";
iframe.src = "https://ykf.uincall.com/telephonebarplugin/index.html";
var telephoneBar = document.getElementById("telephoneBar");
if (telephoneBar) {
telephoneBar.appendChild(iframe);
var uinCall = document.getElementById("uinCall");
console.log('@@@-', uinCall);
iframe.style.width = telephoneBar.getAttribute("data-width");
iframe.style.height = telephoneBar.getAttribute("data-height");
iframe.style.border = 0;
}
},
handleMessage(e) {
console.info(e);
if (e.data) {
try {
var resData = JSON.parse(e.data);
if (resData && resData.key && resData.key === 'changeCallHistoryState') {
var telephoneBar = document.getElementById("telephoneBar");
var uinCall = document.getElementById("uinCall");
var frameHeight = telephoneBar.getAttribute("data-height");
if (resData.data) {
var heightConfig = parseInt(frameHeight);
uinCall.style.height = (!heightConfig || heightConfig < 310) ? '310px' : heightConfig + 'px';
} else {
uinCall.style.height = frameHeight;
}
}
} catch (err) {
console.info(e);
}
}
},
telephoneFunction(data) {
let uincall = document.getElementById('uinCall');
if (uincall) {
uincall.contentWindow.postMessage(data, '*');
} else {
this.init();
uincall = document.getElementById('uinCall');
if (uincall) {
setTimeout(() => {
uincall.contentWindow.postMessage(data, '*');
}, 500);
}
}
}
},
mounted() {
window.addEventListener('message', this.handleMessage);
},
beforeDestroy() {
window.removeEventListener('message', this.handleMessage);
}
});
```
在这个示例中,我们将Vue实例绑定到`#app`元素上,并在`created`生命周期钩子中初始化电话条。`handleMessage`方法用于处理来自电话条的抛出事件。`telephoneFunction`方法用于发送消息给电话条。在`mounted`钩子中添加了`message`事件监听器,在`beforeDestroy`钩子中移除了事件监听器,以确保在组件销毁之前清理事件监听器。