zby-live-sdk
Version:
This is a live SDK for weclassroom.
319 lines (285 loc) • 11.1 kB
JavaScript
/* eslint-disable */
/**
* 消息通道的入口,设置信息后进入房间
* willxue
* fbfbc-1@163.com
* 2016/08/16
**/
// (function () {
// var w = window;
import CONTROL from './interactWithChannelControl'
import bridge from '../../util/bridge'
let ENTRY = {
configInfoObj: null, //配置信息
roomInfoObj: '', //房间信息
enterMaxCount: 1, //尝试连接并进入房间的次数
msgindex: 0,
userid: '',
role: '',
logmode: 1,
channeltype: 1, //0=有所有通道;1=仅有control通道;2=仅有chat通道
istestFromhtml: 0, //是否是测试页
//public 第一步 设置配置信息,含用户信息等
setConfigInfo: function (configstr) {
// configstr = {"address":"http://fwef",
// "user":{"userid":"123","username":"san","token":"fwe","role":"teacher","avatar":"http:\\fwe","code":"1234"},
// "language":0,"logmode":0,"channeltype":0}
try {
this.recordLogToFile('info', 'channnel init params : ' + configstr);
this.configInfoObj = JSON.parse(configstr);
this.configInfoObj.encodetype = 'base64';
if (this.configInfoObj) {
this.configInfoObj.user.role = this.translateRole(this.configInfoObj.user.role);
this.userid = this.configInfoObj.user.userid;
this.role = this.configInfoObj.user.role;
this.logmode = this.configInfoObj.logmode;
var addresslenth = this.configInfoObj.address.length;
if (addresslenth > 0) {
if (this.configInfoObj.address[addresslenth - 1] != '\\' && this.configInfoObj.address[addresslenth - 1] != '/') {
this.configInfoObj.address = this.configInfoObj.address + '/';
}
} else {
this.recordLogToFile('error', 'server addrres is null');
}
if (this.configInfoObj.hasOwnProperty('channeltype')) {
this.channeltype = this.configInfoObj.channeltype;
}
}
} catch (e) {
console.log(e);
}
},
//public 第二步 进入教室
enterRoom: function (roominfo, dealChannelMsg) {
// roominfo = {"roomid":"16","classmode":1,"playmode":1,"institutionid":545}
//此处传入的是客户端定义的课程类型
//0=无效模式;1=1对1模式;2=小组课模式;3=大班课模式;4=小班课模式,同小组课模式,只不过人数更多,最多20个;5=互动大班课
//6=口令互动大班课;1000=励模式的互动大班课(TODO: 太特化了,后面看是否能一般化)
try {
this.initpara();
this.msgindex = 0;
ENTRY.recordLogToFile('info', "enter room info" + roominfo);
this.roomInfoObj = JSON.parse(roominfo);
if (this.channeltype == 2) {
// w.CHAT.enterRoom();
} else {
CONTROL.enterRoom(dealChannelMsg);
}
} catch (e) {
console.log(e);
}
},
leaveRoom: function () {
if (this.channeltype == 2) {
if (w.CHAT) {
w.CHAT.leaveRoom();
}
} else {
CONTROL.leaveRoom();
}
},
//public 获取配置信息
getConfigInfo: function () {
return this.configInfoObj;
},
//public 获取房间信息
getRoomInfo: function () {
return this.roomInfoObj;
},
//转换一下用户角色的定义 用户角色 "1=teacher"/"2=student"/"3=assistant"/"4=parent"
translateRole: function (rolefore) {
var role = this.configInfoObj.user.role;
if (typeof rolefore == "string") {
if (rolefore == 'teacher') {
role = '1';
} else if (rolefore == 'student') {
role = '2';
} else if (rolefore == 'assistant') {
role = '3';
} else if (rolefore == 'parent') {
role = '4';
}
}
return role;
},
//转换一下用户角色的定义 用户角色 "1=teacher"/"2=student"/"3=assistant"/"4=parent"
translateRoleBack: function (rolelocal) {
if (typeof rolelocal == "string") {
if (rolelocal == '1') {
return 'teacher';
} else if (rolelocal == '2') {
return 'student';
} else if (rolelocal == '3') {
return 'assistant';
} else if (rolelocal == '4') {
return 'parent';
}
}
return '';
},
//判断是否是我自己的消息
isMyMsg: function (userid) {
if (this.configInfoObj && this.configInfoObj.user.userid == userid) {
return true;
}
return false;
},
//判断是否是大班课
isLargeClass: function () {
if (this.roomInfoObj) {
if (this.roomInfoObj.classmode == 3 || this.roomInfoObj.classmode == 5 || this.roomInfoObj.classmode == 6 || this.roomInfoObj.classmode == 1000) {
return true;
}
}
return false;
},
//获取课程类型,即将客户端传入的课程类型转换成数据库中定义的课程类型,传给信道
getClassTypeInDb: function () {
//客户端定义的课程类型
//0=无效模式;1=1对1模式;2=小组课模式;3=大班课模式;4=小班课模式,同小组课模式,只不过人数更多,最多20个;5=互动大班课
//6=口令互动大班课;1000=励模式的互动大班课
//数据库定义课程类型
//0=一对一';'3'=小组课';'1'=大班课(口令访问)';'2':'大班课(密码访问)';'6':'互动大班课(口令访问)';'5':'互动大班课(密码访问)'
var classtype = 1;
if (this.roomInfoObj) {
classtype = this.roomInfoObj.classmode;
if (this.roomInfoObj.classmode == 1) {
classtype = 0;
} else if (this.roomInfoObj.classmode == 2) {
classtype = 3;
} else if (this.roomInfoObj.classmode == 3) {
classtype = 1; //客户端把口令和密码方式的大班课都统一为了大班课
} else if (this.roomInfoObj.classmode == 4) {
classtype = 7; //客户端给的4是小班课
}
// 找出互动大班课
else if (this.roomInfoObj.classmode == 5 || this.roomInfoObj.classmode == 6) {
classtype = 5; //为了增加信道给麦加的判断
}
}
return classtype; //默认互动大班课吧
},
//判断是否是我自己的消息
getMsgIndex: function () {
this.msgindex++;
return this.msgindex;
},
//进入房间,初始化参数
initpara: function () {},
parseIntMySimple: function (strvar) {
var ret = 0;
for (var i = 0; i < strvar.length; ++i) {
ret *= 10;
var num1 = Number(strvar[i]);
ret += num1;
}
return ret;
},
getMsgKey: function (index) {
var time = new Date();
var key = this.userid + '_' + this.role + '_' + index + '_' + time.getTime();
return key;
},
//日志记录 msgtype:0:debug,1:info,2:warnning,3:error,4:critical
recordLogToFile: function (msgtype, msgbody, productName) {
try {
if (msgtype == 'f' && this.logmode == 0) {
return;
}
if (this.channeltype == 0 || this.channeltype == 1) {
//日志级别,0:debug,1:info,2:warnning,3:error,4:critical
bridge.writeLog(msgtype, JSON.stringify(msgbody), productName)
}
if (this.channeltype == 2) {
if (w.chatobj && w.chatobj.onRecordJsLog) {
w.chatobj.onRecordJsLog(msgtype, msgbody);
}
}
} catch (e) {}
},
//上报提示信息
reportHintInfo: function (content) {
if (w.controlObj && w.controlObj.onSelfHealthReport) {
w.controlObj.onSelfHealthReport('report', content);
}
},
//记录try catch异常
recordException: function (e) {
try {
var logmsg = e.stack + '_' + e.message;
bridge.writeLog('e', logmsg)
} catch (e) {}
},
//获取bridge
setupWebViewJavascriptBridge: function (callback) {
if (window.WebViewJavascriptBridge) {
return callback(WebViewJavascriptBridge);
}
if (window.WVJBCallbacks) {
return window.WVJBCallbacks.push(callback);
}
window.WVJBCallbacks = [callback];
var WVJBIframe = document.createElement('iframe');
WVJBIframe.style.display = 'none';
WVJBIframe.src = 'wvjbscheme://__BRIDGE_LOADED__';
document.documentElement.appendChild(WVJBIframe);
setTimeout(function () {
document.documentElement.removeChild(WVJBIframe)
}, 0);
},
// //html模拟客户端调用
// htmlEnterRoom: function () {
// var roomid = document.getElementById("roomid").value;
// var userid = document.getElementById("username").value;
// var channel = document.getElementById("channeltype").value;
// var usercodeinput = document.getElementById("usercode").value;
// var serverurl = document.getElementById("serverurl").value;
// var tokeninput = document.getElementById("token").value;
// var instidinput = document.getElementById("instid").value;
// var username = userid;
// var config = {};
// //config.address = 'http://123.56.176.151:14081/';//开发
// //config.address = 'http://dev-im.weclassroom.com:14080/';//开发
// //config.address = 'https://test-im.weclassroom.com';//开发
// //config.address = 'http://123.56.176.151:14082/';//开发
// config.address = serverurl; //开发
// //config.address = 'http://123.56.193.134:10080/';
// config.language = 0;
// config.logmode = 2;
// config.user = {};
// config.user.userid = userid;
// config.user.username = username;
// if (usercodeinput != 0) {
// config.user.code = usercodeinput;
// config.user.token = '';
// } else {
// config.user.token = tokeninput;
// //config.user.token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJyb2xlIjoiMiIsInN1YiI6MTAwMDg0LCJpc3MiOiJodHRwczpcL1wvdGVzdC1hcGkud2VjbGFzc3Jvb20uY29tXC91c2VyXC9sb2dpbiIsImlhdCI6MTQ4NzIxNzk0NCwiZXhwIjoxNDg5ODA5OTQ0LCJuYmYiOjE0ODcyMTc5NDQsImp0aSI6IjlmZDcwYzVmOGRkNjM2YTc0MzA2MjI4OGUxYmVlYmQ5In0.RgpquwjhqcYUKf1l3XjbGggbc8Y1jEY_45xZpfgnOIg';
// }
// if (username.indexOf("teacher") == 0) {
// config.user.role = 'teacher';
// } else if (username.indexOf('assistant') == 0) {
// config.user.role = 'assistant';
// } else {
// config.user.role = 'student';
// }
// config.channeltype = channel;
// config.user.avatar = 'http://www.qq1234.org/uploads/allimg/150709/8_150709170804_8.jpg';
// this.setConfigInfo(JSON.stringify(config));
// var roominfo = {};
// roominfo.roomid = roomid;
// roominfo.classmode = 5; //1=1对1模式;2=1对多模式;3=大班课模式;5=互动大班课密码;6=互动大班课口令
// roominfo.playmode = 1;
// roominfo.institutionid = instidinput;
// this.enterRoom(JSON.stringify(roominfo));
// this.istestFromhtml = 1;
// if (w.CHATEST) {
// w.CHATEST.checkorder = 1;
// }
// },
// htmlLeaveRoom: function () {
// CONTROL.leaveRoom();
// }
};
export default ENTRY
// })();