123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463 |
- var TIM = require('./tim-wx.js');
- var config = require('@/static/config.js');
- import requestConfig from '@/static/lib/requestConfig.js';
- var app, tim;
- var SDKConfig = {
- sdkappid: config.sdkAppID,
- accountType: config.accType,
- accountMode: 0
- };
- var event = {
- onIMReady: function() {
- },
- onGetPusherList: function() {},
- onPusherJoin: function() {},
- onPusherQuit: function() {},
- onRoomClose: function() {},
- onRecvRoomTextMsg: function() {},
- onMsgNotify: function() {},
- onDestoryGroupNotify: function() {},
- onCustomGroupNotify: function() {},
- onLoginSuccess: function() {},
- onBigGroupMsgNotify: function() {},
- };
- function handleUnreadMsg(msg) {
- console.log('处理全局未读消息')
- if (msg.from === app.globalData.single.id + '_' + config.brandId + '_1') {
- return false
- }
- var adviserHouseId = msg.from.split("_")[1];
- if (!adviserHouseId) {
- return
- }
- var unReadMsgs = uni.getStorageSync('unReadMsgs') || {};
- var currentHouseUnReadMsgs = unReadMsgs[adviserHouseId] || {};
- unReadMsgs.total = unReadMsgs.total ? unReadMsgs.total : 0;
- currentHouseUnReadMsgs.total = currentHouseUnReadMsgs.total ? currentHouseUnReadMsgs.total : 0;
- unReadMsgs['total']++;
- currentHouseUnReadMsgs['total']++;
- let list = getCurrentPages();
- if (!currentHouseUnReadMsgs[msg.from]) {
- currentHouseUnReadMsgs[msg.from] = 1;
- } else {
- currentHouseUnReadMsgs[msg.from]++;
- console.log(unReadMsgs[msg.from], '今天你增加了吗')
- }
- console.log('未读计数增加', list)
- unReadMsgs[adviserHouseId] = currentHouseUnReadMsgs
- uni.setStorageSync('unReadMsgs', unReadMsgs)
- list.forEach((item, index) => {
- console.log(item, 'bpbpbpbpbp')
- if (item.$vm && item.$vm.refreshNumber && typeof item.$vm.refreshNumber === 'function') {
- console.log('户型图集增加', unReadMsgs)
- item.$vm.refreshNumber()
- }
- })
- console.log('全局未读消息处理完毕')
- }
- function msgReceived(msg) {
-
- var myMsg = JSON.parse(JSON.stringify(msg));
- console.log('接收到最新消息', myMsg)
- console.log('接收到最新消息', myMsg.data[0].payload)
- if (myMsg.data instanceof Array) {
- myMsg.data.forEach((item, index) => {
- console.log(item.conversationType)
- if (item.conversationType == 'GROUP') {
- console.log('收到群消息,进去处理',item)
-
- event.onBigGroupMsgNotify(myMsg.data)
- parseGroupSystemNotice(item.payload)
- } else if (item.payload && item.payload.data && item.payload.data.includes('InputStatus')) {
- console.log('对方键盘事件')
- } else if (item.conversationType == '@TIM#SYSTEM') {
- console.log('系统通知@_@')
- parseGroupSystemNotice(item.payload)
- } else {
- console.log('传出去了')
- handleUnreadMsg(item)
- console.log('开始传回当前页notify')
- event.onMsgNotify(item)
- }
- })
- }
-
-
-
- }
- function parseGroupSystemNotice(payload) {
- const groupName =
- payload.groupProfile.groupName || payload.groupProfile.groupID
- console.log('审判群组消息类型', payload.operationType)
- switch (payload.operationType) {
- case 1:
- return `申请加入群组:${groupName}`
- case 2:
- event.onCustomGroupNotify(payload);
- case 3:
- return `申请加入群组:${groupName}被拒绝`
- case 4:
- return `被管理员${payload.operatorID}踢出群组:${groupName}`
- case 5:
- return `成功加入群组:${groupName}`
- case 6:
- return `${payload.operatorID}创建群:${groupName}`
- case 7:
- return `${payload.operatorID}邀请你加群:${groupName}`
- case 8:
- return `你退出群组:${groupName}`
- case 9:
- return `你被${payload.operatorID}设置为群:${groupName}的管理员`
- case 10:
- return `你被${payload.operatorID}撤销群:${groupName}的管理员身份`
- case 255:
- event.onCustomGroupNotify(payload);
- }
- }
- function loginIM(data) {
- app = getApp();
- if (app.globalData.globalWebimhandler) {
- event.onIMReady()
- event.onLoginSuccess()
- return
- }
- var loginInfo = {
- 'sdkAppID': SDKConfig.sdkappid,
- 'appIDAt3rd': SDKConfig.sdkappid,
- 'accountType': SDKConfig.accountType,
- 'identifier': app.globalData.identifier,
- 'identifierNick': app.globalData.single.nickname || '小程序用户',
- 'userSig': app.globalData.userSig,
- };
- console.log('开始创建im')
- tim = TIM.create({
- SDKAppID: config.sdkAppID
- })
- tim.setLogLevel(0);
-
- tim.on(TIM.EVENT.SDK_READY, function(msg) {
- console.log('imReady***')
- event.onIMReady()
- console.log('%%%%', event.onIMReady)
- event.onLoginSuccess && event.onLoginSuccess();
- app.globalData.globalWebimhandler = true
- });
- tim.on(TIM.EVENT.MESSAGE_RECEIVED, msgReceived);
- tim.on(TIM.EVENT.MESSAGE_REVOKED, function(event) {
-
-
-
- });
- tim.on(TIM.EVENT.MESSAGE_READ_BY_PEER, function(event) {
-
-
-
- });
- tim.on(TIM.EVENT.CONVERSATION_LIST_UPDATED, function(event) {
- console.log('监听到会话列表更新', event.data)
-
-
-
- });
- tim.on(TIM.EVENT.GROUP_LIST_UPDATED, function(event) {
-
-
-
- });
- tim.on(TIM.EVENT.PROFILE_UPDATED, function(event) {
-
-
-
- });
- tim.on(TIM.EVENT.BLACKLIST_UPDATED, function(event) {
-
-
-
- });
- tim.on(TIM.EVENT.ERROR, function(event) {
- console.log('SDKERROR!!', event)
-
-
-
-
- });
- tim.on(TIM.EVENT.SDK_NOT_READY, function(event) {
- console.log('SDK_NOT_READY!!', event)
- app.globalData.globalWebimhandler = null;
-
-
- });
- tim.on(TIM.EVENT.KICKED_OUT, function(event) {
-
-
-
-
-
-
- });
- tim.on(TIM.EVENT.NET_STATE_CHANGE, function(event) {
-
-
-
-
-
-
- });
-
- let afterLoginIM = tim.login({
- userID: loginInfo.identifier,
- userSig: loginInfo.userSig
- });
- afterLoginIM.then((imResponse) => {
- console.log(imResponse.data, 'niubi');
- if (imResponse.data.repeatLogin === true) {
- data.callback && data.callback()
-
- console.log(imResponse.data.errorInfo);
- }
- }).catch((imError) => {
- console.warn('login error:', imError);
- })
- }
- function logout(callback) {
- app = getApp();
-
- tim.logout();
- console.log('imRoom总im退出登录')
- app.globalData.globalWebimhandler = null;
- callback && callback()
- }
- function sendCustomMsg(data, type, nickname, callback) {
- let message = tim.createCustomMessage({
- to: type.myselToID,
- conversationType: type.TYPE == 'C2C' ? TIM.TYPES.CONV_C2C : TIM.TYPES.GROUP,
-
-
-
- payload: {
- data: '',
- description: '',
- extension: data.ext,
- }
- });
- let promise = tim.sendMessage(message, {
-
- offlinePushInfo: {
- disablePush: false,
- title: '收到一条新消息',
- description: '请进入APP内查看',
- androidOPPOChannelID: ''
- }
- });
- promise.then((imResponse) => {
- callback && callback(data)
- })
- }
- function getC2CHistoryMsgs(options) {
- console.log(tim)
- let promise = tim.getMessageList({
- conversationID: 'C2C' + options.adviserId,
- count: 10,
- nextReqMessageID: options.nextReqMessageID
- })
- promise.then((res) => {
- console.log('拉取到顾问' + options.adviserId + '的历史消息当前第' + options.nextReqMessageID + '页')
- console.log(res)
- options.success && options.success(JSON.parse(JSON.stringify(res.data)))
- }).catch((err) => {
- options.fail && options.fail(err)
- })
- }
- function onSendMsg(message, type, nickname, callback, fail) {
- let Cmessage = tim.createTextMessage({
- to: type.myselToID,
- conversationType: type.TYPE == 'C2C' ? TIM.TYPES.CONV_C2C : TIM.TYPES.GROUP,
-
-
-
- payload: {
- text: message
- }
- });
- let promise = tim.sendMessage(Cmessage);
- promise.then((imResponse) => {
- console.log('发送完毕', imResponse)
- callback && callback({
- content: message
- })
- }).catch((err) => {})
- }
- function getRecentContactList(data, callback) {
- let promise = tim.getConversationList();
- promise.then(function(imResponse) {
- const conversationList = imResponse.data.conversationList;
- callback && callback(conversationList.filter((item) => {
- return item.type !== '@TIM#SYSTEM'
- }))
- }).catch(function(imError) {
- console.warn('getConversationList error:', imError);
- });
- }
- function onBigGroupMsgNotify() {
- }
- function parseGroupTipContent(payload) {
- switch (payload.operationType) {
- case this.TIM.TYPES.GRP_TIP_MBR_JOIN:
- return `群成员:${payload.userIDList.join(',')},加入群组`
- case this.TIM.TYPES.GRP_TIP_MBR_QUIT:
- return `群成员:${payload.userIDList.join(',')},退出群组`
- case this.TIM.TYPES.GRP_TIP_MBR_KICKED_OUT:
- return `群成员:${payload.userIDList.join(',')},被${payload.operatorID}踢出群组`
- case this.TIM.TYPES.GRP_TIP_MBR_SET_ADMIN:
- return `群成员:${payload.userIDList.join(',')},成为管理员`
- case this.TIM.TYPES.GRP_TIP_MBR_CANCELED_ADMIN:
- return `群成员:${payload.userIDList.join(',')},被撤销管理员`
- default:
- return '[群提示消息]'
- }
- }
- function setListener(options) {
- if (!options) {
- console.log('setListener参数错误', options);
- return;
- }
- event.onGetPusherList = options.onGetPusherList || function() {};
- event.onIMReady = options.onIMReady || function() {};
- event.onPusherJoin = options.onPusherJoin || function() {};
- event.onPusherQuit = options.onPusherQuit || function() {};
- event.onRoomClose = options.onRoomClose || function() {};
- event.onRecvRoomTextMsg = options.onRecvRoomTextMsg || function() {};
- event.onMsgNotify = options.onMsgNotify || function() {};
- event.onDestoryGroupNotify = options.onDestoryGroupNotify || function() {};
- event.onCustomGroupNotify = options.onCustomGroupNotify || function() {};
- event.onLoginSuccess = options.onLoginSuccess || function() {};
- event.onBigGroupMsgNotify = options.onBigGroupMsgNotify || function() {};
- }
- function applyJoinBigGroup(roomID, callback, callbackOptions) {
- let res = tim.joinGroup({
- groupID: roomID,
- applyMessage: '视频通话申请加群',
- type: TIM.TYPES.GRP_AVCHATROOM
- })
- res.then((imResponse) => {
- console.log(imResponse, '群组资料')
- switch (imResponse.data.status) {
- case TIM.TYPES.JOIN_STATUS_WAIT_APPROVAL:
- break;
- case TIM.TYPES.JOIN_STATUS_SUCCESS:
- console.log('关键步骤:加群成功!')
- callback && callback({
- errCode: 0,
- callback: callbackOptions
- });
- console.log(imResponse.data.group, '00000');
- break;
- case TIM.TYPES.JOIN_STATUS_ALREADY_IN_GROUP:
- console.log('关键步骤:加群成功2!')
- callback && callback({
- errCode: 0,
- callback: callbackOptions
- });
- break;
- default:
- console.log('关键步骤:加群成功3!')
- break;
- }
- }).catch(function(imError) {
- console.warn('joinGroup error:', imError);
- });
- }
- async function createBigGroup(options, cb) {
- console.log(options.roomID, '为什么要自动id?')
-
-
-
-
-
-
-
-
-
-
-
-
-
- var res = await requestConfig('createGroup', {
- groupID: options.roomID,
- type: TIM.TYPES.GRP_AVCHATROOM,
- maxMemberNum: 500,
- name: options.roomName || '',
- memberList: []
- }, true);
- if (res && res.success) {
- cb()
- console.log(options, '新版建群成功');
- }
- else{
- console.warn('createGroup error:', res.message);
- }
- }
- function quitBigGroup(groupID) {
- tim.quitGroup(groupID).then((res) => {
- console.log('退出群成功')
- });
-
-
-
- }
- function destroyGroup(groupID) {
-
-
-
-
- requestConfig('destroyGroup', {
- groupId: groupID,
- }, true);
- }
- module.exports = {
- loginIM: loginIM,
- setListener: setListener,
- logout: logout,
- sendCustomMsg: sendCustomMsg,
- onSendMsg: onSendMsg,
- applyJoinBigGroup: applyJoinBigGroup,
- quitBigGroup: quitBigGroup,
- destroyGroup: destroyGroup,
- getRecentContactList: getRecentContactList,
- onBigGroupMsgNotify: onBigGroupMsgNotify,
- getC2CHistoryMsgs: getC2CHistoryMsgs,
- createBigGroup: createBigGroup
- }
|