123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222 |
- import {toast} from "../utils/utils";
- import Config from "./config";
- import TIM from "tim-wx-sdk";
- import dayjs from "dayjs";
- // var log = require('../utils/log');
- import {LOG_TAGS} from '../model/enum';
- import MyLogManager from "../utils/log";
- var log = new MyLogManager(LOG_TAGS.LIVE);
- class Tim {
- static instance = null
- static SDKAppID = 0;
- _SDKInstance = null
- _userId = ""
- //IM交互
- static VERSION = "1.0.0";//协议版本
- static IM_ACTION_HAND = 66601;//举手
- static IM_ACTION_HAND_OK = 66602;//举手同意
- static IM_ACTION_HAND_CANCEL = 66603;//举手拒绝
- static IM_ACTION_QUIT_LINK = 66609;//下麦
- static getInstance() {
- if (!Tim.instance) {
- Tim.instance = new Tim();
- }
- return Tim.instance;
- }
- constructor() {
- let options = {
- SDKAppID: Tim.SDKAppID
- }
- let tim = TIM.create(options);
- tim.setLogLevel(Config.IM_LOG_LEVEL);
- this._SDKInstance = tim;
- }
- getSDK() {
- return this._SDKInstance;
- }
- async login(userId, userSig) {
- await this._SDKInstance.login({
- userID: userId,
- userSig: userSig
- });
- }
- async logout() {
- const res = await this._SDKInstance.logout();
- return res.data
- }
- _nextReqMessageID = ''
- isCompleted = false
- _messageList = []
- async getConversationList() {
- const res = await this._SDKInstance.getConversationList();
- return res.data.conversationList
- }
- async getMessageList(targetUserId, count = 10) {
- if (this.isCompleted) {
- return this._messageList
- }
- const res = await this._SDKInstance.getMessageList({
- conversationID: `C2C${targetUserId}`,
- nextReqMessageID: this._nextReqMessageID,
- count: count > 15 ? 15 : count
- });
- this._nextReqMessageID = res.data.nextReqMessageID
- this.isCompleted = res.data.isCompleted
- this._messageList = res.data.messageList
- return this._messageList
- }
- async getUserProfile(userId) {
- const res = await this._SDKInstance.getUserProfile({
- // 请注意:即使只拉取一个用户的资料,也需要用数组类型,例如:userIDList: ['user1']
- userIDList: [userId]
- });
- return res.data
- }
- async updateMyProfile(userInfo) {
- await this._SDKInstance.updateMyProfile({
- nick: userInfo.nickname,
- avatar: userInfo.avatar,
- gender: userInfo.gender === 1 ? TIM.TYPES.GENDER_MALE : TIM.TYPES.GENDER_FEMALE
- });
- }
- async joinGroup(groupID) {
- let data;
- const res = await this._SDKInstance.joinGroup({groupID, type: TIM.TYPES.GRP_AVCHATROOM})
- if (res.data.status == TIM.TYPES.JOIN_STATUS_SUCCESS ||
- res.data.status == TIM.TYPES.JOIN_STATUS_ALREADY_IN_GROUP) {
- const field = await this._SDKInstance.getGroupProfile({
- groupID,
- groupCustomFieldFilter: ['isCanShare', 'isStuVideo', 'isAudVideo', 'isAudText']
- });
- data = field.data?.group?.groupCustomField;
- }
- return data;
- }
- async quitGroup(groupID) {
- try{
- await this._SDKInstance.quitGroup(groupID)
- } catch(err) {
- log.error('退群入口C groupId:', groupID, err);
- }
- }
- createGroupTextMsg(content, to) {
- return this.createMessage(TIM.TYPES.MSG_TEXT, content, to)
- }
- createHandMsg(to) {
- return this.createMessage(TIM.TYPES.MSG_CUSTOM,
- {version: Tim.VERSION, action: Tim.IM_ACTION_HAND, to: '', time: dayjs().valueOf()}
- , to)
- }
- createHandOKMsg(time, to) {
- return this.createMessage(TIM.TYPES.MSG_CUSTOM,
- {version: Tim.VERSION, action: Tim.IM_ACTION_HAND_OK, to: '', time}, to)
- }
- createHandCancelMsg(time, to) {
- return this.createMessage(TIM.TYPES.MSG_CUSTOM,
- {version: Tim.VERSION, action: Tim.IM_ACTION_HAND_CANCEL, to: '', time}, to)
- }
- createMessage(type, content, targetUserId, conversationType = TIM.TYPES.CONV_GROUP) {
- let message
- const params = {
- to: targetUserId,
- // conversationType: TIM.TYPES.CONV_C2C,
- conversationType,
- payload: null
- }
- switch (type) {
- case TIM.TYPES.MSG_TEXT:
- params.payload = {text: content}
- message = this._SDKInstance.createTextMessage({...params})
- break;
- case TIM.TYPES.MSG_FILE:
- params.payload = {file: content}
- message = this._SDKInstance.createImageMessage({...params})
- break;
- case TIM.TYPES.MSG_CUSTOM:
- params.payload = {
- data: JSON.stringify(content),
- description: '',
- extension: '',
- }
- message = this._SDKInstance.createCustomMessage({...params})
- break;
- default:
- throw Error("未知消息类型")
- }
- return message
- }
- async sendCMD(message, errorHandler) {
- try {
- const res = await this._SDKInstance.sendMessage(message);
- return res.data
- } catch (e) {
- if (errorHandler) {
- return errorHandler(e);
- }
- if (e.code == 10017) {
- toast("禁止互动中...")
- } else {
- toast(`互动失败${e.message}`)
- }
- }
- }
- async sendMessage(message) {
- try {
- const res = await this._SDKInstance.sendMessage(message);
- return res.data
- } catch (e) {
- console.log(e)
- if (e.code == 10017) {
- // throw new Error("您已被禁言")
- throw new Error("直播间已禁言")
- } else {
- throw new Error(`发送失败${e.message}`)
- }
- }
- }
- async setMessageRead(targetUserId) {
- const res = await this._SDKInstance.setMessageRead({conversationID: `C2C${targetUserId}`});
- return res.data
- }
- reset() {
- this._nextReqMessageID = ''
- this.isCompleted = false
- this._messageList = []
- return this
- }
- }
- export default Tim;
|