tim.js 5.9 KB

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