message.js 76 KB

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