165 lines
3.6 KiB
JavaScript
165 lines
3.6 KiB
JavaScript
import $store from '@/store/index.js';
|
||
import {
|
||
registerList,
|
||
} from '@/api/consult.js'
|
||
class common {
|
||
constructor(arg) {
|
||
this.timer = null
|
||
this.url = arg.url
|
||
this.isOnline = false //是否在线
|
||
this.socket = null //socket对象
|
||
this.reconnectTime = 0
|
||
this.isOpenReconnect = true //是否重连
|
||
this.loadFinish = false;
|
||
// 获取当前用户相关信息
|
||
let user = uni.getStorageSync('userInfo')
|
||
this.user_token = uni.getStorageSync('token')
|
||
this.user = user ? user : {}
|
||
// 初始化聊天对象
|
||
this.TO = false;
|
||
// 连接和监听
|
||
if (this.user) {
|
||
this.connectSocket()
|
||
}
|
||
}
|
||
// 断线重连
|
||
reconnect() {
|
||
console.log(this.isOnline)
|
||
console.log("进入reconnect准备重新链接")
|
||
if (this.isOnline) {
|
||
return
|
||
}
|
||
if (this.reconnectTime >= 3) {
|
||
return this.reconnectConfirm()
|
||
}
|
||
this.reconnectTime += 1
|
||
console.log("重新链接")
|
||
// this.connectSocket();
|
||
|
||
}
|
||
// 连接socket
|
||
connectSocket() {
|
||
console.log("链接connectSocket")
|
||
this.socket = uni.connectSocket({
|
||
url: this.url,
|
||
header: {
|
||
authorization: `Bearer ${uni.getStorageSync('token')}`
|
||
},
|
||
method: 'POST',
|
||
complete: () => {}
|
||
})
|
||
// 监听连接成功
|
||
this.socket.onOpen && this.socket.onOpen(() => this.onOpen())
|
||
// 监听接收信息
|
||
this.socket.onMessage && this.socket.onMessage((res) => this.onMessage(res))
|
||
// 监听断开
|
||
this.socket.onClose && this.socket.onClose((e) => this.onClose(e))
|
||
// 监听错误
|
||
this.socket.onError && this.socket.onError(() => this.onError())
|
||
}
|
||
onOpen() {
|
||
// 用户上线
|
||
console.log('websocket连接成功')
|
||
this.isOnline = true
|
||
this.isOpenReconnect = true
|
||
console.log(this.url, 'doctor');
|
||
console.log(this.user, 'user')
|
||
// 获取用户离线消息
|
||
if (this.socket != null) {
|
||
var login = {
|
||
cmd: 'login',
|
||
userId: Number(this.user.su_id),
|
||
imStatus: Number(this.user.im_status),
|
||
userType: Number(this.user.role),
|
||
data: ''
|
||
};
|
||
this.initSocketLogin(login);
|
||
}
|
||
}
|
||
// 监听关闭
|
||
onClose(e) {
|
||
// 用户下线
|
||
this.isOnline = false
|
||
this.socket = null
|
||
console.log('socket连接已关闭:' + e)
|
||
if (this.isOpenReconnect) {
|
||
var that = this;
|
||
setTimeout(function() {
|
||
that.reconnect();
|
||
}, 3000);
|
||
}
|
||
}
|
||
// 监听连接错误
|
||
onError() {
|
||
// 用户下线
|
||
this.isOnline = false
|
||
this.socket = null
|
||
console.log('socket连接错误:' + e)
|
||
if (!this.socket) {
|
||
var that = this;
|
||
setTimeout(function() {
|
||
that.reconnect();
|
||
}, 3000);
|
||
}
|
||
}
|
||
// 监听接收消息
|
||
onMessage(data) {
|
||
console.log('监听接收消息:' + data);
|
||
// let res = JSON.parse(data.data)
|
||
// uni.$emit('onMessage', res)
|
||
}
|
||
// 关闭连接
|
||
close() {
|
||
if (this.socket) {
|
||
this.socket.close()
|
||
this.isOpenReconnect = false
|
||
} else if (uni.onSocketClose) {
|
||
uni.onSocketClose()
|
||
this.isOpenReconnect = false
|
||
}
|
||
this.destoryChatObject();
|
||
}
|
||
initSocketLogin(login) {
|
||
this.socket.send({
|
||
data: JSON.stringify(login),
|
||
success() {
|
||
console.log('用户IM登录成功:success')
|
||
},
|
||
fail(e) {
|
||
console.log('用户IM登录失败fail:' + e)
|
||
}
|
||
})
|
||
// setTimeout(() => {
|
||
// this.socket.send({
|
||
// data: JSON.stringify({
|
||
// data: '发送内容是:测试测试测试',
|
||
// }),
|
||
// })
|
||
// }, 4000)
|
||
}
|
||
// 创建聊天对象
|
||
createChatObject(detail) {
|
||
this.TO = detail
|
||
}
|
||
// 销毁聊天对象
|
||
destoryChatObject() {
|
||
this.TO = false
|
||
}
|
||
// 断线重连提示
|
||
reconnectConfirm() {
|
||
// this.close()
|
||
// this.connectSocket()
|
||
// this.reconnectTime = 0
|
||
}
|
||
// 验证是否上线
|
||
checkOnline() {
|
||
if (!this.isOnline) {
|
||
// 断线重连提示
|
||
this.reconnectConfirm()
|
||
return false
|
||
}
|
||
return true
|
||
}
|
||
}
|
||
export default common
|