message.js 81 KB

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