ZwCloud2DRestfulAPI.js 22 KB

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