tim-handler.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /**
  2. * tim v2.7.6
  3. */
  4. const TIM = require('./tim-wx.js');
  5. let selToID
  6. ,loginInfo
  7. ,sdkAppID
  8. ,avChatRoomId
  9. ,selSess
  10. ,tim
  11. ;
  12. function sdkLogin(userInfo, avChatRoomId, callback, callbackOptions) {
  13. tim.login({userID: userInfo.identifier, userSig: userInfo.userSig})
  14. .then(()=> {
  15. //登录成功 加入大群
  16. loginInfo = userInfo;
  17. avChatRoomId = avChatRoomId;
  18. callback & callback({
  19. callback: callbackOptions
  20. });
  21. })
  22. .catch((err) => {
  23. callback & callback({
  24. errCode: err,
  25. errMsg: err.ErrorInfo,
  26. callback: callbackOptions
  27. });
  28. });
  29. }
  30. // 创建群
  31. function createBigGroup(options, callback, callbackOptions) {
  32. avChatRoomId = options.roomID;
  33. tim.createGroup({
  34. groupID: options.roomID,
  35. type: TIM.TYPES.GRP_AVCHATROOM,
  36. maxMemberNum:500,
  37. name: options.roomName || '',
  38. memberList: [],
  39. })
  40. .then(()=> { // 创建成功
  41. selToID = options.roomID;
  42. tim.joinGroup({ groupID: options.roomID, type: TIM.TYPES.GRP_AVCHATROOM })
  43. .then(function(imResponse) {
  44. callback && callback({
  45. errCode: 0,
  46. callback: callbackOptions
  47. });
  48. })
  49. })
  50. .catch((ret) => {
  51. callback && callback({
  52. errCode: ret.ErrorCode,
  53. errMsg: ret.err_msg,
  54. callback: callbackOptions
  55. });
  56. })
  57. }
  58. //进入群
  59. function applyJoinBigGroup(groupId, callback, callbackOptions) {
  60. selSess = null;
  61. tim.joinGroup({ groupID: groupId, type: TIM.TYPES.GRP_AVCHATROOM })
  62. .then(function(imResponse) {
  63. console.log(imResponse.data,'申请加群核心回调')
  64. switch (imResponse.data.status) {
  65. case TIM.TYPES.JOIN_STATUS_WAIT_APPROVAL: // 等待管理员同意
  66. break;
  67. case TIM.TYPES.JOIN_STATUS_SUCCESS: // 加群成功
  68. selToID = groupId;
  69. callback && callback({
  70. errCode: 0,
  71. callback: callbackOptions
  72. });
  73. console.log(imResponse.data.group); // 加入的群组资料
  74. break;
  75. case TIM.TYPES.JOIN_STATUS_ALREADY_IN_GROUP: // 已经在群中
  76. callback && callback({
  77. errCode: 666,
  78. callback: callbackOptions
  79. });
  80. break;
  81. default:
  82. break;
  83. }
  84. }).catch(function(err){
  85. console.error('进群请求失败', err.ErrorInfo);
  86. callback && callback({
  87. errCode: 999,
  88. errMsg: err.ErrorInfo || 'IM进群失败',
  89. callback: callbackOptions
  90. });
  91. console.warn('joinGroup error:', err); // 申请加群失败的相关信息
  92. });
  93. }
  94. // 连麦发送自定义消息
  95. function sendC2CCustomMsg(toUserID, msg, callback) {
  96. console.log('tim-handler发送自定义',msg,toUserID)
  97. let form = {
  98. data: msg.data || '',
  99. description: msg.desc || '',
  100. extension: msg.ext || ''
  101. }
  102. if (
  103. form.data.length === 0 &&
  104. form.description.length === 0 &&
  105. form.extension.length === 0
  106. ) {
  107. return
  108. }
  109. let conversationType = form.data.includes('linkmic') ? TIM.TYPES.CONV_C2C : TIM.TYPES.CONV_GROUP
  110. const message = tim.createCustomMessage({
  111. to: toUserID,
  112. conversationType: conversationType,
  113. payload: {
  114. data: form.data,
  115. description: form.description,
  116. extension: form.extension
  117. }
  118. })
  119. tim.sendMessage(message,{
  120. // 如果接收方不在线,则消息将存入漫游,且进行离线推送(在接收方 App 退后台或者进程被 kill 的情况下)。接入侧可自定义离线推送的标题及内容
  121. offlinePushInfo: {
  122. disablePush:false,
  123. title: '收到一条新消息', // 离线推送标题
  124. description: '请进入APP内查看', // 离线推送内容
  125. androidOPPOChannelID: '' // 离线推送设置 OPPO 手机 8.0 系统及以上的渠道 ID
  126. }
  127. })
  128. .then(() => {
  129. console.log('发自定义消息成功');
  130. callback && callback({
  131. errCode: 0,
  132. errMsg: ""
  133. });
  134. })
  135. .catch(err => {
  136. console.error('发自定义消息失败:', err);
  137. callback && callback({
  138. errCode: -1,
  139. errMsg: '发自定义消息失败:' + err.ErrorInfo
  140. });
  141. })
  142. Object.assign(form, {
  143. data: '',
  144. description: '',
  145. extension: ''
  146. })
  147. }
  148. //发送文本消息
  149. function sendTextMessage(msg,userInfo,callback) {
  150. console.log('tim-handler普通消息',msg)
  151. let message = tim.createCustomMessage({
  152. to: msg.to,
  153. conversationType: TIM.TYPES.CONV_GROUP,
  154. // 消息优先级,用于群聊(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)
  155. // 支持的枚举值:TIM.TYPES.MSG_PRIORITY_HIGH, TIM.TYPES.MSG_PRIORITY_NORMAL(默认), TIM.TYPES.MSG_PRIORITY_LOW, TIM.TYPES.MSG_PRIORITY_LOWEST
  156. // priority: TIM.TYPES.MSG_PRIORITY_NORMAL,
  157. payload: {
  158. description:msg.text,
  159. extension: msg.text,
  160. data:'{"cmd":"CustomTextMsg","data":{"nickName":"'+userInfo.userName+'","headPic":"'+userInfo.userAvatar+'"}}'
  161. }
  162. });
  163. // 2. 发送消息
  164. tim.sendMessage(message,{
  165. // 如果接收方不在线,则消息将存入漫游,且进行离线推送(在接收方 App 退后台或者进程被 kill 的情况下)。接入侧可自定义离线推送的标题及内容
  166. offlinePushInfo: {
  167. disablePush:false,
  168. title: '收到一条新消息', // 离线推送标题
  169. description: '请进入APP内查看', // 离线推送内容
  170. androidOPPOChannelID: '' // 离线推送设置 OPPO 手机 8.0 系统及以上的渠道 ID
  171. }
  172. })
  173. .then(function(imResponse) {
  174. // 发送成功
  175. console.log(imResponse,'文本消息发送成功');
  176. callback && callback();
  177. }).catch(function(imError) {
  178. // 发送失败
  179. console.warn('sendMessage error:', imError);
  180. });
  181. }
  182. //发送文本消息
  183. function sendTextNewMessage(msg,userInfo,callback) {
  184. console.log('tim-handler普通消息',msg)
  185. let message = tim.createTextMessage({
  186. to: msg.to,
  187. conversationType: TIM.TYPES.CONV_GROUP,
  188. // 消息优先级,用于群聊(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)
  189. // 支持的枚举值:TIM.TYPES.MSG_PRIORITY_HIGH, TIM.TYPES.MSG_PRIORITY_NORMAL(默认), TIM.TYPES.MSG_PRIORITY_LOW, TIM.TYPES.MSG_PRIORITY_LOWEST
  190. // priority: TIM.TYPES.MSG_PRIORITY_NORMAL,
  191. payload: {
  192. text:'{"cmd":"CustomTextMsg","data":{"userName":"'+userInfo.userName+'","nickName":"'+userInfo.userName+'","headPic":"'+userInfo.userAvatar+'","text":"'+msg.text+'"}}'
  193. }
  194. });
  195. // 2. 发送消息
  196. tim.sendMessage(message,{
  197. // 如果接收方不在线,则消息将存入漫游,且进行离线推送(在接收方 App 退后台或者进程被 kill 的情况下)。接入侧可自定义离线推送的标题及内容
  198. offlinePushInfo: {
  199. disablePush:false,
  200. title: '', // 离线推送标题
  201. description: '', // 离线推送内容
  202. androidOPPOChannelID: '' // 离线推送设置 OPPO 手机 8.0 系统及以上的渠道 ID
  203. }
  204. })
  205. .then(function(imResponse) {
  206. // 发送成功
  207. console.log(imResponse,'文本消息发送成功');
  208. callback && callback();
  209. }).catch(function(imError) {
  210. // 发送失败
  211. console.warn('sendMessage error:', imError);
  212. });
  213. }
  214. // 解散群
  215. function destroyGroup() {
  216. tim.dismissGroup(avChatRoomId)
  217. .then(function(imResponse) { // 解散成功
  218. avChatRoomId = '';
  219. console.log(imResponse.data.groupID,'解散成功'); // 被解散的群组 ID
  220. }).catch(function(imError) {
  221. console.warn('dismissGroup error:', imError); // 解散群组失败的相关信息
  222. });
  223. }
  224. //退出群
  225. function quitBigGroup() {
  226. if(avChatRoomId){
  227. tim.quitGroup(avChatRoomId)
  228. .then(function(imResponse) {
  229. console.log(imResponse.data.groupID, 'IM退群成功'); // 退出成功的群 ID
  230. }).catch(function(imError){
  231. console.warn('quitGroup error:', imError); // 退出群组失败的相关信息
  232. })
  233. }
  234. }
  235. //登出
  236. function logout() {
  237. //登出
  238. tim.logout()
  239. .then(function(imResponse) {
  240. console.log(imResponse.data,'IM登出成功'); // 登出成功
  241. if(loginInfo) {
  242. loginInfo.identifier = null;
  243. loginInfo.userSig = null;
  244. }
  245. }).catch(function(imError) {
  246. console.warn('logout error:', imError);
  247. })
  248. }
  249. function init(opts){
  250. sdkAppID = opts.sdkAppID;
  251. avChatRoomId = opts.avChatRoomId || 0;
  252. selToID = opts.selToID;
  253. // 初始化 SDK 实例
  254. tim = opts.tim;
  255. }
  256. module.exports = {
  257. init : init,
  258. sdkLogin : sdkLogin,
  259. createBigGroup : createBigGroup,
  260. applyJoinBigGroup : applyJoinBigGroup,
  261. sendC2CCustomMsg : sendC2CCustomMsg,
  262. sendTextMessage : sendTextMessage,
  263. sendTextNewMessage : sendTextNewMessage,
  264. quitBigGroup : quitBigGroup,
  265. destroyGroup : destroyGroup,
  266. logout : logout,
  267. };