message.js 76 KB

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