message.js 82 KB

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