Files
ZhHealth/utils/im/message.js

260 lines
7.7 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import store from '@/store/index.js'
import * as RongIMLib from '@/uni_modules/RongCloud-IMWrapper/js_sdk/index'
const getMessageList = (conversationType, targetId, timeStamp, count, isForward, callback) => {
// 获取消息列表 https://doc.rongcloud.cn/imserver/server/v1/message/objectname#objectName
const objectNames = [
'RC:TxtMsg',
'RC:VcMsg',
'RC:HQVCMsg',
'RC:ImgMsg',
'RC:GIFMsg',
'RC:ImgTextMsg',
'RC:FileMsg',
'RC:LBSMsg',
'RC:SightMsg',
'RC:ReferenceMsg',
'RC:CombineMsg',
'RC:GrpNtf',
'RC:InfoNtf',
'RC:RcNtf',
'RC:IWNormalMsg'
]
RongIMLib.getHistoryMessagesByTimestamp(
conversationType,
targetId,
objectNames,
timeStamp,
count,
isForward,
({
code,
messages
}) => {
if (code === 0) {
callback(messages)
} else {
uni.showToast({
icon: 'error',
title: code
})
}
}
)
}
// 获取好友申请列表
const getPendingList = (callback, total) => {
total = total || 100
RongIMLib.getConversationList([RongIMLib.ConversationType.SYSTEM], total, 0, (res) => {
if (res.code === 0) {
const pendings = res.conversations.filter((item) => {
return item.objectName == RongIMLib.ObjectName.ContactNotification &&
item.latestMessage.operation === 'Request'
})
callback(pendings)
}
})
}
// 群组申请列表,邀请列表
const getGroupPendinglist = (targetId, callback) => {
const conversationType = RongIMLib.ConversationType.SYSTEM
const objectNames = ['RC:ContactNtf']
RongIMLib.getHistoryMessagesByTimestamp(conversationType, targetId, objectNames, 0, 1000, true,
({
messages
}) => {
callback(messages)
}
)
}
/**
* 发送文本消息
* @param {number} conversationType 消息类型
* @param {string} targetId 会话id
* @param {string} content 消息内容
* @param {function} callback 回调函数
*/
const sentText = (conversationType, targetId, content) => {
return new Promise((resolve, reject) => {
const msg = {
conversationType: conversationType,
targetId: String(targetId),
content: {
objectName: 'RC:TxtMsg',
content: content,
userInfo: store.getters.sender
}
}
RongIMLib.sendMessage(msg, ({
code,
messageId
}) => {
if (code === 0) {
if (conversationType == 3) {
RongIMLib.sendReadReceiptRequest(messageId, (res) => {
console.log('发送回执请求', res);
})
}
resolve(messageId)
} else {
uni.showToast({
icon: 'none',
title: '发送失败' + code
})
reject(code)
}
})
})
}
/**
* 发送消息
* @param {number} conversationType 消息类型
* @param {string} targetId 会话id
* @param {string} voiceUrl 录音的本地路径
* @param {integer} time 录音时长
* @param {function} user 本人信息
*/
const sentVoice = (conversationType, targetId, voiceUrl, time) => {
return new Promise((resolve, reject) => {
const msg = {
conversationType: conversationType,
targetId: String(targetId),
content: {
objectName: 'RC:HQVCMsg',
local: 'file://' + plus.io.convertLocalFileSystemURL(voiceUrl),
duration: time == 0 ? 1 : time,
userInfo: store.getters.sender
}
}
RongIMLib.sendMediaMessage(msg, {
success: (messageId) => {
resolve(messageId);
},
progress: (progress, messageId) => {},
cancel: (messageId) => {},
error: (errorCode, messageId) => {
reject(errorCode);
}
})
})
}
// 发送的图片可能会涉及到一次多张用Promise来确保前端一次性清算
const sentImage = (conversationType, targetId, imageUrl) => {
return new Promise((resolve, reject) => {
uni.getImageInfo({
src: imageUrl,
success: (imgInfo) => {
const msg = {
conversationType: conversationType,
targetId: String(targetId),
content: {
objectName: 'RC:ImgMsg',
local: imgInfo.path,
userInfo: store.getters.sender
}
}
RongIMLib.sendMediaMessage(msg, {
success: (messageId) => {
resolve(messageId)
},
progress: (progress, messageId) => {},
cancel: (messageId) => {},
error: (errorCode, messageId) => {
reject(errorCode)
}
})
}
})
})
}
const sentFile = (conversationType, targetId, fileUrl) => {
return new Promise((resolve, reject) => {
const msg = {
conversationType: conversationType,
targetId: String(targetId),
content: {
objectName: 'RC:FileMsg',
local: plus.io.convertLocalFileSystemURL(fileUrl),
userInfo: store.getters.sender
}
}
console.log('发送文件', msg);
RongIMLib.sendMediaMessage(msg, {
success: (messageId) => {
resolve(messageId)
},
progress: (progress, messageId) => {},
cancel: (messageId) => {},
error: (errorCode, messageId) => {
reject(errorCode)
}
})
})
}
// 发送地理位置
const sentLocation = (conversationType, targetId, location) => {
return new Promise((resolve, reject) => {
const msg = {
conversationType: conversationType,
targetId: String(targetId),
objectName: 'RC:LBSMsg',
content: {
customType: 2,
objectName: 'RC:LBSMsg',
customFields: {
name: location.name,
latitude: Number(location.latitude),
longitude: Number(location.longitude),
thumbnail: ''
},
userInfo: store.getters.sender,
}
}
RongIMLib.sendMessage(msg, ({
code,
messageId
}) => {
if (code === 0) {
if (conversationType == 3) {
RongIMLib.sendReadReceiptRequest(messageId, (res) => {
console.log('发送回执请求', res);
})
}
resolve(messageId)
} else {
uni.showToast({
icon: 'none',
title: '发送失败' + code
})
reject(code)
}
})
})
}
export default {
getMessageList,
getPendingList,
getGroupPendinglist,
sentText,
sentVoice,
sentImage,
sentFile,
sentLocation
}