message.js 76 KB

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