tim.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. import {toast} from "../utils/utils";
  2. import Config from "./config";
  3. import TIM from "tim-wx-sdk";
  4. import dayjs from "dayjs";
  5. // var log = require('../utils/log');
  6. import {LOG_TAGS} from '../model/enum';
  7. import MyLogManager from "../utils/log";
  8. var log = new MyLogManager(LOG_TAGS.LIVE);
  9. class Tim {
  10. static instance = null
  11. static SDKAppID = 0;
  12. _SDKInstance = null
  13. _userId = ""
  14. //IM交互
  15. static VERSION = "1.0.0";//协议版本
  16. static IM_ACTION_HAND = 66601;//举手
  17. static IM_ACTION_HAND_OK = 66602;//举手同意
  18. static IM_ACTION_HAND_CANCEL = 66603;//举手拒绝
  19. static IM_ACTION_QUIT_LINK = 66609;//下麦
  20. static getInstance() {
  21. if (!Tim.instance) {
  22. Tim.instance = new Tim();
  23. }
  24. return Tim.instance;
  25. }
  26. constructor() {
  27. let options = {
  28. SDKAppID: Tim.SDKAppID
  29. }
  30. let tim = TIM.create(options);
  31. tim.setLogLevel(Config.IM_LOG_LEVEL);
  32. this._SDKInstance = tim;
  33. }
  34. getSDK() {
  35. return this._SDKInstance;
  36. }
  37. async login(userId, userSig) {
  38. await this._SDKInstance.login({
  39. userID: userId,
  40. userSig: userSig
  41. });
  42. }
  43. async logout() {
  44. const res = await this._SDKInstance.logout();
  45. return res.data
  46. }
  47. _nextReqMessageID = ''
  48. isCompleted = false
  49. _messageList = []
  50. async getConversationList() {
  51. const res = await this._SDKInstance.getConversationList();
  52. return res.data.conversationList
  53. }
  54. async getMessageList(targetUserId, count = 10) {
  55. if (this.isCompleted) {
  56. return this._messageList
  57. }
  58. const res = await this._SDKInstance.getMessageList({
  59. conversationID: `C2C${targetUserId}`,
  60. nextReqMessageID: this._nextReqMessageID,
  61. count: count > 15 ? 15 : count
  62. });
  63. this._nextReqMessageID = res.data.nextReqMessageID
  64. this.isCompleted = res.data.isCompleted
  65. this._messageList = res.data.messageList
  66. return this._messageList
  67. }
  68. async getUserProfile(userId) {
  69. const res = await this._SDKInstance.getUserProfile({
  70. // 请注意:即使只拉取一个用户的资料,也需要用数组类型,例如:userIDList: ['user1']
  71. userIDList: [userId]
  72. });
  73. return res.data
  74. }
  75. async updateMyProfile(userInfo) {
  76. await this._SDKInstance.updateMyProfile({
  77. nick: userInfo.nickname,
  78. avatar: userInfo.avatar,
  79. gender: userInfo.gender === 1 ? TIM.TYPES.GENDER_MALE : TIM.TYPES.GENDER_FEMALE
  80. });
  81. }
  82. async joinGroup(groupID) {
  83. let data;
  84. const res = await this._SDKInstance.joinGroup({groupID, type: TIM.TYPES.GRP_AVCHATROOM})
  85. if (res.data.status == TIM.TYPES.JOIN_STATUS_SUCCESS ||
  86. res.data.status == TIM.TYPES.JOIN_STATUS_ALREADY_IN_GROUP) {
  87. const field = await this._SDKInstance.getGroupProfile({
  88. groupID,
  89. groupCustomFieldFilter: ['isCanShare', 'isStuVideo', 'isAudVideo', 'isAudText']
  90. });
  91. data = field.data?.group?.groupCustomField;
  92. }
  93. return data;
  94. }
  95. async quitGroup(groupID) {
  96. try{
  97. await this._SDKInstance.quitGroup(groupID)
  98. } catch(err) {
  99. log.error('退群入口C groupId:', groupID, err);
  100. }
  101. }
  102. createGroupTextMsg(content, to) {
  103. return this.createMessage(TIM.TYPES.MSG_TEXT, content, to)
  104. }
  105. createHandMsg(to) {
  106. return this.createMessage(TIM.TYPES.MSG_CUSTOM,
  107. {version: Tim.VERSION, action: Tim.IM_ACTION_HAND, to: '', time: dayjs().valueOf()}
  108. , to)
  109. }
  110. createHandOKMsg(time, to) {
  111. return this.createMessage(TIM.TYPES.MSG_CUSTOM,
  112. {version: Tim.VERSION, action: Tim.IM_ACTION_HAND_OK, to: '', time}, to)
  113. }
  114. createHandCancelMsg(time, to) {
  115. return this.createMessage(TIM.TYPES.MSG_CUSTOM,
  116. {version: Tim.VERSION, action: Tim.IM_ACTION_HAND_CANCEL, to: '', time}, to)
  117. }
  118. createMessage(type, content, targetUserId, conversationType = TIM.TYPES.CONV_GROUP) {
  119. let message
  120. const params = {
  121. to: targetUserId,
  122. // conversationType: TIM.TYPES.CONV_C2C,
  123. conversationType,
  124. payload: null
  125. }
  126. switch (type) {
  127. case TIM.TYPES.MSG_TEXT:
  128. params.payload = {text: content}
  129. message = this._SDKInstance.createTextMessage({...params})
  130. break;
  131. case TIM.TYPES.MSG_FILE:
  132. params.payload = {file: content}
  133. message = this._SDKInstance.createImageMessage({...params})
  134. break;
  135. case TIM.TYPES.MSG_CUSTOM:
  136. params.payload = {
  137. data: JSON.stringify(content),
  138. description: '',
  139. extension: '',
  140. }
  141. message = this._SDKInstance.createCustomMessage({...params})
  142. break;
  143. default:
  144. throw Error("未知消息类型")
  145. }
  146. return message
  147. }
  148. async sendCMD(message, errorHandler) {
  149. try {
  150. const res = await this._SDKInstance.sendMessage(message);
  151. return res.data
  152. } catch (e) {
  153. if (errorHandler) {
  154. return errorHandler(e);
  155. }
  156. if (e.code == 10017) {
  157. toast("禁止互动中...")
  158. } else {
  159. toast(`互动失败${e.message}`)
  160. }
  161. }
  162. }
  163. async sendMessage(message) {
  164. try {
  165. const res = await this._SDKInstance.sendMessage(message);
  166. return res.data
  167. } catch (e) {
  168. console.log(e)
  169. if (e.code == 10017) {
  170. // throw new Error("您已被禁言")
  171. throw new Error("直播间已禁言")
  172. } else {
  173. throw new Error(`发送失败${e.message}`)
  174. }
  175. }
  176. }
  177. async setMessageRead(targetUserId) {
  178. const res = await this._SDKInstance.setMessageRead({conversationID: `C2C${targetUserId}`});
  179. return res.data
  180. }
  181. reset() {
  182. this._nextReqMessageID = ''
  183. this.isCompleted = false
  184. this._messageList = []
  185. return this
  186. }
  187. }
  188. export default Tim;