message.js 81 KB

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