【前端-uniapp-vue】GoeasyManager.js
随便撸的一个GoeasyManger管理工具,不算太完善,累了
import GoEasy from 'goeasy'
import Timers from './timer.js';
import request from '@/plugins/axios'
import Event from './event.js';
let GoEasyManager = {
goeasy:null,
im:null,
pubsub:null,
userinfo:{},
userType:'kefu',
tipShowDuration:5000,
tipMsgTime:15000,
init:function(){
this.goeasy = GoEasy.getInstance({
host:"hangzhou.goeasy.io", //若是新加坡区域:singapore.goeasy.io
appkey:"",
modules:['im','pubsub'],//根据需要,传入'im’或'pubusub',或数组方式同时传入
allowNotification: true
});
this.im = this.goeasy.im;
this.pubsub = this.goeasy.pubsub;
},
connect:function(){
let userinfo = this.userinfo;
if(userinfo.user_id && this.goeasy.getConnectionStatus() === 'disconnected'){
this.goeasy.connect({
id: `${this.userType}_${userinfo.user_id}`, //im必填,最大长度60字符
data: {
"avatar": userinfo.avatar,
"nickname": userinfo.nickname
}, //必须是一个对象,im必填,最大长度300字符,显示在会话列表中
onSuccess: ()=> { //连接成功
console.log("线上聊天系统连接成功") //连接成功
Event.$emit("connected");
this.online_init();//初始化上线
},
onFailed: function (error) { //连接失败
console.log("Failed to connect GoEasy, code:" + error.code + ",error:" + error
.content);
},
onProgress: function (attempts) { //连接或自动重连中
console.log("GoEasy is connecting", attempts);
}
});
}
},
disconnect:function(){
if(this.goeasy.getConnectionStatus() === 'connected'){
this.goeasy.disconnect({
onSuccess: ()=> { //断开成功
console.log("线上聊天系统已断开") //连接成功
//注销会话更新监听事件
this.im.off(GoEasy.IM_EVENT.CONVERSATIONS_UPDATED, this.unReadCount);
}
});
}
},
online_init:function(){
this.pubsub.subscribe({
channel: "kefu",//替换为您自己的channel
presence: {
enable: true
},
onMessage: function (message) {
//收到消息
},
onSuccess: function () {
console.log("用户已上线");
},
onFailed: function (error) {
console.log("Channel订阅失败, 错误编码:" + error.code + " 错误信息:" + error.content)
}
});
},
onMessage:function(callback){
this.im.off(GoEasy.IM_EVENT.PRIVATE_MESSAGE_RECEIVED, callback);
this.im.on(GoEasy.IM_EVENT.PRIVATE_MESSAGE_RECEIVED, callback);
},
onThreadUpdate:function(callback){
//添加会话更新监听事件
this.im.off(GoEasy.IM_EVENT.CONVERSATIONS_UPDATED, callback);
this.im.on(GoEasy.IM_EVENT.CONVERSATIONS_UPDATED, callback);
},
sendMsg:function(to,type,content,datas={},msgType=GoEasy.IM_SCENE.PRIVATE){
let content_text = {
"1":content,
"2":"【图片消息】",
"3":"【链接消息】"
}
let msg = {
"type": "private",
"content": {
"type": type,
"to": {
"id": to,
"data": {
"avatar": this.userinfo.avatar,
"nickname": this.userinfo.nickname
}
},
"payload": { "txt": content, ...datas},
"notification" :{
title:"您有新的消息请查看",
body:content_text[type]
}
}
}
let customMessage = this.im.createCustomMessage(msg.content);
this.im.sendMessage({
message: customMessage,
onSuccess: (message)=>{ //发送成功
console.log("发送消息成功", message);
//清空消息时钟短信提醒
console.log("未读消息时钟",Timers.msgTipTimer);
if(Timers.msgTipTimer[to])clearTimeout(Timers.msgTipTimer[toid]);//防止消息重复发送
//15s后发送消息
Timers.msgTipTimer[to] = setTimeout(()=>{
request.post("服务器自己的未读提醒接口",{user_id:to,scene:"MSGTIP"}).then((res)=>{
console.log("消息未读提醒发送",res)
});
}, this.tipMsgTime);
},
onFailed: function (error) { //发送失败
console.log('Failed to send private message,code:', error);
}
});
}
}
GoEasyManager.init();
export default GoEasyManager;

浙公网安备 33010602011771号