12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645 |
- var us = {}; //全局的命名空间
- us.cheerio = require("cheerio"); //dom处理类
- us.qs = require("querystring"); //引用文件解析包
- us.http = require("http"); //引用http server服务
- us.mysql = require("./mysql.js"); //引用执行存储过程包
- /**
- * 保持知识建构的时间变量
- **/
- us.savecscltime = null;
- /**
- * 用户的链接池集合,以用户id作为搜索键
- **/
- us.connect = {
- //userguid:{pageid : us.userConnect, pageid : us.userConnect}
- }
- /**
- * 用户的数据集合集合,以用户id作为搜索键
- **/
- us.user = {
- //userId : userInfo //用户连接(请求), 这里可以是http短连接可以使socket长连接
- }
- /**
- * 未来课堂数据结构
- **/
- us.cscl = {
- //"nodes":[{"id":"9f6d7459-d376-4c0c-944b-a6b140085903","type":"icon-start","x":625,"y":145,"properties":{},"zIndex":1002,"text":{"x":625,"y":145,"value":"起始"}},{"id":"16e18308-7517-4162-8b32-a877b7cf42a0","type":"icon-card","x":325,"y":445,"properties":{},"zIndex":1002},{"id":"5c492b0d-092d-4d84-bd70-4d5734de9266","type":"icon-select","x":970,"y":510,"properties":{},"zIndex":1002}],
- //"edges":[],
- //"backgroundUrl":"img/bg.ccee1c62.png"
- }
- /**
- * 离线数据
- **/
- us.offLineMessage = {
- //pageId : us.userOffLineMessage
- }
- /**
- * 思维网格
- **/
- us.mindNetwork = {
- // mindid: {
- // }
- }
- /**
- * 同步课堂
- **/
- us.realTimeClass = {
- // mindid: {
- // }
- }
- //文档列表
- us.word = {
- //docId : {
- // user : {userId : [pageId, pageId],
- // userId : [pageId, pageId]},
- // content : docel,
- // history : [operand(操作信息对象), operand(操作信息对象)]
- // } //群信息结构
- //docId : {user : {userId : [pageId, pageId], userId : [pageId, pageId]}, content : docel, history : [operand(操作信息对象), operand(操作信息对象)] } //文档信息结构
- }
- //excel列表
- us.excel = {
- //excelId : {user : {userId : [pageId, pageId], userId : [pageId, pageId]}, content : docel, history : [operand(操作信息对象), operand(操作信息对象)], name : 文件名 } //群信息结构
- //excelId : {user : {userId : [pageId, pageId], userId : [pageId, pageId]}, content : docel, history : [operand(操作信息对象), operand(操作信息对象)], name : 文件名 } //群信息结构
- }
- //导航文件列表
- us.nav = {
- //fileId : {
- // user : {userId : [pageId, pageId], userId : [pageId, pageId]},
- // navInfo : {
- // logo : '',
- // nav : [ {pageId: '', name : ''}, {pageId: '', name : '', child :[ {pageId: '', name : ''} ]} ]
- // },
- // page : {pageId : pageContent, pageId : pageContent},
- // history : [operand(操作信息对象), operand(操作信息对象)]
- //} //导航文档数据结构
- }
- //知识建构的数据结构
- us.cscldata = function () {
- return {
- id: "",
- userid: "",
- name: "",
- num: "",
- data: "",
- isClose: "",
- create_at: "",
- createuser: "",
- smailheadportrait: ""
- };
- }
- /**
- * 用户链接类
- *
- * @param {string} pageId 页面ID(用户登录唯一识别ID)
- * @param {object} response 对象
- **/
- us.userConnect = function (pageId, response) {
- return {
- "pageId": pageId, //登录用户唯一识别id
- "response": response, //这里可以是http短连接以后可以换成socket长连接
- "loginTime": new Date(), //用户登录时间
- "offLineTime": null //用户下线时间
- }
- }
- /**
- * 用户数据类
- *
- * @param {string} requset对象
- * @param {object} response对象
- **/
- us.userInfo = function (userid, username) {
- return {
- "userId": userid, //用户id
- "userName": username //用户名
- }
- }
- /**
- * 用户离线数据
- * @param {string} type 用户登录类型
- */
- us.userOffLineMessage = function (type) {
- return {
- "message": [], //用户的消息数组
- "type": type, //链接的端 pc或者pho
- "word": [], //word数据
- "excel": [], //excel数据
- "mindNetwork": [], //思维网格数据
- "realTimeClass": [], //实施课堂数据
- "cscl": [], //未来课堂数据
- "nav": [] //导航模式数据
- }
- }
- /**
- * 用户的离线消息结构
- *
- * @param {string} sendid 发送者id
- * @param {string} type 发送的类型, 俱乐部使用时用us开头,后面带用途。例如us.disk或者us.friend us.word
- * @param {string} messageinfo 的处理
- **/
- us.message = function (sendid, type, messageinfo) {
- return {
- "id": us.createGuid(), //新生成的guid
- "sendId": sendid, //发送消息人的id.
- "time": new Date(), //发送时间
- "type": type, //类型 俱乐部使用时用us开头,后面带用途。例如us.disk或者us.friend us.word
- "messageInfo": messageinfo //{消息结构} 此处为object 如类型为'us.word' 则消息结构为us.word
- }
- }
- /*
- messageinfo 消息结构的处理
- {
- id: string, //行id 多行时用逗号隔开的形式 guid,guid,guid,guid
- type: string, // 处理的类型 如:"add"||"delete"||"update"
- docId: string, // "guid" 文档id
- pageId: pageid, //页面唯一识别ID
- content: string // 变化的内容 add的时候为添加到添加行的下一行
- nextId : string //下一行id
- }
- */
- process.on('uncaughtException', (e) => {
- console.error('process error is:', e.message);
- });
- /**
- * 创建服务
- *
- * @param {object} requset 请求对象
- * @param {object} response 响应对象
- **/
- us.server = us.http.createServer(function (requset, response) {
- //设置跨域
- response.writeHead(200, {
- "Content-Type": "text/html;application/json;charset=utf-8",
- 'Access-Control-Allow-Origin': '*',
- 'Access-Control-Allow-Headers': 'Origin, No-Cache, X-Requested-With, If-Modified-Since, Pragma, Last-Modified, Cache-Control, Expires, Content-Type, X-E4M-With'
- });
- //设置参数,并执行回调
- us.unifyDispose(requset, response, function (param) {
- //console.log(param)
- if (param && param[0]) {
- //参数解码 us.parm为全局变量 记录所有参数
- var _userinfo,
- _funname = param[0],
- //参数解码 中文处理
- _param = param.splice(1);
- //执行回调函数
- switch (_funname) {
- //执行登录函数
- case 'login':
- _userinfo = JSON.parse(_param[0]); //登录用户的信息
- us.login(requset, response, _userinfo.userId, _userinfo.pageId, _userinfo.userName, _userinfo.type);
- break;
- case 'logout':
- us.logout(response, _param[0], _param[1]);
- break;
- //执行轮询函数
- case 'polling':
- us.polling(requset, response, _param[0], _param[1], _param[2], _param[3]);
- break;
- //获取文档信息
- case 'getWord':
- us.getWord(response, _param[0], _param[1], _param[2]);
- break;
- //获取Excel信息
- case 'getExcel':
- us.getExcel(response, _param[0], _param[1], _param[2]);
- break;
- //获取导航文件信息
- case 'getNav':
- us.getNav(response, _param[0], _param[1], _param[2]);
- break;
- //获取思维网格信息
- case 'getMindNetwork':
- us.getMindNetwork(response, _param[0], _param[1], _param[2]);
- break;
- //获取未来课堂信息
- case 'getcscl':
- us.getcscl(response, _param[0], _param[1], _param[2]);
- break;
- //获取未来课堂数据
- case 'getcscldata':
- us.getcscldata(response, _param[0], _param[1], _param[2]);
- break;
- //获取文档信息
- case 'getRealTimeClass':
- us.getRealTimeClass(response, _param[0], _param[1], _param[2]);
- break;
- case 'getNavPageContent':
- us.getNavPageContent(response, _param[0], _param[1]);
- break;
- //执行发送函数
- case 'send':
- us.send(response, _param[0]);
- break;
- case 'quitOffice':
- us.quitOffice(response, _param[0]);
- break;
- case 'quitcscl':
- us.quitcscl(response, _param[0]);
- break;
- case 'newWordSave':
- us.newWordSave(response, _param[0], _param[1], _param[2], _param[3], _param[4]);
- break;
- case 'newExcelSave':
- us.newExcelSave(response, _param[0], _param[1], _param[2], _param[3], _param[4]);
- break;
- //新建导航文件保存
- case 'newNavSave':
- us.newNavSave(response, _param[0], _param[1], _param[2], _param[3], _param[4], _param[5], _param[6]);
- break;
- //加入文档编辑
- case 'addWord':
- break;
- //加入Excel表格编辑
- case 'addExcel':
- us.addExcel(response, _param[0]);
- break;
- //加入导航文件
- case 'addNav':
- break;
- case 'copyPageContent':
- us.copyPageContent(response, _param[0], _param[1], _param[2], _param[3]);
- break;
- case 'updateName':
- us.updateName(response, _param[0], _param[1], _param[2], _param[3], _param[4]);
- break;
- case 'insertModifyRecord':
- us.insertModifyRecord(response, _param[0], _param[1], _param[2], _param[3], _param[4], _param[4]);
- break;
- case 'addUser':
- us.addUser(response, _param[0], _param[1], _param[2], _param[3], _param[4]);
- break;
- //其余执行
- default:
- response.end(0);
- }
- }
- else {
- response.end("");
- }
- });
- });
- /**
- * 协同历史(带保存功能,修改了存储过程 既添加历史也保存内容)
- *
- * @param {object} request 请求对象
- * @param {string} type 类型
- * @param {guid} fileid 文件id
- * @param {string} filename 文件名
- * @param {string} html 内容
- * @param {guid} userid 用户ID
- * @param {guid} pageid 页面ID
- **/
- us.insertModifyRecord = function (response, type, fileid, filename, html, userid, pageid) {
- var _html;
- if (us[type][fileid] && us[type][fileid].content) {
- _html = us[type][fileid].content('body').html().replace(/'/ig, "''").replace(/"/ig, "\\\"");
- } else {
- _html = html;
- }
- //发送保存文件请求
- us.mysql.usselect([
- '127.0.0.1',
- 'UseStudio_Office',
- 'InsertDocumentModifyRecord',
- userid,
- fileid,
- _html,
- filename
- ], function (ret) {
- if (ret) {
- var _historyid = JSON.parse(ret)[0][0].historyid;
- var _message = {};
- var _messageinfo = us.message(userid, 'us.' + type, {
- id: us.createGuid(),
- type: "insertModifyRecord",
- historyid: _historyid,
- docId: fileid
- });
- _message[fileid] = [_messageinfo];
- us.broadcast(_message, pageid, type);
- response.end(_historyid); //返回文档内容
- } else {
- response.end(ret); //返回文档内容
- }
- });
- }
- /**
- * 更新文件名
- *
- * @param {object} request 请求对象
- * @param {string} type 类型
- * @param {guid} fileid 文件id
- * @param {string} filename 文件名
- * @param {guid} userid 用户ID
- * @param {guid} pageid 页面ID
- **/
- us.updateName = function (response, type, fileid, filename, userid, pageid) {
- //发送保存文件请求
- us.mysql.usselect([
- 'sqlserver.1473.cn',
- 'UseStudio_Disk',
- 'UpdateFileInfo',
- fileid,
- filename
- ], function (ret) {
- if (ret) {
- var _message = {};
- var _messageinfo = us.message(userid, 'us.' + type, {
- id: us.createGuid(),
- type: "updateName",
- name: filename,
- docId: fileid
- });
- _message[fileid] = [_messageinfo];
- us.broadcast(_message, pageid, type);
- response.end('updateNameSuccess'); //返回文档内容
- } else {
- response.end('updateNameFail'); //返回文档内容
- }
- });
- }
- /**
- * 添加用户
- *
- * @param {object} request 请求对象
- * @param {string} type 类型
- * @param {guid} fileid 文件id
- * @param {string} userids 用户id(多个用逗号隔开)
- * @param {guid} userid 用户ID
- * @param {guid} pageid 页面ID
- **/
- us.addUser = function (response, type, fileid, userids, userid, pageid) {
- var _message = {};
- var _messageinfo = us.message(userid, 'us.' + type, {
- id: us.createGuid(),
- type: "addUser",
- userIds: userids,
- docId: fileid
- });
- _message[fileid] = [_messageinfo];
- us.broadcast(_message, pageid, type);
- response.end('addUserSuccess'); //返回文档内容
- }
- /**
- * 根据用户集合,获取详细的用户信息
- *
- * @param {object} userids 用户id集合
- **/
- us.getuserbytype = function (userids) {
- var _userinfo = [];
- //console.log(us.user);
- for (var i in userids) {
- if (us.user[i]) {
- _userinfo.push(us.user[i])
- }
- }
- return _userinfo;
- }
- /**
- * 用户加入某个项目后通知所有已经在项目的用户
- *
- * @param {object} userinfo 用户信息
- * @param {string} type 项目类型
- * @param {string} fileid 项目的id
- **/
- us.sendUser = function (userinfo, type, pageid, fileid) {
- var _message = {};
- var _messageinfo = us.message(userinfo.userid, 'us.' + type, {
- id: us.createGuid(),
- type: "addUser",
- userinfo: userinfo,
- docId: fileid
- });
- _message[fileid] = [_messageinfo];
- // console.log("2222222222")
- // console.log(_message)
- us.broadcast(_message, pageid, type);
- }
- /**
- * 获取用户传参异步
- *
- * @param {object} request 请求对象
- * @param {function} cb 回调函数
- **/
- us.unifyDispose = function (req, res, cb) {
- var i, _param = "";
- //接收参数
- req.on("data", function (chunk) {
- _param += chunk;
- });
- //接收完毕
- req.on("end", function () {
- //console.log(_param)
- //前端传入的字符串转化成对象
- if (_param) {
- _param = us.qs.parse(_param);
- }
- //console.log(_param)
- //拆分参数
- try {
- _param = _param.mode.split(",")
- } catch (e) {
- _param = req.query;
- }
- if (_param) {
- //循环解析,由于前端发送过来的信息是经过url加密的,这里是解密
- for (i = 0; i < _param.length; i++) {
- _param[i] = decodeURIComponent(_param[i]); //解析
- }
- } else {
- _param = [];
- }
- //回调到指定处理
- cb(_param);
- });
- //请求超时处理
- res.on('timeout', function (e) {
- try {
- //console.log('请求超时'); //错误消息的输出
- //res.end("false"); //返回值
- }
- catch (e) {
- console.error('process error is:', e.message);
- }
- });
- //设置30秒后超时
- res.setTimeout(60000, function () {
- try {
- //console.log("响应超时.");
- res.end(""); //返回值
- }
- catch (e) {
- console.error('process error is:', e.message);
- }
- });
- //错误处理
- req.on("error", function (e) {
- try {
- console.log(e);
- }
- catch (e) {
- console.error('process error is:', e.message);
- }
- });
- }
- /**
- * 获取用户传参异步
- *
- * @param {object} response 响应对象
- * @param {array} userid 用户ID
- * @param {string} pageid 页面ID(登录唯一识别ID)
- * @param {string} username 用户名
- * @param {type} username 用户登录类型
- **/
- us.login = function (requset, response, userid, pageid, username, type) {
- if (userid && pageid) {
- //用户登录信息表,以后移入redis
- //如果用户信息不存在则新建一个用户信息存入
- if (!us.user[userid]) {
- username = username || "";
- var _userinfo = us.userInfo(userid, username); //初始化用户信息
- us.user[userid] = _userinfo;
- }
- //添加用户轮询列表
- us.polling(requset, response, userid, pageid, type);
- }
- }
- /**
- * 用户下线,清理用户的数据
- */
- us.deluser = function (userid, pageid) {
- if (pageid && us.offLineMessage[pageid]) {
- var i, j, z; //定义循环变量
- var _userid, _fileid, _message, _docinfo, _messageinfo,
- _csclids = us.offLineMessage[pageid].cscl;
- //其他应用的数据
- // _wordids = us.offLineMessage[pageid].word, //得到加入的所有word
- // _excelids = us.offLineMessage[pageid].excel; //得到加入的所有excel
- console.log("1111111111111")
- console.log(_csclids)
- console.log(pageid)
- console.log(us.offLineMessage[pageid])
- //删除用户离校消息记录
- delete us.offLineMessage[pageid];
- //知识架构的下线的处理
- for (i = 0; i < _csclids.length; i++) { //循环处理每一个文档
- _docinfo = us.cscl[_csclids[i]]; //记录文档信息
- if (_docinfo) { //如果存在文档信息
- _fileid = _csclids[i];
- _userid = us.quitUserList(_docinfo, userid, pageid); //将用户在协同列表中删除
- }
- }
- console.log(_userid)
- //下线后通知所有的用户
- if (_userid) {
- _message = {};
- var _messageinfo = us.message(userid, 'us.cscl', {
- id: us.createGuid(),
- type: "delUser",
- userinfo: us.user[_userid],
- docId: _fileid
- });
- _message[_fileid] = [_messageinfo];
- console.log(_message)
- us.broadcast(_message, pageid, "cscl");
- }
- }
- //其他应用的处理
- // //word的下线的处理
- // for (i = 0; i < _wordids.length; i++) { //循环处理每一个文档
- // _docinfo = us.word[_wordids[i]]; //记录文档信息
- // if (_docinfo) { //如果存在文档信息
- // us.quitUserList(_docinfo, userid, pageid); //将用户在协同列表中删除
- // us.insertHistory(_docinfo, 'word'); //插入历史
- // }
- // }
- // //excel的下线的处理
- // for (j = 0; i < _excelids.length; j++) {
- // _docinfo = us.excel[_excelids[j]]; //记录文档信息
- // if (_docinfo) { //如果存在文档信息
- // us.quitUserList(_docinfo, userid, pageid); //将用户在协同列表中删除
- // us.insertHistory(_docinfo, 'excel'); //插入历史
- // }
- // }
- }
- /**
- * 用户下线,用计时器实时的监控用户是否在线
- */
- us.offLine = function () {
- var i, j;
- var _user, _connect = us.connect;
- //循环目前所有的连接用户
- for (i in _connect) {
- _user = _connect[i];
- for (j in _user) {
- console.log("====================offline======================");
- console.log(i);
- console.log(j);
- console.log(_user[j].response.finished);
- console.log((_user[j].response.connection == null || _user[j].response.connection.destroyed));
- //判断用户已下线
- if (_user[j].response.finished || (_user[j].response.connection == null || _user[j].response.connection.destroyed)) {
- //if (_user[j].response.connection == null || _user[j].response.connection.destroyed) {
- //等待五秒后,判断用户是否重连,重连的用户不为下线用户
- us.offLineByUser(i, j, _user);
- }
- }
- }
- }
- /**
- * 用户下线
- */
- us.offLineByUser = function (i, j, _user) {
- setTimeout(() => {
- if (_user[j].response.finished || (_user[j].response.connection == null || _user[j].response.connection.destroyed)) {
- var k, istrue = true;
- us.deluser(i, j); //删除用户的数据
- delete _user[j];
- for (k in _user) { istrue = false; break }
- //这个用户下所有的连接用户都不在了
- if (istrue) {
- //用户登录数据清理,用于已经下线
- delete us.user[i];
- delete us.connect[i];
- }
- }
- }, 5000);
- }
- /**
- * 获取用户传参异步
- *
- * @param {object} response 响应对象
- * @param {string} userid 用户ID
- * @param {string} pageid 页面ID(用户登录唯一识别ID)
- **/
- us.polling = function (requset, response, userid, pageid, type) {
- // 监听请求的close事件
- // requset.on('close', () => {
- // console.log("--------" + userid + "-------------" + pageid);
- // //us.offLine(response, userid, pageid)
- // });
- //判断轮询的用户是否在连接池中
- if (us.connect[userid]) {
- var _data,
- _userarray = us.connect[userid], //用户连接池,获取制定用户连接的数量,
- _messagearray = us.offLineMessage[pageid] //用户离线信息
- ;
- const chunkSize = 100;
- //获取当前连接的用户,如果获取成功那么直接修改用户的连接
- if (_userarray[pageid]) {
- //防止重复多连
- try {
- _userarray[pageid].response.end("");
- }
- catch (e) {
- }
- _userarray[pageid].response = response;
- if (_messagearray && _messagearray.message.length > 0) {
- _data = JSON.stringify(_messagearray.message);
- for (let i = 0; i < _data.length; i += chunkSize) {
- const chunk = _data.slice(i, i + chunkSize);
- response.write(chunk);
- }
- response.write("\r\n");
- _messagearray.message = [];
- }
- else {
- us.offLineMessage[pageid] = us.offLineMessage[pageid] || us.userOffLineMessage(type); //初始化用户离线消息记录
- }
- }
- else {
- //如果用户连接不存在
- _userarray[pageid] = us.userConnect(pageid, response); //那么直接添加用户连接 添加到用户连接数组里
- us.offLineMessage[pageid] = us.userOffLineMessage(type); //初始化用户离线消息记录
- }
- }
- else {
- var _userconect = us.userConnect(pageid, response); //创建用户连接对象
- us.connect[userid] = {}; //添加用户连接对象
- us.connect[userid][pageid] = _userconect;
- us.offLineMessage[pageid] = us.offLineMessage[pageid] || us.userOffLineMessage(type); //初始化用户离线消息记录
- }
- }
- /**
- * 发送消息
- *
- * @param {object} response 响应对象
- * @param {array} messageinfo 参数数组
- **/
- us.send = function (response, messageinfo) {
- var i, j,
- _message,
- _messagelist = {},
- _messageobj = JSON.parse(messageinfo) //获取传入的json对象
- ;
- //循环用户的信息处理
- for (i in _messageobj) {
- //console.log(i);
- switch (i) {
- //思维网格交互
- case "us.mindNetwork":
- _messagearr = _messageobj[i]; //获取word消息
- //得到word所有的用户
- for (j = 0; j < _messagearr.length; j++) {
- //生成发送的消息结构体
- _message = us.message(_messagearr[j].sendId, "us.mindNetwork", _messagearr[j].messageInfo);
- //添加消息列表
- if (_messagelist[_messagearr[j].receiveId]) {
- _messagelist[_messagearr[j].receiveId].push(_message);
- } else {
- _messagelist[_messagearr[j].receiveId] = [_message];
- }
- us.mindNetwork[_message.messageInfo.docId] && us.mindNetwork[_message.messageInfo.docId].history.push(_message);
- us.updatemindNetwork(_message.messageInfo);
- }
- //调用群发函数
- _messagearr[0] && us.broadcast(_messagelist, _messagearr[0].messageInfo.pageId, 'mindNetwork');
- break;
- //未来课堂交互
- case "us.cscl":
- _messagearr = _messageobj[i]; //获取未来消息
- //得到未来课堂所有的用户
- for (j = 0; j < _messagearr.length; j++) {
- // //如果存在则直接返回内层的文档
- // if (!us.cscl[calssid].user[userid]) {
- // us.cscl[calssid].user[userid] = [pageid];
- // }
- // else if (us.cscl[calssid].user[userid].indexOf(pageid) == -1) {
- // us.cscl[calssid].user[userid].push(pageid);
- // }
- // try {
- // us.offLineMessage[pageid].cscl.indexOf(calssid) == -1 && us.offLineMessage[pageid].cscl.push(calssid); //添加到用户离线消息的连接wrod记录中
- // }
- // catch (e) {
- // }
- _messagearr[j].messageInfo.type = _messagearr[j].type;
- //生成发送的消息结构体
- _message = us.message(_messagearr[j].sendId, "us.cscl", _messagearr[j].messageInfo);
- _message.ttype = _messagearr[j].type;
- //添加消息列表
- if (_messagelist[_messagearr[j].receiveId]) {
- _messagelist[_messagearr[j].receiveId].push(_message);
- }
- else {
- _messagelist[_messagearr[j].receiveId] = [_message];
- }
- us.cscl[_messagearr[j].receiveId] && us.cscl[_messagearr[j].receiveId].history.push(_message);
- us.updatecscl(_message.messageInfo, _messagearr[j].receiveId, _messagearr[j].type);
- }
- //调用群发函数
- _messagearr[0] && us.broadcast(_messagelist, _messagearr[0].pageId, 'cscl');
- break;
- //同步课堂
- case "us.realTimeClass":
- //console.log("us.realTimeClass");
- _messagearr = _messageobj[i]; //获取word消息
- //得到word所有的用户
- for (j = 0; j < _messagearr.length; j++) {
- //生成发送的消息结构体
- _message = us.message(_messagearr[j].sendId, "us.realTimeClass", _messagearr[j].messageInfo);
- //console.log(_message)
- // console.log(_messagelist[_messagearr[j].receiveId])
- //添加消息列表
- if (_messagelist[_messagearr[j].receiveId]) {
- _messagelist[_messagearr[j].receiveId].push(_message);
- } else {
- _messagelist[_messagearr[j].receiveId] = [_message];
- }
- us.realTimeClass[_message.messageInfo.docId] && us.realTimeClass[_message.messageInfo.docId].history.push(_message);
- us.updaterealTimeClass(_message.messageInfo);
- }
- //调用群发函数
- _messagearr[0] && us.broadcast(_messagelist, _messagearr[0].messageInfo.pageId, 'realTimeClass');
- break;
- //word信息交互
- case "us.word":
- try {
- _messagearr = _messageobj[i]; //获取word消息
- //得到word所有的用户
- for (j = 0; j < _messagearr.length; j++) {
- //生成发送的消息结构体
- _message = us.message(_messagearr[j].sendId, "us.word", _messagearr[j].messageInfo);
- //添加消息列表
- if (_messagelist[_messagearr[j].receiveId]) {
- _messagelist[_messagearr[j].receiveId].push(_message);
- } else {
- _messagelist[_messagearr[j].receiveId] = [_message];
- }
- us.word[_message.messageInfo.docId] && us.word[_message.messageInfo.docId].history.push(_message);
- us.updateWord(_message.messageInfo);
- }
- //由于不能同时操控多个word发送资料,所有信息只同步一个
- //_users = us.word[_messagearr[0].receiveId].user;
- //调用群发函数
- _messagearr[0] && us.broadcast(_messagelist, _messagearr[0].messageInfo.pageId, 'word');
- }
- catch (e) {
- console.log(e)
- }
- break;
- //用户信息的交互
- case "us.excel":
- _messagearr = _messageobj[i]; //获取excel消息
- //得到excel所有的用户
- for (j = 0; j < _messagearr.length; j++) {
- //生成发送的消息结构体
- _message = us.message(_messagearr[j].sendId, _messagearr[j].type, _messagearr[j].messageInfo);
- //添加消息列表
- if (_messagelist[_messagearr[j].receiveId]) {
- _messagelist[_messagearr[j].receiveId].push(_message);
- } else {
- _messagelist[_messagearr[j].receiveId] = [_message];
- }
- us.excel[_message.messageInfo.docId] && us.excel[_message.messageInfo.docId].history.push(_message);
- us.updateExcel(_message.messageInfo);
- }
- //由于不能同时操控多个excel发送资料,所有信息只同步一个
- //_users = us.excel[_messagearr[0].receiveId].user;
- //调用群发函数
- us.broadcast(_messagelist, _messagearr[0].messageInfo.pageId, 'excel');
- break;
- case "us.nav":
- _messagearr = _messageobj[i]; //获取excel消息
- //得到excel所有的用户
- for (j = 0; j < _messagearr.length; j++) {
- //生成发送的消息结构体
- _message = us.message(_messagearr[j].sendId, "us.nav", _messagearr[j].messageInfo);
- //添加消息列表
- if (_messagelist[_messagearr[j].receiveId]) {
- _messagelist[_messagearr[j].receiveId].push(_message);
- } else {
- _messagelist[_messagearr[j].receiveId] = [_message];
- }
- us.nav[_message.messageInfo.docId] && us.nav[_message.messageInfo.docId].history.push(_message);
- us.updateNav(_message.messageInfo);
- }
- //调用群发函数
- us.broadcast(_messagelist, _messagearr[0].messageInfo.pageId, 'nav');
- break;
- }
- }
- response.end('send');
- }
- /**
- * 更新后台思维网格的内容
- * @param {object} message 消息信息
- */
- us.updatemindNetwork = function (messageInfo) {
- if (us.mindNetwork[messageInfo.docId]) {
- var $ = us.mindNetwork[messageInfo.docId].content;
- // console.log("---------------------------------------start")
- // console.log($);
- // console.log("---------------------------------------end")
- // console.log("---------------------------------------infostart")
- // console.log(messageInfo);
- // console.log("---------------------------------------infoend")
- if ($) {
- us.mindNetwork[messageInfo.docId].content[messageInfo.id] = messageInfo.content;
- }
- }
- }
- /**
- * 更新后台未来课堂内容
- * @param {object} message 消息信息
- */
- us.updatecscl = function (messageInfo, classId, ty) {
- if (us.cscl[classId]) {
- var i, j, _is;
- var _obj = us.cscl[classId].content;
- if (ty == "clean") {
- _obj = us.cscl[classId].content = {};
- }
- else {
- _obj.backgroundUrl = messageInfo.backgroundUrl;
- _obj.edges = _obj.edges || [];
- _obj.nodes = _obj.nodes || [];
- //循环线的处理
- for (j = 0; j < messageInfo.edges.length; j++) {
- _is = true;
- for (i = 0; i < _obj.edges.length; i++) {
- if (_obj.edges[i].id == messageInfo.edges[j].id) {
- //如果是修改线同时先又存在那就直接修改
- if (ty == "update") {
- _obj.edges[i] = messageInfo.edges[j];
- }
- //如果是删除线的操作
- else {
- _obj.edges.splice(i, 1);
- }
- _is = false;
- break;
- }
- }
- //修改包含了添加和修改,如果没有修改说明是添加的功能
- if (_is && ty == "update") {
- _obj.edges.push(messageInfo.edges[j])
- }
- }
- for (j = 0; j < messageInfo.nodes.length; j++) {
- _is = true;
- for (i = 0; i < _obj.nodes.length; i++) {
- if (_obj.nodes[i].id == messageInfo.nodes[j].id) {
- //如果是修改节点
- if (ty == "update") {
- _obj.nodes[i] = messageInfo.nodes[j];
- }
- //如果是删除节点
- else {
- _obj.nodes.splice(i, 1);
- }
- _is = false;
- break;
- }
- }
- //修改包含了添加和修改,如果没有修改说明是添加的功能
- if (_is && ty == "update") {
- _obj.nodes.push(messageInfo.nodes[j])
- }
- }
- }
- //如果不存在则去数据库查找
- try {
- clearTimeout(us.savecscltime);
- }
- catch (e) {
- }
- //每次修改每隔五秒保存一次数据,防止数据操作频繁导致数据库太卡
- us.savecscltime = setTimeout(function () {
- us.mysql.usselect(['172.16.12.5', 'pbl', 'updateRoomData', JSON.stringify(_obj), classId], function (ret) { });
- }, 5000)
- }
- }
- /**
- * 更新后台实施课堂内容
- * @param {object} message 消息信息
- */
- us.updaterealTimeClass = function (messageInfo) {
- if (us.realTimeClass[messageInfo.docId]) {
- var $ = us.realTimeClass[messageInfo.docId].content;
- // console.log("---------------------------------------start")
- // console.log($);
- // console.log("---------------------------------------end")
- // console.log("---------------------------------------infostart")
- // console.log(messageInfo);
- // console.log("---------------------------------------infoend")
- if ($) {
- us.realTimeClass[messageInfo.docId].content[messageInfo.id] = messageInfo.content;
- }
- }
- }
- /**
- * 更新后台Word内容
- * @param {object} message 消息信息
- */
- us.updateWord = function (messageInfo) {
- if (us.word[messageInfo.docId]) {
- var $ = us.word[messageInfo.docId].content;
- if ($) {
- switch (messageInfo.type) { // 判断消息种类
- case 'update':
- us.updateEditorLine(messageInfo, $); //修改
- break;
- case 'add':
- us.addEditorLine(messageInfo, $); //添加
- break;
- case 'delete':
- us.deleteEditorLine(messageInfo, $); //删除
- break;
- }
- }
- }
- }
- /**
- * 更新后台Nav内容
- * @param {object} messageInfo 消息信息
- */
- us.updateNav = function (messageInfo) {
- var _fileinfo = us.nav[messageInfo.docId];
- if (_fileinfo) {
- switch (messageInfo.type) { // 判断消息种类
- case 'update': //修改行
- us.updateEditorLine(messageInfo, _fileinfo.page[messageInfo.navId]); //修改
- break;
- case 'add': //添加行
- us.addEditorLine(messageInfo, _fileinfo.page[messageInfo.navId]); //添加
- break;
- case 'delete': //删除行
- us.deleteEditorLine(messageInfo, _fileinfo.page[messageInfo.navId]); //删除
- break;
- case 'addNav': //添加导航
- us.updateNav.addNav(_fileinfo, messageInfo.parentId, messageInfo.index, messageInfo.addNav, messageInfo.content);
- break;
- case 'updateNav': //修改导航
- us.updateNav.updateNav(_fileinfo.navInfo.nav, messageInfo.navId, messageInfo.name);
- break;
- case 'deleteNav': //删除导航
- us.updateNav.deleteNav(_fileinfo, messageInfo.navId, messageInfo.parentId);
- break;
- case 'updateLogo': //修改logo
- _navinfo.logo = messageInfo.logo;
- break;
- }
- }
- }
- /**
- * 根据NavId查找对应的导航信息
- */
- us.selectByNavId = function (nowinfo, navid) {
- var i = 0;
- for (i = 0; i < nowinfo.length; i++) {
- object = nowinfo[i];
- if (object.pageId == navid) {
- return object;
- } else if (object.child) {
- return us.selectNav(object.child, navid);
- }
- }
- }
- /**
- * 添加Nav
- * @param {object} fileinfo 文件信息
- * @param {object} parentid 父级ID
- * @param {object} addnav 添加导航信息
- * @param {number} index 添加位置
- */
- us.updateNav.addNav = function (fileinfo, parentid, index, addnav, content) {
- var _nav = fileinfo.navInfo.nav;
- var _parent;
- if (parentid) {
- // 寻找父级
- _parent = us.selectByNavId(_nav, parentid);
- // 如果已存在子级处理
- if (_parent.child) {
- //插入指定位置
- _parent.child.splice(index, 0, addnav);
- } else {
- // 为存在子级则新建子级
- _parent.child = [addnav];
- }
- } else {
- _parent = _nav;
- _parent.splice(index, 0, addnav);
- }
- //添加内容
- fileinfo.page[addnav.pageId] = us.cheerio.load(content.replace(/\\'/ig, '\'').replace(/\\"/ig, "\""));
- }
- /**
- * 修改Nav
- * @param {object} nav 导航信息
- * @param {object} navid ID
- * @param {object} name 名字
- */
- us.updateNav.updateNav = function (nav, navid, name) {
- // 寻找需要修改导航信息
- var _nav = us.selectByNavId(nav, navid);
- if (_nav) { //存在导航
- _nav.name = name; //替换名字
- }
- }
- /**
- * 删除Nav
- * @param {object} fielinfo 文件信息
- * @param {object} navid ID
- * @param {object} name 名字
- */
- us.updateNav.deleteNav = function (fielinfo, deletenavid, parentid) {
- var _nav = fielinfo.navInfo.nav;
- var _parent;
- if (parentid) {
- // 寻找父级
- _parent = us.selectByNavId(_nav, parentid);
- } else {
- _parent = _nav;
- }
- if (_parent) {
- var _child = parentid ? _parent.child : _parent;
- _child.find(function (object, index) {
- if (object.pageId == deletenavid) {
- if (!object.child) {
- _child.splice(index, 1); //删除数据
- us.mysql.usselect([ //删除页面
- 'sqlserver.1473.cn',
- 'UseStudio_Office',
- 'DeletePage',
- deletenavid
- ], function (ret) {
- if (ret) {
- delete fielinfo.page[deletenavid]; //删除内容
- }
- });
- }
- return true;
- }
- });
- }
- }
- /**
- * 修改操作
- * @param {object} opera 操作信息对象
- * @param {element} $ 后台word元素形式
- */
- us.updateEditorLine = function (opera, $) {
- // $('#' + opera.id).prop('outerHTML', opera.content); //修改行
- $('#' + opera.id).replaceWith(opera.content);
- }
- /**
- * 删除操作
- * @param {object} opera 操作信息对象
- * @param {element} $ 后台word元素形式
- */
- us.deleteEditorLine = function (opera, $) {
- $('#' + opera.id).remove(); //删除行
- }
- /**
- * 添加操作
- * @param {object} opera 操作信息对象
- * @param {element} $ 后台word元素形式
- */
- us.addEditorLine = function (opera, $) {
- var _next; //创建记录变量
- if (opera.nextId && (_next = $('#' + opera.nextId))[0]) { //判断有没有nextid,nextid是否存在
- //如果存在则插入在nexid 元素前
- _next.before(opera.content);
- } else {
- $('body').append(opera.content); //没有就在最后插入
- }
- }
- /**
- * 更新后台Word内容
- * @param {object} message 消息信息
- */
- us.updateExcel = function (messageInfo) {
- var $ = us.excel[messageInfo.docId].content;
- if ($) {
- switch (messageInfo.type) { // 判断消息种类
- case 'blur':
- us.updateExcel.Blur(messageInfo, $); //失焦
- break;
- case "addLine":
- us.updateExcel.addLine(messageInfo, $); //添加行
- break;
- case "delLine":
- us.updateExcel.delLine(messageInfo, $); //刪除行
- break;
- case "addCol":
- us.updateExcel.addCol(messageInfo, $); //添加列
- break;
- case "delCol":
- us.updateExcel.delCol(messageInfo, $); //刪除列
- break;
- case "merge":
- us.updateExcel.merge(messageInfo, $); //合并单元格
- break;
- case "splitCells":
- us.updateExcel.splitCells(messageInfo, $); //拆分单元格
- break;
- case "rowExpanding":
- us.updateExcel.rowExpanding(messageInfo, $); //行板拉伸
- break;
- case "colExpanding":
- us.updateExcel.colExpanding(messageInfo, $); //列板拉伸
- break;
- case "addTask":
- us.updateExcel.addTask(messageInfo, $); //添加工作表
- break;
- case "deleteTask":
- us.updateExcel.deleteTask(messageInfo, $); //刪除工作表
- break;
- case "reNameTask":
- us.updateExcel.reNameTask(messageInfo, $); //刪除工作表
- break;
- case "img":
- us.updateExcel.img(messageInfo, $); //插入图片
- break;
- }
- }
- // console.log($);
- }
- /**
- * 失焦處理
- */
- us.updateExcel.Blur = function (messageInfo, $) {
- var _taskId = messageInfo.worksheetId, //如果有taskId传过来 则修改 data数据里 taskId里的数据 否则则修改当前工作表id里的数据
- _excelData = $[_taskId], //获取taskId工作表的数据
- _cellData = _excelData.cell, //获取该数据里的单元格数据
- _content = messageInfo.content; //获取需要保存的json数据
- _content.forEach(function (data) {
- var _text = data.innerText.replace(/\s+/g, ""), //去掉空格 给下面做判断 如果内容为空 则删除该数据
- _cellColList = _cellData[data.col], //获取该数据里的所有列
- _cell = null;
- if (!_cellColList) { //如果列不存在 则创建一个列
- _cellColList = _cellData[data.col] = {};
- }
- if (!_cellColList[data.row]) { //如果该行_列 不存在单元格数据 则创建个json 给单元格添加数据
- _cellColList[data.row] = {};
- _cellColList[data.row].cssStyle = {}; //css样式
- }
- _cell = _cellColList[data.row]; //获取该单元格的数据 (json)
- data.content ? _cell.innerHTML = data.content : ""; //修改该单元格的innerHTML
- if (data.attr) { //如果存在样式
- _cell.cssStyle[data.attr] = data.value;
- if (!data.value) {
- delete _cell.cssStyle[data.attr]
- }
- }
- if (data.clearStyle) { //如果需要清除所有样式的话
- data.cssStyle = {}
- }
- if (((!(_text) || _text === ("\u200D")) && !(_cell.endRow)) && !(us.jsonLen(_cell.cssStyle))) { //如果innerHTML为空格 或 不存在 并且 不是合并单元格元素 并且没有样式 则删除掉
- delete (_cellColList[data.row]);
- }
- });
- }
- /*
- 获取json的数量
- */
- us.jsonLen = function (json) {
- var _json = json,
- _len = 0;
- for (var i in json) {
- _len++;
- }
- return _len;
- }
- us.updateExcel.addLine = function (messageInfo, $) {
- var _taskId = messageInfo.worksheetId, //单元格id
- _content = messageInfo.content,
- _excelData = $[_taskId],
- _rowData = _excelData.row,
- _cellData = _excelData.cell,
- _start = _content.start, //开始行
- _lineNum = _content.lineNum, //添加行的数量
- _sizeChange = _content.sizeChange, //添加的行的高度
- _mergeArr = _content.mergeArr, //合并单元格 元素 数组集合
- _cellCol = null;
- if (_mergeArr.length) {
- _mergeArr.forEach(function (info) {
- _cellData[info.col][info.row].endRow = _cellData[info.col][info.row].endRow + _lineNum;
- });
- }
- for (var k in _cellData) {
- _cellCol = _cellData[k]; //该列下的所有行
- var temp = {};
- for (var row in _cellCol) {
- if (row > _start) {
- temp[(+row) + _lineNum] = _cellCol[row];
- temp[(+row) + _lineNum].endRow ? temp[(+row) + _lineNum].endRow = temp[(+row) + _lineNum].endRow + _lineNum : ""; //如果是合并单元格的元素 则将endRow + _lineNum;
- } else {
- temp[row] = _cellCol[row];
- }
- }
- _cellData[k] = temp;
- } /* 该处for循环是修改数据中单元格的行数 */
- var _rowDataChange = _rowData.change, //获取数据表中 所有被改变高度的 行版信息
- _temp = {},
- _rowChange = null;
- for (var k in _rowDataChange) {
- _rowChange = _rowDataChange[k]; //该行板的所有被改动的行数据
- if (k > _start) {
- _temp[(+k) + _lineNum] = _rowChange;
- } else {
- _temp[k] = _rowChange;
- }
- } /* 该处for循环是修改数据中行版的行数 */
- _rowData.change = _temp;
- if (_sizeChange) {
- for (var i = 1; i <= _lineNum; i++) {
- _rowData.change[_start + i] = _sizeChange;
- }
- }
- _rowData.num = _rowData.num + _lineNum; //行板数量 = 行板数量 + 添加行的数量
- };
- us.updateExcel.delLine = function (messageInfo, $) {
- var _taskId = messageInfo.worksheetId, //单元格id
- _excelData = $[_taskId],
- _content = messageInfo.content,
- _rowData = _excelData.row,
- _cellData = _excelData.cell,
- _start = _content.start, //被选中的第一行
- _end = _content.end, //被选中的最后一行
- _lineNum = _content.lineNum, //所删除的行数
- _mergeArr = _content.mergeArr, //合并单元格 元素 数组集合
- _cellCol = null;
- for (var k in _cellData) {
- _cellCol = _cellData[k];
- var temp = {};
- for (var row in _cellCol) {
- if (row > _end) {
- temp[(+row) - _lineNum] = _cellCol[row];
- temp[(+row) - _lineNum].endRow ? temp[(+row) - _lineNum].endRow = temp[(+row) - _lineNum].endRow - _lineNum : ""; //如果是合并单元格的元素 则将endRow - _lineNum;
- } else if (row < _start) {
- temp[row] = _cellCol[row];
- }
- }
- _cellData[k] = temp;
- }
- if (_mergeArr.length) {
- var _cloneMessage = null;
- _mergeArr.forEach(function (info) {
- _cloneMessage = JSON.parse(JSON.stringify(messageInfo));
- _cloneMessage.content = info;
- us.updateExcel.merge(_cloneMessage, $);
- });
- }
- var _rowDataChange = _rowData.change, //获取数据表中 所有被改变高度的 行版信息
- _temp = {},
- _rowChange = null;
- for (var k in _rowDataChange) {
- _rowChange = _rowDataChange[k]; //该行板的所有被改动的行数据
- if (k < _start) { //如果大于结束行 则将该行下面的所有的数字信息 - 被删除的行
- _temp[k] = _rowChange;
- } else if (k > _end) {
- _temp[(+k) - _lineNum] = _rowChange;
- }
- } /* 该处for循环是修改数据中行版的行数 */
- _rowData.change = _temp;
- _rowData.num = _rowData.num - _lineNum; //行板数量 = 行板数量 - 删除行的数量
- };
- us.updateExcel.addCol = function (messageInfo, $) {
- var _taskId = messageInfo.worksheetId, //单元格id
- _excelData = $[_taskId],
- _content = messageInfo.content,
- _colData = _excelData.column,
- _cellData = _excelData.cell,
- _start = _content.start, //被选中的第一列
- _sizeChange = _content.sizeChange, //添加的列的宽度
- _colNums = _content.colNums, //所添加的列数
- _mergeArr = _content.mergeArr, //合并单元格 元素 数组集合
- _cellCol = null,
- _temp = {};
- if (_mergeArr.length) {
- _mergeArr.forEach(function (info) {
- _cellData[info.col][info.row].endCol = _cellData[info.col][info.row].endCol + _colNums;
- });
- }
- for (var k in _cellData) {
- _cellCol = _cellData[k];
- if (k > _start) {
- _temp[+(k) + _colNums] = _cellCol;
- var _temp2 = _temp[(+k) + _colNums];
- for (var i in _temp2) {
- _temp2[i].endCol ? _temp2[i].endCol = _temp2[i].endCol + _colNums : ""; //如果是合并单元格的元素 则将endCol + _colNums;
- }
- } else {
- _temp[k] = _cellCol;
- }
- }
- _excelData.cell = _temp;
- var _colDataChange = _colData.change, //获取数据表中 所有被改变宽度的 列版信息
- _temp = {},
- _colChange = null;
- for (var k in _colDataChange) {
- _colChange = _colDataChange[k]; //该列板的所有被改动的列数据
- if (k > _start) {
- _temp[(+k) + _colNums] = _colChange;
- } else {
- _temp[k] = _colChange;
- }
- } /* 该处for循环是修改数据中行版的行数 */
- _colData.change = _temp;
- if (_sizeChange) {
- for (var i = 1; i <= _colNums; i++) {
- _colData.change[_start + i] = _sizeChange;
- }
- }
- _colData.num = _colData.num + _colNums; //列板数量 = 列板数量 + 添加列的数量
- };
- us.updateExcel.delCol = function (messageInfo, $) {
- var _taskId = messageInfo.worksheetId, //单元格id
- _excelData = $[_taskId],
- _content = messageInfo.content,
- _colData = _excelData.column,
- _cellData = _excelData.cell,
- _start = _content.start, //被选中的第一行
- _end = _content.end, //被选中的最后一行
- _colNums = _content.colNums, //所删除的列数
- _mergeArr = _content.mergeArr, //合并单元格 元素 数组集合
- _cellCol = null,
- _temp = {};
- for (var k in _cellData) {
- _cellCol = _cellData[k];
- if (k > _end) {
- _temp[+(k) - _colNums] = _cellCol;
- var _temp2 = _temp[(+k) - _colNums];
- for (var i in _temp2) {
- _temp2[i].endCol ? _temp2[i].endCol = _temp2[i].endCol - _colNums : ""; //如果是合并单元格的元素 则将endCol - _colNums;
- }
- } else if (k < _start) {
- _temp[k] = _cellCol;
- }
- }
- _excelData.cell = _temp;
- if (_mergeArr.length) {
- var _cloneMessage = null;
- _mergeArr.forEach(function (info) {
- _cloneMessage = JSON.parse(JSON.stringify(messageInfo));
- _cloneMessage.content = info;
- us.updateExcel.merge(_cloneMessage, $);
- });
- }
- var _colDataChange = _colData.change, //获取数据表中 所有被改变宽度的 列版信息
- _temp = {},
- _colChange;
- for (var k in _colDataChange) {
- _colChange = _colDataChange[k]; //该列板的所有被改动的列数据
- if (k < _start) { //如果大于结束列 则将该列下面的所有的数字信息 - 被删除的列
- _temp[k] = _colChange;
- } else if (k > _end) {
- _temp[(+k) - _colNums] = _colChange;
- }
- } /* 该处for循环是修改数据中行版的行数 */
- _colData.change = _temp;
- _colData.num = _colData.num - _colNums; //列板数量 = 列板数量 - 删除列的数量
- };
- us.updateExcel.merge = function (messageInfo, $) {
- var _taskId = messageInfo.worksheetId, //单元格id
- _excelData = $[_taskId],
- _content = messageInfo.content,
- _cellData = _excelData.cell,
- _cellColList = _cellData[_content.col];
- if (_content.row === _content.endRow && _content.col === _content.endCol) {
- _cellData[_content.col][_content.row].endRow = "";
- _cellData[_content.col][_content.row].endCol = ""
- return;
- }
- if (!_cellColList) {
- _cellColList = _cellData[_content.col] = {};
- }
- if (!_cellColList[_content.row]) {
- _cellColList[_content.row] = {};
- }
- _cellColList[_content.row].innerHTML = _content.content;
- _cellColList[_content.row].endRow = _content.endRow;
- _cellColList[_content.row].endCol = _content.endCol;
- };
- us.updateExcel.splitCells = function (messageInfo, $) {
- var _taskId = messageInfo.worksheetId, //单元格id
- _excelData = $[_taskId],
- _content = messageInfo.content,
- _cellData = _excelData.cell,
- _cell = _cellData[_content.col][_content.row]; //需要拆分的单元格
- _cell.endRow = "";
- _cell.endCol = "";
- };
- us.updateExcel.rowExpanding = function (messageInfo, $) {
- var _taskId = messageInfo.worksheetId, //单元格id
- _excelData = $[_taskId],
- _content = messageInfo.content,
- _rowData = _excelData.row;
- _content.forEach(function (data) {
- var _rowNum = data.rowName, //拉伸行的位置 (数字)
- _rowHeight = data.height; //被拉伸后的高度
- _rowData.change[_rowNum] = _rowHeight;
- });
- };
- us.updateExcel.colExpanding = function (messageInfo, $) {
- var _taskId = messageInfo.worksheetId, //单元格id
- _excelData = $[_taskId],
- _content = messageInfo.content,
- _colData = _excelData.column; //该列的数据
- _content.forEach(function (data) {
- var _colNum = data.colName, //拉伸行的位置 (数字)
- _colWidth = data.width; //被拉伸后的宽度
- _colData.change[_colNum] = _colWidth;
- });
- };
- /*添加工作表*/
- us.updateExcel.addTask = function (messageInfo, $) {
- var _content = messageInfo.content,
- _taskId = _content.id;
- $[_taskId] = _content.data;
- };
- /*刪除工作表*/
- us.updateExcel.deleteTask = function (messageInfo, $) {
- var _content = messageInfo.content,
- _taskId = _content.id;
- delete ($[_taskId]);
- };
- /*命名工作表*/
- us.updateExcel.reNameTask = function (messageInfo, $) {
- var _content = messageInfo.content,
- _taskId = _content.id;
- $[_taskId].taskName = _content.name;
- };
- us.updateExcel.img = function (messageInfo, $) { //此处修改
- var _content = messageInfo.content,
- _taskId = messageInfo.worksheetId,
- _excelData = $[_taskId],
- _imgData = _excelData.img;
- _imgData.push(_content);
- };
- /**
- * 用户群发处理
- *
- * @param {string} 接收信息的用户id
- * @param {string} 发送的用户信息
- **/
- us.setword = function (messageinfo) {
- // us.userConnect[userid].response.end(messageinfo);
- }
- /**
- * 用户群发处理
- *
- * @param {string} userid 接收信息的用户id
- * @param {string} messageinfo 发送的用户信息
- **/
- us.senduser = function (userid, messageinfo) {
- us.userConnect[userid].response.end(messageinfo); //发送消息
- }
- /**
- * 用户群发处理
- *
- * @param {object} messageinfo 发送消息的内容
- * @param {guid} pageid 页面id
- * @param {string} type 类型
- **/
- us.broadcast = function (messageinfo, pageid, type) {
- var i, j, o, //循环变量
- _data,
- _messageinfo, //消息信息
- _users, //用户ID集合
- _pageids, //用户页面唯一识别集合
- _recievedpageid, //接收用户的页面唯一识别ID
- _issend, // 是否发送
- _userconnect = us.connect; //用户连接池
- const chunkSize = 100;
- for (i in messageinfo) {
- _messageinfo = messageinfo[i]; //记录消息消息b
- _users = us[type][i] ? us[type][i].user : {}; //记录用户集合
- for (j in _users) { //循环处理每个用户处理 给每个用户集合下的用户发送消息
- _pageids = _users[j]; //记录当前用户在每个端登录的页面唯一识别ID
- for (o = 0; o < _pageids.length; o++) { //循环发送给当前用户在每个端登录的用户
- _recievedpageid = _pageids[o]; //记录接受者页面唯一识别ID 用于区分是否是发送用户
- if (_recievedpageid != pageid) { //如果接受用户不等于发送用户则发送消息
- try {
- if (_userconnect[j] && _userconnect[j][_recievedpageid]) {
- console.log(_recievedpageid);
- _data = JSON.stringify(_messageinfo);
- _issend = _userconnect[j][_recievedpageid].response.write(""); //发送消息
- //用户在轮询切换的时候的中间,会产生数据无法接受,这里是存储数据
- if (!_issend) {
- us.offLineMessage[_recievedpageid].message.push(_messageinfo); //将消息加入用户离线消息中
- }
- else {
- for (let i = 0; i < _data.length; i += chunkSize) {
- const chunk = _data.slice(i, i + chunkSize);
- _userconnect[j][_recievedpageid].response.write(chunk);
- }
- _userconnect[j][_recievedpageid].response.write("\r\n");
- }
- }
- }
- catch (e) {
- console.log(e)
- }
- }
- }
- }
- }
- }
- /**
- * 加入文档编辑
- *
- * @param {object} response 用户相应对象
- * @param {string} wordinfo 文档信息 形如: {docId : 'guid,guid,guid', userId : guid, pageId : guid}
- **/
- us.addWord = function (response, wordinfo) {
- //获取用户参数
- wordinfo = JSON.parse(wordinfo);
- var _docidarray = wordinfo.docId.split(','), //根据','号分割多个文档ID
- _userid = wordinfo.userId, //用户ID
- _pageid = wordinfo.pageId, //页面唯一识别ID
- i, //定义循环变量
- _docid, //记录文档ID变量
- _docinfo; //记录文档信息变量
- for (i = 0; i < _docidarray.length; i++) { //循环处理每一份问的加入请求
- _docid = _docidarray[i]; //记录文档ID变量
- _docinfo = us.word[_docid]; //记录文档信息变量
- if (!_docinfo) { //判断群列表是否已存在不存在则创建
- us.word[_docid] = {
- user: {},
- history: []
- }; //创建word文档信息
- us.word[_docid].user[_userid] = [_pageid]; //加入用户
- } else if (!_docinfo.user[_userid]) { //判断文档的用户是否已登陆
- _docinfo.user[_userid] = [_pageid];
- } else if (_docinfo.user[_userid].indexOf(_pageid) == -1) { //将用户添加到群列表中
- _docinfo.user[_userid].push(_pageid);
- }
- us.offLineMessage[_pageid].word.push(_docid); //添加到用户离线消息的连接wrod记录中
- }
- response.end('addword'); //即时相应请求成功
- }
- /**
- * 退出文档编辑
- *
- * @param {object} response 用户相应对象
- * @param {string} wordinfo 文档信息
- **/
- us.quitOffice = function (response, wordinfo) {
- //获取用户参数
- wordinfo = JSON.parse(wordinfo);
- var _docid = wordinfo.docId,
- _userid = wordinfo.userId,
- _pageid = wordinfo.pageId,
- _type = wordinfo.type,
- _messagetype = 'us.' + _type;
- var _docinfo = us[_type][_docid]; //获取文档信息
- //判断是否存在文档信息
- if (_docinfo) {
- us.quitUserList(_docinfo, _userid, _pageid); //将用户在协同列表中删除
- var _j, //创建循环变量
- _message = us.offLineMessage[_pageid] ? us.offLineMessage[_pageid].message : []; //获取该用户的离线消息
- for (_j = _message.length - 1; _j > -1; _j--) { //循环移除该文档的离线消息
- if (_message[_j].type == _messagetype && _message[_j].messageInfo.docId == _docid) { //判断是否为word文档
- _message.splice(_j, 1); //删除记录
- }
- }
- us.insertHistory(_docinfo, _docid, _type);
- }
- response.end('quitOffice'); //响应退出请求
- }
- /**
- * 退出知识建构
- *
- * @param {object} response 用户相应对象
- * @param {string} wordinfo 文档信息
- **/
- us.quitcscl = function (response, info) {
- console.log(info)
- //获取用户参数
- info = JSON.parse(info);
- var _docid = info.docId,
- _userid = info.userId,
- _pageid = info.pageId,
- _type = info.type;
- var _docinfo = us[_type][_docid]; //获取文档信息
- //判断是否存在文档信息
- if (_docinfo) {
- _userid = us.quitUserList(_docinfo, _userid, _pageid); //将用户在协同列表中删除
- console.log(_userid)
- //下线后通知所有的用户
- if (_userid) {
- _message = {};
- var _messageinfo = us.message(_userid, 'us.cscl', {
- id: us.createGuid(),
- type: "delUser",
- userinfo: us.user[_userid],
- docId: _docid
- });
- _message[_docid] = [_messageinfo];
- console.log(_message)
- us.broadcast(_message, _pageid, "cscl");
- }
- // var _j, //创建循环变量
- // _message = us.offLineMessage[_pageid] ? us.offLineMessage[_pageid].message : []; //获取该用户的离线消息
- // for (_j = _message.length - 1; _j > -1; _j--) { //循环移除该文档的离线消息
- // if (_message[_j].type == _messagetype && _message[_j].messageInfo.docId == _docid) { //判断是否为word文档
- // _message.splice(_j, 1); //删除记录
- // }
- // }
- }
- response.end('quitcscl'); //响应退出请求
- }
- /**
- * 将用户在协同列表中删除
- *
- * @param {object} docinfo 文档信息
- * @param {string} userid 用户ID
- * @param {string} pageid 用户页面唯一识别ID
- */
- us.quitUserList = function (docinfo, userid, pageid) {
- var _users = docinfo.user[userid]; //记录文档的用户列表
- console.log(_users)
- //获取用户在文档用户列表的位置
- if (_users) {
- var _index = _users.indexOf(pageid);
- // 如果存在则将该用户删除 表示退出协同
- if (_index != -1) {
- _users.splice(_index, 1); //删除该用户
- //如果该用户的全部端用户已退出则删除用户的记录
- if (_users.length == 0) {
- delete docinfo.user[userid];
- return userid;
- }
- }
- }
- }
- /**
- * 插入文档操作历史到数据库
- *
- * @param {object} docinfo 文档信息
- */
- us.insertHistory = function (docinfo, docid, type) {
- var i,
- _info, //操作信息记录
- _isuser = false;
- for (i in docinfo.user) {
- _isuser = true;
- break;
- }
- //如果该文档没有协同用户
- if (!_isuser) {
- switch (type) {
- case 'word':
- var _content = docinfo.content ? docinfo.content('body').html().replace(/'/ig, '\\\'').replace(/"/ig, "\\\"") : "";
- us.mysql.usselect(['sqlserver.1473.cn', 'UseStudio_Disk', 'SaveFileContent', docid, _content, ''],
- function (ret) { });
- break;
- case 'nav':
- us.mysql.usselect(['sqlserver.1473.cn', 'UseStudio_Disk', 'SaveFileContent', docid, JSON.stringify(docinfo.navInfo), ''],
- function (ret) {
- var _key;
- for (_key in docinfo.page) {
- //发送保存内容请求
- var _content = docinfo.page[_key] ? docinfo.page[_key]('body').html().replace(/'/ig, '\\\'').replace(/"/ig, "\\\"") : "";
- us.mysql.usselect(['sqlserver.1473.cn', 'UseStudio_Office', 'SaveNavPageContent', _key, _content], function (ret) { });
- }
- });
- break;
- }
- }
- }
- /**
- * 下线操作
- * @param {object} response 响应对象
- * @param {string} userid 用户ID
- * @param {string} pageid 用户页面唯一识别ID
- */
- us.logout = function (response, userid, pageid) {
- console.log("234566666666666666")
- console.log(userid)
- var j, istrue = true;
- var _user = us.connect[userid];
- //循环目前所有的连接用户
- us.deluser(userid, pageid); //删除用户的数据
- if (_user) {
- delete _user[pageid]
- for (j in _user) { istrue = false; break }
- //这个用户下所有的连接用户都不在了
- if (istrue) {
- //用户登录数据清理,用于已经下线
- delete us.user[userid];
- delete us.connect[userid];
- }
- console.log(pageid)
- }
- response.end('logout'); //响应退出请求
- // var i, j, _docinfo,
- // _wordids = us.offLineMessage[pageid].word, //得到加入的所有word
- // _excelids = us.offLineMessage[pageid].excel; //得到加入的所有excel
- // us.connect[userid][pageid].offLineTime = new Date();
- // for (i = 0; i < _wordids.length; i++) { //循环处理每一个文档
- // _docinfo = us.word[_wordids[i]]; //记录文档信息
- // if (_docinfo) { //如果存在文档信息
- // us.quitUserList(_docinfo, userid, pageid); //将用户在协同列表中删除
- // delete us.offLineMessage[pageid]; //删除用户离线消息记录
- // us.insertHistory(_docinfo, 'word'); //插入历史
- // }
- // }
- // for (j = 0; i < _excelids.length; j++) {
- // _docinfo = us.excel[_excelids[j]]; //记录文档信息
- // if (_docinfo) { //如果存在文档信息
- // us.quitUserList(_docinfo, userid, pageid); //将用户在协同列表中删除
- // delete us.offLineMessage[pageid]; //删除用户离线消息记录
- // us.insertHistory(_docinfo, 'excel'); //插入历史
- // }
- // }
- }
- /**
- * 获取Excel,并生成后端数据
- *
- * @param {object} response 用户相应对象
- * @param {string} docid 文档ID
- * @param {guid} pageid 页面ID
- * @param {guid} userid 用户ID
- */
- us.getExcel = function (response, docid, pageid, userid) {
- //判断文档是否已在内层中存在
- if (us.excel[docid]) {
- //如果存在则直接返回内层的文档
- if (!us.excel[docid].user[userid]) {
- us.excel[docid].user[userid] = [pageid];
- } else {
- us.excel[docid].user[userid].push(pageid);
- }
- us.offLineMessage[pageid].excel.indexOf(docid) == -1 && us.offLineMessage[pageid].excel.push(docid); //添加到用户离线消息的连接wrod记录中
- var _office = us.excel[docid].content;
- response.end(JSON.stringify(_office));
- } else {
- us.excel[docid] = {
- user: {},
- history: []
- };
- us.excel[docid].user[userid] = [pageid];
- us.offLineMessage[pageid].excel.indexOf(docid) == -1 && us.offLineMessage[pageid].excel.push(docid); //添加到用户离线消息的连接wrod记录中
- //如果不存在则去数据库查找
- us.mysql.usselect([
- 'sqlserver.1473.cn',
- 'UseStudio_Disk',
- 'GetFileContent',
- docid
- ], function (ret) {
- if (ret) {
- var _office = JSON.parse(ret)[0][0].UsOffice;
- us.excel[docid].content = JSON.parse(_office); //将文档添加到内层中
- response.end(_office); //返回文档内容
- } else {
- us.excel[docid].content = ''; //将文档添加到内层中
- response.end(''); //返回文档内容
- }
- });
- }
- }
- /**
- * 获取文档,并生成DOM
- *
- * @param {object} response 用户相应对象
- * @param {string} fileid 导航文件ID
- * @param {guid} pageid 页面ID
- * @param {guid} userid 用户ID
- */
- us.getNav = function (response, fileid, pageid, userid) {
- //判断文档是否已在内层中存在
- if (us.nav[fileid]) {
- //如果存在则直接返回内层的文档
- if (!us.nav[fileid].user[userid]) {
- us.nav[fileid].user[userid] = [pageid];
- } else {
- us.nav[fileid].user[userid].push(pageid);
- }
- us.offLineMessage[pageid].nav.indexOf(fileid) == -1 && us.offLineMessage[pageid].nav.push(fileid); //添加到用户离线消息的连接wrod记录中
- response.end(JSON.stringify(us.nav[fileid].navInfo));
- } else {
- //如果不存在则去数据库查找
- us.mysql.usselect([
- 'sqlserver.1473.cn',
- 'UseStudio_Disk',
- 'GetFileContent',
- fileid
- ], function (ret) {
- if (ret) {
- var _navinfo = JSON.parse(ret)[0][0].UsOffice;
- if (_navinfo) {
- var _navdata = us.nav[fileid] = { //生成数据结构
- user: {},
- navInfo: JSON.parse(_navinfo),
- page: {},
- history: []
- };
- _navdata.user[userid] = [pageid]; //记录用户
- us.offLineMessage[pageid].nav.indexOf(fileid) == -1 && us.offLineMessage[pageid].nav.push(fileid); //添加到用户离线消息的连接wrod记录中
- response.end(JSON.stringify(_navinfo)); //返回文档内容
- } else {
- response.end(''); //查询不到文档
- }
- } else {
- response.end(''); //返回文档内容
- }
- });
- }
- }
- /**
- * 获取导航页面内容
- * @param {object} response 用户相应对象
- * @param {guid} fileid 文档ID
- * @param {string} navid 文档内容
- * @param {string} name 文档名
- */
- us.getNavPageContent = function (response, fileid, navid) {
- if (us.nav[fileid] && us.nav[fileid].page[navid]) {
- response.end(us.nav[fileid].page[navid]('body').html()); //后台直接返回
- } else {
- //如果不存在则去数据库查找
- us.mysql.usselect([
- 'sqlserver.1473.cn',
- 'UseStudio_Office',
- 'GetNavPageContent',
- navid
- ], function (ret) {
- if (ret && JSON.parse(ret)[0][0]) {
- var _content = JSON.parse(ret)[0][0].content.replace(/\\'/ig, '\'').replace(/\\"/ig, "\""); //记录查找内容
- us.nav[fileid] && (us.nav[fileid].page[navid] = us.cheerio.load(_content)); //将文档添加到内层中
- response.end(_content); //查询不到文档
- } else {
- response.end(''); //返回文档内容
- }
- });
- }
- }
- /**
- * 新建文档保存内容并加入协同
- * @param {object} response 用户相应对象
- * @param {guid} docid 文档ID
- * @param {string} content 文档内容
- * @param {string} name 文档名
- * @param {guid} userid 用户ID
- * @param {guid} pageid 页面唯一识别ID
- */
- us.newWordSave = function (response, docid, content, name, userid, pageid) {
- us.word[docid] = {
- user: {},
- history: []
- }; //创建Word类
- us.word[docid].user[userid] = [pageid]; //将用户加入协同记录数组
- us.offLineMessage[pageid].word.indexOf(docid) == -1 && us.offLineMessage[pageid].word.push(docid); //添加到用户离线消息的连接wrod记录中
- //发送保存请求
- us.mysql.usselect([
- 'sqlserver.1473.cn',
- 'UseStudio_Disk',
- 'SaveFileContent',
- docid,
- content,
- name
- ], function (ret) {
- if (ret) {
- //设置内容
- us.word[docid].content = us.cheerio.load(content.replace(/\\'/ig, '\'').replace(/\\"/ig, "\"")); //将文档添加到内层中
- response.end('save'); //返回保存成功
- } else {
- response.end('fail'); //返回文档内容
- }
- });
- }
- /**
- * 新建文档保存内容并加入协同
- * @param {object} response 用户相应对象
- * @param {guid} docid 文档ID
- * @param {string} content 文档内容
- * @param {string} name 文档名
- * @param {guid} userid 用户ID
- * @param {guid} pageid 页面唯一识别ID
- */
- us.newExcelSave = function (response, docid, content, name, userid, pageid) {
- us.excel[docid] = {
- user: {},
- history: []
- }; //创建excel类
- us.excel[docid].user[userid] = [pageid]; //将用户加入协同记录数组
- us.offLineMessage[pageid].excel.indexOf(docid) == -1 && us.offLineMessage[pageid].word.push(docid); //添加到用户离线消息的连接wrod记录中
- //发送保存请求
- us.mysql.usselect([
- 'sqlserver.1473.cn',
- 'UseStudio_Disk',
- 'SaveFileContent',
- docid,
- content,
- name
- ], function (ret) {
- if (ret) {
- //设置内容
- us.excel[docid].content = JSON.parse(content); //将文档添加到内层中
- response.end('saveOk'); //返回保存成功
- } else {
- response.end('fail'); //返回文档内容
- }
- });
- }
- /**
- * 新建导航文件保存
- * @param {object} response 用户相应对象
- * @param {guid} fileid 文档ID
- * @param {string} navinfo 导航信息
- * @param {string} nowpageid 当前页面ID
- * @param {string} nowpagecontent 当前页面内容
- * @param {string} name 文档名
- * @param {guid} userid 用户ID
- * @param {guid} pageid 页面唯一识别ID
- */
- us.newNavSave = function (response, fileid, navinfo, name, nowpageid, nowpagecontent, userid, pageid) {
- //发送保存文件请求
- us.mysql.usselect([
- 'sqlserver.1473.cn',
- 'UseStudio_Disk',
- 'SaveFileContent',
- fileid,
- navinfo,
- name
- ], function (ret) {
- if (ret) {
- var _navinfo = JSON.parse(navinfo);
- if (!us.nav[fileid]) {
- us.nav[fileid] = {
- user: {},
- navInfo: _navinfo,
- page: {},
- history: []
- };
- } else {
- us.nav[fileid].navInfo = _navinfo;
- }
- var _navdata = us.nav[fileid];
- _navdata.user[userid] = [pageid]; //将用户加入协同记录数组
- us.offLineMessage[pageid].nav.indexOf(fileid) == -1 && us.offLineMessage[pageid].nav.push(fileid); //添加到用户离线消息的连接wrod记录中
- if (nowpageid) {
- us.SaveNavPageContent(response, fileid, nowpageid, nowpagecontent);
- } else {
- response.end('saveContentSuccess'); //返回文档内容
- }
- } else {
- response.end('saveNavFail'); //返回文档内容
- }
- });
- }
- us.SaveNavPageContent = function (response, fileid, nowpageid, nowpagecontent) {
- var _navdata = us.nav[fileid];
- //发送保存内容请求
- us.mysql.usselect([
- 'sqlserver.1473.cn',
- 'UseStudio_Office',
- 'SaveNavPageContent',
- nowpageid,
- nowpagecontent
- ], function (ret) {
- if (ret) {
- _navdata.page[nowpageid] = us.cheerio.load(nowpagecontent.replace(/\\'/ig, '\'').replace(/\\"/ig, "\""));
- response.end('saveContentSuccess'); //返回文档内容
- } else {
- response.end('saveContentFail'); //返回文档内容
- }
- });
- }
- /**
- * 新建导航页面
- * @param {object} response 用户相应对象
- * @param {string} nowpageid 当前页面ID
- * @param {string} nowpagecontent 当前页面内容
- */
- us.copyPageContent = function (response, fileid, nowpageid, nowpagecontent, copypageid) {
- if (!us.nav[fileid]) {
- us.nav[fileid] = {
- user: {},
- navInfo: "",
- page: {},
- history: []
- };
- }
- var _navdata = us.nav[fileid];
- //发送保存内容请求
- us.mysql.usselect([
- 'sqlserver.1473.cn',
- 'UseStudio_Office',
- 'CopyPageContent',
- nowpageid,
- nowpagecontent,
- copypageid
- ], function (ret) {
- if (ret) {
- nowpagecontent == "" && (nowpagecontent = JSON.parse(ret)[0][0].content);
- _navdata.page[nowpageid] = us.cheerio.load(nowpagecontent.replace(/\\'/ig, '\'').replace(/\\"/ig, "\""));
- response.end('saveContentSuccess'); //返回文档内容
- } else {
- response.end('saveContentFail'); //返回文档内容
- }
- });
- }
- /**
- * 加入文档编辑 该函数暂无使用
- *
- * @param {object} response 用户相应对象
- * @param {string} wordinfo 文档信息 形如: {docId : 'guid,guid,guid', userId : guid, pageId : guid}
- **/
- us.addWord = function (response, wordinfo) {
- //获取用户参数
- wordinfo = JSON.parse(wordinfo);
- var _docidarray = wordinfo.docId.split(','), //根据','号分割多个文档ID
- _userid = wordinfo.userId, //用户ID
- _pageid = wordinfo.pageId, //页面唯一识别ID
- i, //定义循环变量
- _docid, //记录文档ID变量
- _docinfo; //记录文档信息变量
- for (i = 0; i < _docidarray.length; i++) { //循环处理每一份问的加入请求
- _docid = _docidarray[i]; //记录文档ID变量
- _docinfo = us.word[_docid]; //记录文档信息变量
- if (!_docinfo) { //判断群列表是否已存在不存在则创建
- us.word[_docid] = {
- user: {},
- history: []
- }; //创建word文档信息
- us.word[_docid].user[_userid] = [_pageid]; //加入用户
- } else if (!_docinfo.user[_userid]) { //判断文档的用户是否已登陆
- _docinfo.user[_userid] = [_pageid];
- } else if (_docinfo.user[_userid].indexOf(_pageid) == -1) { //将用户添加到群列表中
- _docinfo.user[_userid].push(_pageid);
- }
- us.offLineMessage[_pageid].word.push(_docid); //添加到用户离线消息的连接wrod记录中
- }
- response.end('addword'); //即时相应请求成功
- }
- /**
- * 加入Excel编辑
- *
- * @param {object} response 用户相应对象
- * @param {string} excelinfo 表格信息
- **/
- us.addExcel = function (response, excelinfo) {
- //获取用户id
- //获取用户id
- excelinfo = JSON.parse(excelinfo);
- var _excelinfo = us.excel[excelinfo.docId],
- _userid = excelinfo.userId,
- _pageid = excelinfo.pageId,
- _docid = excelinfo.docId;
- if (!_excelinfo) { //判断群列表是否已存在不存在则创建
- us.excel[_docid] = {
- user: {},
- history: []
- };
- us.excel[_docid].user[_userid] = [_pageid];
- } else if (!_excelinfo.user[_userid]) { //判断文档的用户是否已登陆 如果只有1个页面 则直接添加
- _excelinfo.user[_userid] = [_pageid];
- } else if (_excelinfo.user[_userid].indexOf(_pageid) == -1) { //将用户添加到群列表中 假如是一个用户多个页面 这里 则就是将每个页面添加到该用户下
- _excelinfo.user[_userid].push(_pageid);
- }
- us.offLineMessage[_pageid].excel.push(_docid); //添加到用户离线消息的连接excel记录中
- response.end('OK'); //返回到前台
- }
- /**
- * 初始化获取未来课堂数据
- *
- */
- us.getcscl = function (response, calssid, pageid, userid) {
- //判断文档是否已在内层中存在
- if (us.cscl[calssid]) {
- //如果存在则直接返回内层的文档
- if (!us.cscl[calssid].user[userid]) {
- us.cscl[calssid].user[userid] = [pageid];
- }
- else if (us.cscl[calssid].user[userid].indexOf(pageid) == -1) {
- us.cscl[calssid].user[userid].push(pageid);
- }
- try {
- us.offLineMessage[pageid].cscl.indexOf(calssid) == -1 && us.offLineMessage[pageid].cscl.push(calssid); //添加到用户离线消息的连接wrod记录中
- }
- catch (e) {
- }
- //后续优化到redis获取
- us.mysql.usselect(['172.16.12.5', 'pbl', 'select_room', calssid],
- function (ret) {
- var _user = us.getuserbytype(us.cscl[calssid].user); //获取当前项目下所有的用户信息
- var _data = us.cscl[calssid].content; //保存在内存的知识建构数据
- if (ret) {
- ret[0][0].user = _user; //这里是项目当前所有用户
- ret[0][0].data = _data; //把思维网格的内容转化成数组
- if (userid && us.user[userid]) {
- us.sendUser(us.user[userid], "cscl", pageid, calssid); //加入知识建构项目后通知所有的用户
- }
- response.end(JSON.stringify(ret)); //返回文档内容
- }
- else {
- response.end(''); //返回文档内容
- }
- });
- }
- else {
- us.cscl[calssid] = {
- user: {},
- history: []
- };
- us.cscl[calssid].user[userid] = [pageid];
- if (us.offLineMessage[pageid]) {
- us.offLineMessage[pageid].cscl.indexOf(calssid) == -1 && us.offLineMessage[pageid].cscl.push(calssid); //添加到用户离线消息的连接wrod记录中
- }
- //如果不存在则去数据库查找
- us.mysql.usselect(['172.16.12.5', 'pbl', 'select_room', calssid],
- function (ret) {
- var _user = us.getuserbytype(us.cscl[calssid].user);
- if (ret) {
- ret[0][0].user = _user; //这里是项目当前所有用户
- try {
- us.cscl[calssid].content = JSON.parse(ret[0][0].data) || {}; //把思维网格的内容转化成数组
- }
- catch (e) {
- us.cscl[calssid].content = ret[0][0].data || {};
- }
- response.end(JSON.stringify(ret)); //返回文档内容
- }
- else {
- us.cscl[calssid].content = {}; //创建一个空数组
- response.end(''); //返回文档内容
- }
- });
- }
- }
- /**
- * 初始化获取未来课堂数据
- *
- */
- us.getcscldata = function (response, calssid, pageid, userid) {
- //判断文档是否已在内层中存在
- if (us.cscl[calssid]) {
- //后续优化到redis获取
- us.mysql.usselect(['172.16.12.5', 'pbl', 'select_room', calssid],
- function (ret) {
- var _user = us.getuserbytype(us.cscl[calssid].user); //获取当前项目下所有的用户信息
- var _data = us.cscl[calssid].content; //保存在内存的知识建构数据
- if (ret) {
- ret[0][0].user = _user; //这里是项目当前所有用户
- ret[0][0].data = _data; //把思维网格的内容转化成数组
- response.end(JSON.stringify(ret)); //返回文档内容
- }
- else {
- response.end(''); //返回文档内容
- }
- });
- }
- else {
- //如果不存在则去数据库查找
- us.mysql.usselect(['172.16.12.5', 'pbl', 'select_room', calssid],
- function (ret) {
- var _user = [];
- if (ret) {
- ret[0][0].user = _user; //这里是项目当前所有用户
- response.end(JSON.stringify(ret)); //返回文档内容
- }
- else {
- response.end(''); //返回文档内容
- }
- });
- }
- }
- /**
- * 获取思维网格数据,转成成数组
- *
- */
- us.getMindNetwork = function (response, docid, pageid, userid) {
- //判断文档是否已在内层中存在
- if (us.mindNetwork[docid]) {
- //如果存在则直接返回内层的文档
- if (!us.mindNetwork[docid].user[userid]) {
- us.mindNetwork[docid].user[userid] = [pageid];
- } else {
- us.mindNetwork[docid].user[userid].push(pageid);
- }
- try {
- us.offLineMessage[pageid].mindNetwork.indexOf(docid) == -1 && us.offLineMessage[pageid].mindNetwork.push(docid); //添加到用户离线消息的连接wrod记录中
- }
- catch (e) {
- }
- //console.log(us.mindNetwork[docid]);
- //如果不存在则去数据库查找
- us.mysql.usselect(['172.16.12.5', 'pbl', 'select_room', docid],
- function (ret) {
- if (ret) {
- ret[0][0].data = us.mindNetwork[docid].content; //把思维网格的内容转化成数组
- response.end(JSON.stringify(ret)); //返回文档内容
- }
- else {
- response.end(''); //返回文档内容
- }
- });
- }
- else {
- us.mindNetwork[docid] = {
- user: {},
- history: []
- };
- us.mindNetwork[docid].user[userid] = [pageid];
- if (us.offLineMessage[pageid]) {
- us.offLineMessage[pageid].mindNetwork.indexOf(docid) == -1 && us.offLineMessage[pageid].mindNetwork.push(docid); //添加到用户离线消息的连接wrod记录中
- }
- //如果不存在则去数据库查找
- us.mysql.usselect(['172.16.12.5', 'pbl', 'select_room', docid],
- function (ret) {
- if (ret) {
- us.mindNetwork[docid].content = JSON.parse(ret[0][0].data); //把思维网格的内容转化成数组
- response.end(JSON.stringify(ret)); //返回文档内容
- }
- else {
- us.mindNetwork[docid].content = []; //创建一个空数组
- response.end(''); //返回文档内容
- }
- });
- }
- }
- /**
- * 获取实时课堂据,转成成数组
- *
- */
- us.getRealTimeClass = function (response, docid, pageid, userid) {
- //判断文档是否已在内层中存在
- if (us.realTimeClass[docid]) {
- //如果存在则直接返回内层的文档
- if (!us.realTimeClass[docid].user[userid]) {
- us.realTimeClass[docid].user[userid] = [pageid];
- } else {
- us.realTimeClass[docid].user[userid].push(pageid);
- }
- try {
- us.offLineMessage[pageid].realTimeClass.indexOf(docid) == -1 && us.offLineMessage[pageid].realTimeClass.push(docid); //添加到用户离线消息的连接wrod记录中
- }
- catch (e) {
- }
- //console.log(us.realTimeClass[docid]);
- //如果不存在则去数据库查找
- us.mysql.usselect(['172.16.12.5', 'pbl', 'selectPptPage', docid],
- function (ret) {
- if (ret) {
- ret[0][0].page = us.realTimeClass[docid].content; //把思维网格的内容转化成数组
- response.end(JSON.stringify(ret)); //返回文档内容
- }
- else {
- response.end(''); //返回文档内容
- }
- });
- }
- else {
- us.realTimeClass[docid] = {
- user: {},
- history: []
- };
- us.realTimeClass[docid].user[userid] = [pageid];
- if (us.offLineMessage[pageid]) {
- us.offLineMessage[pageid].realTimeClass.indexOf(docid) == -1 && us.offLineMessage[pageid].realTimeClass.push(docid); //添加到用户离线消息的连接wrod记录中
- }
- //如果不存在则去数据库查找
- us.mysql.usselect(['172.16.12.5', 'pbl', 'selectPptPage', docid],
- function (ret) {
- if (ret) {
- us.realTimeClass[docid].content = JSON.parse(ret[0][0].page); //把思维网格的内容转化成数组
- response.end(JSON.stringify(ret)); //返回文档内容
- }
- else {
- us.realTimeClass[docid].content = []; //创建一个空数组
- response.end(''); //返回文档内容
- }
- });
- }
- }
- /**
- * 获取文档,并生成DOM
- *
- * @param {object} response 用户相应对象
- * @param {string} docid 文档ID
- */
- us.getWord = function (response, docid, pageid, userid) {
- // //判断文档是否已在内层中存在
- // if (us.word[docid]) {
- // //如果存在则直接返回内层的文档
- // if (!us.word[docid].user[userid]) {
- // us.word[docid].user[userid] = [pageid];
- // } else {
- // us.word[docid].user[userid].push(pageid);
- // }
- // us.offLineMessage[pageid].word.indexOf(docid) == -1 && us.offLineMessage[pageid].word.push(docid); //添加到用户离线消息的连接wrod记录中
- // response.end(us.word[docid].content('body').html());
- // }
- // else {
- // us.word[docid] = {
- // user: {},
- // history: []
- // };
- // us.word[docid].user[userid] = [pageid];
- // if (us.offLineMessage[pageid]) {
- // us.offLineMessage[pageid].word.indexOf(docid) == -1 && us.offLineMessage[pageid].word.push(docid); //添加到用户离线消息的连接wrod记录中
- // }
- // //如果不存在则去数据库查找
- // us.mysql.usselect(['172.16.12.5', 'pbl', 'select_file', docid],
- // function (ret) {
- // if (ret) {
- // var _office = JSON.parse(ret)[0][0].data ? JSON.parse(ret)[0][0].data.replace(/\\'/ig, '\'').replace(/\\"/ig, "\"") : ""; //记录查找内容
- // us.word[docid].content = us.cheerio.load(_office); //将文档添加到内层中
- // response.end(_office); //返回文档内容
- // } else {
- // us.word[docid].content = us.cheerio.load(''); //将文档添加到内层中
- // response.end(''); //返回文档内容
- // }
- // });
- // }
- //判断文档是否已在内层中存在
- if (us.word[docid]) {
- //如果存在则直接返回内层的文档
- if (!us.word[docid].user[userid]) {
- us.word[docid].user[userid] = [pageid];
- } else {
- us.word[docid].user[userid].push(pageid);
- }
- // console.log(us.offLineMessage[pageid]);
- // console.log(pageid);
- try {
- us.offLineMessage[pageid].word.indexOf(docid) == -1 && us.offLineMessage[pageid].word.push(docid); //添加到用户离线消息的连接wrod记录中
- }
- catch (e) {
- }
- //如果不存在则去数据库查找
- us.mysql.usselect(['172.16.12.5', 'pbl', 'select_file', docid],
- function (ret) {
- if (ret) {
- ret[0][0].data = us.word[docid].content('body').html();
- response.end(JSON.stringify(ret)); //返回文档内容
- } else {
- us.word[docid].content = us.cheerio.load(''); //将文档添加到内层中
- response.end(''); //返回文档内容
- }
- });
- }
- else {
- us.word[docid] = {
- user: {},
- history: []
- };
- us.word[docid].user[userid] = [pageid];
- if (us.offLineMessage[pageid]) {
- us.offLineMessage[pageid].word.indexOf(docid) == -1 && us.offLineMessage[pageid].word.push(docid); //添加到用户离线消息的连接wrod记录中
- }
- //如果不存在则去数据库查找
- us.mysql.usselect(['172.16.12.5', 'pbl', 'select_file', docid],
- function (ret) {
- if (ret) {
- var _office = ret[0][0].data ? ret[0][0].data.replace(/\\'/ig, '\'').replace(/\\"/ig, "\"") : ""; //记录查找内容
- us.word[docid].content = us.cheerio.load(_office); //将文档添加到内层中
- response.end(_office); //返回文档内容
- } else {
- us.word[docid].content = us.cheerio.load(''); //将文档添加到内层中
- response.end(''); //返回文档内容
- }
- });
- }
- }
- us.getExcel = function (response, docid, pageid, userid) {
- //判断文档是否已在内层中存在
- if (us.excel[docid]) {
- //如果存在则直接返回内层的文档
- if (!us.excel[docid].user[userid]) {
- us.excel[docid].user[userid] = [pageid];
- } else {
- us.excel[docid].user[userid].push(pageid);
- }
- us.offLineMessage[pageid].excel.indexOf(docid) == -1 && us.offLineMessage[pageid].excel.push(docid); //添加到用户离线消息的连接wrod记录中
- var _office = us.excel[docid].content;
- response.end(JSON.stringify(_office));
- } else {
- us.excel[docid] = {
- user: {},
- history: []
- };
- us.excel[docid].user[userid] = [pageid];
- us.offLineMessage[pageid].excel.indexOf(docid) == -1 && us.offLineMessage[pageid].excel.push(docid); //添加到用户离线消息的连接wrod记录中
- //如果不存在则去数据库查找
- us.mysql.usselect([
- 'sqlserver.1473.cn',
- 'UseStudio_Disk',
- 'GetFileContent',
- docid
- ], function (ret) {
- if (ret) {
- var _office = JSON.parse(ret)[0][0].UsOffice;
- us.excel[docid].content = JSON.parse(_office); //将文档添加到内层中
- response.end(_office); //返回文档内容
- } else {
- us.excel[docid].content = ''; //将文档添加到内层中
- response.end(''); //返回文档内容
- }
- });
- }
- }
- /**
- * 新建文档保存内容并加入协同
- * @param {object} response 用户相应对象
- * @param {guid} docid 文档ID
- * @param {string} content 文档内容
- * @param {string} name 文档名
- * @param {guid} userid 用户ID
- * @param {guid} pageid 页面唯一识别ID
- */
- us.newExcelSave = function (response, docid, content, name, userid, pageid) {
- us.excel[docid] = {
- user: {},
- history: []
- }; //创建excel类
- us.excel[docid].user[userid] = [pageid]; //将用户加入协同记录数组
- us.offLineMessage[pageid].excel.indexOf(docid) == -1 && us.offLineMessage[pageid].word.push(docid); //添加到用户离线消息的连接wrod记录中
- //发送保存请求
- us.mysql.usselect([
- 'sqlserver.1473.cn',
- 'UseStudio_Disk',
- 'SaveFileContent',
- docid,
- content,
- name
- ], function (ret) {
- if (ret) {
- //设置内容
- us.excel[docid].content = JSON.parse(content); //将文档添加到内层中
- response.end('saveOk'); //返回保存成功
- } else {
- response.end('fail'); //返回文档内容
- }
- });
- }
- /**
- * 生成Guid
- *
- **/
- us.createGuid = function () {
- //返回guid的处理
- return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
- var r = Math.random() * 16 | 0,
- v = c == 'x' ? r : (r & 0x3 | 0x8);
- return v.toString(16);
- });
- };
- /**
- * 监听1473端口的处理
- *
- **/
- us.server.listen('1473', '', function () {
- //用户下线监控
- setInterval(function () { us.offLine() }, 60000);
- //在控制台输出监听提示
- console.log("开始监听" + us.server.address().port + "......");
- });
- // 同意处理错误
- //process.on('uncaughtException', function (e) {
- // console.log("错误:" + e);
- //});
|