123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284 |
- 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,{
-
- offlinePushInfo: {
- disablePush:false,
- title: '收到一条新消息',
- description: '请进入APP内查看',
- androidOPPOChannelID: ''
- }
- })
- .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,
-
-
-
- payload: {
- description:msg.text,
- extension: msg.text,
- data:'{"cmd":"CustomTextMsg","data":{"nickName":"'+userInfo.userName+'","headPic":"'+userInfo.userAvatar+'"}}'
- }
- });
- tim.sendMessage(message,{
-
- offlinePushInfo: {
- disablePush:false,
- title: '收到一条新消息',
- description: '请进入APP内查看',
- androidOPPOChannelID: ''
- }
- })
- .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,
-
-
-
- payload: {
- text:'{"cmd":"CustomTextMsg","data":{"userName":"'+userInfo.userName+'","nickName":"'+userInfo.userName+'","headPic":"'+userInfo.userAvatar+'","text":"'+msg.text+'"}}'
- }
- });
- tim.sendMessage(message,{
-
- offlinePushInfo: {
- disablePush:false,
- title: '',
- description: '',
- androidOPPOChannelID: ''
- }
- })
- .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,'解散成功');
- }).catch(function(imError) {
- console.warn('dismissGroup error:', imError);
- });
- }
- function quitBigGroup() {
- if(avChatRoomId){
- tim.quitGroup(avChatRoomId)
- .then(function(imResponse) {
- console.log(imResponse.data.groupID, 'IM退群成功');
- }).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;
-
- 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,
- };
|