message.js 99 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645
  1. var us = {}; //全局的命名空间
  2. us.cheerio = require("cheerio"); //dom处理类
  3. us.qs = require("querystring"); //引用文件解析包
  4. us.http = require("http"); //引用http server服务
  5. us.mysql = require("./mysql.js"); //引用执行存储过程包
  6. /**
  7. * 保持知识建构的时间变量
  8. **/
  9. us.savecscltime = null;
  10. /**
  11. * 用户的链接池集合,以用户id作为搜索键
  12. **/
  13. us.connect = {
  14. //userguid:{pageid : us.userConnect, pageid : us.userConnect}
  15. }
  16. /**
  17. * 用户的数据集合集合,以用户id作为搜索键
  18. **/
  19. us.user = {
  20. //userId : userInfo //用户连接(请求), 这里可以是http短连接可以使socket长连接
  21. }
  22. /**
  23. * 未来课堂数据结构
  24. **/
  25. us.cscl = {
  26. //"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}],
  27. //"edges":[],
  28. //"backgroundUrl":"img/bg.ccee1c62.png"
  29. }
  30. /**
  31. * 离线数据
  32. **/
  33. us.offLineMessage = {
  34. //pageId : us.userOffLineMessage
  35. }
  36. /**
  37. * 思维网格
  38. **/
  39. us.mindNetwork = {
  40. // mindid: {
  41. // }
  42. }
  43. /**
  44. * 同步课堂
  45. **/
  46. us.realTimeClass = {
  47. // mindid: {
  48. // }
  49. }
  50. //文档列表
  51. us.word = {
  52. //docId : {
  53. // user : {userId : [pageId, pageId],
  54. // userId : [pageId, pageId]},
  55. // content : docel,
  56. // history : [operand(操作信息对象), operand(操作信息对象)]
  57. // } //群信息结构
  58. //docId : {user : {userId : [pageId, pageId], userId : [pageId, pageId]}, content : docel, history : [operand(操作信息对象), operand(操作信息对象)] } //文档信息结构
  59. }
  60. //excel列表
  61. us.excel = {
  62. //excelId : {user : {userId : [pageId, pageId], userId : [pageId, pageId]}, content : docel, history : [operand(操作信息对象), operand(操作信息对象)], name : 文件名 } //群信息结构
  63. //excelId : {user : {userId : [pageId, pageId], userId : [pageId, pageId]}, content : docel, history : [operand(操作信息对象), operand(操作信息对象)], name : 文件名 } //群信息结构
  64. }
  65. //导航文件列表
  66. us.nav = {
  67. //fileId : {
  68. // user : {userId : [pageId, pageId], userId : [pageId, pageId]},
  69. // navInfo : {
  70. // logo : '',
  71. // nav : [ {pageId: '', name : ''}, {pageId: '', name : '', child :[ {pageId: '', name : ''} ]} ]
  72. // },
  73. // page : {pageId : pageContent, pageId : pageContent},
  74. // history : [operand(操作信息对象), operand(操作信息对象)]
  75. //} //导航文档数据结构
  76. }
  77. //知识建构的数据结构
  78. us.cscldata = function () {
  79. return {
  80. id: "",
  81. userid: "",
  82. name: "",
  83. num: "",
  84. data: "",
  85. isClose: "",
  86. create_at: "",
  87. createuser: "",
  88. smailheadportrait: ""
  89. };
  90. }
  91. /**
  92. * 用户链接类
  93. *
  94. * @param {string} pageId 页面ID(用户登录唯一识别ID)
  95. * @param {object} response 对象
  96. **/
  97. us.userConnect = function (pageId, response) {
  98. return {
  99. "pageId": pageId, //登录用户唯一识别id
  100. "response": response, //这里可以是http短连接以后可以换成socket长连接
  101. "loginTime": new Date(), //用户登录时间
  102. "offLineTime": null //用户下线时间
  103. }
  104. }
  105. /**
  106. * 用户数据类
  107. *
  108. * @param {string} requset对象
  109. * @param {object} response对象
  110. **/
  111. us.userInfo = function (userid, username) {
  112. return {
  113. "userId": userid, //用户id
  114. "userName": username //用户名
  115. }
  116. }
  117. /**
  118. * 用户离线数据
  119. * @param {string} type 用户登录类型
  120. */
  121. us.userOffLineMessage = function (type) {
  122. return {
  123. "message": [], //用户的消息数组
  124. "type": type, //链接的端 pc或者pho
  125. "word": [], //word数据
  126. "excel": [], //excel数据
  127. "mindNetwork": [], //思维网格数据
  128. "realTimeClass": [], //实施课堂数据
  129. "cscl": [], //未来课堂数据
  130. "nav": [] //导航模式数据
  131. }
  132. }
  133. /**
  134. * 用户的离线消息结构
  135. *
  136. * @param {string} sendid 发送者id
  137. * @param {string} type 发送的类型, 俱乐部使用时用us开头,后面带用途。例如us.disk或者us.friend us.word
  138. * @param {string} messageinfo 的处理
  139. **/
  140. us.message = function (sendid, type, messageinfo) {
  141. return {
  142. "id": us.createGuid(), //新生成的guid
  143. "sendId": sendid, //发送消息人的id.
  144. "time": new Date(), //发送时间
  145. "type": type, //类型 俱乐部使用时用us开头,后面带用途。例如us.disk或者us.friend us.word
  146. "messageInfo": messageinfo //{消息结构} 此处为object 如类型为'us.word' 则消息结构为us.word
  147. }
  148. }
  149. /*
  150. messageinfo 消息结构的处理
  151. {
  152. id: string, //行id 多行时用逗号隔开的形式 guid,guid,guid,guid
  153. type: string, // 处理的类型 如:"add"||"delete"||"update"
  154. docId: string, // "guid" 文档id
  155. pageId: pageid, //页面唯一识别ID
  156. content: string // 变化的内容 add的时候为添加到添加行的下一行
  157. nextId : string //下一行id
  158. }
  159. */
  160. process.on('uncaughtException', (e) => {
  161. console.error('process error is:', e.message);
  162. });
  163. /**
  164. * 创建服务
  165. *
  166. * @param {object} requset 请求对象
  167. * @param {object} response 响应对象
  168. **/
  169. us.server = us.http.createServer(function (requset, response) {
  170. //设置跨域
  171. response.writeHead(200, {
  172. "Content-Type": "text/html;application/json;charset=utf-8",
  173. 'Access-Control-Allow-Origin': '*',
  174. 'Access-Control-Allow-Headers': 'Origin, No-Cache, X-Requested-With, If-Modified-Since, Pragma, Last-Modified, Cache-Control, Expires, Content-Type, X-E4M-With'
  175. });
  176. //设置参数,并执行回调
  177. us.unifyDispose(requset, response, function (param) {
  178. //console.log(param)
  179. if (param && param[0]) {
  180. //参数解码 us.parm为全局变量 记录所有参数
  181. var _userinfo,
  182. _funname = param[0],
  183. //参数解码 中文处理
  184. _param = param.splice(1);
  185. //执行回调函数
  186. switch (_funname) {
  187. //执行登录函数
  188. case 'login':
  189. _userinfo = JSON.parse(_param[0]); //登录用户的信息
  190. us.login(requset, response, _userinfo.userId, _userinfo.pageId, _userinfo.userName, _userinfo.type);
  191. break;
  192. case 'logout':
  193. us.logout(response, _param[0], _param[1]);
  194. break;
  195. //执行轮询函数
  196. case 'polling':
  197. us.polling(requset, response, _param[0], _param[1], _param[2], _param[3]);
  198. break;
  199. //获取文档信息
  200. case 'getWord':
  201. us.getWord(response, _param[0], _param[1], _param[2]);
  202. break;
  203. //获取Excel信息
  204. case 'getExcel':
  205. us.getExcel(response, _param[0], _param[1], _param[2]);
  206. break;
  207. //获取导航文件信息
  208. case 'getNav':
  209. us.getNav(response, _param[0], _param[1], _param[2]);
  210. break;
  211. //获取思维网格信息
  212. case 'getMindNetwork':
  213. us.getMindNetwork(response, _param[0], _param[1], _param[2]);
  214. break;
  215. //获取未来课堂信息
  216. case 'getcscl':
  217. us.getcscl(response, _param[0], _param[1], _param[2]);
  218. break;
  219. //获取未来课堂数据
  220. case 'getcscldata':
  221. us.getcscldata(response, _param[0], _param[1], _param[2]);
  222. break;
  223. //获取文档信息
  224. case 'getRealTimeClass':
  225. us.getRealTimeClass(response, _param[0], _param[1], _param[2]);
  226. break;
  227. case 'getNavPageContent':
  228. us.getNavPageContent(response, _param[0], _param[1]);
  229. break;
  230. //执行发送函数
  231. case 'send':
  232. us.send(response, _param[0]);
  233. break;
  234. case 'quitOffice':
  235. us.quitOffice(response, _param[0]);
  236. break;
  237. case 'quitcscl':
  238. us.quitcscl(response, _param[0]);
  239. break;
  240. case 'newWordSave':
  241. us.newWordSave(response, _param[0], _param[1], _param[2], _param[3], _param[4]);
  242. break;
  243. case 'newExcelSave':
  244. us.newExcelSave(response, _param[0], _param[1], _param[2], _param[3], _param[4]);
  245. break;
  246. //新建导航文件保存
  247. case 'newNavSave':
  248. us.newNavSave(response, _param[0], _param[1], _param[2], _param[3], _param[4], _param[5], _param[6]);
  249. break;
  250. //加入文档编辑
  251. case 'addWord':
  252. break;
  253. //加入Excel表格编辑
  254. case 'addExcel':
  255. us.addExcel(response, _param[0]);
  256. break;
  257. //加入导航文件
  258. case 'addNav':
  259. break;
  260. case 'copyPageContent':
  261. us.copyPageContent(response, _param[0], _param[1], _param[2], _param[3]);
  262. break;
  263. case 'updateName':
  264. us.updateName(response, _param[0], _param[1], _param[2], _param[3], _param[4]);
  265. break;
  266. case 'insertModifyRecord':
  267. us.insertModifyRecord(response, _param[0], _param[1], _param[2], _param[3], _param[4], _param[4]);
  268. break;
  269. case 'addUser':
  270. us.addUser(response, _param[0], _param[1], _param[2], _param[3], _param[4]);
  271. break;
  272. //其余执行
  273. default:
  274. response.end(0);
  275. }
  276. }
  277. else {
  278. response.end("");
  279. }
  280. });
  281. });
  282. /**
  283. * 协同历史(带保存功能,修改了存储过程 既添加历史也保存内容)
  284. *
  285. * @param {object} request 请求对象
  286. * @param {string} type 类型
  287. * @param {guid} fileid 文件id
  288. * @param {string} filename 文件名
  289. * @param {string} html 内容
  290. * @param {guid} userid 用户ID
  291. * @param {guid} pageid 页面ID
  292. **/
  293. us.insertModifyRecord = function (response, type, fileid, filename, html, userid, pageid) {
  294. var _html;
  295. if (us[type][fileid] && us[type][fileid].content) {
  296. _html = us[type][fileid].content('body').html().replace(/'/ig, "''").replace(/"/ig, "\\\"");
  297. } else {
  298. _html = html;
  299. }
  300. //发送保存文件请求
  301. us.mysql.usselect([
  302. '127.0.0.1',
  303. 'UseStudio_Office',
  304. 'InsertDocumentModifyRecord',
  305. userid,
  306. fileid,
  307. _html,
  308. filename
  309. ], function (ret) {
  310. if (ret) {
  311. var _historyid = JSON.parse(ret)[0][0].historyid;
  312. var _message = {};
  313. var _messageinfo = us.message(userid, 'us.' + type, {
  314. id: us.createGuid(),
  315. type: "insertModifyRecord",
  316. historyid: _historyid,
  317. docId: fileid
  318. });
  319. _message[fileid] = [_messageinfo];
  320. us.broadcast(_message, pageid, type);
  321. response.end(_historyid); //返回文档内容
  322. } else {
  323. response.end(ret); //返回文档内容
  324. }
  325. });
  326. }
  327. /**
  328. * 更新文件名
  329. *
  330. * @param {object} request 请求对象
  331. * @param {string} type 类型
  332. * @param {guid} fileid 文件id
  333. * @param {string} filename 文件名
  334. * @param {guid} userid 用户ID
  335. * @param {guid} pageid 页面ID
  336. **/
  337. us.updateName = function (response, type, fileid, filename, userid, pageid) {
  338. //发送保存文件请求
  339. us.mysql.usselect([
  340. 'sqlserver.1473.cn',
  341. 'UseStudio_Disk',
  342. 'UpdateFileInfo',
  343. fileid,
  344. filename
  345. ], function (ret) {
  346. if (ret) {
  347. var _message = {};
  348. var _messageinfo = us.message(userid, 'us.' + type, {
  349. id: us.createGuid(),
  350. type: "updateName",
  351. name: filename,
  352. docId: fileid
  353. });
  354. _message[fileid] = [_messageinfo];
  355. us.broadcast(_message, pageid, type);
  356. response.end('updateNameSuccess'); //返回文档内容
  357. } else {
  358. response.end('updateNameFail'); //返回文档内容
  359. }
  360. });
  361. }
  362. /**
  363. * 添加用户
  364. *
  365. * @param {object} request 请求对象
  366. * @param {string} type 类型
  367. * @param {guid} fileid 文件id
  368. * @param {string} userids 用户id(多个用逗号隔开)
  369. * @param {guid} userid 用户ID
  370. * @param {guid} pageid 页面ID
  371. **/
  372. us.addUser = function (response, type, fileid, userids, userid, pageid) {
  373. var _message = {};
  374. var _messageinfo = us.message(userid, 'us.' + type, {
  375. id: us.createGuid(),
  376. type: "addUser",
  377. userIds: userids,
  378. docId: fileid
  379. });
  380. _message[fileid] = [_messageinfo];
  381. us.broadcast(_message, pageid, type);
  382. response.end('addUserSuccess'); //返回文档内容
  383. }
  384. /**
  385. * 根据用户集合,获取详细的用户信息
  386. *
  387. * @param {object} userids 用户id集合
  388. **/
  389. us.getuserbytype = function (userids) {
  390. var _userinfo = [];
  391. //console.log(us.user);
  392. for (var i in userids) {
  393. if (us.user[i]) {
  394. _userinfo.push(us.user[i])
  395. }
  396. }
  397. return _userinfo;
  398. }
  399. /**
  400. * 用户加入某个项目后通知所有已经在项目的用户
  401. *
  402. * @param {object} userinfo 用户信息
  403. * @param {string} type 项目类型
  404. * @param {string} fileid 项目的id
  405. **/
  406. us.sendUser = function (userinfo, type, pageid, fileid) {
  407. var _message = {};
  408. var _messageinfo = us.message(userinfo.userid, 'us.' + type, {
  409. id: us.createGuid(),
  410. type: "addUser",
  411. userinfo: userinfo,
  412. docId: fileid
  413. });
  414. _message[fileid] = [_messageinfo];
  415. // console.log("2222222222")
  416. // console.log(_message)
  417. us.broadcast(_message, pageid, type);
  418. }
  419. /**
  420. * 获取用户传参异步
  421. *
  422. * @param {object} request 请求对象
  423. * @param {function} cb 回调函数
  424. **/
  425. us.unifyDispose = function (req, res, cb) {
  426. var i, _param = "";
  427. //接收参数
  428. req.on("data", function (chunk) {
  429. _param += chunk;
  430. });
  431. //接收完毕
  432. req.on("end", function () {
  433. //console.log(_param)
  434. //前端传入的字符串转化成对象
  435. if (_param) {
  436. _param = us.qs.parse(_param);
  437. }
  438. //console.log(_param)
  439. //拆分参数
  440. try {
  441. _param = _param.mode.split(",")
  442. } catch (e) {
  443. _param = req.query;
  444. }
  445. if (_param) {
  446. //循环解析,由于前端发送过来的信息是经过url加密的,这里是解密
  447. for (i = 0; i < _param.length; i++) {
  448. _param[i] = decodeURIComponent(_param[i]); //解析
  449. }
  450. } else {
  451. _param = [];
  452. }
  453. //回调到指定处理
  454. cb(_param);
  455. });
  456. //请求超时处理
  457. res.on('timeout', function (e) {
  458. try {
  459. //console.log('请求超时'); //错误消息的输出
  460. //res.end("false"); //返回值
  461. }
  462. catch (e) {
  463. console.error('process error is:', e.message);
  464. }
  465. });
  466. //设置30秒后超时
  467. res.setTimeout(60000, function () {
  468. try {
  469. //console.log("响应超时.");
  470. res.end(""); //返回值
  471. }
  472. catch (e) {
  473. console.error('process error is:', e.message);
  474. }
  475. });
  476. //错误处理
  477. req.on("error", function (e) {
  478. try {
  479. console.log(e);
  480. }
  481. catch (e) {
  482. console.error('process error is:', e.message);
  483. }
  484. });
  485. }
  486. /**
  487. * 获取用户传参异步
  488. *
  489. * @param {object} response 响应对象
  490. * @param {array} userid 用户ID
  491. * @param {string} pageid 页面ID(登录唯一识别ID)
  492. * @param {string} username 用户名
  493. * @param {type} username 用户登录类型
  494. **/
  495. us.login = function (requset, response, userid, pageid, username, type) {
  496. if (userid && pageid) {
  497. //用户登录信息表,以后移入redis
  498. //如果用户信息不存在则新建一个用户信息存入
  499. if (!us.user[userid]) {
  500. username = username || "";
  501. var _userinfo = us.userInfo(userid, username); //初始化用户信息
  502. us.user[userid] = _userinfo;
  503. }
  504. //添加用户轮询列表
  505. us.polling(requset, response, userid, pageid, type);
  506. }
  507. }
  508. /**
  509. * 用户下线,清理用户的数据
  510. */
  511. us.deluser = function (userid, pageid) {
  512. if (pageid && us.offLineMessage[pageid]) {
  513. var i, j, z; //定义循环变量
  514. var _userid, _fileid, _message, _docinfo, _messageinfo,
  515. _csclids = us.offLineMessage[pageid].cscl;
  516. //其他应用的数据
  517. // _wordids = us.offLineMessage[pageid].word, //得到加入的所有word
  518. // _excelids = us.offLineMessage[pageid].excel; //得到加入的所有excel
  519. console.log("1111111111111")
  520. console.log(_csclids)
  521. console.log(pageid)
  522. console.log(us.offLineMessage[pageid])
  523. //删除用户离校消息记录
  524. delete us.offLineMessage[pageid];
  525. //知识架构的下线的处理
  526. for (i = 0; i < _csclids.length; i++) { //循环处理每一个文档
  527. _docinfo = us.cscl[_csclids[i]]; //记录文档信息
  528. if (_docinfo) { //如果存在文档信息
  529. _fileid = _csclids[i];
  530. _userid = us.quitUserList(_docinfo, userid, pageid); //将用户在协同列表中删除
  531. }
  532. }
  533. console.log(_userid)
  534. //下线后通知所有的用户
  535. if (_userid) {
  536. _message = {};
  537. var _messageinfo = us.message(userid, 'us.cscl', {
  538. id: us.createGuid(),
  539. type: "delUser",
  540. userinfo: us.user[_userid],
  541. docId: _fileid
  542. });
  543. _message[_fileid] = [_messageinfo];
  544. console.log(_message)
  545. us.broadcast(_message, pageid, "cscl");
  546. }
  547. }
  548. //其他应用的处理
  549. // //word的下线的处理
  550. // for (i = 0; i < _wordids.length; i++) { //循环处理每一个文档
  551. // _docinfo = us.word[_wordids[i]]; //记录文档信息
  552. // if (_docinfo) { //如果存在文档信息
  553. // us.quitUserList(_docinfo, userid, pageid); //将用户在协同列表中删除
  554. // us.insertHistory(_docinfo, 'word'); //插入历史
  555. // }
  556. // }
  557. // //excel的下线的处理
  558. // for (j = 0; i < _excelids.length; j++) {
  559. // _docinfo = us.excel[_excelids[j]]; //记录文档信息
  560. // if (_docinfo) { //如果存在文档信息
  561. // us.quitUserList(_docinfo, userid, pageid); //将用户在协同列表中删除
  562. // us.insertHistory(_docinfo, 'excel'); //插入历史
  563. // }
  564. // }
  565. }
  566. /**
  567. * 用户下线,用计时器实时的监控用户是否在线
  568. */
  569. us.offLine = function () {
  570. var i, j;
  571. var _user, _connect = us.connect;
  572. //循环目前所有的连接用户
  573. for (i in _connect) {
  574. _user = _connect[i];
  575. for (j in _user) {
  576. console.log("====================offline======================");
  577. console.log(i);
  578. console.log(j);
  579. console.log(_user[j].response.finished);
  580. console.log((_user[j].response.connection == null || _user[j].response.connection.destroyed));
  581. //判断用户已下线
  582. if (_user[j].response.finished || (_user[j].response.connection == null || _user[j].response.connection.destroyed)) {
  583. //if (_user[j].response.connection == null || _user[j].response.connection.destroyed) {
  584. //等待五秒后,判断用户是否重连,重连的用户不为下线用户
  585. us.offLineByUser(i, j, _user);
  586. }
  587. }
  588. }
  589. }
  590. /**
  591. * 用户下线
  592. */
  593. us.offLineByUser = function (i, j, _user) {
  594. setTimeout(() => {
  595. if (_user[j].response.finished || (_user[j].response.connection == null || _user[j].response.connection.destroyed)) {
  596. var k, istrue = true;
  597. us.deluser(i, j); //删除用户的数据
  598. delete _user[j];
  599. for (k in _user) { istrue = false; break }
  600. //这个用户下所有的连接用户都不在了
  601. if (istrue) {
  602. //用户登录数据清理,用于已经下线
  603. delete us.user[i];
  604. delete us.connect[i];
  605. }
  606. }
  607. }, 5000);
  608. }
  609. /**
  610. * 获取用户传参异步
  611. *
  612. * @param {object} response 响应对象
  613. * @param {string} userid 用户ID
  614. * @param {string} pageid 页面ID(用户登录唯一识别ID)
  615. **/
  616. us.polling = function (requset, response, userid, pageid, type) {
  617. // 监听请求的close事件
  618. // requset.on('close', () => {
  619. // console.log("--------" + userid + "-------------" + pageid);
  620. // //us.offLine(response, userid, pageid)
  621. // });
  622. //判断轮询的用户是否在连接池中
  623. if (us.connect[userid]) {
  624. var _data,
  625. _userarray = us.connect[userid], //用户连接池,获取制定用户连接的数量,
  626. _messagearray = us.offLineMessage[pageid] //用户离线信息
  627. ;
  628. const chunkSize = 100;
  629. //获取当前连接的用户,如果获取成功那么直接修改用户的连接
  630. if (_userarray[pageid]) {
  631. //防止重复多连
  632. try {
  633. _userarray[pageid].response.end("");
  634. }
  635. catch (e) {
  636. }
  637. _userarray[pageid].response = response;
  638. if (_messagearray && _messagearray.message.length > 0) {
  639. _data = JSON.stringify(_messagearray.message);
  640. for (let i = 0; i < _data.length; i += chunkSize) {
  641. const chunk = _data.slice(i, i + chunkSize);
  642. response.write(chunk);
  643. }
  644. response.write("\r\n");
  645. _messagearray.message = [];
  646. }
  647. else {
  648. us.offLineMessage[pageid] = us.offLineMessage[pageid] || us.userOffLineMessage(type); //初始化用户离线消息记录
  649. }
  650. }
  651. else {
  652. //如果用户连接不存在
  653. _userarray[pageid] = us.userConnect(pageid, response); //那么直接添加用户连接 添加到用户连接数组里
  654. us.offLineMessage[pageid] = us.userOffLineMessage(type); //初始化用户离线消息记录
  655. }
  656. }
  657. else {
  658. var _userconect = us.userConnect(pageid, response); //创建用户连接对象
  659. us.connect[userid] = {}; //添加用户连接对象
  660. us.connect[userid][pageid] = _userconect;
  661. us.offLineMessage[pageid] = us.offLineMessage[pageid] || us.userOffLineMessage(type); //初始化用户离线消息记录
  662. }
  663. }
  664. /**
  665. * 发送消息
  666. *
  667. * @param {object} response 响应对象
  668. * @param {array} messageinfo 参数数组
  669. **/
  670. us.send = function (response, messageinfo) {
  671. var i, j,
  672. _message,
  673. _messagelist = {},
  674. _messageobj = JSON.parse(messageinfo) //获取传入的json对象
  675. ;
  676. //循环用户的信息处理
  677. for (i in _messageobj) {
  678. //console.log(i);
  679. switch (i) {
  680. //思维网格交互
  681. case "us.mindNetwork":
  682. _messagearr = _messageobj[i]; //获取word消息
  683. //得到word所有的用户
  684. for (j = 0; j < _messagearr.length; j++) {
  685. //生成发送的消息结构体
  686. _message = us.message(_messagearr[j].sendId, "us.mindNetwork", _messagearr[j].messageInfo);
  687. //添加消息列表
  688. if (_messagelist[_messagearr[j].receiveId]) {
  689. _messagelist[_messagearr[j].receiveId].push(_message);
  690. } else {
  691. _messagelist[_messagearr[j].receiveId] = [_message];
  692. }
  693. us.mindNetwork[_message.messageInfo.docId] && us.mindNetwork[_message.messageInfo.docId].history.push(_message);
  694. us.updatemindNetwork(_message.messageInfo);
  695. }
  696. //调用群发函数
  697. _messagearr[0] && us.broadcast(_messagelist, _messagearr[0].messageInfo.pageId, 'mindNetwork');
  698. break;
  699. //未来课堂交互
  700. case "us.cscl":
  701. _messagearr = _messageobj[i]; //获取未来消息
  702. //得到未来课堂所有的用户
  703. for (j = 0; j < _messagearr.length; j++) {
  704. // //如果存在则直接返回内层的文档
  705. // if (!us.cscl[calssid].user[userid]) {
  706. // us.cscl[calssid].user[userid] = [pageid];
  707. // }
  708. // else if (us.cscl[calssid].user[userid].indexOf(pageid) == -1) {
  709. // us.cscl[calssid].user[userid].push(pageid);
  710. // }
  711. // try {
  712. // us.offLineMessage[pageid].cscl.indexOf(calssid) == -1 && us.offLineMessage[pageid].cscl.push(calssid); //添加到用户离线消息的连接wrod记录中
  713. // }
  714. // catch (e) {
  715. // }
  716. _messagearr[j].messageInfo.type = _messagearr[j].type;
  717. //生成发送的消息结构体
  718. _message = us.message(_messagearr[j].sendId, "us.cscl", _messagearr[j].messageInfo);
  719. _message.ttype = _messagearr[j].type;
  720. //添加消息列表
  721. if (_messagelist[_messagearr[j].receiveId]) {
  722. _messagelist[_messagearr[j].receiveId].push(_message);
  723. }
  724. else {
  725. _messagelist[_messagearr[j].receiveId] = [_message];
  726. }
  727. us.cscl[_messagearr[j].receiveId] && us.cscl[_messagearr[j].receiveId].history.push(_message);
  728. us.updatecscl(_message.messageInfo, _messagearr[j].receiveId, _messagearr[j].type);
  729. }
  730. //调用群发函数
  731. _messagearr[0] && us.broadcast(_messagelist, _messagearr[0].pageId, 'cscl');
  732. break;
  733. //同步课堂
  734. case "us.realTimeClass":
  735. //console.log("us.realTimeClass");
  736. _messagearr = _messageobj[i]; //获取word消息
  737. //得到word所有的用户
  738. for (j = 0; j < _messagearr.length; j++) {
  739. //生成发送的消息结构体
  740. _message = us.message(_messagearr[j].sendId, "us.realTimeClass", _messagearr[j].messageInfo);
  741. //console.log(_message)
  742. // console.log(_messagelist[_messagearr[j].receiveId])
  743. //添加消息列表
  744. if (_messagelist[_messagearr[j].receiveId]) {
  745. _messagelist[_messagearr[j].receiveId].push(_message);
  746. } else {
  747. _messagelist[_messagearr[j].receiveId] = [_message];
  748. }
  749. us.realTimeClass[_message.messageInfo.docId] && us.realTimeClass[_message.messageInfo.docId].history.push(_message);
  750. us.updaterealTimeClass(_message.messageInfo);
  751. }
  752. //调用群发函数
  753. _messagearr[0] && us.broadcast(_messagelist, _messagearr[0].messageInfo.pageId, 'realTimeClass');
  754. break;
  755. //word信息交互
  756. case "us.word":
  757. try {
  758. _messagearr = _messageobj[i]; //获取word消息
  759. //得到word所有的用户
  760. for (j = 0; j < _messagearr.length; j++) {
  761. //生成发送的消息结构体
  762. _message = us.message(_messagearr[j].sendId, "us.word", _messagearr[j].messageInfo);
  763. //添加消息列表
  764. if (_messagelist[_messagearr[j].receiveId]) {
  765. _messagelist[_messagearr[j].receiveId].push(_message);
  766. } else {
  767. _messagelist[_messagearr[j].receiveId] = [_message];
  768. }
  769. us.word[_message.messageInfo.docId] && us.word[_message.messageInfo.docId].history.push(_message);
  770. us.updateWord(_message.messageInfo);
  771. }
  772. //由于不能同时操控多个word发送资料,所有信息只同步一个
  773. //_users = us.word[_messagearr[0].receiveId].user;
  774. //调用群发函数
  775. _messagearr[0] && us.broadcast(_messagelist, _messagearr[0].messageInfo.pageId, 'word');
  776. }
  777. catch (e) {
  778. console.log(e)
  779. }
  780. break;
  781. //用户信息的交互
  782. case "us.excel":
  783. _messagearr = _messageobj[i]; //获取excel消息
  784. //得到excel所有的用户
  785. for (j = 0; j < _messagearr.length; j++) {
  786. //生成发送的消息结构体
  787. _message = us.message(_messagearr[j].sendId, _messagearr[j].type, _messagearr[j].messageInfo);
  788. //添加消息列表
  789. if (_messagelist[_messagearr[j].receiveId]) {
  790. _messagelist[_messagearr[j].receiveId].push(_message);
  791. } else {
  792. _messagelist[_messagearr[j].receiveId] = [_message];
  793. }
  794. us.excel[_message.messageInfo.docId] && us.excel[_message.messageInfo.docId].history.push(_message);
  795. us.updateExcel(_message.messageInfo);
  796. }
  797. //由于不能同时操控多个excel发送资料,所有信息只同步一个
  798. //_users = us.excel[_messagearr[0].receiveId].user;
  799. //调用群发函数
  800. us.broadcast(_messagelist, _messagearr[0].messageInfo.pageId, 'excel');
  801. break;
  802. case "us.nav":
  803. _messagearr = _messageobj[i]; //获取excel消息
  804. //得到excel所有的用户
  805. for (j = 0; j < _messagearr.length; j++) {
  806. //生成发送的消息结构体
  807. _message = us.message(_messagearr[j].sendId, "us.nav", _messagearr[j].messageInfo);
  808. //添加消息列表
  809. if (_messagelist[_messagearr[j].receiveId]) {
  810. _messagelist[_messagearr[j].receiveId].push(_message);
  811. } else {
  812. _messagelist[_messagearr[j].receiveId] = [_message];
  813. }
  814. us.nav[_message.messageInfo.docId] && us.nav[_message.messageInfo.docId].history.push(_message);
  815. us.updateNav(_message.messageInfo);
  816. }
  817. //调用群发函数
  818. us.broadcast(_messagelist, _messagearr[0].messageInfo.pageId, 'nav');
  819. break;
  820. }
  821. }
  822. response.end('send');
  823. }
  824. /**
  825. * 更新后台思维网格的内容
  826. * @param {object} message 消息信息
  827. */
  828. us.updatemindNetwork = function (messageInfo) {
  829. if (us.mindNetwork[messageInfo.docId]) {
  830. var $ = us.mindNetwork[messageInfo.docId].content;
  831. // console.log("---------------------------------------start")
  832. // console.log($);
  833. // console.log("---------------------------------------end")
  834. // console.log("---------------------------------------infostart")
  835. // console.log(messageInfo);
  836. // console.log("---------------------------------------infoend")
  837. if ($) {
  838. us.mindNetwork[messageInfo.docId].content[messageInfo.id] = messageInfo.content;
  839. }
  840. }
  841. }
  842. /**
  843. * 更新后台未来课堂内容
  844. * @param {object} message 消息信息
  845. */
  846. us.updatecscl = function (messageInfo, classId, ty) {
  847. if (us.cscl[classId]) {
  848. var i, j, _is;
  849. var _obj = us.cscl[classId].content;
  850. if (ty == "clean") {
  851. _obj = us.cscl[classId].content = {};
  852. }
  853. else {
  854. _obj.backgroundUrl = messageInfo.backgroundUrl;
  855. _obj.edges = _obj.edges || [];
  856. _obj.nodes = _obj.nodes || [];
  857. //循环线的处理
  858. for (j = 0; j < messageInfo.edges.length; j++) {
  859. _is = true;
  860. for (i = 0; i < _obj.edges.length; i++) {
  861. if (_obj.edges[i].id == messageInfo.edges[j].id) {
  862. //如果是修改线同时先又存在那就直接修改
  863. if (ty == "update") {
  864. _obj.edges[i] = messageInfo.edges[j];
  865. }
  866. //如果是删除线的操作
  867. else {
  868. _obj.edges.splice(i, 1);
  869. }
  870. _is = false;
  871. break;
  872. }
  873. }
  874. //修改包含了添加和修改,如果没有修改说明是添加的功能
  875. if (_is && ty == "update") {
  876. _obj.edges.push(messageInfo.edges[j])
  877. }
  878. }
  879. for (j = 0; j < messageInfo.nodes.length; j++) {
  880. _is = true;
  881. for (i = 0; i < _obj.nodes.length; i++) {
  882. if (_obj.nodes[i].id == messageInfo.nodes[j].id) {
  883. //如果是修改节点
  884. if (ty == "update") {
  885. _obj.nodes[i] = messageInfo.nodes[j];
  886. }
  887. //如果是删除节点
  888. else {
  889. _obj.nodes.splice(i, 1);
  890. }
  891. _is = false;
  892. break;
  893. }
  894. }
  895. //修改包含了添加和修改,如果没有修改说明是添加的功能
  896. if (_is && ty == "update") {
  897. _obj.nodes.push(messageInfo.nodes[j])
  898. }
  899. }
  900. }
  901. //如果不存在则去数据库查找
  902. try {
  903. clearTimeout(us.savecscltime);
  904. }
  905. catch (e) {
  906. }
  907. }
  908. //每次修改每隔五秒保存一次数据,防止数据操作频繁导致数据库太卡
  909. us.savecscltime = setTimeout(function () {
  910. us.mysql.usselect(['172.16.12.5', 'pbl', 'updateRoomData', JSON.stringify(_obj), classId], function (ret) { });
  911. }, 5000)
  912. }
  913. /**
  914. * 更新后台实施课堂内容
  915. * @param {object} message 消息信息
  916. */
  917. us.updaterealTimeClass = function (messageInfo) {
  918. if (us.realTimeClass[messageInfo.docId]) {
  919. var $ = us.realTimeClass[messageInfo.docId].content;
  920. // console.log("---------------------------------------start")
  921. // console.log($);
  922. // console.log("---------------------------------------end")
  923. // console.log("---------------------------------------infostart")
  924. // console.log(messageInfo);
  925. // console.log("---------------------------------------infoend")
  926. if ($) {
  927. us.realTimeClass[messageInfo.docId].content[messageInfo.id] = messageInfo.content;
  928. }
  929. }
  930. }
  931. /**
  932. * 更新后台Word内容
  933. * @param {object} message 消息信息
  934. */
  935. us.updateWord = function (messageInfo) {
  936. if (us.word[messageInfo.docId]) {
  937. var $ = us.word[messageInfo.docId].content;
  938. if ($) {
  939. switch (messageInfo.type) { // 判断消息种类
  940. case 'update':
  941. us.updateEditorLine(messageInfo, $); //修改
  942. break;
  943. case 'add':
  944. us.addEditorLine(messageInfo, $); //添加
  945. break;
  946. case 'delete':
  947. us.deleteEditorLine(messageInfo, $); //删除
  948. break;
  949. }
  950. }
  951. }
  952. }
  953. /**
  954. * 更新后台Nav内容
  955. * @param {object} messageInfo 消息信息
  956. */
  957. us.updateNav = function (messageInfo) {
  958. var _fileinfo = us.nav[messageInfo.docId];
  959. if (_fileinfo) {
  960. switch (messageInfo.type) { // 判断消息种类
  961. case 'update': //修改行
  962. us.updateEditorLine(messageInfo, _fileinfo.page[messageInfo.navId]); //修改
  963. break;
  964. case 'add': //添加行
  965. us.addEditorLine(messageInfo, _fileinfo.page[messageInfo.navId]); //添加
  966. break;
  967. case 'delete': //删除行
  968. us.deleteEditorLine(messageInfo, _fileinfo.page[messageInfo.navId]); //删除
  969. break;
  970. case 'addNav': //添加导航
  971. us.updateNav.addNav(_fileinfo, messageInfo.parentId, messageInfo.index, messageInfo.addNav, messageInfo.content);
  972. break;
  973. case 'updateNav': //修改导航
  974. us.updateNav.updateNav(_fileinfo.navInfo.nav, messageInfo.navId, messageInfo.name);
  975. break;
  976. case 'deleteNav': //删除导航
  977. us.updateNav.deleteNav(_fileinfo, messageInfo.navId, messageInfo.parentId);
  978. break;
  979. case 'updateLogo': //修改logo
  980. _navinfo.logo = messageInfo.logo;
  981. break;
  982. }
  983. }
  984. }
  985. /**
  986. * 根据NavId查找对应的导航信息
  987. */
  988. us.selectByNavId = function (nowinfo, navid) {
  989. var i = 0;
  990. for (i = 0; i < nowinfo.length; i++) {
  991. object = nowinfo[i];
  992. if (object.pageId == navid) {
  993. return object;
  994. } else if (object.child) {
  995. return us.selectNav(object.child, navid);
  996. }
  997. }
  998. }
  999. /**
  1000. * 添加Nav
  1001. * @param {object} fileinfo 文件信息
  1002. * @param {object} parentid 父级ID
  1003. * @param {object} addnav 添加导航信息
  1004. * @param {number} index 添加位置
  1005. */
  1006. us.updateNav.addNav = function (fileinfo, parentid, index, addnav, content) {
  1007. var _nav = fileinfo.navInfo.nav;
  1008. var _parent;
  1009. if (parentid) {
  1010. // 寻找父级
  1011. _parent = us.selectByNavId(_nav, parentid);
  1012. // 如果已存在子级处理
  1013. if (_parent.child) {
  1014. //插入指定位置
  1015. _parent.child.splice(index, 0, addnav);
  1016. } else {
  1017. // 为存在子级则新建子级
  1018. _parent.child = [addnav];
  1019. }
  1020. } else {
  1021. _parent = _nav;
  1022. _parent.splice(index, 0, addnav);
  1023. }
  1024. //添加内容
  1025. fileinfo.page[addnav.pageId] = us.cheerio.load(content.replace(/\\'/ig, '\'').replace(/\\"/ig, "\""));
  1026. }
  1027. /**
  1028. * 修改Nav
  1029. * @param {object} nav 导航信息
  1030. * @param {object} navid ID
  1031. * @param {object} name 名字
  1032. */
  1033. us.updateNav.updateNav = function (nav, navid, name) {
  1034. // 寻找需要修改导航信息
  1035. var _nav = us.selectByNavId(nav, navid);
  1036. if (_nav) { //存在导航
  1037. _nav.name = name; //替换名字
  1038. }
  1039. }
  1040. /**
  1041. * 删除Nav
  1042. * @param {object} fielinfo 文件信息
  1043. * @param {object} navid ID
  1044. * @param {object} name 名字
  1045. */
  1046. us.updateNav.deleteNav = function (fielinfo, deletenavid, parentid) {
  1047. var _nav = fielinfo.navInfo.nav;
  1048. var _parent;
  1049. if (parentid) {
  1050. // 寻找父级
  1051. _parent = us.selectByNavId(_nav, parentid);
  1052. } else {
  1053. _parent = _nav;
  1054. }
  1055. if (_parent) {
  1056. var _child = parentid ? _parent.child : _parent;
  1057. _child.find(function (object, index) {
  1058. if (object.pageId == deletenavid) {
  1059. if (!object.child) {
  1060. _child.splice(index, 1); //删除数据
  1061. us.mysql.usselect([ //删除页面
  1062. 'sqlserver.1473.cn',
  1063. 'UseStudio_Office',
  1064. 'DeletePage',
  1065. deletenavid
  1066. ], function (ret) {
  1067. if (ret) {
  1068. delete fielinfo.page[deletenavid]; //删除内容
  1069. }
  1070. });
  1071. }
  1072. return true;
  1073. }
  1074. });
  1075. }
  1076. }
  1077. /**
  1078. * 修改操作
  1079. * @param {object} opera 操作信息对象
  1080. * @param {element} $ 后台word元素形式
  1081. */
  1082. us.updateEditorLine = function (opera, $) {
  1083. // $('#' + opera.id).prop('outerHTML', opera.content); //修改行
  1084. $('#' + opera.id).replaceWith(opera.content);
  1085. }
  1086. /**
  1087. * 删除操作
  1088. * @param {object} opera 操作信息对象
  1089. * @param {element} $ 后台word元素形式
  1090. */
  1091. us.deleteEditorLine = function (opera, $) {
  1092. $('#' + opera.id).remove(); //删除行
  1093. }
  1094. /**
  1095. * 添加操作
  1096. * @param {object} opera 操作信息对象
  1097. * @param {element} $ 后台word元素形式
  1098. */
  1099. us.addEditorLine = function (opera, $) {
  1100. var _next; //创建记录变量
  1101. if (opera.nextId && (_next = $('#' + opera.nextId))[0]) { //判断有没有nextid,nextid是否存在
  1102. //如果存在则插入在nexid 元素前
  1103. _next.before(opera.content);
  1104. } else {
  1105. $('body').append(opera.content); //没有就在最后插入
  1106. }
  1107. }
  1108. /**
  1109. * 更新后台Word内容
  1110. * @param {object} message 消息信息
  1111. */
  1112. us.updateExcel = function (messageInfo) {
  1113. var $ = us.excel[messageInfo.docId].content;
  1114. if ($) {
  1115. switch (messageInfo.type) { // 判断消息种类
  1116. case 'blur':
  1117. us.updateExcel.Blur(messageInfo, $); //失焦
  1118. break;
  1119. case "addLine":
  1120. us.updateExcel.addLine(messageInfo, $); //添加行
  1121. break;
  1122. case "delLine":
  1123. us.updateExcel.delLine(messageInfo, $); //刪除行
  1124. break;
  1125. case "addCol":
  1126. us.updateExcel.addCol(messageInfo, $); //添加列
  1127. break;
  1128. case "delCol":
  1129. us.updateExcel.delCol(messageInfo, $); //刪除列
  1130. break;
  1131. case "merge":
  1132. us.updateExcel.merge(messageInfo, $); //合并单元格
  1133. break;
  1134. case "splitCells":
  1135. us.updateExcel.splitCells(messageInfo, $); //拆分单元格
  1136. break;
  1137. case "rowExpanding":
  1138. us.updateExcel.rowExpanding(messageInfo, $); //行板拉伸
  1139. break;
  1140. case "colExpanding":
  1141. us.updateExcel.colExpanding(messageInfo, $); //列板拉伸
  1142. break;
  1143. case "addTask":
  1144. us.updateExcel.addTask(messageInfo, $); //添加工作表
  1145. break;
  1146. case "deleteTask":
  1147. us.updateExcel.deleteTask(messageInfo, $); //刪除工作表
  1148. break;
  1149. case "reNameTask":
  1150. us.updateExcel.reNameTask(messageInfo, $); //刪除工作表
  1151. break;
  1152. case "img":
  1153. us.updateExcel.img(messageInfo, $); //插入图片
  1154. break;
  1155. }
  1156. }
  1157. // console.log($);
  1158. }
  1159. /**
  1160. * 失焦處理
  1161. */
  1162. us.updateExcel.Blur = function (messageInfo, $) {
  1163. var _taskId = messageInfo.worksheetId, //如果有taskId传过来 则修改 data数据里 taskId里的数据 否则则修改当前工作表id里的数据
  1164. _excelData = $[_taskId], //获取taskId工作表的数据
  1165. _cellData = _excelData.cell, //获取该数据里的单元格数据
  1166. _content = messageInfo.content; //获取需要保存的json数据
  1167. _content.forEach(function (data) {
  1168. var _text = data.innerText.replace(/\s+/g, ""), //去掉空格 给下面做判断 如果内容为空 则删除该数据
  1169. _cellColList = _cellData[data.col], //获取该数据里的所有列
  1170. _cell = null;
  1171. if (!_cellColList) { //如果列不存在 则创建一个列
  1172. _cellColList = _cellData[data.col] = {};
  1173. }
  1174. if (!_cellColList[data.row]) { //如果该行_列 不存在单元格数据 则创建个json 给单元格添加数据
  1175. _cellColList[data.row] = {};
  1176. _cellColList[data.row].cssStyle = {}; //css样式
  1177. }
  1178. _cell = _cellColList[data.row]; //获取该单元格的数据 (json)
  1179. data.content ? _cell.innerHTML = data.content : ""; //修改该单元格的innerHTML
  1180. if (data.attr) { //如果存在样式
  1181. _cell.cssStyle[data.attr] = data.value;
  1182. if (!data.value) {
  1183. delete _cell.cssStyle[data.attr]
  1184. }
  1185. }
  1186. if (data.clearStyle) { //如果需要清除所有样式的话
  1187. data.cssStyle = {}
  1188. }
  1189. if (((!(_text) || _text === ("\u200D")) && !(_cell.endRow)) && !(us.jsonLen(_cell.cssStyle))) { //如果innerHTML为空格 或 不存在 并且 不是合并单元格元素 并且没有样式 则删除掉
  1190. delete (_cellColList[data.row]);
  1191. }
  1192. });
  1193. }
  1194. /*
  1195. 获取json的数量
  1196. */
  1197. us.jsonLen = function (json) {
  1198. var _json = json,
  1199. _len = 0;
  1200. for (var i in json) {
  1201. _len++;
  1202. }
  1203. return _len;
  1204. }
  1205. us.updateExcel.addLine = function (messageInfo, $) {
  1206. var _taskId = messageInfo.worksheetId, //单元格id
  1207. _content = messageInfo.content,
  1208. _excelData = $[_taskId],
  1209. _rowData = _excelData.row,
  1210. _cellData = _excelData.cell,
  1211. _start = _content.start, //开始行
  1212. _lineNum = _content.lineNum, //添加行的数量
  1213. _sizeChange = _content.sizeChange, //添加的行的高度
  1214. _mergeArr = _content.mergeArr, //合并单元格 元素 数组集合
  1215. _cellCol = null;
  1216. if (_mergeArr.length) {
  1217. _mergeArr.forEach(function (info) {
  1218. _cellData[info.col][info.row].endRow = _cellData[info.col][info.row].endRow + _lineNum;
  1219. });
  1220. }
  1221. for (var k in _cellData) {
  1222. _cellCol = _cellData[k]; //该列下的所有行
  1223. var temp = {};
  1224. for (var row in _cellCol) {
  1225. if (row > _start) {
  1226. temp[(+row) + _lineNum] = _cellCol[row];
  1227. temp[(+row) + _lineNum].endRow ? temp[(+row) + _lineNum].endRow = temp[(+row) + _lineNum].endRow + _lineNum : ""; //如果是合并单元格的元素 则将endRow + _lineNum;
  1228. } else {
  1229. temp[row] = _cellCol[row];
  1230. }
  1231. }
  1232. _cellData[k] = temp;
  1233. } /* 该处for循环是修改数据中单元格的行数 */
  1234. var _rowDataChange = _rowData.change, //获取数据表中 所有被改变高度的 行版信息
  1235. _temp = {},
  1236. _rowChange = null;
  1237. for (var k in _rowDataChange) {
  1238. _rowChange = _rowDataChange[k]; //该行板的所有被改动的行数据
  1239. if (k > _start) {
  1240. _temp[(+k) + _lineNum] = _rowChange;
  1241. } else {
  1242. _temp[k] = _rowChange;
  1243. }
  1244. } /* 该处for循环是修改数据中行版的行数 */
  1245. _rowData.change = _temp;
  1246. if (_sizeChange) {
  1247. for (var i = 1; i <= _lineNum; i++) {
  1248. _rowData.change[_start + i] = _sizeChange;
  1249. }
  1250. }
  1251. _rowData.num = _rowData.num + _lineNum; //行板数量 = 行板数量 + 添加行的数量
  1252. };
  1253. us.updateExcel.delLine = function (messageInfo, $) {
  1254. var _taskId = messageInfo.worksheetId, //单元格id
  1255. _excelData = $[_taskId],
  1256. _content = messageInfo.content,
  1257. _rowData = _excelData.row,
  1258. _cellData = _excelData.cell,
  1259. _start = _content.start, //被选中的第一行
  1260. _end = _content.end, //被选中的最后一行
  1261. _lineNum = _content.lineNum, //所删除的行数
  1262. _mergeArr = _content.mergeArr, //合并单元格 元素 数组集合
  1263. _cellCol = null;
  1264. for (var k in _cellData) {
  1265. _cellCol = _cellData[k];
  1266. var temp = {};
  1267. for (var row in _cellCol) {
  1268. if (row > _end) {
  1269. temp[(+row) - _lineNum] = _cellCol[row];
  1270. temp[(+row) - _lineNum].endRow ? temp[(+row) - _lineNum].endRow = temp[(+row) - _lineNum].endRow - _lineNum : ""; //如果是合并单元格的元素 则将endRow - _lineNum;
  1271. } else if (row < _start) {
  1272. temp[row] = _cellCol[row];
  1273. }
  1274. }
  1275. _cellData[k] = temp;
  1276. }
  1277. if (_mergeArr.length) {
  1278. var _cloneMessage = null;
  1279. _mergeArr.forEach(function (info) {
  1280. _cloneMessage = JSON.parse(JSON.stringify(messageInfo));
  1281. _cloneMessage.content = info;
  1282. us.updateExcel.merge(_cloneMessage, $);
  1283. });
  1284. }
  1285. var _rowDataChange = _rowData.change, //获取数据表中 所有被改变高度的 行版信息
  1286. _temp = {},
  1287. _rowChange = null;
  1288. for (var k in _rowDataChange) {
  1289. _rowChange = _rowDataChange[k]; //该行板的所有被改动的行数据
  1290. if (k < _start) { //如果大于结束行 则将该行下面的所有的数字信息 - 被删除的行
  1291. _temp[k] = _rowChange;
  1292. } else if (k > _end) {
  1293. _temp[(+k) - _lineNum] = _rowChange;
  1294. }
  1295. } /* 该处for循环是修改数据中行版的行数 */
  1296. _rowData.change = _temp;
  1297. _rowData.num = _rowData.num - _lineNum; //行板数量 = 行板数量 - 删除行的数量
  1298. };
  1299. us.updateExcel.addCol = function (messageInfo, $) {
  1300. var _taskId = messageInfo.worksheetId, //单元格id
  1301. _excelData = $[_taskId],
  1302. _content = messageInfo.content,
  1303. _colData = _excelData.column,
  1304. _cellData = _excelData.cell,
  1305. _start = _content.start, //被选中的第一列
  1306. _sizeChange = _content.sizeChange, //添加的列的宽度
  1307. _colNums = _content.colNums, //所添加的列数
  1308. _mergeArr = _content.mergeArr, //合并单元格 元素 数组集合
  1309. _cellCol = null,
  1310. _temp = {};
  1311. if (_mergeArr.length) {
  1312. _mergeArr.forEach(function (info) {
  1313. _cellData[info.col][info.row].endCol = _cellData[info.col][info.row].endCol + _colNums;
  1314. });
  1315. }
  1316. for (var k in _cellData) {
  1317. _cellCol = _cellData[k];
  1318. if (k > _start) {
  1319. _temp[+(k) + _colNums] = _cellCol;
  1320. var _temp2 = _temp[(+k) + _colNums];
  1321. for (var i in _temp2) {
  1322. _temp2[i].endCol ? _temp2[i].endCol = _temp2[i].endCol + _colNums : ""; //如果是合并单元格的元素 则将endCol + _colNums;
  1323. }
  1324. } else {
  1325. _temp[k] = _cellCol;
  1326. }
  1327. }
  1328. _excelData.cell = _temp;
  1329. var _colDataChange = _colData.change, //获取数据表中 所有被改变宽度的 列版信息
  1330. _temp = {},
  1331. _colChange = null;
  1332. for (var k in _colDataChange) {
  1333. _colChange = _colDataChange[k]; //该列板的所有被改动的列数据
  1334. if (k > _start) {
  1335. _temp[(+k) + _colNums] = _colChange;
  1336. } else {
  1337. _temp[k] = _colChange;
  1338. }
  1339. } /* 该处for循环是修改数据中行版的行数 */
  1340. _colData.change = _temp;
  1341. if (_sizeChange) {
  1342. for (var i = 1; i <= _colNums; i++) {
  1343. _colData.change[_start + i] = _sizeChange;
  1344. }
  1345. }
  1346. _colData.num = _colData.num + _colNums; //列板数量 = 列板数量 + 添加列的数量
  1347. };
  1348. us.updateExcel.delCol = function (messageInfo, $) {
  1349. var _taskId = messageInfo.worksheetId, //单元格id
  1350. _excelData = $[_taskId],
  1351. _content = messageInfo.content,
  1352. _colData = _excelData.column,
  1353. _cellData = _excelData.cell,
  1354. _start = _content.start, //被选中的第一行
  1355. _end = _content.end, //被选中的最后一行
  1356. _colNums = _content.colNums, //所删除的列数
  1357. _mergeArr = _content.mergeArr, //合并单元格 元素 数组集合
  1358. _cellCol = null,
  1359. _temp = {};
  1360. for (var k in _cellData) {
  1361. _cellCol = _cellData[k];
  1362. if (k > _end) {
  1363. _temp[+(k) - _colNums] = _cellCol;
  1364. var _temp2 = _temp[(+k) - _colNums];
  1365. for (var i in _temp2) {
  1366. _temp2[i].endCol ? _temp2[i].endCol = _temp2[i].endCol - _colNums : ""; //如果是合并单元格的元素 则将endCol - _colNums;
  1367. }
  1368. } else if (k < _start) {
  1369. _temp[k] = _cellCol;
  1370. }
  1371. }
  1372. _excelData.cell = _temp;
  1373. if (_mergeArr.length) {
  1374. var _cloneMessage = null;
  1375. _mergeArr.forEach(function (info) {
  1376. _cloneMessage = JSON.parse(JSON.stringify(messageInfo));
  1377. _cloneMessage.content = info;
  1378. us.updateExcel.merge(_cloneMessage, $);
  1379. });
  1380. }
  1381. var _colDataChange = _colData.change, //获取数据表中 所有被改变宽度的 列版信息
  1382. _temp = {},
  1383. _colChange;
  1384. for (var k in _colDataChange) {
  1385. _colChange = _colDataChange[k]; //该列板的所有被改动的列数据
  1386. if (k < _start) { //如果大于结束列 则将该列下面的所有的数字信息 - 被删除的列
  1387. _temp[k] = _colChange;
  1388. } else if (k > _end) {
  1389. _temp[(+k) - _colNums] = _colChange;
  1390. }
  1391. } /* 该处for循环是修改数据中行版的行数 */
  1392. _colData.change = _temp;
  1393. _colData.num = _colData.num - _colNums; //列板数量 = 列板数量 - 删除列的数量
  1394. };
  1395. us.updateExcel.merge = function (messageInfo, $) {
  1396. var _taskId = messageInfo.worksheetId, //单元格id
  1397. _excelData = $[_taskId],
  1398. _content = messageInfo.content,
  1399. _cellData = _excelData.cell,
  1400. _cellColList = _cellData[_content.col];
  1401. if (_content.row === _content.endRow && _content.col === _content.endCol) {
  1402. _cellData[_content.col][_content.row].endRow = "";
  1403. _cellData[_content.col][_content.row].endCol = ""
  1404. return;
  1405. }
  1406. if (!_cellColList) {
  1407. _cellColList = _cellData[_content.col] = {};
  1408. }
  1409. if (!_cellColList[_content.row]) {
  1410. _cellColList[_content.row] = {};
  1411. }
  1412. _cellColList[_content.row].innerHTML = _content.content;
  1413. _cellColList[_content.row].endRow = _content.endRow;
  1414. _cellColList[_content.row].endCol = _content.endCol;
  1415. };
  1416. us.updateExcel.splitCells = function (messageInfo, $) {
  1417. var _taskId = messageInfo.worksheetId, //单元格id
  1418. _excelData = $[_taskId],
  1419. _content = messageInfo.content,
  1420. _cellData = _excelData.cell,
  1421. _cell = _cellData[_content.col][_content.row]; //需要拆分的单元格
  1422. _cell.endRow = "";
  1423. _cell.endCol = "";
  1424. };
  1425. us.updateExcel.rowExpanding = function (messageInfo, $) {
  1426. var _taskId = messageInfo.worksheetId, //单元格id
  1427. _excelData = $[_taskId],
  1428. _content = messageInfo.content,
  1429. _rowData = _excelData.row;
  1430. _content.forEach(function (data) {
  1431. var _rowNum = data.rowName, //拉伸行的位置 (数字)
  1432. _rowHeight = data.height; //被拉伸后的高度
  1433. _rowData.change[_rowNum] = _rowHeight;
  1434. });
  1435. };
  1436. us.updateExcel.colExpanding = function (messageInfo, $) {
  1437. var _taskId = messageInfo.worksheetId, //单元格id
  1438. _excelData = $[_taskId],
  1439. _content = messageInfo.content,
  1440. _colData = _excelData.column; //该列的数据
  1441. _content.forEach(function (data) {
  1442. var _colNum = data.colName, //拉伸行的位置 (数字)
  1443. _colWidth = data.width; //被拉伸后的宽度
  1444. _colData.change[_colNum] = _colWidth;
  1445. });
  1446. };
  1447. /*添加工作表*/
  1448. us.updateExcel.addTask = function (messageInfo, $) {
  1449. var _content = messageInfo.content,
  1450. _taskId = _content.id;
  1451. $[_taskId] = _content.data;
  1452. };
  1453. /*刪除工作表*/
  1454. us.updateExcel.deleteTask = function (messageInfo, $) {
  1455. var _content = messageInfo.content,
  1456. _taskId = _content.id;
  1457. delete ($[_taskId]);
  1458. };
  1459. /*命名工作表*/
  1460. us.updateExcel.reNameTask = function (messageInfo, $) {
  1461. var _content = messageInfo.content,
  1462. _taskId = _content.id;
  1463. $[_taskId].taskName = _content.name;
  1464. };
  1465. us.updateExcel.img = function (messageInfo, $) { //此处修改
  1466. var _content = messageInfo.content,
  1467. _taskId = messageInfo.worksheetId,
  1468. _excelData = $[_taskId],
  1469. _imgData = _excelData.img;
  1470. _imgData.push(_content);
  1471. };
  1472. /**
  1473. * 用户群发处理
  1474. *
  1475. * @param {string} 接收信息的用户id
  1476. * @param {string} 发送的用户信息
  1477. **/
  1478. us.setword = function (messageinfo) {
  1479. // us.userConnect[userid].response.end(messageinfo);
  1480. }
  1481. /**
  1482. * 用户群发处理
  1483. *
  1484. * @param {string} userid 接收信息的用户id
  1485. * @param {string} messageinfo 发送的用户信息
  1486. **/
  1487. us.senduser = function (userid, messageinfo) {
  1488. us.userConnect[userid].response.end(messageinfo); //发送消息
  1489. }
  1490. /**
  1491. * 用户群发处理
  1492. *
  1493. * @param {object} messageinfo 发送消息的内容
  1494. * @param {guid} pageid 页面id
  1495. * @param {string} type 类型
  1496. **/
  1497. us.broadcast = function (messageinfo, pageid, type) {
  1498. var i, j, o, //循环变量
  1499. _data,
  1500. _messageinfo, //消息信息
  1501. _users, //用户ID集合
  1502. _pageids, //用户页面唯一识别集合
  1503. _recievedpageid, //接收用户的页面唯一识别ID
  1504. _issend, // 是否发送
  1505. _userconnect = us.connect; //用户连接池
  1506. const chunkSize = 100;
  1507. for (i in messageinfo) {
  1508. _messageinfo = messageinfo[i]; //记录消息消息b
  1509. _users = us[type][i] ? us[type][i].user : {}; //记录用户集合
  1510. for (j in _users) { //循环处理每个用户处理 给每个用户集合下的用户发送消息
  1511. _pageids = _users[j]; //记录当前用户在每个端登录的页面唯一识别ID
  1512. for (o = 0; o < _pageids.length; o++) { //循环发送给当前用户在每个端登录的用户
  1513. _recievedpageid = _pageids[o]; //记录接受者页面唯一识别ID 用于区分是否是发送用户
  1514. if (_recievedpageid != pageid) { //如果接受用户不等于发送用户则发送消息
  1515. try {
  1516. if (_userconnect[j] && _userconnect[j][_recievedpageid]) {
  1517. console.log(_recievedpageid);
  1518. _data = JSON.stringify(_messageinfo);
  1519. _issend = _userconnect[j][_recievedpageid].response.write(""); //发送消息
  1520. //用户在轮询切换的时候的中间,会产生数据无法接受,这里是存储数据
  1521. if (!_issend) {
  1522. us.offLineMessage[_recievedpageid].message.push(_messageinfo); //将消息加入用户离线消息中
  1523. }
  1524. else {
  1525. for (let i = 0; i < _data.length; i += chunkSize) {
  1526. const chunk = _data.slice(i, i + chunkSize);
  1527. _userconnect[j][_recievedpageid].response.write(chunk);
  1528. }
  1529. _userconnect[j][_recievedpageid].response.write("\r\n");
  1530. }
  1531. }
  1532. }
  1533. catch (e) {
  1534. console.log(e)
  1535. }
  1536. }
  1537. }
  1538. }
  1539. }
  1540. }
  1541. /**
  1542. * 加入文档编辑
  1543. *
  1544. * @param {object} response 用户相应对象
  1545. * @param {string} wordinfo 文档信息 形如: {docId : 'guid,guid,guid', userId : guid, pageId : guid}
  1546. **/
  1547. us.addWord = function (response, wordinfo) {
  1548. //获取用户参数
  1549. wordinfo = JSON.parse(wordinfo);
  1550. var _docidarray = wordinfo.docId.split(','), //根据','号分割多个文档ID
  1551. _userid = wordinfo.userId, //用户ID
  1552. _pageid = wordinfo.pageId, //页面唯一识别ID
  1553. i, //定义循环变量
  1554. _docid, //记录文档ID变量
  1555. _docinfo; //记录文档信息变量
  1556. for (i = 0; i < _docidarray.length; i++) { //循环处理每一份问的加入请求
  1557. _docid = _docidarray[i]; //记录文档ID变量
  1558. _docinfo = us.word[_docid]; //记录文档信息变量
  1559. if (!_docinfo) { //判断群列表是否已存在不存在则创建
  1560. us.word[_docid] = {
  1561. user: {},
  1562. history: []
  1563. }; //创建word文档信息
  1564. us.word[_docid].user[_userid] = [_pageid]; //加入用户
  1565. } else if (!_docinfo.user[_userid]) { //判断文档的用户是否已登陆
  1566. _docinfo.user[_userid] = [_pageid];
  1567. } else if (_docinfo.user[_userid].indexOf(_pageid) == -1) { //将用户添加到群列表中
  1568. _docinfo.user[_userid].push(_pageid);
  1569. }
  1570. us.offLineMessage[_pageid].word.push(_docid); //添加到用户离线消息的连接wrod记录中
  1571. }
  1572. response.end('addword'); //即时相应请求成功
  1573. }
  1574. /**
  1575. * 退出文档编辑
  1576. *
  1577. * @param {object} response 用户相应对象
  1578. * @param {string} wordinfo 文档信息
  1579. **/
  1580. us.quitOffice = function (response, wordinfo) {
  1581. //获取用户参数
  1582. wordinfo = JSON.parse(wordinfo);
  1583. var _docid = wordinfo.docId,
  1584. _userid = wordinfo.userId,
  1585. _pageid = wordinfo.pageId,
  1586. _type = wordinfo.type,
  1587. _messagetype = 'us.' + _type;
  1588. var _docinfo = us[_type][_docid]; //获取文档信息
  1589. //判断是否存在文档信息
  1590. if (_docinfo) {
  1591. us.quitUserList(_docinfo, _userid, _pageid); //将用户在协同列表中删除
  1592. var _j, //创建循环变量
  1593. _message = us.offLineMessage[_pageid] ? us.offLineMessage[_pageid].message : []; //获取该用户的离线消息
  1594. for (_j = _message.length - 1; _j > -1; _j--) { //循环移除该文档的离线消息
  1595. if (_message[_j].type == _messagetype && _message[_j].messageInfo.docId == _docid) { //判断是否为word文档
  1596. _message.splice(_j, 1); //删除记录
  1597. }
  1598. }
  1599. us.insertHistory(_docinfo, _docid, _type);
  1600. }
  1601. response.end('quitOffice'); //响应退出请求
  1602. }
  1603. /**
  1604. * 退出知识建构
  1605. *
  1606. * @param {object} response 用户相应对象
  1607. * @param {string} wordinfo 文档信息
  1608. **/
  1609. us.quitcscl = function (response, info) {
  1610. console.log(info)
  1611. //获取用户参数
  1612. info = JSON.parse(info);
  1613. var _docid = info.docId,
  1614. _userid = info.userId,
  1615. _pageid = info.pageId,
  1616. _type = info.type;
  1617. var _docinfo = us[_type][_docid]; //获取文档信息
  1618. //判断是否存在文档信息
  1619. if (_docinfo) {
  1620. _userid = us.quitUserList(_docinfo, _userid, _pageid); //将用户在协同列表中删除
  1621. console.log(_userid)
  1622. //下线后通知所有的用户
  1623. if (_userid) {
  1624. _message = {};
  1625. var _messageinfo = us.message(_userid, 'us.cscl', {
  1626. id: us.createGuid(),
  1627. type: "delUser",
  1628. userinfo: us.user[_userid],
  1629. docId: _docid
  1630. });
  1631. _message[_docid] = [_messageinfo];
  1632. console.log(_message)
  1633. us.broadcast(_message, _pageid, "cscl");
  1634. }
  1635. // var _j, //创建循环变量
  1636. // _message = us.offLineMessage[_pageid] ? us.offLineMessage[_pageid].message : []; //获取该用户的离线消息
  1637. // for (_j = _message.length - 1; _j > -1; _j--) { //循环移除该文档的离线消息
  1638. // if (_message[_j].type == _messagetype && _message[_j].messageInfo.docId == _docid) { //判断是否为word文档
  1639. // _message.splice(_j, 1); //删除记录
  1640. // }
  1641. // }
  1642. }
  1643. response.end('quitcscl'); //响应退出请求
  1644. }
  1645. /**
  1646. * 将用户在协同列表中删除
  1647. *
  1648. * @param {object} docinfo 文档信息
  1649. * @param {string} userid 用户ID
  1650. * @param {string} pageid 用户页面唯一识别ID
  1651. */
  1652. us.quitUserList = function (docinfo, userid, pageid) {
  1653. var _users = docinfo.user[userid]; //记录文档的用户列表
  1654. console.log(_users)
  1655. //获取用户在文档用户列表的位置
  1656. if (_users) {
  1657. var _index = _users.indexOf(pageid);
  1658. // 如果存在则将该用户删除 表示退出协同
  1659. if (_index != -1) {
  1660. _users.splice(_index, 1); //删除该用户
  1661. //如果该用户的全部端用户已退出则删除用户的记录
  1662. if (_users.length == 0) {
  1663. delete docinfo.user[userid];
  1664. return userid;
  1665. }
  1666. }
  1667. }
  1668. }
  1669. /**
  1670. * 插入文档操作历史到数据库
  1671. *
  1672. * @param {object} docinfo 文档信息
  1673. */
  1674. us.insertHistory = function (docinfo, docid, type) {
  1675. var i,
  1676. _info, //操作信息记录
  1677. _isuser = false;
  1678. for (i in docinfo.user) {
  1679. _isuser = true;
  1680. break;
  1681. }
  1682. //如果该文档没有协同用户
  1683. if (!_isuser) {
  1684. switch (type) {
  1685. case 'word':
  1686. var _content = docinfo.content ? docinfo.content('body').html().replace(/'/ig, '\\\'').replace(/"/ig, "\\\"") : "";
  1687. us.mysql.usselect(['sqlserver.1473.cn', 'UseStudio_Disk', 'SaveFileContent', docid, _content, ''],
  1688. function (ret) { });
  1689. break;
  1690. case 'nav':
  1691. us.mysql.usselect(['sqlserver.1473.cn', 'UseStudio_Disk', 'SaveFileContent', docid, JSON.stringify(docinfo.navInfo), ''],
  1692. function (ret) {
  1693. var _key;
  1694. for (_key in docinfo.page) {
  1695. //发送保存内容请求
  1696. var _content = docinfo.page[_key] ? docinfo.page[_key]('body').html().replace(/'/ig, '\\\'').replace(/"/ig, "\\\"") : "";
  1697. us.mysql.usselect(['sqlserver.1473.cn', 'UseStudio_Office', 'SaveNavPageContent', _key, _content], function (ret) { });
  1698. }
  1699. });
  1700. break;
  1701. }
  1702. }
  1703. }
  1704. /**
  1705. * 下线操作
  1706. * @param {object} response 响应对象
  1707. * @param {string} userid 用户ID
  1708. * @param {string} pageid 用户页面唯一识别ID
  1709. */
  1710. us.logout = function (response, userid, pageid) {
  1711. console.log("234566666666666666")
  1712. console.log(userid)
  1713. var j, istrue = true;
  1714. var _user = us.connect[userid];
  1715. //循环目前所有的连接用户
  1716. us.deluser(userid, pageid); //删除用户的数据
  1717. if (_user) {
  1718. delete _user[pageid]
  1719. for (j in _user) { istrue = false; break }
  1720. //这个用户下所有的连接用户都不在了
  1721. if (istrue) {
  1722. //用户登录数据清理,用于已经下线
  1723. delete us.user[userid];
  1724. delete us.connect[userid];
  1725. }
  1726. console.log(pageid)
  1727. }
  1728. response.end('logout'); //响应退出请求
  1729. // var i, j, _docinfo,
  1730. // _wordids = us.offLineMessage[pageid].word, //得到加入的所有word
  1731. // _excelids = us.offLineMessage[pageid].excel; //得到加入的所有excel
  1732. // us.connect[userid][pageid].offLineTime = new Date();
  1733. // for (i = 0; i < _wordids.length; i++) { //循环处理每一个文档
  1734. // _docinfo = us.word[_wordids[i]]; //记录文档信息
  1735. // if (_docinfo) { //如果存在文档信息
  1736. // us.quitUserList(_docinfo, userid, pageid); //将用户在协同列表中删除
  1737. // delete us.offLineMessage[pageid]; //删除用户离线消息记录
  1738. // us.insertHistory(_docinfo, 'word'); //插入历史
  1739. // }
  1740. // }
  1741. // for (j = 0; i < _excelids.length; j++) {
  1742. // _docinfo = us.excel[_excelids[j]]; //记录文档信息
  1743. // if (_docinfo) { //如果存在文档信息
  1744. // us.quitUserList(_docinfo, userid, pageid); //将用户在协同列表中删除
  1745. // delete us.offLineMessage[pageid]; //删除用户离线消息记录
  1746. // us.insertHistory(_docinfo, 'excel'); //插入历史
  1747. // }
  1748. // }
  1749. }
  1750. /**
  1751. * 获取Excel,并生成后端数据
  1752. *
  1753. * @param {object} response 用户相应对象
  1754. * @param {string} docid 文档ID
  1755. * @param {guid} pageid 页面ID
  1756. * @param {guid} userid 用户ID
  1757. */
  1758. us.getExcel = function (response, docid, pageid, userid) {
  1759. //判断文档是否已在内层中存在
  1760. if (us.excel[docid]) {
  1761. //如果存在则直接返回内层的文档
  1762. if (!us.excel[docid].user[userid]) {
  1763. us.excel[docid].user[userid] = [pageid];
  1764. } else {
  1765. us.excel[docid].user[userid].push(pageid);
  1766. }
  1767. us.offLineMessage[pageid].excel.indexOf(docid) == -1 && us.offLineMessage[pageid].excel.push(docid); //添加到用户离线消息的连接wrod记录中
  1768. var _office = us.excel[docid].content;
  1769. response.end(JSON.stringify(_office));
  1770. } else {
  1771. us.excel[docid] = {
  1772. user: {},
  1773. history: []
  1774. };
  1775. us.excel[docid].user[userid] = [pageid];
  1776. us.offLineMessage[pageid].excel.indexOf(docid) == -1 && us.offLineMessage[pageid].excel.push(docid); //添加到用户离线消息的连接wrod记录中
  1777. //如果不存在则去数据库查找
  1778. us.mysql.usselect([
  1779. 'sqlserver.1473.cn',
  1780. 'UseStudio_Disk',
  1781. 'GetFileContent',
  1782. docid
  1783. ], function (ret) {
  1784. if (ret) {
  1785. var _office = JSON.parse(ret)[0][0].UsOffice;
  1786. us.excel[docid].content = JSON.parse(_office); //将文档添加到内层中
  1787. response.end(_office); //返回文档内容
  1788. } else {
  1789. us.excel[docid].content = ''; //将文档添加到内层中
  1790. response.end(''); //返回文档内容
  1791. }
  1792. });
  1793. }
  1794. }
  1795. /**
  1796. * 获取文档,并生成DOM
  1797. *
  1798. * @param {object} response 用户相应对象
  1799. * @param {string} fileid 导航文件ID
  1800. * @param {guid} pageid 页面ID
  1801. * @param {guid} userid 用户ID
  1802. */
  1803. us.getNav = function (response, fileid, pageid, userid) {
  1804. //判断文档是否已在内层中存在
  1805. if (us.nav[fileid]) {
  1806. //如果存在则直接返回内层的文档
  1807. if (!us.nav[fileid].user[userid]) {
  1808. us.nav[fileid].user[userid] = [pageid];
  1809. } else {
  1810. us.nav[fileid].user[userid].push(pageid);
  1811. }
  1812. us.offLineMessage[pageid].nav.indexOf(fileid) == -1 && us.offLineMessage[pageid].nav.push(fileid); //添加到用户离线消息的连接wrod记录中
  1813. response.end(JSON.stringify(us.nav[fileid].navInfo));
  1814. } else {
  1815. //如果不存在则去数据库查找
  1816. us.mysql.usselect([
  1817. 'sqlserver.1473.cn',
  1818. 'UseStudio_Disk',
  1819. 'GetFileContent',
  1820. fileid
  1821. ], function (ret) {
  1822. if (ret) {
  1823. var _navinfo = JSON.parse(ret)[0][0].UsOffice;
  1824. if (_navinfo) {
  1825. var _navdata = us.nav[fileid] = { //生成数据结构
  1826. user: {},
  1827. navInfo: JSON.parse(_navinfo),
  1828. page: {},
  1829. history: []
  1830. };
  1831. _navdata.user[userid] = [pageid]; //记录用户
  1832. us.offLineMessage[pageid].nav.indexOf(fileid) == -1 && us.offLineMessage[pageid].nav.push(fileid); //添加到用户离线消息的连接wrod记录中
  1833. response.end(JSON.stringify(_navinfo)); //返回文档内容
  1834. } else {
  1835. response.end(''); //查询不到文档
  1836. }
  1837. } else {
  1838. response.end(''); //返回文档内容
  1839. }
  1840. });
  1841. }
  1842. }
  1843. /**
  1844. * 获取导航页面内容
  1845. * @param {object} response 用户相应对象
  1846. * @param {guid} fileid 文档ID
  1847. * @param {string} navid 文档内容
  1848. * @param {string} name 文档名
  1849. */
  1850. us.getNavPageContent = function (response, fileid, navid) {
  1851. if (us.nav[fileid] && us.nav[fileid].page[navid]) {
  1852. response.end(us.nav[fileid].page[navid]('body').html()); //后台直接返回
  1853. } else {
  1854. //如果不存在则去数据库查找
  1855. us.mysql.usselect([
  1856. 'sqlserver.1473.cn',
  1857. 'UseStudio_Office',
  1858. 'GetNavPageContent',
  1859. navid
  1860. ], function (ret) {
  1861. if (ret && JSON.parse(ret)[0][0]) {
  1862. var _content = JSON.parse(ret)[0][0].content.replace(/\\'/ig, '\'').replace(/\\"/ig, "\""); //记录查找内容
  1863. us.nav[fileid] && (us.nav[fileid].page[navid] = us.cheerio.load(_content)); //将文档添加到内层中
  1864. response.end(_content); //查询不到文档
  1865. } else {
  1866. response.end(''); //返回文档内容
  1867. }
  1868. });
  1869. }
  1870. }
  1871. /**
  1872. * 新建文档保存内容并加入协同
  1873. * @param {object} response 用户相应对象
  1874. * @param {guid} docid 文档ID
  1875. * @param {string} content 文档内容
  1876. * @param {string} name 文档名
  1877. * @param {guid} userid 用户ID
  1878. * @param {guid} pageid 页面唯一识别ID
  1879. */
  1880. us.newWordSave = function (response, docid, content, name, userid, pageid) {
  1881. us.word[docid] = {
  1882. user: {},
  1883. history: []
  1884. }; //创建Word类
  1885. us.word[docid].user[userid] = [pageid]; //将用户加入协同记录数组
  1886. us.offLineMessage[pageid].word.indexOf(docid) == -1 && us.offLineMessage[pageid].word.push(docid); //添加到用户离线消息的连接wrod记录中
  1887. //发送保存请求
  1888. us.mysql.usselect([
  1889. 'sqlserver.1473.cn',
  1890. 'UseStudio_Disk',
  1891. 'SaveFileContent',
  1892. docid,
  1893. content,
  1894. name
  1895. ], function (ret) {
  1896. if (ret) {
  1897. //设置内容
  1898. us.word[docid].content = us.cheerio.load(content.replace(/\\'/ig, '\'').replace(/\\"/ig, "\"")); //将文档添加到内层中
  1899. response.end('save'); //返回保存成功
  1900. } else {
  1901. response.end('fail'); //返回文档内容
  1902. }
  1903. });
  1904. }
  1905. /**
  1906. * 新建文档保存内容并加入协同
  1907. * @param {object} response 用户相应对象
  1908. * @param {guid} docid 文档ID
  1909. * @param {string} content 文档内容
  1910. * @param {string} name 文档名
  1911. * @param {guid} userid 用户ID
  1912. * @param {guid} pageid 页面唯一识别ID
  1913. */
  1914. us.newExcelSave = function (response, docid, content, name, userid, pageid) {
  1915. us.excel[docid] = {
  1916. user: {},
  1917. history: []
  1918. }; //创建excel类
  1919. us.excel[docid].user[userid] = [pageid]; //将用户加入协同记录数组
  1920. us.offLineMessage[pageid].excel.indexOf(docid) == -1 && us.offLineMessage[pageid].word.push(docid); //添加到用户离线消息的连接wrod记录中
  1921. //发送保存请求
  1922. us.mysql.usselect([
  1923. 'sqlserver.1473.cn',
  1924. 'UseStudio_Disk',
  1925. 'SaveFileContent',
  1926. docid,
  1927. content,
  1928. name
  1929. ], function (ret) {
  1930. if (ret) {
  1931. //设置内容
  1932. us.excel[docid].content = JSON.parse(content); //将文档添加到内层中
  1933. response.end('saveOk'); //返回保存成功
  1934. } else {
  1935. response.end('fail'); //返回文档内容
  1936. }
  1937. });
  1938. }
  1939. /**
  1940. * 新建导航文件保存
  1941. * @param {object} response 用户相应对象
  1942. * @param {guid} fileid 文档ID
  1943. * @param {string} navinfo 导航信息
  1944. * @param {string} nowpageid 当前页面ID
  1945. * @param {string} nowpagecontent 当前页面内容
  1946. * @param {string} name 文档名
  1947. * @param {guid} userid 用户ID
  1948. * @param {guid} pageid 页面唯一识别ID
  1949. */
  1950. us.newNavSave = function (response, fileid, navinfo, name, nowpageid, nowpagecontent, userid, pageid) {
  1951. //发送保存文件请求
  1952. us.mysql.usselect([
  1953. 'sqlserver.1473.cn',
  1954. 'UseStudio_Disk',
  1955. 'SaveFileContent',
  1956. fileid,
  1957. navinfo,
  1958. name
  1959. ], function (ret) {
  1960. if (ret) {
  1961. var _navinfo = JSON.parse(navinfo);
  1962. if (!us.nav[fileid]) {
  1963. us.nav[fileid] = {
  1964. user: {},
  1965. navInfo: _navinfo,
  1966. page: {},
  1967. history: []
  1968. };
  1969. } else {
  1970. us.nav[fileid].navInfo = _navinfo;
  1971. }
  1972. var _navdata = us.nav[fileid];
  1973. _navdata.user[userid] = [pageid]; //将用户加入协同记录数组
  1974. us.offLineMessage[pageid].nav.indexOf(fileid) == -1 && us.offLineMessage[pageid].nav.push(fileid); //添加到用户离线消息的连接wrod记录中
  1975. if (nowpageid) {
  1976. us.SaveNavPageContent(response, fileid, nowpageid, nowpagecontent);
  1977. } else {
  1978. response.end('saveContentSuccess'); //返回文档内容
  1979. }
  1980. } else {
  1981. response.end('saveNavFail'); //返回文档内容
  1982. }
  1983. });
  1984. }
  1985. us.SaveNavPageContent = function (response, fileid, nowpageid, nowpagecontent) {
  1986. var _navdata = us.nav[fileid];
  1987. //发送保存内容请求
  1988. us.mysql.usselect([
  1989. 'sqlserver.1473.cn',
  1990. 'UseStudio_Office',
  1991. 'SaveNavPageContent',
  1992. nowpageid,
  1993. nowpagecontent
  1994. ], function (ret) {
  1995. if (ret) {
  1996. _navdata.page[nowpageid] = us.cheerio.load(nowpagecontent.replace(/\\'/ig, '\'').replace(/\\"/ig, "\""));
  1997. response.end('saveContentSuccess'); //返回文档内容
  1998. } else {
  1999. response.end('saveContentFail'); //返回文档内容
  2000. }
  2001. });
  2002. }
  2003. /**
  2004. * 新建导航页面
  2005. * @param {object} response 用户相应对象
  2006. * @param {string} nowpageid 当前页面ID
  2007. * @param {string} nowpagecontent 当前页面内容
  2008. */
  2009. us.copyPageContent = function (response, fileid, nowpageid, nowpagecontent, copypageid) {
  2010. if (!us.nav[fileid]) {
  2011. us.nav[fileid] = {
  2012. user: {},
  2013. navInfo: "",
  2014. page: {},
  2015. history: []
  2016. };
  2017. }
  2018. var _navdata = us.nav[fileid];
  2019. //发送保存内容请求
  2020. us.mysql.usselect([
  2021. 'sqlserver.1473.cn',
  2022. 'UseStudio_Office',
  2023. 'CopyPageContent',
  2024. nowpageid,
  2025. nowpagecontent,
  2026. copypageid
  2027. ], function (ret) {
  2028. if (ret) {
  2029. nowpagecontent == "" && (nowpagecontent = JSON.parse(ret)[0][0].content);
  2030. _navdata.page[nowpageid] = us.cheerio.load(nowpagecontent.replace(/\\'/ig, '\'').replace(/\\"/ig, "\""));
  2031. response.end('saveContentSuccess'); //返回文档内容
  2032. } else {
  2033. response.end('saveContentFail'); //返回文档内容
  2034. }
  2035. });
  2036. }
  2037. /**
  2038. * 加入文档编辑 该函数暂无使用
  2039. *
  2040. * @param {object} response 用户相应对象
  2041. * @param {string} wordinfo 文档信息 形如: {docId : 'guid,guid,guid', userId : guid, pageId : guid}
  2042. **/
  2043. us.addWord = function (response, wordinfo) {
  2044. //获取用户参数
  2045. wordinfo = JSON.parse(wordinfo);
  2046. var _docidarray = wordinfo.docId.split(','), //根据','号分割多个文档ID
  2047. _userid = wordinfo.userId, //用户ID
  2048. _pageid = wordinfo.pageId, //页面唯一识别ID
  2049. i, //定义循环变量
  2050. _docid, //记录文档ID变量
  2051. _docinfo; //记录文档信息变量
  2052. for (i = 0; i < _docidarray.length; i++) { //循环处理每一份问的加入请求
  2053. _docid = _docidarray[i]; //记录文档ID变量
  2054. _docinfo = us.word[_docid]; //记录文档信息变量
  2055. if (!_docinfo) { //判断群列表是否已存在不存在则创建
  2056. us.word[_docid] = {
  2057. user: {},
  2058. history: []
  2059. }; //创建word文档信息
  2060. us.word[_docid].user[_userid] = [_pageid]; //加入用户
  2061. } else if (!_docinfo.user[_userid]) { //判断文档的用户是否已登陆
  2062. _docinfo.user[_userid] = [_pageid];
  2063. } else if (_docinfo.user[_userid].indexOf(_pageid) == -1) { //将用户添加到群列表中
  2064. _docinfo.user[_userid].push(_pageid);
  2065. }
  2066. us.offLineMessage[_pageid].word.push(_docid); //添加到用户离线消息的连接wrod记录中
  2067. }
  2068. response.end('addword'); //即时相应请求成功
  2069. }
  2070. /**
  2071. * 加入Excel编辑
  2072. *
  2073. * @param {object} response 用户相应对象
  2074. * @param {string} excelinfo 表格信息
  2075. **/
  2076. us.addExcel = function (response, excelinfo) {
  2077. //获取用户id
  2078. //获取用户id
  2079. excelinfo = JSON.parse(excelinfo);
  2080. var _excelinfo = us.excel[excelinfo.docId],
  2081. _userid = excelinfo.userId,
  2082. _pageid = excelinfo.pageId,
  2083. _docid = excelinfo.docId;
  2084. if (!_excelinfo) { //判断群列表是否已存在不存在则创建
  2085. us.excel[_docid] = {
  2086. user: {},
  2087. history: []
  2088. };
  2089. us.excel[_docid].user[_userid] = [_pageid];
  2090. } else if (!_excelinfo.user[_userid]) { //判断文档的用户是否已登陆 如果只有1个页面 则直接添加
  2091. _excelinfo.user[_userid] = [_pageid];
  2092. } else if (_excelinfo.user[_userid].indexOf(_pageid) == -1) { //将用户添加到群列表中 假如是一个用户多个页面 这里 则就是将每个页面添加到该用户下
  2093. _excelinfo.user[_userid].push(_pageid);
  2094. }
  2095. us.offLineMessage[_pageid].excel.push(_docid); //添加到用户离线消息的连接excel记录中
  2096. response.end('OK'); //返回到前台
  2097. }
  2098. /**
  2099. * 初始化获取未来课堂数据
  2100. *
  2101. */
  2102. us.getcscl = function (response, calssid, pageid, userid) {
  2103. //判断文档是否已在内层中存在
  2104. if (us.cscl[calssid]) {
  2105. //如果存在则直接返回内层的文档
  2106. if (!us.cscl[calssid].user[userid]) {
  2107. us.cscl[calssid].user[userid] = [pageid];
  2108. }
  2109. else if (us.cscl[calssid].user[userid].indexOf(pageid) == -1) {
  2110. us.cscl[calssid].user[userid].push(pageid);
  2111. }
  2112. try {
  2113. us.offLineMessage[pageid].cscl.indexOf(calssid) == -1 && us.offLineMessage[pageid].cscl.push(calssid); //添加到用户离线消息的连接wrod记录中
  2114. }
  2115. catch (e) {
  2116. }
  2117. //后续优化到redis获取
  2118. us.mysql.usselect(['172.16.12.5', 'pbl', 'select_room', calssid],
  2119. function (ret) {
  2120. var _user = us.getuserbytype(us.cscl[calssid].user); //获取当前项目下所有的用户信息
  2121. var _data = us.cscl[calssid].content; //保存在内存的知识建构数据
  2122. if (ret) {
  2123. ret[0][0].user = _user; //这里是项目当前所有用户
  2124. ret[0][0].data = _data; //把思维网格的内容转化成数组
  2125. if (userid && us.user[userid]) {
  2126. us.sendUser(us.user[userid], "cscl", pageid, calssid); //加入知识建构项目后通知所有的用户
  2127. }
  2128. response.end(JSON.stringify(ret)); //返回文档内容
  2129. }
  2130. else {
  2131. response.end(''); //返回文档内容
  2132. }
  2133. });
  2134. }
  2135. else {
  2136. us.cscl[calssid] = {
  2137. user: {},
  2138. history: []
  2139. };
  2140. us.cscl[calssid].user[userid] = [pageid];
  2141. if (us.offLineMessage[pageid]) {
  2142. us.offLineMessage[pageid].cscl.indexOf(calssid) == -1 && us.offLineMessage[pageid].cscl.push(calssid); //添加到用户离线消息的连接wrod记录中
  2143. }
  2144. //如果不存在则去数据库查找
  2145. us.mysql.usselect(['172.16.12.5', 'pbl', 'select_room', calssid],
  2146. function (ret) {
  2147. var _user = us.getuserbytype(us.cscl[calssid].user);
  2148. if (ret) {
  2149. ret[0][0].user = _user; //这里是项目当前所有用户
  2150. try {
  2151. us.cscl[calssid].content = JSON.parse(ret[0][0].data) || {}; //把思维网格的内容转化成数组
  2152. }
  2153. catch (e) {
  2154. us.cscl[calssid].content = ret[0][0].data || {};
  2155. }
  2156. response.end(JSON.stringify(ret)); //返回文档内容
  2157. }
  2158. else {
  2159. us.cscl[calssid].content = {}; //创建一个空数组
  2160. response.end(''); //返回文档内容
  2161. }
  2162. });
  2163. }
  2164. }
  2165. /**
  2166. * 初始化获取未来课堂数据
  2167. *
  2168. */
  2169. us.getcscldata = function (response, calssid, pageid, userid) {
  2170. //判断文档是否已在内层中存在
  2171. if (us.cscl[calssid]) {
  2172. //后续优化到redis获取
  2173. us.mysql.usselect(['172.16.12.5', 'pbl', 'select_room', calssid],
  2174. function (ret) {
  2175. var _user = us.getuserbytype(us.cscl[calssid].user); //获取当前项目下所有的用户信息
  2176. var _data = us.cscl[calssid].content; //保存在内存的知识建构数据
  2177. if (ret) {
  2178. ret[0][0].user = _user; //这里是项目当前所有用户
  2179. ret[0][0].data = _data; //把思维网格的内容转化成数组
  2180. response.end(JSON.stringify(ret)); //返回文档内容
  2181. }
  2182. else {
  2183. response.end(''); //返回文档内容
  2184. }
  2185. });
  2186. }
  2187. else {
  2188. //如果不存在则去数据库查找
  2189. us.mysql.usselect(['172.16.12.5', 'pbl', 'select_room', calssid],
  2190. function (ret) {
  2191. var _user = [];
  2192. if (ret) {
  2193. ret[0][0].user = _user; //这里是项目当前所有用户
  2194. response.end(JSON.stringify(ret)); //返回文档内容
  2195. }
  2196. else {
  2197. response.end(''); //返回文档内容
  2198. }
  2199. });
  2200. }
  2201. }
  2202. /**
  2203. * 获取思维网格数据,转成成数组
  2204. *
  2205. */
  2206. us.getMindNetwork = function (response, docid, pageid, userid) {
  2207. //判断文档是否已在内层中存在
  2208. if (us.mindNetwork[docid]) {
  2209. //如果存在则直接返回内层的文档
  2210. if (!us.mindNetwork[docid].user[userid]) {
  2211. us.mindNetwork[docid].user[userid] = [pageid];
  2212. } else {
  2213. us.mindNetwork[docid].user[userid].push(pageid);
  2214. }
  2215. try {
  2216. us.offLineMessage[pageid].mindNetwork.indexOf(docid) == -1 && us.offLineMessage[pageid].mindNetwork.push(docid); //添加到用户离线消息的连接wrod记录中
  2217. }
  2218. catch (e) {
  2219. }
  2220. //console.log(us.mindNetwork[docid]);
  2221. //如果不存在则去数据库查找
  2222. us.mysql.usselect(['172.16.12.5', 'pbl', 'select_room', docid],
  2223. function (ret) {
  2224. if (ret) {
  2225. ret[0][0].data = us.mindNetwork[docid].content; //把思维网格的内容转化成数组
  2226. response.end(JSON.stringify(ret)); //返回文档内容
  2227. }
  2228. else {
  2229. response.end(''); //返回文档内容
  2230. }
  2231. });
  2232. }
  2233. else {
  2234. us.mindNetwork[docid] = {
  2235. user: {},
  2236. history: []
  2237. };
  2238. us.mindNetwork[docid].user[userid] = [pageid];
  2239. if (us.offLineMessage[pageid]) {
  2240. us.offLineMessage[pageid].mindNetwork.indexOf(docid) == -1 && us.offLineMessage[pageid].mindNetwork.push(docid); //添加到用户离线消息的连接wrod记录中
  2241. }
  2242. //如果不存在则去数据库查找
  2243. us.mysql.usselect(['172.16.12.5', 'pbl', 'select_room', docid],
  2244. function (ret) {
  2245. if (ret) {
  2246. us.mindNetwork[docid].content = JSON.parse(ret[0][0].data); //把思维网格的内容转化成数组
  2247. response.end(JSON.stringify(ret)); //返回文档内容
  2248. }
  2249. else {
  2250. us.mindNetwork[docid].content = []; //创建一个空数组
  2251. response.end(''); //返回文档内容
  2252. }
  2253. });
  2254. }
  2255. }
  2256. /**
  2257. * 获取实时课堂据,转成成数组
  2258. *
  2259. */
  2260. us.getRealTimeClass = function (response, docid, pageid, userid) {
  2261. //判断文档是否已在内层中存在
  2262. if (us.realTimeClass[docid]) {
  2263. //如果存在则直接返回内层的文档
  2264. if (!us.realTimeClass[docid].user[userid]) {
  2265. us.realTimeClass[docid].user[userid] = [pageid];
  2266. } else {
  2267. us.realTimeClass[docid].user[userid].push(pageid);
  2268. }
  2269. try {
  2270. us.offLineMessage[pageid].realTimeClass.indexOf(docid) == -1 && us.offLineMessage[pageid].realTimeClass.push(docid); //添加到用户离线消息的连接wrod记录中
  2271. }
  2272. catch (e) {
  2273. }
  2274. //console.log(us.realTimeClass[docid]);
  2275. //如果不存在则去数据库查找
  2276. us.mysql.usselect(['172.16.12.5', 'pbl', 'selectPptPage', docid],
  2277. function (ret) {
  2278. if (ret) {
  2279. ret[0][0].page = us.realTimeClass[docid].content; //把思维网格的内容转化成数组
  2280. response.end(JSON.stringify(ret)); //返回文档内容
  2281. }
  2282. else {
  2283. response.end(''); //返回文档内容
  2284. }
  2285. });
  2286. }
  2287. else {
  2288. us.realTimeClass[docid] = {
  2289. user: {},
  2290. history: []
  2291. };
  2292. us.realTimeClass[docid].user[userid] = [pageid];
  2293. if (us.offLineMessage[pageid]) {
  2294. us.offLineMessage[pageid].realTimeClass.indexOf(docid) == -1 && us.offLineMessage[pageid].realTimeClass.push(docid); //添加到用户离线消息的连接wrod记录中
  2295. }
  2296. //如果不存在则去数据库查找
  2297. us.mysql.usselect(['172.16.12.5', 'pbl', 'selectPptPage', docid],
  2298. function (ret) {
  2299. if (ret) {
  2300. us.realTimeClass[docid].content = JSON.parse(ret[0][0].page); //把思维网格的内容转化成数组
  2301. response.end(JSON.stringify(ret)); //返回文档内容
  2302. }
  2303. else {
  2304. us.realTimeClass[docid].content = []; //创建一个空数组
  2305. response.end(''); //返回文档内容
  2306. }
  2307. });
  2308. }
  2309. }
  2310. /**
  2311. * 获取文档,并生成DOM
  2312. *
  2313. * @param {object} response 用户相应对象
  2314. * @param {string} docid 文档ID
  2315. */
  2316. us.getWord = function (response, docid, pageid, userid) {
  2317. // //判断文档是否已在内层中存在
  2318. // if (us.word[docid]) {
  2319. // //如果存在则直接返回内层的文档
  2320. // if (!us.word[docid].user[userid]) {
  2321. // us.word[docid].user[userid] = [pageid];
  2322. // } else {
  2323. // us.word[docid].user[userid].push(pageid);
  2324. // }
  2325. // us.offLineMessage[pageid].word.indexOf(docid) == -1 && us.offLineMessage[pageid].word.push(docid); //添加到用户离线消息的连接wrod记录中
  2326. // response.end(us.word[docid].content('body').html());
  2327. // }
  2328. // else {
  2329. // us.word[docid] = {
  2330. // user: {},
  2331. // history: []
  2332. // };
  2333. // us.word[docid].user[userid] = [pageid];
  2334. // if (us.offLineMessage[pageid]) {
  2335. // us.offLineMessage[pageid].word.indexOf(docid) == -1 && us.offLineMessage[pageid].word.push(docid); //添加到用户离线消息的连接wrod记录中
  2336. // }
  2337. // //如果不存在则去数据库查找
  2338. // us.mysql.usselect(['172.16.12.5', 'pbl', 'select_file', docid],
  2339. // function (ret) {
  2340. // if (ret) {
  2341. // var _office = JSON.parse(ret)[0][0].data ? JSON.parse(ret)[0][0].data.replace(/\\'/ig, '\'').replace(/\\"/ig, "\"") : ""; //记录查找内容
  2342. // us.word[docid].content = us.cheerio.load(_office); //将文档添加到内层中
  2343. // response.end(_office); //返回文档内容
  2344. // } else {
  2345. // us.word[docid].content = us.cheerio.load(''); //将文档添加到内层中
  2346. // response.end(''); //返回文档内容
  2347. // }
  2348. // });
  2349. // }
  2350. //判断文档是否已在内层中存在
  2351. if (us.word[docid]) {
  2352. //如果存在则直接返回内层的文档
  2353. if (!us.word[docid].user[userid]) {
  2354. us.word[docid].user[userid] = [pageid];
  2355. } else {
  2356. us.word[docid].user[userid].push(pageid);
  2357. }
  2358. // console.log(us.offLineMessage[pageid]);
  2359. // console.log(pageid);
  2360. try {
  2361. us.offLineMessage[pageid].word.indexOf(docid) == -1 && us.offLineMessage[pageid].word.push(docid); //添加到用户离线消息的连接wrod记录中
  2362. }
  2363. catch (e) {
  2364. }
  2365. //如果不存在则去数据库查找
  2366. us.mysql.usselect(['172.16.12.5', 'pbl', 'select_file', docid],
  2367. function (ret) {
  2368. if (ret) {
  2369. ret[0][0].data = us.word[docid].content('body').html();
  2370. response.end(JSON.stringify(ret)); //返回文档内容
  2371. } else {
  2372. us.word[docid].content = us.cheerio.load(''); //将文档添加到内层中
  2373. response.end(''); //返回文档内容
  2374. }
  2375. });
  2376. }
  2377. else {
  2378. us.word[docid] = {
  2379. user: {},
  2380. history: []
  2381. };
  2382. us.word[docid].user[userid] = [pageid];
  2383. if (us.offLineMessage[pageid]) {
  2384. us.offLineMessage[pageid].word.indexOf(docid) == -1 && us.offLineMessage[pageid].word.push(docid); //添加到用户离线消息的连接wrod记录中
  2385. }
  2386. //如果不存在则去数据库查找
  2387. us.mysql.usselect(['172.16.12.5', 'pbl', 'select_file', docid],
  2388. function (ret) {
  2389. if (ret) {
  2390. var _office = ret[0][0].data ? ret[0][0].data.replace(/\\'/ig, '\'').replace(/\\"/ig, "\"") : ""; //记录查找内容
  2391. us.word[docid].content = us.cheerio.load(_office); //将文档添加到内层中
  2392. response.end(_office); //返回文档内容
  2393. } else {
  2394. us.word[docid].content = us.cheerio.load(''); //将文档添加到内层中
  2395. response.end(''); //返回文档内容
  2396. }
  2397. });
  2398. }
  2399. }
  2400. us.getExcel = function (response, docid, pageid, userid) {
  2401. //判断文档是否已在内层中存在
  2402. if (us.excel[docid]) {
  2403. //如果存在则直接返回内层的文档
  2404. if (!us.excel[docid].user[userid]) {
  2405. us.excel[docid].user[userid] = [pageid];
  2406. } else {
  2407. us.excel[docid].user[userid].push(pageid);
  2408. }
  2409. us.offLineMessage[pageid].excel.indexOf(docid) == -1 && us.offLineMessage[pageid].excel.push(docid); //添加到用户离线消息的连接wrod记录中
  2410. var _office = us.excel[docid].content;
  2411. response.end(JSON.stringify(_office));
  2412. } else {
  2413. us.excel[docid] = {
  2414. user: {},
  2415. history: []
  2416. };
  2417. us.excel[docid].user[userid] = [pageid];
  2418. us.offLineMessage[pageid].excel.indexOf(docid) == -1 && us.offLineMessage[pageid].excel.push(docid); //添加到用户离线消息的连接wrod记录中
  2419. //如果不存在则去数据库查找
  2420. us.mysql.usselect([
  2421. 'sqlserver.1473.cn',
  2422. 'UseStudio_Disk',
  2423. 'GetFileContent',
  2424. docid
  2425. ], function (ret) {
  2426. if (ret) {
  2427. var _office = JSON.parse(ret)[0][0].UsOffice;
  2428. us.excel[docid].content = JSON.parse(_office); //将文档添加到内层中
  2429. response.end(_office); //返回文档内容
  2430. } else {
  2431. us.excel[docid].content = ''; //将文档添加到内层中
  2432. response.end(''); //返回文档内容
  2433. }
  2434. });
  2435. }
  2436. }
  2437. /**
  2438. * 新建文档保存内容并加入协同
  2439. * @param {object} response 用户相应对象
  2440. * @param {guid} docid 文档ID
  2441. * @param {string} content 文档内容
  2442. * @param {string} name 文档名
  2443. * @param {guid} userid 用户ID
  2444. * @param {guid} pageid 页面唯一识别ID
  2445. */
  2446. us.newExcelSave = function (response, docid, content, name, userid, pageid) {
  2447. us.excel[docid] = {
  2448. user: {},
  2449. history: []
  2450. }; //创建excel类
  2451. us.excel[docid].user[userid] = [pageid]; //将用户加入协同记录数组
  2452. us.offLineMessage[pageid].excel.indexOf(docid) == -1 && us.offLineMessage[pageid].word.push(docid); //添加到用户离线消息的连接wrod记录中
  2453. //发送保存请求
  2454. us.mysql.usselect([
  2455. 'sqlserver.1473.cn',
  2456. 'UseStudio_Disk',
  2457. 'SaveFileContent',
  2458. docid,
  2459. content,
  2460. name
  2461. ], function (ret) {
  2462. if (ret) {
  2463. //设置内容
  2464. us.excel[docid].content = JSON.parse(content); //将文档添加到内层中
  2465. response.end('saveOk'); //返回保存成功
  2466. } else {
  2467. response.end('fail'); //返回文档内容
  2468. }
  2469. });
  2470. }
  2471. /**
  2472. * 生成Guid
  2473. *
  2474. **/
  2475. us.createGuid = function () {
  2476. //返回guid的处理
  2477. return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
  2478. var r = Math.random() * 16 | 0,
  2479. v = c == 'x' ? r : (r & 0x3 | 0x8);
  2480. return v.toString(16);
  2481. });
  2482. };
  2483. /**
  2484. * 监听1473端口的处理
  2485. *
  2486. **/
  2487. us.server.listen('1473', '', function () {
  2488. //用户下线监控
  2489. setInterval(function () { us.offLine() }, 60000);
  2490. //在控制台输出监听提示
  2491. console.log("开始监听" + us.server.address().port + "......");
  2492. });
  2493. // 同意处理错误
  2494. //process.on('uncaughtException', function (e) {
  2495. // console.log("错误:" + e);
  2496. //});