// import axios from "axios";
|
import useWebSockStore from "@/store/modules/websock";
|
|
import { WebSockCode, useType } from "./index";
|
export default useType;
|
/**
|
*webSockHelper 辅助类
|
*/
|
export class webSockHelper extends WebSockCode {
|
useSockStore = useWebSockStore();
|
constructor(_useType: useType, url: string, userId?: string) {
|
super(url, _useType, userId || "", "");
|
this.useSockStore.webSock = this.settingLong();
|
this.onMessage = this.onMessageWebsock;
|
}
|
/**
|
* 初始化连接
|
*/
|
init = (_useType?: useType, fnc?: () => void) => {
|
if (!this.useSockStore.webSock || _useType == useType.WEB) {
|
this.createdWebSock(this.jointUrl());
|
this.onMessage = this.onMessageWebsock;
|
this.useSockStore.webSock = this.webSock;
|
if (fnc != null) {
|
this.updataSessions();
|
fnc();
|
}
|
}
|
if (_useType == useType.LCON) {
|
// console.log("创建长连接");
|
this.useSockStore.webSock = this.settingLong();
|
}
|
return this.useSockStore.webSock;
|
};
|
|
private onMessageWebsock = (e?: any) => {
|
let data = JSON.parse(e.data);
|
// console.log("sdasdsd", data);
|
if (data.id == this.useSockStore.sessionObj.id && this.useSockStore.isopen) {
|
this.getMsgList(1);
|
} else {
|
// console.log("更细未读数量", data);
|
let index = this.useSockStore.sessionList.findIndex((f) => f.id == data.id); //更新会话对象
|
// console.log("更细未读数量1", index);
|
if (index > -1) {
|
this.useSockStore.sessionList[index].unReadCount = data.unReadCount;
|
this.useSockStore.updateUnread();
|
} else {
|
this.updataSessions();
|
}
|
}
|
// return data;
|
};
|
/**
|
* 是否可以加载数据
|
*/
|
private isLoadData = (type = 0) => {
|
// console.log("type=======>", type);
|
if (type == 1) {
|
return true;
|
}
|
//当前会话不能为空
|
if (!this.useSockStore.sessionObj.toUserId || !this.useSockStore.sessionObj.userId) {
|
return false;
|
}
|
if (this.pageNum > 1) {
|
if (this.useSockStore.msgList.length == this.total) {
|
this.pageNum = 1;
|
return false;
|
}
|
}
|
return true;
|
};
|
|
/**
|
* 加载消息记录
|
* @param type 1 推送
|
* @returns
|
*/
|
public getMsgList = (type = 0) => {
|
return new Promise<void>(async (resolve, reject) => {
|
if (this.isLoadData(type)) {
|
this.getMessages(type).then((res: any[]) => {
|
let _rows = res;
|
if (type == 0) {
|
let list = _rows!.reverse();
|
if (this.pageNum > 1 && list.length > 0) {
|
this.useSockStore.msgList = list.concat(this.useSockStore.msgList); //
|
} else {
|
this.useSockStore.msgList = list;
|
this.toScrollBottom();
|
}
|
} else {
|
//获取推送的消息
|
if (_rows.length > 0) {
|
this.useSockStore.msgList.push(_rows[0]);
|
this.toScrollBottom();
|
}
|
}
|
if (_rows.length > 0 && this.pageNum == 1) {
|
this.updataSession();
|
}
|
resolve();
|
});
|
} else {
|
reject("加载异常");
|
}
|
});
|
};
|
|
/**
|
* 更细会话未读数量
|
* @returns
|
*/
|
public updataSession = () => {
|
return new Promise<any>(async (resolve, reject) => {
|
this.getSessionInfo()
|
.then((data: any) => {
|
this.updataUnReadCount(data);
|
resolve(data);
|
})
|
.catch((e) => {
|
reject(e);
|
});
|
});
|
};
|
/**
|
* 更新未读数量角标
|
*/
|
private updataUnReadCount = (data: any) => {
|
let index = this.useSockStore.sessionList.findIndex((f) => f.id == data.id);
|
if (index > -1) this.useSockStore.sessionList[index].unReadCount = data.unReadCount;
|
this.useSockStore.updateUnread();
|
};
|
|
/**
|
* 发送消息
|
*/
|
public sendMsg = (content: string) => {
|
return new Promise<void>(async (resolve, reject) => {
|
this.send(content)
|
.then((msgobj: any) => {
|
this.useSockStore.msgList.push(msgobj);
|
this.toScrollBottom();
|
resolve();
|
})
|
.catch((e) => {
|
reject(e);
|
});
|
});
|
};
|
|
/**
|
* 关闭连接
|
*/
|
public breakConnect = () => {
|
this.offConcat().then(() => {
|
this.useSockStore.webSock = undefined;
|
});
|
};
|
|
/**
|
* 更新消息数据
|
*/
|
public updataMessages = () => {
|
this.getMsgList().then(() => {
|
this.mountNextPage(this.getMsgList);
|
});
|
// return this.getMsgList();
|
};
|
|
/**
|
* 加载会话列表
|
*/
|
public updataSessions = () => {
|
return new Promise<any>(async (resolve, reject) => {
|
this.getSessions()
|
.then((res: any[]) => {
|
this.useSockStore.sessionList = res;
|
this.useSockStore.updateUnread();
|
resolve(res);
|
})
|
.catch((e) => {
|
reject(e);
|
});
|
});
|
};
|
}
|