|
@@ -478,9 +478,11 @@ import { setData } from '@/pagesGood/debug/GenerateTestUserSig';
|
|
|
import UserController from './controller/user-controller';
|
|
|
import Pusher from './model/pusher';
|
|
|
import { EVENT } from './common/constants';
|
|
|
+import TIM from './common/tim-wx';
|
|
|
import Event from './utils/event';
|
|
|
import * as ENV from './utils/environment';
|
|
|
const TAG_NAME = 'TRTC-ROOM';
|
|
|
+const IM_GROUP_TYPE = TIM.TYPES.GRP_CHATROOM // TIM.TYPES.GRP_CHATROOM 体验版IM无数量限制,成员20个, TIM.TYPES.GRP_AVCHATROOM IM体验版最多10个,升级后无限制
|
|
|
|
|
|
export default {
|
|
|
data() {
|
|
@@ -504,7 +506,14 @@ export default {
|
|
|
remoteVolumeList: [],
|
|
|
appVersion: ENV.APP_VERSION,
|
|
|
libVersion: ENV.LIB_VERSION,
|
|
|
- debugMode: ''
|
|
|
+ debugMode: '',
|
|
|
+ enableIM: true, // 用于组件内渲染
|
|
|
+ showIMPanel: false,
|
|
|
+ exitIMThrottle: false,
|
|
|
+ messageContent: '',
|
|
|
+ messageList: [], // 仅保留10条消息
|
|
|
+ maxMessageListLength: 10,
|
|
|
+ messageListScrollTop: 0,
|
|
|
};
|
|
|
},
|
|
|
|
|
@@ -518,7 +527,14 @@ export default {
|
|
|
userID: '',
|
|
|
userSig: '',
|
|
|
template: '',
|
|
|
- debugMode: ''
|
|
|
+ debugMode: '',
|
|
|
+ enableIM: true, // 用于组件内渲染
|
|
|
+ showIMPanel: false,
|
|
|
+ exitIMThrottle: false,
|
|
|
+ messageContent: '',
|
|
|
+ messageList: [], // 仅保留10条消息
|
|
|
+ maxMessageListLength: 10,
|
|
|
+ messageListScrollTop: 0
|
|
|
})
|
|
|
}
|
|
|
},
|
|
@@ -608,12 +624,16 @@ export default {
|
|
|
*/
|
|
|
enterRoom: function(params) {
|
|
|
return new Promise((resolve, reject) => {
|
|
|
- console.log(TAG_NAME, 'enterRoom');
|
|
|
+ console.log(TAG_NAME, 'enterRoom');
|
|
|
console.log(TAG_NAME, 'params', params);
|
|
|
console.log(TAG_NAME, 'config', this.config);
|
|
|
console.log(TAG_NAME, 'pusher', this.pusher); // 1. 补齐进房参数,校验必要参数是否齐全
|
|
|
|
|
|
console.log('进房......', params, this.config, this.pusher);
|
|
|
+ if (this.config.enableIM && this.config.sdkAppID) {
|
|
|
+ this._initIM(this.config,params.roomID)
|
|
|
+ // this._loginIM({ ...this.config, roomID: params.roomID })
|
|
|
+ }
|
|
|
if (params) {
|
|
|
Object.assign(this.pusher, params);
|
|
|
Object.assign(this.config, params);
|
|
@@ -658,6 +678,7 @@ export default {
|
|
|
this.pusher.reset();
|
|
|
this.status.isPush = false;
|
|
|
const result = this.userController.reset();
|
|
|
+ this._exitIM();
|
|
|
this.setData(
|
|
|
{
|
|
|
pusher: this.pusher,
|
|
@@ -2302,6 +2323,338 @@ export default {
|
|
|
})
|
|
|
.catch(() => {});
|
|
|
}
|
|
|
+ },
|
|
|
+ // ______ __ __ ______ __ __
|
|
|
+ // | \| \ / \ | \ | \ | \
|
|
|
+ // \$$$$$$| $$\ / $$ \$$$$$$ _______ _| $$_ ______ ______ _______ ______ | $$
|
|
|
+ // | $$ | $$$\ / $$$ | $$ | \| $$ \ / \ / \ | \ | \ | $$
|
|
|
+ // | $$ | $$$$\ $$$$ | $$ | $$$$$$$\\$$$$$$ | $$$$$$\| $$$$$$\| $$$$$$$\ \$$$$$$\| $$
|
|
|
+ // | $$ | $$\$$ $$ $$ | $$ | $$ | $$ | $$ __ | $$ $$| $$ \$$| $$ | $$ / $$| $$
|
|
|
+ // _| $$_ | $$ \$$$| $$ _| $$_ | $$ | $$ | $$| \| $$$$$$$$| $$ | $$ | $$| $$$$$$$| $$
|
|
|
+ // | $$ \| $$ \$ | $$ | $$ \| $$ | $$ \$$ $$ \$$ \| $$ | $$ | $$ \$$ $$| $$
|
|
|
+ // \$$$$$$ \$$ \$$ \$$$$$$ \$$ \$$ \$$$$ \$$$$$$$ \$$ \$$ \$$ \$$$$$$$ \$$
|
|
|
+
|
|
|
+ getCountNum(params) {
|
|
|
+ let promise = this.tim.getGroupProfile({ groupID: this.config.roomID+"" });
|
|
|
+ promise.then(function(imResponse) {
|
|
|
+ return imResponse.data.group.memberCount;
|
|
|
+ console.log(imResponse.data.group);
|
|
|
+ }).catch(function(imError) {
|
|
|
+ console.warn('getGroupProfile error:', imError); // 获取群详细资料失败的相关信息
|
|
|
+ });
|
|
|
+ },
|
|
|
+ /**
|
|
|
+ * 初始化 IM SDK
|
|
|
+ * @param {Object} config sdkAppID
|
|
|
+ */
|
|
|
+ sendGroupCustomMessage(params) {
|
|
|
+ if (!this.tim) {
|
|
|
+ console.warn(TAG_NAME, '未开启IM功能,该方法无法使用', params)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ console.log(TAG_NAME, 'sendGroupCustomMessage', params)
|
|
|
+ const message = this.tim.createCustomMessage({
|
|
|
+ to: params.roomID + '',
|
|
|
+ conversationType: TIM.TYPES.CONV_GROUP,
|
|
|
+ payload: params.payload,
|
|
|
+ })
|
|
|
+ const promise = this.tim.sendMessage(message)
|
|
|
+ promise.then(function(imResponse) {
|
|
|
+ // 发送成功
|
|
|
+ console.log(TAG_NAME, 'sendMessage success', imResponse)
|
|
|
+ }).catch(function(imError) {
|
|
|
+ // 发送失败
|
|
|
+ console.warn(TAG_NAME, 'sendMessage error:', imError)
|
|
|
+ })
|
|
|
+ return promise
|
|
|
+ },
|
|
|
+ _initIM(config,roomId) {
|
|
|
+ if (!config.enableIM || !config.sdkAppID || this.tim) {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ console.log(TAG_NAME, '_initIM', config)
|
|
|
+ // 初始化 sdk 实例
|
|
|
+ const tim = TIM.create({
|
|
|
+ SDKAppID: config.sdkAppID,
|
|
|
+ })
|
|
|
+ // 0 普通级别,日志量较多,接入时建议使用
|
|
|
+ // 1 release级别,SDK 输出关键信息,生产环境时建议使用
|
|
|
+ // 2 告警级别,SDK 只输出告警和错误级别的日志
|
|
|
+ // 3 错误级别,SDK 只输出错误级别的日志
|
|
|
+ // 4 无日志级别,SDK 将不打印任何日志
|
|
|
+ if (config.debugMode) {
|
|
|
+ tim.setLogLevel(1)
|
|
|
+ } else {
|
|
|
+ tim.setLogLevel(4)
|
|
|
+ }
|
|
|
+ // 取消监听
|
|
|
+ tim.off(TIM.EVENT.SDK_READY, this._onIMReady)
|
|
|
+ tim.off(TIM.EVENT.MESSAGE_RECEIVED, this._onIMMessageReceived)
|
|
|
+ tim.off(TIM.EVENT.SDK_NOT_READY, this._onIMNotReady)
|
|
|
+ tim.off(TIM.EVENT.KICKED_OUT, this._onIMKickedOut)
|
|
|
+ tim.off(TIM.EVENT.ERROR, this._onIMError)
|
|
|
+ // 监听事件
|
|
|
+ tim.on(TIM.EVENT.SDK_READY, this._onIMReady, this)
|
|
|
+ tim.on(TIM.EVENT.MESSAGE_RECEIVED, this._onIMMessageReceived, this)
|
|
|
+ tim.on(TIM.EVENT.SDK_NOT_READY, this._onIMNotReady, this)
|
|
|
+ tim.on(TIM.EVENT.KICKED_OUT, this._onIMKickedOut, this)
|
|
|
+ tim.on(TIM.EVENT.ERROR, this._onIMError, this)
|
|
|
+ this.tim = tim
|
|
|
+ wx.tim = tim
|
|
|
+ this._loginIM({ ...this.config, roomID: roomId })
|
|
|
+ },
|
|
|
+ _loginIM(params) {
|
|
|
+ if (!this.tim) {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ console.log(TAG_NAME, '_loginIM', params)
|
|
|
+ var that=this;
|
|
|
+ setTimeout(function(){
|
|
|
+ that.sendGroupCustomMessage({
|
|
|
+ roomID: Number(that.config.roomID), // 房间 ID
|
|
|
+ payload: {
|
|
|
+ data: 'genxin',
|
|
|
+ description: 'genxin',
|
|
|
+ extension: 'genxin'
|
|
|
+ }
|
|
|
+ })
|
|
|
+ },2000)
|
|
|
+ return this.tim.login({
|
|
|
+ userID: params.userID,
|
|
|
+ userSig: params.userSig,
|
|
|
+ })
|
|
|
+ },
|
|
|
+ _logoutIM() {
|
|
|
+ if (!this.tim) {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ console.log(TAG_NAME, '_logoutIM')
|
|
|
+ return this.tim.logout()
|
|
|
+ },
|
|
|
+ _exitIM() {
|
|
|
+ this.sendGroupCustomMessage({
|
|
|
+ roomID: Number(this.config.roomID), // 房间 ID
|
|
|
+ payload: {
|
|
|
+ data: 'genxin',
|
|
|
+ description: 'genxin',
|
|
|
+ extension: 'genxin'
|
|
|
+ }
|
|
|
+ })
|
|
|
+ var that=this;
|
|
|
+ setTimeout(function(){
|
|
|
+ // 方法需要调用限制,否则重复解散群 退群会有warn
|
|
|
+ if (that.config.exitIMThrottle || !that.tim) {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ that.config.exitIMThrottle = true
|
|
|
+ const userList = that.getRemoteUserList()
|
|
|
+ const roomID = that.config.roomID
|
|
|
+ const userID = that.config.userID
|
|
|
+ that._searchGroup({ roomID }).then((imResponse) => {
|
|
|
+ // 查询群资料,判断是否为群主
|
|
|
+ if (imResponse.data.group.ownerID === userID && userList.length === 0) {
|
|
|
+ // 如果 userList 为 0 群主可以解散群,并登出IM
|
|
|
+ that._dismissGroup({ roomID }).then(()=>{
|
|
|
+ that.config.exitIMThrottle = false
|
|
|
+ that._logoutIM()
|
|
|
+ }).catch((imError) => {
|
|
|
+ that.config.exitIMThrottle = false
|
|
|
+ that._logoutIM()
|
|
|
+ })
|
|
|
+ } else if (imResponse.data.group.ownerID === userID) {
|
|
|
+ that.config.exitIMThrottle = false
|
|
|
+ // 群主不能退群只能登出
|
|
|
+ that._logoutIM()
|
|
|
+ } else {
|
|
|
+ // 普通成员退群并登出IM
|
|
|
+ that._quitGroup({ roomID }).then(()=>{
|
|
|
+ that.config.exitIMThrottle = false
|
|
|
+ that._logoutIM()
|
|
|
+ }).catch((imError) => {
|
|
|
+ that.config.exitIMThrottle = false
|
|
|
+ that._logoutIM()
|
|
|
+ })
|
|
|
+ }
|
|
|
+ }).catch((imError) => {
|
|
|
+ that.config.exitIMThrottle = false
|
|
|
+ // 查询异常直接登出
|
|
|
+ that._logoutIM()
|
|
|
+ })
|
|
|
+ },2000)
|
|
|
+ },
|
|
|
+ _searchGroup(params) {
|
|
|
+ if (!this.tim) {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ console.log(TAG_NAME, '_searchGroup', params)
|
|
|
+ const tim = this.tim
|
|
|
+ const promise = tim.searchGroupByID(params.roomID + '')
|
|
|
+ promise.then(function(imResponse) {
|
|
|
+ // const group = imResponse.data.group // 群组信息
|
|
|
+ console.log(TAG_NAME, '_searchGroup success', imResponse)
|
|
|
+ }).catch(function(imError) {
|
|
|
+ console.warn(TAG_NAME, '_searchGroup fail,TIM 报错信息不影响后续逻辑,可以忽略', imError) // 搜素群组失败的相关信息
|
|
|
+ })
|
|
|
+ return promise
|
|
|
+ },
|
|
|
+ /**
|
|
|
+ * 创建 AVchatroom
|
|
|
+ * @param {*} params roomID
|
|
|
+ * @returns {Promise}
|
|
|
+ */
|
|
|
+ _createGroup(params) {
|
|
|
+ if (!this.tim) {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ console.log(TAG_NAME, '_createGroup', params)
|
|
|
+ const promise = this.tim.createGroup({
|
|
|
+ groupID: params.roomID + '',
|
|
|
+ name: params.roomID + '',
|
|
|
+ type: IM_GROUP_TYPE,
|
|
|
+ })
|
|
|
+ promise.then((imResponse) => { // 创建成功
|
|
|
+ console.log(TAG_NAME, '_createGroup success', imResponse.data.group) // 创建的群的资料
|
|
|
+ }).catch((imError) => {
|
|
|
+ console.warn(TAG_NAME, '_createGroup error', imError) // 创建群组失败的相关信息
|
|
|
+ })
|
|
|
+ return promise
|
|
|
+ },
|
|
|
+ /**
|
|
|
+ * 进入 AVchatroom
|
|
|
+ * @param {*} params roomID
|
|
|
+ * @returns {Promise}
|
|
|
+ */
|
|
|
+ _joinGroup(params) {
|
|
|
+ if (!this.tim) {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ console.log(TAG_NAME, '_joinGroup', params)
|
|
|
+ const promise = this.tim.joinGroup({ groupID: params.roomID + '', type: IM_GROUP_TYPE })
|
|
|
+ promise.then((imResponse) => {
|
|
|
+ switch (imResponse.data.status) {
|
|
|
+ case TIM.TYPES.JOIN_STATUS_WAIT_APPROVAL: // 等待管理员同意
|
|
|
+ break
|
|
|
+ case TIM.TYPES.JOIN_STATUS_SUCCESS: // 加群成功
|
|
|
+ case TIM.TYPES.JOIN_STATUS_ALREADY_IN_GROUP: // 已经在群中
|
|
|
+ // console.log(imResponse.data.group) // 加入的群组资料
|
|
|
+ // wx.showToast({
|
|
|
+ // title: '进群成功',
|
|
|
+ // })
|
|
|
+ console.log(TAG_NAME, '_joinGroup success', imResponse)
|
|
|
+ break
|
|
|
+ default:
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }).catch((imError) => {
|
|
|
+ console.warn(TAG_NAME, 'joinGroup error', imError) // 申请加群失败的相关信息
|
|
|
+ })
|
|
|
+ return promise
|
|
|
+ },
|
|
|
+ _quitGroup(params) {
|
|
|
+ if (!this.tim) {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ console.log(TAG_NAME, '_quitGroup', params)
|
|
|
+ const promise = this.tim.quitGroup(params.roomID + '')
|
|
|
+ promise.then((imResponse) => {
|
|
|
+ console.log(TAG_NAME, '_quitGroup success', imResponse)
|
|
|
+ }).catch((imError) => {
|
|
|
+ console.warn(TAG_NAME, 'quitGroup error', imError)
|
|
|
+ })
|
|
|
+ return promise
|
|
|
+ },
|
|
|
+ _dismissGroup(params) {
|
|
|
+ if (!this.tim) {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ console.log(TAG_NAME, '_dismissGroup', params)
|
|
|
+ const promise = this.tim.dismissGroup(params.roomID + '')
|
|
|
+ promise.then((imResponse) => {
|
|
|
+ console.log(TAG_NAME, '_dismissGroup success', imResponse)
|
|
|
+ }).catch((imError) => {
|
|
|
+ console.warn(TAG_NAME, '_dismissGroup error', imError)
|
|
|
+ })
|
|
|
+ return promise
|
|
|
+ },
|
|
|
+ _onIMReady(event) {
|
|
|
+ console.log(TAG_NAME, 'IM.READY', event)
|
|
|
+ this._emitter.emit(EVENT.IM_READY, event)
|
|
|
+ const roomID = this.config.roomID
|
|
|
+ // 查询群组是否存在
|
|
|
+ this._searchGroup({ roomID }).then((res) => {
|
|
|
+ // console.log(TAG_NAME, 'searchGroup', res)
|
|
|
+ // 存在直接进群
|
|
|
+ this._joinGroup({ roomID })
|
|
|
+ }).catch(() => {
|
|
|
+ // 不存在则创建,如果是avchatroom 创建后进群
|
|
|
+ this._createGroup({ roomID }).then((res) => {
|
|
|
+ // 进群
|
|
|
+ this._joinGroup({ roomID })
|
|
|
+ }).catch((imError)=> {
|
|
|
+ if (imError.code === 10021) {
|
|
|
+ console.log(TAG_NAME, '群已存在,直接进群', event)
|
|
|
+ this._joinGroup({ roomID })
|
|
|
+ }
|
|
|
+ })
|
|
|
+ })
|
|
|
+ // 收到离线消息和会话列表同步完毕通知,接入侧可以调用 sendMessage 等需要鉴权的接口
|
|
|
+ // event.name - TIM.EVENT.IM_READY
|
|
|
+ },
|
|
|
+ _onIMMessageReceived(event) {
|
|
|
+ // 收到推送的单聊、群聊、群提示、群系统通知的新消息,可通过遍历 event.data 获取消息列表数据并渲染到页面
|
|
|
+ console.log(TAG_NAME, 'IM.MESSAGE_RECEIVED', event)
|
|
|
+ // messageList 仅保留10条消息
|
|
|
+ const messageData = event.data
|
|
|
+ const roomID = this.config.roomID + ''
|
|
|
+ const userID = this.config.userID + ''
|
|
|
+ for (let i = 0; i < messageData.length; i++) {
|
|
|
+ const message = messageData[i]
|
|
|
+ // console.log(TAG_NAME, 'IM.MESSAGE_RECEIVED', message, this.config, TIM.TYPES.MSG_TEXT)
|
|
|
+ if (message.to === roomID + '' || message.to === userID) {
|
|
|
+ // 遍历messageData 获取当前room 或者当前user的消息
|
|
|
+ console.log(TAG_NAME, 'IM.MESSAGE_RECEIVED', message, message.type, TIM.TYPES.MSG_TEXT)
|
|
|
+ if (message.type === TIM.TYPES.MSG_TEXT) {
|
|
|
+ /*this._pushMessageList({
|
|
|
+ name: message.from,
|
|
|
+ message: message.payload.text,
|
|
|
+ })*/
|
|
|
+ } else {
|
|
|
+ if (message.type === TIM.TYPES.MSG_GRP_SYS_NOTICE && message.payload.operationType === 2) {
|
|
|
+ // 群系统通知
|
|
|
+ /*this._pushMessageList({
|
|
|
+ name: '系统通知',
|
|
|
+ message: `欢迎 ${userID}`,
|
|
|
+ })*/
|
|
|
+ }
|
|
|
+ // 其他消息暂不处理
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ this._emitter.emit(EVENT.IM_MESSAGE_RECEIVED, event)
|
|
|
+ },
|
|
|
+ _onIMNotReady(event) {
|
|
|
+ console.log(TAG_NAME, 'IM.NOT_READY', event)
|
|
|
+ this._emitter.emit(EVENT.IM_NOT_READY, event)
|
|
|
+ // 收到 SDK 进入 not ready 状态通知,此时 SDK 无法正常工作
|
|
|
+ // event.name - TIM.EVENT.IM_NOT_READY
|
|
|
+ },
|
|
|
+ _onIMKickedOut(event) {
|
|
|
+ console.log(TAG_NAME, 'IM.KICKED_OUT', event)
|
|
|
+ this._emitter.emit(EVENT.IM_KICKED_OUT, 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 签名过期被踢。使用前需要将SDK版本升级至v2.4.0或以上。
|
|
|
+ },
|
|
|
+ _onIMError(event) {
|
|
|
+ console.log(TAG_NAME, 'IM.ERROR', event)
|
|
|
+ this._emitter.emit(EVENT.IM_ERROR, event)
|
|
|
+ // 收到 SDK 发生错误通知,可以获取错误码和错误信息
|
|
|
+ // event.name - TIM.EVENT.ERROR
|
|
|
+ // event.data.code - 错误码
|
|
|
+ // event.data.message - 错误信息
|
|
|
}
|
|
|
}
|
|
|
};
|