import NIM from 'nim-web-sdk-ng/dist/v2/NIM_MINIAPP_SDK' import { service } from '../config.js' const IM = (account, token) => { let nim = NIM.getInstance({ debugLevel: 'off', appkey: '9bc3ed1f7d8197b6b69f8f5b742824b1', account: account, token: token, lbsUrls: [ 'https://lbs.netease.im/lbs/wxwebconf.jsp' ], linkUrl: 'wlnimsc0.netease.im' }) const eventList = [ 'logined', 'willReconnect', 'disconnect', 'msg', 'syncdone', 'syncRoamingMsgs', 'sessions', 'updateMuteList', 'friends', 'updateSession', 'teams', 'myTeamMembers', 'updateMyMemberInfo', 'sendTeamMsg' ] eventList.forEach((key) => { nim.on(key, (res) => { // console.log(`Receive ${key} event:`, res ? JSON.parse(JSON.stringify(res)) : res) }) }) nim.connect() nim.on('logined', function () { console.log('连接成功') }) nim.on('willReconnect', function (e) { console.log(e, '即将重连') }) nim.on('disconnect', function (e) { console.log(e, '丢失连接') }) nim.on('sessions', function(e) { console.log(e, '会话消息') // 如果群是开启了免打扰自动标记未读数已读 let mute = wx.getStorageSync('muteNotList') || [] if (mute && mute.length > 0) { mute.forEach((j) => { nim.session.resetSessionUnreadCount({ id: `team-${j.teamId}` }) }) } // 获取群消息未读数并赋值在tabBar显示 let teamsSsions = [] e.forEach((i) => { if (i.scene === 'team') { teamsSsions.push({id: i.to, unread: i.unread}) } }) wx.setStorageSync('teamsSessionsCount', teamsSsions) let groupCount = 0 if (teamsSsions.length > 0) { for (let i = 0; i < teamsSsions.length; i++) { groupCount += teamsSsions[i].unread } } if (groupCount > 0) { wx.setTabBarBadge({ index: 2, text: `${groupCount}` }) } else { wx.removeTabBarBadge({ index: 2 }) } }) nim.on('msg', function (e) { console.log(e, '收到最新消息') let pages = getCurrentPages() let currentPage = pages[pages.length - 1] let notiTypeData = { muteTeam: false } if (wx.getStorageSync('muteNotList') && wx.getStorageSync('muteNotList').length > 0) { notiTypeData = wx.getStorageSync('muteNotList').find((item) => item.teamId == e.to) if (!notiTypeData) { notiTypeData = { muteTeam: false } } console.log(notiTypeData, '77') } if (e.scene == 'team') { // 判断是否是@功能 if (e.apns) { let teamAt = wx.getStorageSync('teamAtList') || [] let userInfo = wx.getStorageSync('userInfo') || {nickName: ''} if (!e.apns.accounts || e.apns.accounts.indexOf(userInfo.id + '') >= 0) { // 存入@ if (teamAt.indexOf(e.to + '') < 0) { teamAt.push(e.to) wx.setStorageSync('teamAtList', teamAt) } } } // 群消息未读数增加逻辑 let teamCount = wx.getStorageSync('teamsSessionsCount') || [] if (e.attach && e.attach.type == 'removeTeamMembers') { teamCount.map((item, index) => { if (item.id == e.to) { teamCount.splice(index, 1) wx.setStorageSync('teamsSessionsCount', teamCount) } }) } if (e.attach && e.attach.type == 'addTeamMembers') { teamCount.push({id: e.to, unread: 1}) wx.setStorageSync('teamsSessionsCount', teamCount) } let newTeamCount = teamCount.filter(item => { return item.id == e.to }) if (e.attach && (e.attach.type === 'dismissTeam' || e.attach.type === 'addTeamMembers')) { } else { if (newTeamCount.length > 0) { teamCount = teamCount.map(item => { if (item.id == newTeamCount[0].id && !notiTypeData.muteTeam) { item.unread += 1 } return item }) } else { teamCount.push({id: e.to, unread: notiTypeData.muteTeam ? 0 : 1}) } wx.setStorageSync('teamsSessionsCount', teamCount) } } wx.request({ url: `${service.host}/notice/count`, header: { 'Authorization': 'Bearer ' + wx.getStorageSync('token'), 'X-Requested-With': 'XMLHttpRequest' }, method: 'get', success: ({data}) => { let { notice_count } = data.data console.log(notice_count, '新的未读数') let groupCountList = wx.getStorageSync('teamsSessionsCount') || [] let groupCount = 0 if (groupCountList.length > 0) { for (let i = 0; i < groupCountList.length; i++) { groupCount += groupCountList[i].unread } } if ((notice_count + groupCount) > 0) { wx.setTabBarBadge({ index: 2, text: `${notice_count + groupCount}` }) } else { wx.removeTabBarBadge({ index: 2 }) } } }) setTimeout(() => { if (currentPage.route == 'pages/tabBar/news') { currentPage.onShow() } wx.vibrateLong({ success: () => { console.log('来消息了,震动~~') } }) }, 800) }) nim.on('myTeamMembers', function (e) { let pages = getCurrentPages() let currentPage = pages[pages.length - 1] if (currentPage.route == 'pages/news/groupChitchatDetail') { return } let muteNotList = [] console.log(e, 'e===') if (e && e.length > 0) { e.forEach((i) => { if (i.muteTeam) { console.log(i, 'i====') muteNotList.push({ teamId: i.teamId, muteNotiType: i.muteTeam ? 1 : 0, muteTeam: i.muteTeam }) } }) setTimeout(() => { console.log(muteNotList, '99399') wx.setStorageSync('muteNotList', muteNotList) }) } }) return nim } module.exports = { IM }