message.js 77 KB

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