module.exports = {
// websocket实例
ws: null,
// 是否重连标记
lockReconnect: false,
// 重连次数
reconnectCount: 0,
// 重连定时
reconnectTimeout: null,
// 响应事件列表
callBackMap: new Map(),
// 呼叫信息
callInfo: {},
// IVR随路数据
IvrData: {},
// 配置信息
config: {},
// 外呼标记
callOutFlag: false,
// 质检标记
qualityTest: false,
// 求助标记
consultFlag: false,
// 排队信息获取定时器
queueTimer: null,
// 排队定时时长
queueTimerTime: 2000,
// 验密方法
checkMessageFun: null,
//外呼信息
outentity: null,
called: '',
// 心跳检测包
startHeartPack: {
// 心跳检测间隔
timeout: 30000,
timeoutObj: null,
serverTimeoutObj: null,
// 重置
reset: function () {
clearTimeout(this.timeoutObj)
clearTimeout(this.serverTimeoutObj)
return this
},
// 开始
start: function (ws) {
console.log('【心跳开始】')
const self = this
this.timeoutObj && clearTimeout(this.timeoutObj)
this.serverTimeoutObj && clearTimeout(this.serverTimeoutObj)
this.timeoutObj = setTimeout(function () {
// 这里发送一个心跳,后端收到后,返回一个心跳消息,
// onmessage拿到返回的心跳就说明连接正常
ws.send(JSON.stringify({
msgType: 'heartbeat',
subType: 'heartbeat',
version: 'V1',
sequenceNo: '0000',
msgContent: {}
}))
// 超过超时时长未应答,自动关闭连接
self.serverTimeoutObj = setTimeout(function () {
ws.close()
}, self.timeout)
}, this.timeout)
}
},
// 重连
reconnect() {
// 判断是否锁定重连
if (this.lockReconnect) {//不需要重连地址链接异常,弹出信任证书页面
var url = 'https://' + this.config.serverAddress + '/agentgateway/resource/health.jsp'
var newWindow = window.open('about:blank')
newWindow.location.href = url
return
}
this.lockReconnect = true
// 如果重连次数大于10次,停止重连
if (this.reconnectCount > 10) {
return
}
// 没连接上会一直重连,设置延迟避免请求过多
this.reconnectCount++
this.reconnectTimeout && clearTimeout(this.reconnectTimeout)
this.reconnectTimeout = setTimeout(() => {
// 重连完毕后执行重置坐席信息操作
this.init(this.config).then(() => {
this.syncagentinfo()
})
this.lockReconnect = false
}, 5000)
},
// 初始化
init(params) {
return new Promise((resolve, reject) => {
this.config = params
// this.$api.userManage.roleManage.queryParamPage({
// length: 5,
// paramAbbr: "CTI_SERVER",
// start: 0
// }).then((res) => {
// console.log('坐席信息', res.data.data[0] )
// this.config.serverAddress = res.data.data[0].paramValue
// resolve()
// })
// 'wss://172.1.2.174:8043'
// this.config.serverAddress = 'wss://11.19.104.24:8043'
const host = window.location.hostname
if (host === '11.11.36.104' || host === '11.11.36.105') {
this.config.serverAddress = '11.11.36.145:8043'
} else {
this.config.serverAddress = '11.19.102.248:8043'
}
resolve()
// host: '172.16.4.191',
// port: '8043',
})
},
// 初始化 连接
initWebSocket(fun) {
return new Promise((resolve, reject) => {
// 判断当前是否有
if ('WebSocket' in window) {
const url = 'wss://' + this.config.serverAddress + '/agentgateway/ccgateway/agent/' + this.config.workNo
console.log('【链接地址】', url)
this.ws = new WebSocket(url)
this.ws.onopen = () => {
console.log('【连接开启】')
this.startHeartPack.reset().start(this.ws)
resolve()
}
// 绑定消息监听事件
this.ws.onmessage = (event) => {
const message = JSON.parse(event.data)
const msgType = message.msgType
const msgContent = message.msgContent
// 筛选消息,根据不同的消息类型,处理消息
switch (msgType) {
case 'operation':
console.log('【操作类消息】', msgContent)
break
case 'response':
// console.log('【响应类消息】', msgContent)
if (message.subType == 'heartbeat') {
// 如果是心跳检测响应,则重新开始心跳流程
this.startHeartPack.reset().start(this.ws)
} else if (message.subType == 'onlineagent/syncagentinfo' && msgContent.retcode == 0) {
// 如果当前为重置坐席信息,则将所有的信息初始化
this.startHeartPack.reset().start(this.ws)
// 重连次数置0
this.reconnectCount = 0
// 清除重连定时
this.reconnectTimeout && clearTimeout(this.reconnectTimeout)
} else {
const callback = this.callBackMap.get(message.sequenceNo)
callback(msgContent)
}
break
case 'event':
this.handleEvent(msgContent)
break
case 'error':
console.log('【错误信息】', msgContent)
break
}
}
// 连接出错
this.ws.onerror = (err) => {
console.log('【链接出错】', err)
// 连接出错,执行重连操作
this.reconnect()
}
// 连接关闭
this.ws.onclose = (err) => {
console.log('【链接关闭】', err)
this.onLogoffSucc()
clearInterval(this.queueTimer)
this.$store.commit('softPhone/resetQueueNumber', 0)
// 刷新
this.startHeartPack.reset()
// 如果关闭原因不是正常关闭,执行重连操作
if (err.reason != 'normal_close') {
this.reconnect()
}
}
} else {
reject('Not support websocket')
}
})
},
// 同步坐席信息
syncagentinfo() {
this.sendOperationMesssage({
$subType: 'onlineagent/syncagentinfo',
$entity: { 'password': this.config.password },
$callback: function (entity) {
const retcode = entity.retcode
switch (retcode) {
case '0':
console.log('【同步坐席信息成功】')
break
default:
console.log('【同步坐席信息失败】, Retcode : ' + 3 + '. RetMessage:' + entity.message)
break
}
}
})
},
// 生成序列号
getRandomCode(length) {
return Number(Math.random().toString().substr(3, length) + Date.now()).toString(36)
},
// 发送 "操作类" 消息
sendOperationMesssage(param) {
if (this.ws.readyState != 1) {
console.log('【】',this.ws.readyState)
return
}
const sequenceNo = this.getRandomCode(32)
// 在回调方法体内进行存储
this.callBackMap.set(sequenceNo, param.$callback)
const msgbody = {
msgType: 'operation',
subType: param.$subType,
version: 'V1',
sequenceNo: sequenceNo,
msgContent: param.$entity ? param.$entity : {}
}
// console.log('【即将发送消息】', JSON.stringify(msgbody))
this.ws.send(JSON.stringify(msgbody))
},
// 签入
loginFn(params) {
const userInfo = JSON.parse(sessionStorage.getItem('sunyard_user_info'))
// const autoAnswer = this.$store.state.soft