123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308 |
- // #ifdef H5
- import './runtime'
- // #endif
- import TIM from './tim-wx';
- import LibGenerateTestUserSig from "./lib-generate-test-usersig-es.min";
- const EXPIRETIME = 604800;
- const LOGTAG = '--JHIM--:';
- const LOGTAGGROUP = '--JHIM-GROUP--:';
- class JhImGroup {
- constructor(jhim, groupId, groupType, name ,avatar) {
- this.jhim = jhim;
- this.groupId = groupId;
- this.groupType = groupType;
- this.name = name;
- this.avatar = avatar;
- this.groupReady = false;
- return this;
- }
- get ready() {
- return this.jhim.ready && this.groupReady;
- }
- join(isOwner) {
- console.log(LOGTAGGROUP + 'join', this.groupId, isOwner);
-
- let join = () => {
- console.log(LOGTAGGROUP + 'dojoin', this.groupId);
- return this.jhim.tim.joinGroup({
- groupID: this.groupId,
- type: TIM.TYPES.GRP_AVCHATROOM
- }).then(res => {
- console.log(LOGTAGGROUP + 'joinGrouped', res.data.status);
- switch (res.data.status) {
- case TIM.TYPES.JOIN_STATUS_WAIT_APPROVAL: // 等待管理员同意
- break;
- case TIM.TYPES.JOIN_STATUS_SUCCESS: // 加群成功
- case TIM.TYPES.JOIN_STATUS_ALREADY_IN_GROUP: // 已经在群中
- this.groupReady = true;
- break;
- }
- });
- }
- return this.jhim.tim.searchGroupByID(this.groupId).then(() => {
- return join();
- }, () => {
- console.log(LOGTAGGROUP + 'createGroup');
- return this.jhim.tim.createGroup({
- groupID: this.groupId,
- name: this.groupId,
- type: this.groupType,
- }).then(() => {
- return join();
- });
- });
- }
- exit(dismiss) {
- console.log(LOGTAGGROUP + 'exit', this.groupId, dismiss);
- if(dismiss) {
- return this.jhim.tim.dismissGroup(this.groupId).then(res=>{
- this.groupReady = false;
- console.log(LOGTAGGROUP+'dismissGroup', res);
- return res;
- });
- }else {
- return this.jhim.tim.quitGroup(this.groupId).then(res=>{
- this.groupReady = false;
- console.log(LOGTAGGROUP+'quitGroup', res);
- return res;
- });
- }
- }
- getProfile() {
- return this.jhim.tim.getGroupProfile({
- groupID: this.groupId
- }).then(res => {
- return res.data.group;
- });
- // memberCount
- }
- on(cb, type) {
- if(!cb) return this;
- cb.jhimgroupcb = event => {
- let msgs = [];
- event.data.forEach(v => {
- if(v.conversationType==TIM.TYPES.CONV_GROUP && v.to==this.groupId && (!type || type==v.type)){
- msgs.push(v)
- }
- });
- if(msgs.length) cb(msgs);
- }
- this.jhim.tim.on(TIM.EVENT.MESSAGE_RECEIVED, cb.jhimgroupcb);
- return this;
- }
- off(cb) {
- if(!cb) return this;
- this.jhim.tim.off(TIM.EVENT.MESSAGE_RECEIVED, cb.jhimgroupcb);
- return this;
- }
- onReady(cb) {
- if(!cb) return this;
- if(this.jhim.ready) cb(true);//cb(this.ready);
- cb.jhimcbready = event => {
- cb(this.ready)
- }
- cb.jhimcbnotready = event => {
- cb(this.ready)
- }
- this.jhim.tim.on(TIM.EVENT.SDK_READY, cb.jhimcbready);
- this.jhim.tim.on(TIM.EVENT.SDK_NOT_READY, cb.jhimcbnotready);
- return this;
- }
- offReady(cb) {
- if(!cb) return this;
- this.jhim.tim.off(TIM.EVENT.SDK_READY, cb.jhimcbready);
- this.jhim.tim.off(TIM.EVENT.SDK_NOT_READY, cb.jhimcbnotready);
- return this;
- }
- sendText(text) {
- const message = this.jhim.tim.createTextMessage({
- to: this.groupId,
- conversationType: TIM.TYPES.CONV_GROUP,
- payload: {
- text
- },
- })
- message.nick = this.name;
- message.avatar = this.avatar;
- return this.jhim.tim.sendMessage(message).then(() => message);
- }
- sendCustomMessage(payload) {
- const message = this.tim.createCustomMessage({
- to: this.groupId,
- conversationType: TIM.TYPES.CONV_GROUP,
- payload,
- })
- message.nick = this.name;
- message.avatar = this.avatar;
- return this.tim.sendMessage(message).then(() => message);
- }
- }
- export default class JhIm {
- static getInstance(userId, sdkAppID, secretKey, userSig) {
- if (JhIm.instance) return JhIm.instance;
- return JhIm.instance = new JhIm(userId, sdkAppID, secretKey, userSig);
- }
- constructor(userId, sdkAppID, secretKey, userSig) {
- this.userId = userId;
- this.sdkAppID = sdkAppID;
- this.secretKey = secretKey;
- this.tim = TIM.create({
- SDKAppID: this.sdkAppID
- });
- const generator = new LibGenerateTestUserSig(this.sdkAppID, this.secretKey, EXPIRETIME);
- this.userSig = userSig || generator.genTestUserSig(this.userId);
- this.ready = false;
- this.roomId = null;
- this.groupReady = false;
- // 0 普通级别,日志量较多,接入时建议使用
- // 1 release级别,SDK 输出关键信息,生产环境时建议使用
- // 2 告警级别,SDK 只输出告警和错误级别的日志
- // 3 错误级别,SDK 只输出错误级别的日志
- // 4 无日志级别,SDK 将不打印任何日志
- this.tim.setLogLevel(3);
-
- this.TYPES = TIM.TYPES;
- this.EVENT = TIM.EVENT;
- this._readyCb = event => {
- this.ready = true;
- };
- this.tim.on(TIM.EVENT.SDK_READY, this._readyCb);
- this._notReadyCb = event => {
- this.ready = false;
- };
- this.tim.on(TIM.EVENT.SDK_NOT_READY, this._notReadyCb);
- return this;
- }
- destory() {
- this.tim.off(this._readyCb);
- this.tim.off(this._notReadyCb);
- this.tim = null;
- }
- on(name, cb) {
- // 事件
- // https://imsdk-1252463788.file.myqcloud.com/IM_DOC/Web/module-EVENT.html
- // 1. 收到消息 TIM.EVENT.MESSAGE_RECEIVED
- // https://imsdk-1252463788.file.myqcloud.com/IM_DOC/Web/Message.html
- // -- type,
- // -- nick,
- // -- avatar,
- // -- payload
- // -- conversationType
- // -- time,
- // 2. 接入 TIM.EVENT.SDK_READY
- // 3. 掉线 TIM.EVENT.SDK_NOT_READY
- // 4. 收到被踢下线通知 TIM.EVENT.KICKED_OUT
- // TIM.EVENT.KICKED_OUT
- // 5. 收到 SDK 发生错误通知 TIM.EVENT.ERROR
- // - event.name - TIM.EVENT.ERROR
- // - event.data.code - 错误码
- // - event.data.message - 错误信息
- this.tim.on(name, cb);
- return this;
- }
- off(name, cb) {
- this.tim.off(name, cb);
- return this;
- }
- login(name, avatar) {
- if (this.ready) return Promise.resolve();
-
- console.log(LOGTAG + 'login');
- this.name = name;
- this.avatar = avatar;
-
- return new Promise(resolve => {
- let cb = event => {
- console.log(LOGTAG + 'ready');
- this.ready = true;
- this.tim.updateMyProfile({
- nick: name,
- avatar: avatar,
- allowType: TIM.TYPES.ALLOW_TYPE_ALLOW_ANY
- });
- // this.tim.on(TIM.EVENT.MESSAGE_RECEIVED, event=>{
- // debugger
- // });
- resolve();
- this.tim.off(TIM.EVENT.SDK_READY, cb, this);
- }
- this.tim.on(TIM.EVENT.SDK_READY, cb, this);
- this.tim.login({
- userID: this.userId,
- userSig: this.userSig,
- })
- });
- }
- logout() {
- return this.tim.logout().then(() => {
- this.ready = false;
- });
- }
- onReady(cb) {
- if(!cb) return this;
- cb.jhimcbready = event => {
- this.ready = true;
- }
- cb.jhimcbnotready = event => {
- this.ready = false;
- }
- this.tim.on(TIM.EVENT.SDK_READY, cb.jhimcbready);
- this.tim.on(TIM.EVENT.SDK_NOT_READY, cb.jhimcbnotready);
- }
- offReady(cb) {
- if(!cb) return this;
- this.tim.off(TIM.EVENT.SDK_READY, cb.jhimcbready);
- this.tim.off(TIM.EVENT.SDK_NOT_READY, cb.jhimcbnotready);
- }
- createGroup(groupId, type=TIM.TYPES.GRP_AVCHATROOM) {
- return new JhImGroup(this, groupId, type, this.name ,this.avatar);
- }
- onMessageRecv(cb, from, type, conversationType=TIM.TIM.TYPES.CONV_C2C) {
- if(!cb) return;
- cb.jhimcb = event => {
- let msgs = event.data.filter( v=> {
- if(from && v.from != from) return false;
- if(conversationType && v.conversationType != conversationType) return false;
- if(type && v.type != type) return false;
- return true;
- });
- msgs.length && cb(msgs);
- }
- this.tim.on(TIM.EVENT.MESSAGE_RECEIVED, cb.jhimcb);
- }
- offMessageRecv(cb) {
- if(!cb) return;
- this.time.off(TIM.EVENT.MESSAGE_RECEIVED,cb.jhimcb);
- }
- sendText(text, to) {
- const message = this.tim.createTextMessage({
- to,
- conversationType: TIM.TIM.TYPES.CONV_C2C,
- payload: {
- text
- },
- })
- message.nick = this.name;
- message.avatar = this.avatar;
- return this.tim.sendMessage(message).then(() => message);
- }
- sendCustomMessage(payload, to) {
- const message = this.tim.createCustomMessage({
- to: to,
- conversationType: TIM.TYPES.CONV_C2C,
- payload,
- })
- message.nick = this.name;
- message.avatar = this.avatar;
- return this.tim.sendMessage(message).then(() => message);
- }
- }
|