ZwCloud2DRestfulAPI.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  1. let baseUrl = 'http://192.168.51.174:9092'
  2. let userUrl = 'http://192.168.51.174:9091'
  3. let appKey = ''
  4. let docId = ''
  5. let sourceFileId = ''
  6. let fullVersionPermissions = {
  7. 'productName': 'ZW2D_CLOUD_FULL_2024',
  8. 'featureId': 167,
  9. 'projectId': 0,
  10. 'expiredTime': '2022-12-12T00:00:00',
  11. 'authClientNum': 10,
  12. 'forCloud2D': true,
  13. 'permissions': [
  14. {
  15. 'description': 'DWG 100',
  16. 'name': 'DWG 100',
  17. 'permissionId': '100',
  18. 'type': 'SDK'
  19. },
  20. {
  21. 'description': 'DXF 101',
  22. 'name': 'DXF 101',
  23. 'permissionId': '101',
  24. 'type': 'SDK'
  25. },
  26. {
  27. 'description': '天正 102',
  28. 'name': '天正 102',
  29. 'permissionId': '102',
  30. 'type': 'SDK'
  31. },
  32. {
  33. 'description': '图纸预览图获取',
  34. 'name': '图纸预览图获取',
  35. 'permissionId': '201',
  36. 'type': 'SDK'
  37. },
  38. {
  39. 'description': '视口重生成',
  40. 'name': '视口重生成',
  41. 'permissionId': '202',
  42. 'type': 'SDK'
  43. },
  44. {
  45. 'description': '外部参照图片',
  46. 'name': '外部参照图片',
  47. 'permissionId': '203',
  48. 'type': 'SDK'
  49. },
  50. {
  51. 'description': 'DWG轻量数据生成',
  52. 'name': 'DWG轻量数据生成',
  53. 'permissionId': '204',
  54. 'type': 'SDK'
  55. },
  56. {
  57. 'description': '字体设置',
  58. 'name': '字体设置',
  59. 'permissionId': '205',
  60. 'type': 'SDK'
  61. },
  62. {
  63. 'description': '字体替换设置',
  64. 'name': '字体替换设置',
  65. 'permissionId': '206',
  66. 'type': 'SDK'
  67. },
  68. {
  69. 'description': '字体库',
  70. 'name': '字体库',
  71. 'permissionId': '207',
  72. 'type': 'SDK'
  73. },
  74. {
  75. 'description': '图纸二维线框显示',
  76. 'name': '图纸二维线框显示',
  77. 'permissionId': '300',
  78. 'type': 'SDK'
  79. },
  80. {
  81. 'description': '大图浏览(20M)',
  82. 'name': '大图浏览(20M)',
  83. 'permissionId': '301',
  84. 'type': 'SDK'
  85. },
  86. {
  87. 'description': '着色模式显示支持',
  88. 'name': '着色模式显示支持',
  89. 'permissionId': '302',
  90. 'type': 'SDK'
  91. },
  92. {
  93. 'description': '缩放',
  94. 'name': '缩放',
  95. 'permissionId': '303',
  96. 'type': 'SDK'
  97. },
  98. {
  99. 'description': '平移',
  100. 'name': '平移',
  101. 'permissionId': '304',
  102. 'type': 'SDK'
  103. },
  104. {
  105. 'description': '图片显示',
  106. 'name': '图片显示',
  107. 'permissionId': '305',
  108. 'type': 'SDK'
  109. },
  110. {
  111. 'description': '外部参照显示',
  112. 'name': '外部参照显示',
  113. 'permissionId': '306',
  114. 'type': 'SDK'
  115. },
  116. {
  117. 'description': '布局显示和切换',
  118. 'name': '布局显示和切换',
  119. 'permissionId': '307',
  120. 'type': 'SDK'
  121. },
  122. {
  123. 'description': '图层面板',
  124. 'name': '图层面板',
  125. 'permissionId': '308',
  126. 'type': 'SDK'
  127. },
  128. {
  129. 'description': '图层显示/隐藏',
  130. 'name': '图层显示/隐藏',
  131. 'permissionId': '309',
  132. 'type': 'SDK'
  133. },
  134. {
  135. 'description': '命令行窗口',
  136. 'name': '命令行窗口',
  137. 'permissionId': '310',
  138. 'type': 'SDK'
  139. },
  140. {
  141. 'description': '线宽显示',
  142. 'name': '线宽显示',
  143. 'permissionId': '311',
  144. 'type': 'SDK'
  145. },
  146. {
  147. 'description': '栅格显示',
  148. 'name': '栅格显示',
  149. 'permissionId': '312',
  150. 'type': 'SDK'
  151. },
  152. {
  153. 'description': '外部参照面板',
  154. 'name': '外部参照面板',
  155. 'permissionId': '313',
  156. 'type': 'SDK'
  157. },
  158. {
  159. 'description': '系统参数设置',
  160. 'name': '系统参数设置',
  161. 'permissionId': '314',
  162. 'type': 'SDK'
  163. },
  164. {
  165. 'description': '测量长度/面积/角度/坐标/弧长',
  166. 'name': '测量长度/面积/角度/坐标/弧长',
  167. 'permissionId': '400',
  168. 'type': 'SDK'
  169. },
  170. {
  171. 'description': 'Text 单行文字',
  172. 'name': 'Text 单行文字',
  173. 'permissionId': '500',
  174. 'type': 'SDK'
  175. },
  176. {
  177. 'description': 'Revcloud 云线',
  178. 'name': 'Revcloud 云线',
  179. 'permissionId': '501',
  180. 'type': 'SDK'
  181. },
  182. {
  183. 'description': 'Leader 引线',
  184. 'name': 'Leader 引线',
  185. 'permissionId': '502',
  186. 'type': 'SDK'
  187. },
  188. {
  189. 'description': 'MLeader 多重引线',
  190. 'name': 'MLeader 多重引线',
  191. 'permissionId': '503',
  192. 'type': 'SDK'
  193. },
  194. {
  195. 'description': '图片(即图章功能)',
  196. 'name': '图片(即图章功能)',
  197. 'permissionId': '504',
  198. 'type': 'SDK'
  199. },
  200. {
  201. 'description': 'PLine 多段线',
  202. 'name': 'PLine 多段线',
  203. 'permissionId': '505',
  204. 'type': 'SDK'
  205. },
  206. {
  207. 'description': 'Rectangle 矩形',
  208. 'name': 'Rectangle 矩形',
  209. 'permissionId': '506',
  210. 'type': 'SDK'
  211. },
  212. {
  213. 'description': 'Circle 圆',
  214. 'name': 'Circle 圆',
  215. 'permissionId': '507',
  216. 'type': 'SDK'
  217. },
  218. {
  219. 'description': 'Arc 弧线',
  220. 'name': 'Arc 弧线',
  221. 'permissionId': '508',
  222. 'type': 'SDK'
  223. },
  224. {
  225. 'description': 'DWG导出',
  226. 'name': 'DWG导出',
  227. 'permissionId': '600',
  228. 'type': 'SDK'
  229. },
  230. {
  231. 'description': '图片输出',
  232. 'name': '图片输出',
  233. 'permissionId': '601',
  234. 'type': 'SDK'
  235. },
  236. {
  237. 'description': 'PDF输出',
  238. 'name': 'PDF输出',
  239. 'permissionId': '602',
  240. 'type': 'SDK'
  241. },
  242. {
  243. 'description': '添加预览水印',
  244. 'name': '添加预览水印',
  245. 'permissionId': '700',
  246. 'type': 'SDK'
  247. },
  248. {
  249. 'description': '书签、审图',
  250. 'name': '书签、审图',
  251. 'permissionId': '800',
  252. 'type': 'SDK'
  253. },
  254. {
  255. 'description': '批注面版',
  256. 'name': '批注面版',
  257. 'permissionId': '801',
  258. 'type': 'SDK'
  259. },
  260. {
  261. 'description': '书签定义和调用接口、布局信息和坐标转换接口、缩放比例和生成截图接口等',
  262. 'name': '书签定义和调用接口、布局信息和坐标转换接口、缩放比例和生成截图接口等',
  263. 'permissionId': '900',
  264. 'type': 'SDK'
  265. },
  266. ]
  267. }
  268. class Request {
  269. constructor() {
  270. }
  271. getData(url, ticket, params, resType) {
  272. return new Promise((res, rej) => {
  273. let paramsArr = []
  274. for (let k in params) {
  275. paramsArr.push(encodeURIComponent(k) + '=' + encodeURIComponent(params[k]))
  276. }
  277. if (params) {
  278. url = url + '?' + paramsArr.join('&')
  279. }
  280. let ajax = new XMLHttpRequest()
  281. let ip = ticket ? userUrl : baseUrl
  282. ajax.open('GET', ip + url)
  283. resType && (ajax.responseType = resType)
  284. ticket && Object.keys(ticket).forEach(key => {
  285. ajax.setRequestHeader(key, ticket[key])
  286. })
  287. ajax.send()
  288. ajax.onload = function () {
  289. if (resType) res(ajax.response)
  290. else {
  291. let data = JSON.parse(ajax.response)
  292. if (data.code === 0) {
  293. res(data.data)
  294. } else if (data.code === 2060) {
  295. rej(data.code)
  296. } else rej(data.msg)
  297. }
  298. }
  299. })
  300. }
  301. postData(url, ticket, params, resType) {
  302. return new Promise((res, rej) => {
  303. let ajax = new XMLHttpRequest()
  304. let ip = ticket ? userUrl : baseUrl
  305. ajax.open('POST', ip + url)
  306. resType && (ajax.responseType = resType)
  307. ajax.setRequestHeader('content-type', 'application/json')
  308. ticket && Object.keys(ticket).forEach(key => {
  309. ajax.setRequestHeader(key, ticket[key])
  310. })
  311. ajax.send(params)
  312. ajax.onreadystatechange = function () {
  313. if (ajax.readyState === 4) {
  314. if (ajax.status === 200) {
  315. res(ajax.response)
  316. } else {
  317. rej()
  318. }
  319. }
  320. }
  321. })
  322. }
  323. get(url, params, resType) {
  324. if (appKey) {
  325. let ticket = { 'x-ca-key': appKey }
  326. return this.getData(url, ticket, params, resType)
  327. } else {
  328. return this.getData('/auth/js/ticket?url=' + url).then(res => {
  329. let ticket = {
  330. 'x-ca-key': res.appKey,
  331. 'x-ca-nonce': res.nonce,
  332. 'x-ca-date': res.timestamp,
  333. 'x-ca-signature': res.signature,
  334. }
  335. return ticket
  336. }).then(ticket => {
  337. return this.getData(url, ticket, params, resType)
  338. })
  339. }
  340. }
  341. post(url, params, resType) {
  342. if (appKey) {
  343. let ticket = { 'x-ca-key': appKey }
  344. return this.postData(url, ticket, params, resType)
  345. } else {
  346. return this.getData('/auth/js/ticket?url=' + url).then(res => {
  347. let ticket = {
  348. 'x-ca-key': res.appKey,
  349. 'x-ca-nonce': res.nonce,
  350. 'x-ca-date': res.timestamp,
  351. 'x-ca-signature': res.signature,
  352. }
  353. return ticket
  354. }).then(ticket => {
  355. return this.postData(url, ticket, params, resType)
  356. })
  357. }
  358. }
  359. arrayBufferToBase64(buffer) {
  360. let binary = '';
  361. let bytes = new Uint8Array(buffer);
  362. let len = bytes.byteLength;
  363. for (let i = 0; i < len; i++) {
  364. binary += String.fromCharCode(bytes[i]);
  365. }
  366. return btoa(binary);
  367. }
  368. fontDownload(fileId) {
  369. return this.get('/api/js/v1/font/gzip', { fileId: fileId, projectId: 0 }, 'arraybuffer')
  370. }
  371. getTransferStatus() {
  372. let params = { id: docId }
  373. params = JSON.stringify(params)
  374. return this.post('/api/js/v1/doc/transferStatus', params, 'json')
  375. }
  376. transferDoc() {
  377. return this.get('/api/js/v1/doc/details', { docId: docId })
  378. }
  379. getDisplayData(handleId = 34) {
  380. return this.get('/api/js/v1/layout/lmf', { projectId: 0, handleId, docId: docId })
  381. }
  382. filePartDownload(lmfId) {
  383. return this.get('/api/js/v1/file/partDownload', { fileId: lmfId }, 'arraybuffer')
  384. }
  385. getFileInfo(lmfId) {
  386. return this.get('/api/js/v1/file/info', { id: lmfId })
  387. }
  388. getXrefRelation() {
  389. return this.get('/api/js/v1/xref/info', { id: docId })
  390. }
  391. getXrefThumb(fileId) {
  392. return this.get('/api/js/v1/file/partDownload', { fileId: fileId }, 'arraybuffer').then(res => {
  393. let url = this.arrayBufferToBase64(res);
  394. return 'data:image/jpeg;base64,' + url;
  395. })
  396. }
  397. getXrefFile(handleId, xrefIds) {
  398. let params = { docId: docId, handleId: handleId, xrefIds: xrefIds }
  399. params = JSON.stringify(params)
  400. return this.post('/api/js/v1/xref', params, 'json')
  401. }
  402. getImageInfo(filePath) {
  403. let params = { docId: docId, path: filePath }
  404. params = JSON.stringify(params)
  405. return this.post('/api/js/v1/link', params, 'json')
  406. }
  407. getImageData(fileId) {
  408. return this.get('/api/js/v1/file/partDownload', { fileId: fileId }, 'arraybuffer').then(res => {
  409. let url = this.arrayBufferToBase64(res);
  410. return 'data:image/jpeg;base64,' + url;
  411. })
  412. }
  413. getPlotData(params) {
  414. let data = JSON.stringify(params)
  415. return this.post('/api/js/v1/doc/print', data, 'arraybuffer')
  416. }
  417. getBatchPlot(params) {
  418. let data = JSON.stringify(params)
  419. return this.post('/api/js/v1/doc/batchPrint', data, 'arraybuffer')
  420. }
  421. getRegenData(params) {
  422. Object.assign(params, { docId: docId })
  423. let data = JSON.stringify(params)
  424. return this.post('/api/js/v1/layout/regen', data, 'arraybuffer')
  425. }
  426. getDwgFile(fileId) {
  427. return this.get('/api/js/v1/file/download', { id: fileId }, 'arraybuffer')
  428. }
  429. }
  430. ZwCloud2D.ZwDataProcessor.ZwSetConnectUrl = (url1, url2) => {
  431. userUrl = url1
  432. baseUrl = url2;
  433. }
  434. ZwCloud2D.ZwDataProcessor.ZwSetAppKey = (key) => {
  435. appKey = key
  436. }
  437. ZwCloud2D.ZwDataProcessor.ZwSetLoadDwg = (path) => {
  438. docId = path;
  439. }
  440. ZwCloud2D.ZwDataProcessor.ZwLoad = () => {
  441. let req = new Request();
  442. // 切换布局时候触发回调
  443. ZwCloud2D.ZwMessageCallback.ZwEvtChangeLayout = layout => {
  444. req.getDisplayData(layout.handleId).then(result => {
  445. ZwCloud2D.ZwDataProcessor.ZwGetLmfData(result.lmfId).then(lmfInfo => {
  446. if (lmfInfo) {
  447. function zcGetFileInfo() {
  448. req.getFileInfo(result.lmfId).then(fileInfo => {
  449. let curTimestamp = new Date(fileInfo.updateTime || fileInfo.createTime).getTime();
  450. if (lmfInfo.hasCache && curTimestamp.toString() === lmfInfo.timestamp) {
  451. ZwCloud2D.ZwDataManager.ZwSetDwgData(result.lmfId)
  452. } else {
  453. req.filePartDownload(result.lmfId).then(lmfInfo => {
  454. ZwCloud2D.ZwDataManager.ZwSetDwgData(result.lmfId, lmfInfo, curTimestamp)
  455. })
  456. }
  457. }).catch((res) => {
  458. if (res === 2060) {
  459. this.timer = setTimeout(() => {
  460. clearTimeout(this.timer)
  461. zcGetFileInfo()
  462. }, 3000);
  463. } else {
  464. console.error(res)
  465. }
  466. })
  467. }
  468. zcGetFileInfo()
  469. }
  470. });
  471. }).catch(error => {
  472. console.error(error)
  473. })
  474. };
  475. ZwCloud2D.ZwMessageCallback.ZwEvtLoadXrefData = data => {
  476. req.getXrefFile(data.handleId, data.xrefIds).then(xrefFile => {
  477. if (xrefFile.data.fileId) {
  478. req.filePartDownload(xrefFile.data.fileId).then(fileData => {
  479. ZwCloud2D.ZwDataManager.ZwSetXrefData(fileData);
  480. })
  481. } else {
  482. ZwCloud2D.ZwDataManager.ZwSetXrefData();
  483. }
  484. }).catch(() => {
  485. ZwCloud2D.ZwDataManager.ZwSetXrefData();
  486. })
  487. };
  488. ZwCloud2D.ZwMessageCallback.ZwEvtLoadImageData = data => {
  489. let pArr = new Array();
  490. let imageMap = new Map();
  491. data.forEach((imageUrl) => {
  492. pArr.push(new Promise((res, rej) => {
  493. req.getImageInfo(imageUrl).then(info => {
  494. if (info.data.fileId) {
  495. req.getImageData(info.data.fileId).then(result => {
  496. imageMap.set(imageUrl, result);
  497. res(true)
  498. })
  499. } else {
  500. res(true)
  501. }
  502. }).catch(err => {
  503. res(true);
  504. });
  505. }));
  506. });
  507. Promise.all(pArr).then(() => {
  508. ZwCloud2D.ZwDataManager.ZwSetImageData(imageMap);
  509. }).catch(error => {
  510. console.error(error)
  511. });
  512. };
  513. ZwCloud2D.ZwMessageCallback.ZwEvtPlotDwg = (data) => {
  514. let params = Object.assign(data.params, { docId: docId })
  515. if (params.isZwPlot) {
  516. req.getBatchPlot(params).then(res => {
  517. ZwCloud2D.ZwDataManager.ZwSetPlotData(data, res);
  518. ZwCloud2D.ZwEditor.ZwSetLoadingState(false);
  519. })
  520. } else {
  521. req.getPlotData(params).then(res => {
  522. ZwCloud2D.ZwDataManager.ZwSetPlotData(data, res);
  523. ZwCloud2D.ZwEditor.ZwSetLoadingState(false);
  524. })
  525. }
  526. };
  527. ZwCloud2D.ZwMessageCallback.ZwEvtGetRegenData = (data) => {
  528. req.getRegenData(data).then(res => {
  529. ZwCloud2D.ZwDataManager.ZwSetRegenData(res);
  530. }).catch(() => {
  531. ZwCloud2D.ZwDataManager.ZwSetRegenData(null);
  532. });
  533. };
  534. ZwCloud2D.ZwMessageCallback.ZwEvtLoadDwgFile = () => {
  535. ZwCloud2D.ZwEditor.ZwSetLoadingState(true)
  536. req.getDwgFile(sourceFileId).then(dwgFile => {
  537. ZwCloud2D.ZwDataManager.ZwSetDwgFileData(dwgFile);
  538. ZwCloud2D.ZwEditor.ZwSetLoadingState(false)
  539. })
  540. };
  541. ZwCloud2D.ZwMessageCallback.ZwEvtUpdateCommentData = async(data) => {
  542. let commentList = {
  543. code: 0,
  544. data: {
  545. rows: [data.data]
  546. }
  547. };
  548. ZwCloud2D.ZwDataManager.ZwSetCommentData({ commentList: commentList });
  549. };
  550. let ZwMain = () => {
  551. req.transferDoc().then(data => {
  552. sourceFileId = data.sourceFileId
  553. ZwCloud2D.ZwDataManager.ZwSetSdkPermission(fullVersionPermissions);
  554. // 显示当前布局的数据
  555. ZwCloud2D.ZwDataManager.ZwSetDwgInfo({ fonts: data.fonts, layouts: data.layouts, name: data.name }).then(res => {
  556. let pArr = []
  557. let fontArr = []
  558. res.missingFonts.forEach(font => {
  559. let arr = font.name.split('.');
  560. let type = (arr[arr.length - 1] || '').toUpperCase();
  561. pArr.push(req.fontDownload(font.fileId).then(data => {
  562. if (data.byteLength > 0) {
  563. fontArr.push(
  564. {
  565. name: font.name,
  566. type: type,
  567. data: data,
  568. }
  569. );
  570. }
  571. }));
  572. });
  573. Promise.all(pArr).then(() => {
  574. ZwCloud2D.ZwDataManager.ZwSetFontDataList(fontArr).then(() => {
  575. req.getDisplayData(res.layout.handleId).then(result => {
  576. req.getXrefRelation().then(xrefRelations => {
  577. let xrefPromises = [];
  578. let srcMap = new Map();
  579. xrefRelations.forEach((xrefRelation) => {
  580. if (xrefRelation.fileId) {
  581. xrefPromises.push(new Promise((res, rej) => {
  582. req.getXrefThumb(xrefRelation.fileId).then(xrefThumb => {
  583. srcMap.set(xrefRelation.fileId, xrefThumb);
  584. res(true);
  585. });
  586. }));
  587. }
  588. });
  589. Promise.all(xrefPromises).then(() => {
  590. let params = { docName: data.name, xrefRelations: xrefRelations, srcMap: srcMap };
  591. ZwCloud2D.ZwDataManager.ZwSetXrefList(params);
  592. }).catch(() => {
  593. let params = { docName: data.name, xrefRelations: xrefRelations, srcMap: srcMap };
  594. ZwCloud2D.ZwDataManager.ZwSetXrefList(params);
  595. });
  596. });
  597. req.getFileInfo(result.lmfId).then(lmfInfo => {
  598. let curTimestamp = new Date(lmfInfo.updateTime || lmfInfo.createTime).getTime();
  599. if (res.hasCache && res.timestamp === curTimestamp.toString()) {
  600. ZwCloud2D.ZwDataManager.ZwSetDwgData(result.lmfId)
  601. } else {
  602. req.filePartDownload(result.lmfId).then(lmfInfo => {
  603. ZwCloud2D.ZwDataManager.ZwSetDwgData(result.lmfId, lmfInfo, curTimestamp)
  604. })
  605. }
  606. }).catch(error => {
  607. console.error(error)
  608. })
  609. }).catch(error => {
  610. console.error(error)
  611. })
  612. })
  613. }).catch(error => {
  614. console.error(error)
  615. })
  616. });
  617. }).catch(error => {
  618. console.error(error)
  619. });
  620. }
  621. let getTransferStatus = () => {
  622. req.getTransferStatus().then(fileInfo => {
  623. let status = fileInfo.data.status
  624. if (status === 1) {
  625. let timer = setTimeout(() => {
  626. clearTimeout(timer)
  627. getTransferStatus()
  628. }, 3000);
  629. } else if (status === 2) {
  630. ZwMain()
  631. } else if (status === 3) {
  632. console.error(fileInfo.failedReason)
  633. }
  634. }).catch(err => {
  635. console.error(err)
  636. })
  637. }
  638. getTransferStatus()
  639. }