This commit is contained in:
2021-04-07 14:32:34 +08:00
parent 3ec8d96fff
commit dfdf73fdfc
5 changed files with 235 additions and 186 deletions

31
app.js
View File

@@ -1,16 +1,29 @@
// app.js
import ImSdk from "./utils/im"
import GoEasyIM from './utils/goeasy-im-1.5.1.js';
App({
onLaunch() {
// 展示本地存储能力
// 登录
this.globalData.im = new ImSdk(1400438486,'')
console.log(this.globalData.im)
onLaunch: function () {
wx.im = GoEasyIM.getInstance({
host: 'hangzhou.goeasy.io',
appkey: 'BC-5d0a3ae138494c93869320535ee155fc'
});
wx.GoEasyIM = GoEasyIM;
},
formatDate: function (time) {
const date = new Date(time);
const month = date.getMonth() + 1;
const day = date.getDate();
const hour = date.getHours();
const minute = date.getMinutes();
return [month, day].map(this.formatNumber).join('-') + ' ' + [hour, minute].map(this.formatNumber).join(':');
},
formatNumber: function (n) {
n = n.toString();
return n[1] ? n : '0' + n;
},
globalData: {
userInfo: null,
im:{}
im: {},
imService: null,
}
})

View File

@@ -57,6 +57,7 @@
"servePath": ""
},
"isGameTourist": false,
"cloudfunctionTemplateRoot": "cloudfunctionTemplate",
"condition": {
"search": {
"list": []

2
utils/goeasy-im-1.5.1.js Normal file

File diff suppressed because one or more lines are too long

View File

@@ -1,187 +1,33 @@
import TIM from '../miniprogram_npm/tim-wx-sdk/index.js';
import GoEasy from './goeasy-1.2.1';
class ImSdk {
tim = {};
readyRes = function(){
}
messageRes = function(){
}
messageRevokedRes = function(){
}
messagePeer = function(){
}
listUpdate = function(){
}
groupListUpdate = function(){
}
profileUpdate = function(){
}
blacklistUpdate = function(){
}
erroeRes = function(){
}
setNotReady = function(){
}
kickedOut = function(){
}
netState = function(){
}
constructor(SDKAppID) {
let options = {
SDKAppID // 接入时需要将0替换为您的即时通信 IM 应用的 SDKAppID
};
// 创建 SDK 实例,`TIM.create()`方法对于同一个 `SDKAppID` 只会返回同一份实例
this.tim = TIM.create(options); // SDK 实例通常用 tim 表示
// 设置 SDK 日志输出级别,详细分级请参见 <a href="https://imsdk-1252463788.file.myqcloud.com/IM_DOC/Web/SDK.html#setLogLevel">setLogLevel 接口的说明</a>
this.tim.setLogLevel(0); // 普通级别,日志量较多,接入时建议使用
// tim.setLogLevel(1); // release 级别SDK 输出关键信息,生产环境时建议使用
this.tim.on(TIM.EVENT.SDK_READY, function (event) {
// 收到离线消息和会话列表同步完毕通知,接入侧可以调用 sendMessage 等需要鉴权的接口
// event.name - TIM.EVENT.SDK_READY
this.readyres(event)
});
this.tim.on(TIM.EVENT.MESSAGE_RECEIVED, function (event) {
// 收到推送的单聊、群聊、群提示、群系统通知的新消息,可通过遍历 event.data 获取消息列表数据并渲染到页面
// event.name - TIM.EVENT.MESSAGE_RECEIVED
// event.data - 存储 Message 对象的数组 - [Message]
this.messageRes(event)
});
this.tim.on(TIM.EVENT.MESSAGE_REVOKED, function (event) {
// 收到消息被撤回的通知
// event.name - TIM.EVENT.MESSAGE_REVOKED
// event.data - 存储 Message 对象的数组 - [Message] - 每个 Message 对象的 isRevoked 属性值为 true
this.messageRevokedRes(event)
});
this.tim.on(TIM.EVENT.MESSAGE_READ_BY_PEER, function (event) {
// SDK 收到对端已读消息的通知,即已读回执。使用前需要将 SDK 版本升级至 v2.7.0 或以上。仅支持单聊会话。
// event.name - TIM.EVENT.MESSAGE_READ_BY_PEER
// event.data - event.data - 存储 Message 对象的数组 - [Message] - 每个 Message 对象的 isPeerRead 属性值为 true
this.messagePeer(event)
});
this.tim.on(TIM.EVENT.CONVERSATION_LIST_UPDATED, function (event) {
// 收到会话列表更新通知,可通过遍历 event.data 获取会话列表数据并渲染到页面
// event.name - TIM.EVENT.CONVERSATION_LIST_UPDATED
// event.data - 存储 Conversation 对象的数组 - [Conversation]
this.listUpdate(event)
});
this.tim.on(TIM.EVENT.GROUP_LIST_UPDATED, function (event) {
// 收到群组列表更新通知,可通过遍历 event.data 获取群组列表数据并渲染到页面
// event.name - TIM.EVENT.GROUP_LIST_UPDATED
// event.data - 存储 Group 对象的数组 - [Group]
this.setGroupListUpdate(event)
});
this.tim.on(TIM.EVENT.PROFILE_UPDATED, function (event) {
// 收到自己或好友的资料变更通知
// event.name - TIM.EVENT.PROFILE_UPDATED
// event.data - 存储 Profile 对象的数组 - [Profile]
this.setProfileUpdate(event)
});
this.tim.on(TIM.EVENT.BLACKLIST_UPDATED, function (event) {
// 收到黑名单列表更新通知
// event.name - TIM.EVENT.BLACKLIST_UPDATED
// event.data - 存储 userID 的数组 - [userID]
this.blacklistUpdate(event)
});
this.tim.on(TIM.EVENT.ERROR, function (event) {
// 收到 SDK 发生错误通知,可以获取错误码和错误信息
// event.name - TIM.EVENT.ERROR
// event.data.code - 错误码
// event.data.message - 错误信息
this.erroeRes(event)
});
this.tim.on(TIM.EVENT.SDK_NOT_READY, function (event) {
// 收到 SDK 进入 not ready 状态通知,此时 SDK 无法正常工作
// event.name - TIM.EVENT.SDK_NOT_READY
this.setNotReady(event)
});
this.tim.on(TIM.EVENT.KICKED_OUT, function (event) {
// 收到被踢下线通知
// event.name - TIM.EVENT.KICKED_OUT
// event.data.type - 被踢下线的原因,例如:
// - TIM.TYPES.KICKED_OUT_MULT_ACCOUNT 多实例登录被踢
// - TIM.TYPES.KICKED_OUT_MULT_DEVICE 多终端登录被踢
// - TIM.TYPES.KICKED_OUT_USERSIG_EXPIRED 签名过期被踢 v2.4.0起支持)。
this.kickedOut(event)
});
this.tim.on(TIM.EVENT.NET_STATE_CHANGE, function (event) {
// 网络状态发生改变v2.5.0 起支持)。
// event.name - TIM.EVENT.NET_STATE_CHANGE
// event.data.state 当前网络状态,枚举值及说明如下:
// \- TIM.TYPES.NET_STATE_CONNECTED - 已接入网络
// \- TIM.TYPES.NET_STATE_CONNECTING - 连接中。很可能遇到网络抖动SDK 在重试。接入侧可根据此状态提示“当前网络不稳定”或“连接中”
// \- TIM.TYPES.NET_STATE_DISCONNECTED - 未接入网络。接入侧可根据此状态提示“当前网络不可用”。SDK 仍会继续重试若用户网络恢复SDK 会自动同步消息
this.netState(event)
});
im = {}
constructor(appkey){
im = GoEasy.getInstance({
host: "hangzhou.goeasy.io", //应用所在的区域地址: 【hangzhou.goeasy.io |singapore.goeasy.io】
appkey
})
}
setReady(res) {
this.readyRes = res
connect(){
this.im.connect({
onSuccess: function () { //连接成功
console.log("GoEasy connect successfully.") //连接成功
},
onFailed: function (error) { //连接失败
console.log("Failed to connect GoEasy, code:"+error.code+ ",error:"+error.content);
},
onProgress:function(attempts) { //连接或自动重连中
console.log("GoEasy is connecting", attempts);
}
})
}
setMessage(res) {
this.messageRes = res
}
setMessageRevoked(res) {
this.messageRevokedRes = res
}
setMessagePeer(res) {
this.messagePeer = res
}
setListUpdate(res) {
this.listUpdate = res
}
setGroupListUpdate(res) {
this.groupListUpdate = res
}
setProfileUpdate(res) {
this.profileUpdate = res
}
setBlacklistUpdate(res) {
this.blacklistUpdate = res
}
setError(res) {
this.erroeRes = res
}
setNotReady(res) {
this.setNotReady = res
}
setKickedOut(res) {
this.kickedOut = res
}
setNetState(res) {
this.netState = res
}
login(userID, userSig) {
this.tim.login({ userID, userSig });
}
}
export default ImSdk;
export default ImSdk

187
utils/imback.js Normal file
View File

@@ -0,0 +1,187 @@
import TIM from '../miniprogram_npm/tim-wx-sdk/index.js';
class ImSdk {
tim = {};
readyRes = function(){
}
messageRes = function(){
}
messageRevokedRes = function(){
}
messagePeer = function(){
}
listUpdate = function(){
}
groupListUpdate = function(){
}
profileUpdate = function(){
}
blacklistUpdate = function(){
}
erroeRes = function(){
}
setNotReady = function(){
}
kickedOut = function(){
}
netState = function(){
}
constructor(SDKAppID) {
let options = {
SDKAppID // 接入时需要将0替换为您的即时通信 IM 应用的 SDKAppID
};
// 创建 SDK 实例,`TIM.create()`方法对于同一个 `SDKAppID` 只会返回同一份实例
this.tim = TIM.create(options); // SDK 实例通常用 tim 表示
// 设置 SDK 日志输出级别,详细分级请参见 <a href="https://imsdk-1252463788.file.myqcloud.com/IM_DOC/Web/SDK.html#setLogLevel">setLogLevel 接口的说明</a>
this.tim.setLogLevel(0); // 普通级别,日志量较多,接入时建议使用
// tim.setLogLevel(1); // release 级别SDK 输出关键信息,生产环境时建议使用
this.tim.on(TIM.EVENT.SDK_READY, function (event) {
// 收到离线消息和会话列表同步完毕通知,接入侧可以调用 sendMessage 等需要鉴权的接口
// event.name - TIM.EVENT.SDK_READY
this.readyres(event)
});
this.tim.on(TIM.EVENT.MESSAGE_RECEIVED, function (event) {
// 收到推送的单聊、群聊、群提示、群系统通知的新消息,可通过遍历 event.data 获取消息列表数据并渲染到页面
// event.name - TIM.EVENT.MESSAGE_RECEIVED
// event.data - 存储 Message 对象的数组 - [Message]
this.messageRes(event)
});
this.tim.on(TIM.EVENT.MESSAGE_REVOKED, function (event) {
// 收到消息被撤回的通知
// event.name - TIM.EVENT.MESSAGE_REVOKED
// event.data - 存储 Message 对象的数组 - [Message] - 每个 Message 对象的 isRevoked 属性值为 true
this.messageRevokedRes(event)
});
this.tim.on(TIM.EVENT.MESSAGE_READ_BY_PEER, function (event) {
// SDK 收到对端已读消息的通知,即已读回执。使用前需要将 SDK 版本升级至 v2.7.0 或以上。仅支持单聊会话。
// event.name - TIM.EVENT.MESSAGE_READ_BY_PEER
// event.data - event.data - 存储 Message 对象的数组 - [Message] - 每个 Message 对象的 isPeerRead 属性值为 true
this.messagePeer(event)
});
this.tim.on(TIM.EVENT.CONVERSATION_LIST_UPDATED, function (event) {
// 收到会话列表更新通知,可通过遍历 event.data 获取会话列表数据并渲染到页面
// event.name - TIM.EVENT.CONVERSATION_LIST_UPDATED
// event.data - 存储 Conversation 对象的数组 - [Conversation]
this.listUpdate(event)
});
this.tim.on(TIM.EVENT.GROUP_LIST_UPDATED, function (event) {
// 收到群组列表更新通知,可通过遍历 event.data 获取群组列表数据并渲染到页面
// event.name - TIM.EVENT.GROUP_LIST_UPDATED
// event.data - 存储 Group 对象的数组 - [Group]
this.setGroupListUpdate(event)
});
this.tim.on(TIM.EVENT.PROFILE_UPDATED, function (event) {
// 收到自己或好友的资料变更通知
// event.name - TIM.EVENT.PROFILE_UPDATED
// event.data - 存储 Profile 对象的数组 - [Profile]
this.setProfileUpdate(event)
});
this.tim.on(TIM.EVENT.BLACKLIST_UPDATED, function (event) {
// 收到黑名单列表更新通知
// event.name - TIM.EVENT.BLACKLIST_UPDATED
// event.data - 存储 userID 的数组 - [userID]
this.blacklistUpdate(event)
});
this.tim.on(TIM.EVENT.ERROR, function (event) {
// 收到 SDK 发生错误通知,可以获取错误码和错误信息
// event.name - TIM.EVENT.ERROR
// event.data.code - 错误码
// event.data.message - 错误信息
this.erroeRes(event)
});
this.tim.on(TIM.EVENT.SDK_NOT_READY, function (event) {
// 收到 SDK 进入 not ready 状态通知,此时 SDK 无法正常工作
// event.name - TIM.EVENT.SDK_NOT_READY
this.setNotReady(event)
});
this.tim.on(TIM.EVENT.KICKED_OUT, function (event) {
// 收到被踢下线通知
// event.name - TIM.EVENT.KICKED_OUT
// event.data.type - 被踢下线的原因,例如:
// - TIM.TYPES.KICKED_OUT_MULT_ACCOUNT 多实例登录被踢
// - TIM.TYPES.KICKED_OUT_MULT_DEVICE 多终端登录被踢
// - TIM.TYPES.KICKED_OUT_USERSIG_EXPIRED 签名过期被踢 v2.4.0起支持)。
this.kickedOut(event)
});
this.tim.on(TIM.EVENT.NET_STATE_CHANGE, function (event) {
// 网络状态发生改变v2.5.0 起支持)。
// event.name - TIM.EVENT.NET_STATE_CHANGE
// event.data.state 当前网络状态,枚举值及说明如下:
// \- TIM.TYPES.NET_STATE_CONNECTED - 已接入网络
// \- TIM.TYPES.NET_STATE_CONNECTING - 连接中。很可能遇到网络抖动SDK 在重试。接入侧可根据此状态提示“当前网络不稳定”或“连接中”
// \- TIM.TYPES.NET_STATE_DISCONNECTED - 未接入网络。接入侧可根据此状态提示“当前网络不可用”。SDK 仍会继续重试若用户网络恢复SDK 会自动同步消息
this.netState(event)
});
}
setReady(res) {
this.readyRes = res
}
setMessage(res) {
this.messageRes = res
}
setMessageRevoked(res) {
this.messageRevokedRes = res
}
setMessagePeer(res) {
this.messagePeer = res
}
setListUpdate(res) {
this.listUpdate = res
}
setGroupListUpdate(res) {
this.groupListUpdate = res
}
setProfileUpdate(res) {
this.profileUpdate = res
}
setBlacklistUpdate(res) {
this.blacklistUpdate = res
}
setError(res) {
this.erroeRes = res
}
setNotReady(res) {
this.setNotReady = res
}
setKickedOut(res) {
this.kickedOut = res
}
setNetState(res) {
this.netState = res
}
login(userID, userSig) {
this.tim.login({ userID, userSig });
}
}
export default ImSdk;