195 lines
5.7 KiB
JavaScript
195 lines
5.7 KiB
JavaScript
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: 0,
|
||
text: `${groupCount}`
|
||
})
|
||
} else {
|
||
wx.removeTabBarBadge({
|
||
index: 0
|
||
})
|
||
}
|
||
})
|
||
|
||
nim.on('msg', function (e) {
|
||
console.log(e, '收到最新消息')
|
||
let pages = getCurrentPages()
|
||
let currentPage = pages[pages.length - 1]
|
||
let notiTypeData = { muteTeam: false }
|
||
if (wx.getStorageSync('nuteNotList') && wx.getStorageSync('nuteNotList').length > 0) {
|
||
notiTypeData = wx.getStorageSync('nuteNotList').find((item) => item.teamId == e.to)
|
||
}
|
||
// 如果是开启了免打扰的群消息,自动调用群消息已读接口
|
||
// if (notiTypeData.muteTeam) {
|
||
// wx.request({
|
||
// url: `${service.host}/chat/group/read`,
|
||
// header: {
|
||
// 'Authorization': 'Bearer ' + wx.getStorageSync('token'),
|
||
// 'X-Requested-With': 'XMLHttpRequest'
|
||
// },
|
||
// method: 'post',
|
||
// data: {team_id: notiTypeData.teamId},
|
||
// success: ({data}) => {
|
||
// console.log(data, '已读')
|
||
// }
|
||
// })
|
||
// }
|
||
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') || []
|
||
let newTeamCount = teamCount.filter(item => { return item.id == e.to })
|
||
if (e.attach && (e.attach.type === 'leaveTeam' || e.attach.type === 'dismissTeam' || e.attach.type === 'removeTeamMembers' || e.attach.type === 'addTeamMembers')) {
|
||
} else {
|
||
if (newTeamCount.length > 0) {
|
||
console.log(newTeamCount, '2222')
|
||
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 === false ? 0 : 1})
|
||
}
|
||
wx.setStorageSync('teamsSessionsCount', teamCount)
|
||
}
|
||
}
|
||
if (currentPage.route == 'pages/tabBar/home') {
|
||
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: 0,
|
||
text: `${notice_count + groupCount}`
|
||
})
|
||
} else {
|
||
wx.removeTabBarBadge({
|
||
index: 0
|
||
})
|
||
}
|
||
}
|
||
})
|
||
}
|
||
setTimeout(() => {
|
||
currentPage.onShow()
|
||
wx.vibrateLong({
|
||
success: () => {
|
||
console.log('来消息了,震动~~')
|
||
}
|
||
})
|
||
}, 800)
|
||
})
|
||
|
||
nim.on('myTeamMembers', function (e) {
|
||
let muteNotList = []
|
||
if (e && e.length > 0) {
|
||
e.forEach((i) => {
|
||
if (i.muteTeam) {
|
||
muteNotList.push({
|
||
teamId: i.teamId,
|
||
muteNotiType: i.muteTeam ? 1 : 0,
|
||
muteTeam: i.muteTeam
|
||
})
|
||
}
|
||
})
|
||
setTimeout(() => {
|
||
wx.setStorageSync('muteNotList', muteNotList)
|
||
})
|
||
}
|
||
})
|
||
return nim
|
||
}
|
||
|
||
module.exports = {
|
||
IM
|
||
}
|