123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284 |
- /**
- * tim v2.7.6
- */
- const TIM = require('./tim-wx.js');
- let selToID
- ,loginInfo
- ,sdkAppID
- ,avChatRoomId
- ,selSess
- ,tim
- ;
- function sdkLogin(userInfo, avChatRoomId, callback, callbackOptions) {
- tim.login({userID: userInfo.identifier, userSig: userInfo.userSig})
- .then(()=> {
- //登录成功 加入大群
- loginInfo = userInfo;
- avChatRoomId = avChatRoomId;
- callback & callback({
- callback: callbackOptions
- });
- })
- .catch((err) => {
- callback & callback({
- errCode: err,
- errMsg: err.ErrorInfo,
- callback: callbackOptions
- });
- });
- }
- // 创建群
- function createBigGroup(options, callback, callbackOptions) {
- avChatRoomId = options.roomID;
- tim.createGroup({
- groupID: options.roomID,
- type: TIM.TYPES.GRP_AVCHATROOM,
- maxMemberNum:500,
- name: options.roomName || '',
- memberList: [],
- })
- .then(()=> { // 创建成功
- selToID = options.roomID;
- tim.joinGroup({ groupID: options.roomID, type: TIM.TYPES.GRP_AVCHATROOM })
- .then(function(imResponse) {
- callback && callback({
- errCode: 0,
- callback: callbackOptions
- });
- })
- })
- .catch((ret) => {
- callback && callback({
- errCode: ret.ErrorCode,
- errMsg: ret.err_msg,
- callback: callbackOptions
- });
- })
- }
- //进入群
- function applyJoinBigGroup(groupId, callback, callbackOptions) {
- selSess = null;
- tim.joinGroup({ groupID: groupId, type: TIM.TYPES.GRP_AVCHATROOM })
- .then(function(imResponse) {
- console.log(imResponse.data,'申请加群核心回调')
- switch (imResponse.data.status) {
- case TIM.TYPES.JOIN_STATUS_WAIT_APPROVAL: // 等待管理员同意
- break;
- case TIM.TYPES.JOIN_STATUS_SUCCESS: // 加群成功
- selToID = groupId;
- callback && callback({
- errCode: 0,
- callback: callbackOptions
- });
- console.log(imResponse.data.group); // 加入的群组资料
- break;
- case TIM.TYPES.JOIN_STATUS_ALREADY_IN_GROUP: // 已经在群中
- callback && callback({
- errCode: 666,
- callback: callbackOptions
- });
- break;
- default:
- break;
- }
- }).catch(function(err){
- console.error('进群请求失败', err.ErrorInfo);
- callback && callback({
- errCode: 999,
- errMsg: err.ErrorInfo || 'IM进群失败',
- callback: callbackOptions
- });
- console.warn('joinGroup error:', err); // 申请加群失败的相关信息
- });
- }
- // 连麦发送自定义消息
- function sendC2CCustomMsg(toUserID, msg, callback) {
- console.log('tim-handler发送自定义',msg,toUserID)
- let form = {
- data: msg.data || '',
- description: msg.desc || '',
- extension: msg.ext || ''
- }
- if (
- form.data.length === 0 &&
- form.description.length === 0 &&
- form.extension.length === 0
- ) {
- return
- }
- let conversationType = form.data.includes('linkmic') ? TIM.TYPES.CONV_C2C : TIM.TYPES.CONV_GROUP
- const message = tim.createCustomMessage({
- to: toUserID,
- conversationType: conversationType,
- payload: {
- data: form.data,
- description: form.description,
- extension: form.extension
- }
- })
- tim.sendMessage(message,{
- // 如果接收方不在线,则消息将存入漫游,且进行离线推送(在接收方 App 退后台或者进程被 kill 的情况下)。接入侧可自定义离线推送的标题及内容
- offlinePushInfo: {
- disablePush:false,
- title: '收到一条新消息', // 离线推送标题
- description: '请进入APP内查看', // 离线推送内容
- androidOPPOChannelID: '' // 离线推送设置 OPPO 手机 8.0 系统及以上的渠道 ID
- }
- })
- .then(() => {
- console.log('发自定义消息成功');
- callback && callback({
- errCode: 0,
- errMsg: ""
- });
- })
- .catch(err => {
- console.error('发自定义消息失败:', err);
- callback && callback({
- errCode: -1,
- errMsg: '发自定义消息失败:' + err.ErrorInfo
- });
- })
- Object.assign(form, {
- data: '',
- description: '',
- extension: ''
- })
- }
- //发送文本消息
- function sendTextMessage(msg,userInfo,callback) {
- console.log('tim-handler普通消息',msg)
- let message = tim.createCustomMessage({
- to: msg.to,
- conversationType: TIM.TYPES.CONV_GROUP,
- // 消息优先级,用于群聊(v2.4.2起支持)。如果某个群的消息超过了频率限制,后台会优先下发高优先级的消息,详细请参考:https://cloud.tencent.com/document/product/269/3663#.E6.B6.88.E6.81.AF.E4.BC.98.E5.85.88.E7.BA.A7.E4.B8.8E.E9.A2.91.E7.8E.87.E6.8E.A7.E5.88.B6)
- // 支持的枚举值:TIM.TYPES.MSG_PRIORITY_HIGH, TIM.TYPES.MSG_PRIORITY_NORMAL(默认), TIM.TYPES.MSG_PRIORITY_LOW, TIM.TYPES.MSG_PRIORITY_LOWEST
- // priority: TIM.TYPES.MSG_PRIORITY_NORMAL,
- payload: {
- description:msg.text,
- extension: msg.text,
- data:'{"cmd":"CustomTextMsg","data":{"nickName":"'+userInfo.userName+'","headPic":"'+userInfo.userAvatar+'"}}'
- }
- });
- // 2. 发送消息
- tim.sendMessage(message,{
- // 如果接收方不在线,则消息将存入漫游,且进行离线推送(在接收方 App 退后台或者进程被 kill 的情况下)。接入侧可自定义离线推送的标题及内容
- offlinePushInfo: {
- disablePush:false,
- title: '收到一条新消息', // 离线推送标题
- description: '请进入APP内查看', // 离线推送内容
- androidOPPOChannelID: '' // 离线推送设置 OPPO 手机 8.0 系统及以上的渠道 ID
- }
- })
- .then(function(imResponse) {
- // 发送成功
- console.log(imResponse,'文本消息发送成功');
- callback && callback();
- }).catch(function(imError) {
- // 发送失败
- console.warn('sendMessage error:', imError);
- });
- }
- //发送文本消息
- function sendTextNewMessage(msg,userInfo,callback) {
- console.log('tim-handler普通消息',msg)
- let message = tim.createTextMessage({
- to: msg.to,
- conversationType: TIM.TYPES.CONV_GROUP,
- // 消息优先级,用于群聊(v2.4.2起支持)。如果某个群的消息超过了频率限制,后台会优先下发高优先级的消息,详细请参考:https://cloud.tencent.com/document/product/269/3663#.E6.B6.88.E6.81.AF.E4.BC.98.E5.85.88.E7.BA.A7.E4.B8.8E.E9.A2.91.E7.8E.87.E6.8E.A7.E5.88.B6)
- // 支持的枚举值:TIM.TYPES.MSG_PRIORITY_HIGH, TIM.TYPES.MSG_PRIORITY_NORMAL(默认), TIM.TYPES.MSG_PRIORITY_LOW, TIM.TYPES.MSG_PRIORITY_LOWEST
- // priority: TIM.TYPES.MSG_PRIORITY_NORMAL,
- payload: {
- text:'{"cmd":"CustomTextMsg","data":{"userName":"'+userInfo.userName+'","nickName":"'+userInfo.userName+'","headPic":"'+userInfo.userAvatar+'","text":"'+msg.text+'"}}'
- }
- });
- // 2. 发送消息
- tim.sendMessage(message,{
- // 如果接收方不在线,则消息将存入漫游,且进行离线推送(在接收方 App 退后台或者进程被 kill 的情况下)。接入侧可自定义离线推送的标题及内容
- offlinePushInfo: {
- disablePush:false,
- title: '', // 离线推送标题
- description: '', // 离线推送内容
- androidOPPOChannelID: '' // 离线推送设置 OPPO 手机 8.0 系统及以上的渠道 ID
- }
- })
- .then(function(imResponse) {
- // 发送成功
- console.log(imResponse,'文本消息发送成功');
- callback && callback();
- }).catch(function(imError) {
- // 发送失败
- console.warn('sendMessage error:', imError);
- });
- }
- // 解散群
- function destroyGroup() {
- tim.dismissGroup(avChatRoomId)
- .then(function(imResponse) { // 解散成功
- avChatRoomId = '';
- console.log(imResponse.data.groupID,'解散成功'); // 被解散的群组 ID
- }).catch(function(imError) {
- console.warn('dismissGroup error:', imError); // 解散群组失败的相关信息
- });
- }
- //退出群
- function quitBigGroup() {
- if(avChatRoomId){
- tim.quitGroup(avChatRoomId)
- .then(function(imResponse) {
- console.log(imResponse.data.groupID, 'IM退群成功'); // 退出成功的群 ID
- }).catch(function(imError){
- console.warn('quitGroup error:', imError); // 退出群组失败的相关信息
- })
- }
- }
- //登出
- function logout() {
- //登出
- tim.logout()
- .then(function(imResponse) {
- console.log(imResponse.data,'IM登出成功'); // 登出成功
- if(loginInfo) {
- loginInfo.identifier = null;
- loginInfo.userSig = null;
- }
- }).catch(function(imError) {
- console.warn('logout error:', imError);
- })
- }
- function init(opts){
- sdkAppID = opts.sdkAppID;
- avChatRoomId = opts.avChatRoomId || 0;
- selToID = opts.selToID;
- // 初始化 SDK 实例
- tim = opts.tim;
- }
- module.exports = {
- init : init,
- sdkLogin : sdkLogin,
- createBigGroup : createBigGroup,
- applyJoinBigGroup : applyJoinBigGroup,
- sendC2CCustomMsg : sendC2CCustomMsg,
- sendTextMessage : sendTextMessage,
- sendTextNewMessage : sendTextNewMessage,
- quitBigGroup : quitBigGroup,
- destroyGroup : destroyGroup,
- logout : logout,
- };
|