ZwCloud2DPrivateAPI.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. let baseUrl = 'http://192.168.51.174:5199';
  2. let wsUrl = '';
  3. let dwgPath = '';
  4. let javaUrl = '';
  5. class Request {
  6. constructor() {}
  7. get(url, params, resType, type) {
  8. return new Promise((res, rej) => {
  9. let paramsArr = [];
  10. for (let k in params) {
  11. paramsArr.push(
  12. encodeURIComponent(k) + '=' + encodeURIComponent(params[k]),
  13. );
  14. }
  15. url = url + '?' + paramsArr.join('&');
  16. let ajax = new XMLHttpRequest();
  17. let checkUrl = type === 'java' ? javaUrl + url : baseUrl + url;
  18. ajax.open('GET', checkUrl);
  19. resType && (ajax.responseType = resType);
  20. ajax.send();
  21. ajax.onload = function () {
  22. if (resType) res(ajax.response);
  23. else {
  24. let data = JSON.parse(ajax.response);
  25. if (data.code === 0) {
  26. res(data.data);
  27. } else {
  28. rej(data.msg);
  29. }
  30. }
  31. };
  32. });
  33. }
  34. post(url, params, resType, type) {
  35. return new Promise((res, rej) => {
  36. let ajax = new XMLHttpRequest();
  37. let checkUrl = type === 'java' ? javaUrl + url : baseUrl + url;
  38. ajax.open('POST', checkUrl);
  39. resType && (ajax.responseType = resType);
  40. if (!(params instanceof FormData)) {
  41. ajax.setRequestHeader('content-type', 'application/json');
  42. }
  43. ajax.send(params);
  44. ajax.onreadystatechange = function () {
  45. if (ajax.readyState === 4) {
  46. if (ajax.status === 200) {
  47. res(ajax.response);
  48. } else {
  49. rej();
  50. }
  51. }
  52. };
  53. });
  54. }
  55. arrayBufferToBase64(buffer) {
  56. let binary = '';
  57. let bytes = new Uint8Array(buffer);
  58. let len = bytes.byteLength;
  59. for (let i = 0; i < len; i++) {
  60. binary += String.fromCharCode(bytes[i]);
  61. }
  62. return btoa(binary);
  63. }
  64. fontDownload(fontName) {
  65. return this.get('/sdk/font/download', { fontName }, 'blob');
  66. }
  67. transferDoc() {
  68. return this.get('/sdk/transfer', {
  69. projectId: 0,
  70. dwgPath: dwgPath,
  71. type: 1,
  72. });
  73. }
  74. getDisplayData(handleId = 34) {
  75. return this.get(
  76. '/sdk/layout/lmf',
  77. { projectId: 0, handleId, dwgPath: dwgPath },
  78. 'arraybuffer',
  79. );
  80. }
  81. getXrefData(handleId, xrefIds) {
  82. let params = { handleId: handleId, xrefIds: xrefIds, dwgPath: dwgPath };
  83. params = JSON.stringify(params);
  84. return this.post('/sdk/xref', params, 'arraybuffer');
  85. }
  86. getXrefRelation() {
  87. return this.get('/sdk/xref/list', { dwgPath: dwgPath });
  88. }
  89. getXrefThumb(filePath) {
  90. return this.get(
  91. '/sdk/xref/thumb',
  92. { filePath: filePath },
  93. 'arraybuffer',
  94. ).then((res) => {
  95. let url = this.arrayBufferToBase64(res);
  96. return 'data:image/jpeg;base64,' + url;
  97. });
  98. }
  99. getPlotData(params) {
  100. let data = JSON.stringify(params);
  101. return this.post('/sdk/print', data, 'arraybuffer');
  102. }
  103. getImageSrc(filePath) {
  104. return this.get(
  105. '/sdk/image/download',
  106. { filePath: filePath },
  107. 'arraybuffer',
  108. ).then((res) => {
  109. let url = this.arrayBufferToBase64(res);
  110. return 'data:image/jpeg;base64,' + url;
  111. });
  112. }
  113. getImageData(filePath) {
  114. return this.get(
  115. '/sdk/image/download',
  116. { filePath: filePath },
  117. 'arraybuffer',
  118. );
  119. }
  120. getImageList() {
  121. return this.get('/sdk/image/list');
  122. }
  123. getPermissionList() {
  124. return this.get('/sdk/getStatus');
  125. }
  126. getRegenData(params) {
  127. Object.assign(params, { dwgPath: dwgPath });
  128. let data = JSON.stringify(params);
  129. return this.post('/sdk/regen', data, 'arraybuffer');
  130. }
  131. getDwgFile() {
  132. return this.get(
  133. '/sdk/doc/download',
  134. { dwgPath: dwgPath },
  135. 'arrayBuffer',
  136. 'java',
  137. );
  138. }
  139. uploadFile(file) {
  140. let formData = new FormData();
  141. formData.append('file', file);
  142. return this.post('/sdk/doc/upload', formData, 'json', 'java');
  143. }
  144. }
  145. function resizeCheck() {
  146. if (Date.now() - timer > 400) {
  147. const windowInfo = {
  148. width: window.innerWidth,
  149. height: window.innerHeight,
  150. };
  151. console.log(windowInfo);
  152. if (windowInfo.width <= 1190 || windowInfo.height <= 620) {
  153. mainMobile.style.display = 'unset';
  154. main.style.display = 'none';
  155. } else {
  156. mainMobile.style.display = 'none';
  157. main.style.display = 'unset';
  158. }
  159. timer = Date.now();
  160. }
  161. }
  162. ZwCloud2D.ZwDataProcessor.ZwSetConnectUrl = (url1, url2, url3) => {
  163. javaUrl = url1;
  164. baseUrl = url2;
  165. wsUrl = url3;
  166. };
  167. ZwCloud2D.ZwDataProcessor.ZwSetLoadDwg = (path) => {
  168. dwgPath = path;
  169. };
  170. ZwCloud2D.ZwDataProcessor.uploadDwg = (file) => {
  171. let req = new Request();
  172. return new Promise((resolve, reject) => {
  173. req.uploadFile(file).then((res) => {
  174. resolve(res);
  175. });
  176. });
  177. // req.uploadFile(file).then(res => {
  178. // dwgPath = res.data.path;
  179. // ZwCloud2D.ZwDataProcessor.ZwLoad();
  180. // let main = document.getElementById('main');
  181. // main.style.display = "none"
  182. // let mainMobile = document.getElementById('main-mobile');
  183. // mainMobile.style.display = "none"
  184. // let showDwgBtn = document.getElementById('showDwgBtn');
  185. // let showFontBtn = document.getElementById('showFontBtn');
  186. // showDwgBtn.style.display = "unset"
  187. // showFontBtn.style.display = "unset"
  188. // })
  189. };
  190. ZwCloud2D.ZwDataProcessor.uploadFont = (file) => {
  191. let req = new Request();
  192. let pArray = new Array();
  193. for (let i = 0; i < file.length; i++) {
  194. pArray.push(
  195. new Promise((resolve, reject) => {
  196. req.uploadFile(file[i]).then((res) => {
  197. if (res.code === 200) {
  198. resolve();
  199. } else {
  200. reject();
  201. }
  202. });
  203. }),
  204. );
  205. }
  206. Promise.all(pArray)
  207. .then(() => {
  208. alert('上传成功');
  209. })
  210. .catch(() => {
  211. alert('上传失败');
  212. });
  213. };
  214. ZwCloud2D.ZwDataProcessor.ZwLoad = () => {
  215. let docId = new Date().getTime().toString();
  216. let req = new Request();
  217. // 切换布局时候触发回调
  218. ZwCloud2D.ZwMessageCallback.ZwEvtChangeLayout = (layout) => {
  219. req
  220. .getDisplayData(layout.handleId)
  221. .then((data) => {
  222. let timestamp = new Date().getTime();
  223. ZwCloud2D.ZwDataManager.ZwSetDwgData(layout.handleId, data, timestamp);
  224. })
  225. .catch((error) => {
  226. console.error(error);
  227. });
  228. };
  229. ZwCloud2D.ZwMessageCallback.ZwEvtLoadXrefData = (data) => {
  230. req
  231. .getXrefData(data.handleId, data.xrefIds)
  232. .then((res) => {
  233. ZwCloud2D.ZwDataManager.ZwSetXrefData(res);
  234. })
  235. .catch(() => {
  236. ZwCloud2D.ZwDataManager.ZwSetXrefData();
  237. });
  238. };
  239. ZwCloud2D.ZwMessageCallback.ZwEvtDrawEnd = () => {};
  240. ZwCloud2D.ZwMessageCallback.ZwEvtGetRegenData = (data) => {
  241. req
  242. .getRegenData(data)
  243. .then((res) => {
  244. ZwCloud2D.ZwDataManager.ZwSetRegenData(res);
  245. })
  246. .catch(() => {
  247. ZwCloud2D.ZwDataManager.ZwSetRegenData(null);
  248. });
  249. };
  250. ZwCloud2D.ZwMessageCallback.ZwEvtLoadImageData = (data) => {
  251. let pArr = new Array();
  252. let imageMap = new Map();
  253. data.forEach((imageUrl) => {
  254. pArr.push(
  255. new Promise((res, rej) => {
  256. req
  257. .getImageData(imageUrl)
  258. .then((result) => {
  259. imageMap.set(imageUrl, result);
  260. res(true);
  261. })
  262. .catch((err) => {
  263. res(true);
  264. });
  265. }),
  266. );
  267. });
  268. Promise.all(pArr)
  269. .then(() => {
  270. ZwCloud2D.ZwDataManager.ZwSetImageData(imageMap);
  271. })
  272. .catch((error) => {
  273. console.error(error);
  274. });
  275. };
  276. ZwCloud2D.ZwMessageCallback.ZwEvtLoadImageList = () => {
  277. req.getImageList().then((res) => {
  278. res.forEach((image) => {
  279. let path = image.completePath + image.name;
  280. req.getImageSrc(path).then((imageData) => {
  281. let item = {
  282. name: image.name,
  283. data: imageData,
  284. completePath: image.completePath,
  285. };
  286. ZwCloud2D.ZwDataManager.ZwSetImageList(item);
  287. });
  288. });
  289. });
  290. };
  291. ZwCloud2D.ZwMessageCallback.ZwEvtPlotDwg = (data) => {
  292. let params = Object.assign(data.params, { dwgPath: dwgPath });
  293. req
  294. .getPlotData(params)
  295. .then((res) => {
  296. ZwCloud2D.ZwDataManager.ZwSetPlotData(data, res);
  297. ZwCloud2D.ZwEditor.ZwSetLoadingState(false);
  298. })
  299. .catch((error) => {
  300. console.error(error);
  301. });
  302. };
  303. ZwCloud2D.ZwEditor.ZwSetUserOptions({ panel: { commentPanel: false } });
  304. req
  305. .getPermissionList()
  306. .then((data) => {
  307. ZwCloud2D.ZwDataManager.ZwSetSdkPermission(data);
  308. })
  309. .catch((error) => {
  310. console.error(error);
  311. });
  312. if (!dwgPath) return;
  313. req
  314. .transferDoc()
  315. .then((data) => {
  316. // 显示当前布局的数据
  317. let layouts = [];
  318. data.layouts.forEach((layout) => {
  319. let res = {
  320. handleId: layout.handleId,
  321. isDefault: layout.isDefault === 1 ? 'YES' : 'NO',
  322. name: layout.layoutName,
  323. tabOrder: layout.order,
  324. };
  325. layouts.push(res);
  326. });
  327. ZwCloud2D.ZwDataManager.ZwSetDwgInfo({
  328. id: docId,
  329. fonts: data.fontList,
  330. layouts: layouts,
  331. name: data.name,
  332. }).then((res) => {
  333. let pArr = [];
  334. let fontArr = [];
  335. res.missingFonts.forEach((fontName) => {
  336. let arr = fontName.split('.');
  337. let type = (arr[arr.length - 1] || '').toUpperCase();
  338. pArr.push(
  339. req.fontDownload(fontName).then(async (data) => {
  340. if (data.type === 'application/octet-stream') {
  341. let arraybuffer = await data.arrayBuffer();
  342. fontArr.push({
  343. name: fontName,
  344. type: type,
  345. data: arraybuffer,
  346. });
  347. }
  348. }),
  349. );
  350. });
  351. Promise.all(pArr)
  352. .then(() => {
  353. ZwCloud2D.ZwDataManager.ZwSetFontDataList(fontArr).then(() => {
  354. req
  355. .getDisplayData(res.layout.handleId)
  356. .then((lmfInfo) => {
  357. if (res.timestamp === '')
  358. res.timestamp = new Date().getTime();
  359. ZwCloud2D.ZwDataManager.ZwSetDwgData(
  360. res.layout.handleId,
  361. lmfInfo,
  362. res.timestamp,
  363. );
  364. })
  365. .catch((error) => {
  366. console.error(error);
  367. });
  368. req
  369. .getXrefRelation()
  370. .then((xrefRelations) => {
  371. let xrefPromises = [];
  372. let srcMap = new Map();
  373. xrefRelations.forEach((xrefRelation) => {
  374. if (xrefRelation.foundPath) {
  375. xrefPromises.push(
  376. new Promise((res, rej) => {
  377. req
  378. .getXrefThumb(xrefRelation.foundPath)
  379. .then((xrefThumb) => {
  380. srcMap.set(xrefRelation.foundPath, xrefThumb);
  381. res(true);
  382. });
  383. }),
  384. );
  385. }
  386. });
  387. Promise.all(xrefPromises)
  388. .then(() => {
  389. let params = {
  390. docName: data.name,
  391. xrefRelations: xrefRelations,
  392. srcMap: srcMap,
  393. };
  394. ZwCloud2D.ZwDataManager.ZwSetXrefList(params);
  395. })
  396. .catch(() => {
  397. let params = {
  398. docName: data.name,
  399. xrefRelations: xrefRelations,
  400. srcMap: srcMap,
  401. };
  402. ZwCloud2D.ZwDataManager.ZwSetXrefList(params);
  403. });
  404. })
  405. .catch((error) => {
  406. console.error(error);
  407. });
  408. if (wsUrl) {
  409. let url =
  410. wsUrl +
  411. '/CadService?sub=' +
  412. Math.floor(Math.random() * 1000) +
  413. '&docId=' +
  414. docId +
  415. '&dwgPath=' +
  416. dwgPath;
  417. ZwCloud2D.ZwDataManager.ZwEntryEdit(url);
  418. }
  419. });
  420. })
  421. .catch((error) => {
  422. console.error(error);
  423. });
  424. });
  425. })
  426. .catch((error) => {
  427. ZwCloud2D.ZwEditor.ZwSetLoadingState(false);
  428. alert(error);
  429. let main = document.getElementById('main');
  430. main.style.display = 'unset';
  431. let mainMobile = document.getElementById('main-mobile');
  432. mainMobile.style.display = 'unset';
  433. let showDwgBtn = document.getElementById('showDwgBtn');
  434. let showFontBtn = document.getElementById('showFontBtn');
  435. showDwgBtn.style.display = 'none';
  436. showFontBtn.style.display = 'none';
  437. resizeCheck();
  438. window.addEventListener('resize', resizeCheck);
  439. console.error(error);
  440. });
  441. };