123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465 |
- let baseUrl = 'http://192.168.51.174:5199';
- let wsUrl = '';
- let dwgPath = '';
- let javaUrl = '';
- class Request {
- constructor() {}
- get(url, params, resType, type) {
- return new Promise((res, rej) => {
- let paramsArr = [];
- for (let k in params) {
- paramsArr.push(
- encodeURIComponent(k) + '=' + encodeURIComponent(params[k]),
- );
- }
- url = url + '?' + paramsArr.join('&');
- let ajax = new XMLHttpRequest();
- let checkUrl = type === 'java' ? javaUrl + url : baseUrl + url;
- ajax.open('GET', checkUrl);
- resType && (ajax.responseType = resType);
- ajax.send();
- ajax.onload = function () {
- if (resType) res(ajax.response);
- else {
- let data = JSON.parse(ajax.response);
- if (data.code === 0) {
- res(data.data);
- } else {
- rej(data.msg);
- }
- }
- };
- });
- }
- post(url, params, resType, type) {
- return new Promise((res, rej) => {
- let ajax = new XMLHttpRequest();
- let checkUrl = type === 'java' ? javaUrl + url : baseUrl + url;
- ajax.open('POST', checkUrl);
- resType && (ajax.responseType = resType);
- if (!(params instanceof FormData)) {
- ajax.setRequestHeader('content-type', 'application/json');
- }
- ajax.send(params);
- ajax.onreadystatechange = function () {
- if (ajax.readyState === 4) {
- if (ajax.status === 200) {
- res(ajax.response);
- } else {
- rej();
- }
- }
- };
- });
- }
- arrayBufferToBase64(buffer) {
- let binary = '';
- let bytes = new Uint8Array(buffer);
- let len = bytes.byteLength;
- for (let i = 0; i < len; i++) {
- binary += String.fromCharCode(bytes[i]);
- }
- return btoa(binary);
- }
- fontDownload(fontName) {
- return this.get('/sdk/font/download', { fontName }, 'blob');
- }
- transferDoc() {
- return this.get('/sdk/transfer', {
- projectId: 0,
- dwgPath: dwgPath,
- type: 1,
- });
- }
- getDisplayData(handleId = 34) {
- return this.get(
- '/sdk/layout/lmf',
- { projectId: 0, handleId, dwgPath: dwgPath },
- 'arraybuffer',
- );
- }
- getXrefData(handleId, xrefIds) {
- let params = { handleId: handleId, xrefIds: xrefIds, dwgPath: dwgPath };
- params = JSON.stringify(params);
- return this.post('/sdk/xref', params, 'arraybuffer');
- }
- getXrefRelation() {
- return this.get('/sdk/xref/list', { dwgPath: dwgPath });
- }
- getXrefThumb(filePath) {
- return this.get(
- '/sdk/xref/thumb',
- { filePath: filePath },
- 'arraybuffer',
- ).then((res) => {
- let url = this.arrayBufferToBase64(res);
- return 'data:image/jpeg;base64,' + url;
- });
- }
- getPlotData(params) {
- let data = JSON.stringify(params);
- return this.post('/sdk/print', data, 'arraybuffer');
- }
- getImageSrc(filePath) {
- return this.get(
- '/sdk/image/download',
- { filePath: filePath },
- 'arraybuffer',
- ).then((res) => {
- let url = this.arrayBufferToBase64(res);
- return 'data:image/jpeg;base64,' + url;
- });
- }
- getImageData(filePath) {
- return this.get(
- '/sdk/image/download',
- { filePath: filePath },
- 'arraybuffer',
- );
- }
- getImageList() {
- return this.get('/sdk/image/list');
- }
- getPermissionList() {
- return this.get('/sdk/getStatus');
- }
- getRegenData(params) {
- Object.assign(params, { dwgPath: dwgPath });
- let data = JSON.stringify(params);
- return this.post('/sdk/regen', data, 'arraybuffer');
- }
- getDwgFile() {
- return this.get(
- '/sdk/doc/download',
- { dwgPath: dwgPath },
- 'arrayBuffer',
- 'java',
- );
- }
- uploadFile(file) {
- let formData = new FormData();
- formData.append('file', file);
- return this.post('/sdk/doc/upload', formData, 'json', 'java');
- }
- }
- function resizeCheck() {
- if (Date.now() - timer > 400) {
- const windowInfo = {
- width: window.innerWidth,
- height: window.innerHeight,
- };
- console.log(windowInfo);
- if (windowInfo.width <= 1190 || windowInfo.height <= 620) {
- mainMobile.style.display = 'unset';
- main.style.display = 'none';
- } else {
- mainMobile.style.display = 'none';
- main.style.display = 'unset';
- }
- timer = Date.now();
- }
- }
- ZwCloud2D.ZwDataProcessor.ZwSetConnectUrl = (url1, url2, url3) => {
- javaUrl = url1;
- baseUrl = url2;
- wsUrl = url3;
- };
- ZwCloud2D.ZwDataProcessor.ZwSetLoadDwg = (path) => {
- dwgPath = path;
- };
- ZwCloud2D.ZwDataProcessor.uploadDwg = (file) => {
- let req = new Request();
- return new Promise((resolve, reject) => {
- req.uploadFile(file).then((res) => {
- resolve(res);
- });
- });
- // req.uploadFile(file).then(res => {
- // dwgPath = res.data.path;
- // ZwCloud2D.ZwDataProcessor.ZwLoad();
- // let main = document.getElementById('main');
- // main.style.display = "none"
- // let mainMobile = document.getElementById('main-mobile');
- // mainMobile.style.display = "none"
- // let showDwgBtn = document.getElementById('showDwgBtn');
- // let showFontBtn = document.getElementById('showFontBtn');
- // showDwgBtn.style.display = "unset"
- // showFontBtn.style.display = "unset"
- // })
- };
- ZwCloud2D.ZwDataProcessor.uploadFont = (file) => {
- let req = new Request();
- let pArray = new Array();
- for (let i = 0; i < file.length; i++) {
- pArray.push(
- new Promise((resolve, reject) => {
- req.uploadFile(file[i]).then((res) => {
- if (res.code === 200) {
- resolve();
- } else {
- reject();
- }
- });
- }),
- );
- }
- Promise.all(pArray)
- .then(() => {
- alert('上传成功');
- })
- .catch(() => {
- alert('上传失败');
- });
- };
- ZwCloud2D.ZwDataProcessor.ZwLoad = () => {
- let docId = new Date().getTime().toString();
- let req = new Request();
- // 切换布局时候触发回调
- ZwCloud2D.ZwMessageCallback.ZwEvtChangeLayout = (layout) => {
- req
- .getDisplayData(layout.handleId)
- .then((data) => {
- let timestamp = new Date().getTime();
- ZwCloud2D.ZwDataManager.ZwSetDwgData(layout.handleId, data, timestamp);
- })
- .catch((error) => {
- console.error(error);
- });
- };
- ZwCloud2D.ZwMessageCallback.ZwEvtLoadXrefData = (data) => {
- req
- .getXrefData(data.handleId, data.xrefIds)
- .then((res) => {
- ZwCloud2D.ZwDataManager.ZwSetXrefData(res);
- })
- .catch(() => {
- ZwCloud2D.ZwDataManager.ZwSetXrefData();
- });
- };
- ZwCloud2D.ZwMessageCallback.ZwEvtDrawEnd = () => {};
- ZwCloud2D.ZwMessageCallback.ZwEvtGetRegenData = (data) => {
- req
- .getRegenData(data)
- .then((res) => {
- ZwCloud2D.ZwDataManager.ZwSetRegenData(res);
- })
- .catch(() => {
- ZwCloud2D.ZwDataManager.ZwSetRegenData(null);
- });
- };
- ZwCloud2D.ZwMessageCallback.ZwEvtLoadImageData = (data) => {
- let pArr = new Array();
- let imageMap = new Map();
- data.forEach((imageUrl) => {
- pArr.push(
- new Promise((res, rej) => {
- req
- .getImageData(imageUrl)
- .then((result) => {
- imageMap.set(imageUrl, result);
- res(true);
- })
- .catch((err) => {
- res(true);
- });
- }),
- );
- });
- Promise.all(pArr)
- .then(() => {
- ZwCloud2D.ZwDataManager.ZwSetImageData(imageMap);
- })
- .catch((error) => {
- console.error(error);
- });
- };
- ZwCloud2D.ZwMessageCallback.ZwEvtLoadImageList = () => {
- req.getImageList().then((res) => {
- res.forEach((image) => {
- let path = image.completePath + image.name;
- req.getImageSrc(path).then((imageData) => {
- let item = {
- name: image.name,
- data: imageData,
- completePath: image.completePath,
- };
- ZwCloud2D.ZwDataManager.ZwSetImageList(item);
- });
- });
- });
- };
- ZwCloud2D.ZwMessageCallback.ZwEvtPlotDwg = (data) => {
- let params = Object.assign(data.params, { dwgPath: dwgPath });
- req
- .getPlotData(params)
- .then((res) => {
- ZwCloud2D.ZwDataManager.ZwSetPlotData(data, res);
- ZwCloud2D.ZwEditor.ZwSetLoadingState(false);
- })
- .catch((error) => {
- console.error(error);
- });
- };
- ZwCloud2D.ZwEditor.ZwSetUserOptions({ panel: { commentPanel: false } });
- req
- .getPermissionList()
- .then((data) => {
- ZwCloud2D.ZwDataManager.ZwSetSdkPermission(data);
- })
- .catch((error) => {
- console.error(error);
- });
- if (!dwgPath) return;
- req
- .transferDoc()
- .then((data) => {
- // 显示当前布局的数据
- let layouts = [];
- data.layouts.forEach((layout) => {
- let res = {
- handleId: layout.handleId,
- isDefault: layout.isDefault === 1 ? 'YES' : 'NO',
- name: layout.layoutName,
- tabOrder: layout.order,
- };
- layouts.push(res);
- });
- ZwCloud2D.ZwDataManager.ZwSetDwgInfo({
- id: docId,
- fonts: data.fontList,
- layouts: layouts,
- name: data.name,
- }).then((res) => {
- let pArr = [];
- let fontArr = [];
- res.missingFonts.forEach((fontName) => {
- let arr = fontName.split('.');
- let type = (arr[arr.length - 1] || '').toUpperCase();
- pArr.push(
- req.fontDownload(fontName).then(async (data) => {
- if (data.type === 'application/octet-stream') {
- let arraybuffer = await data.arrayBuffer();
- fontArr.push({
- name: fontName,
- type: type,
- data: arraybuffer,
- });
- }
- }),
- );
- });
- Promise.all(pArr)
- .then(() => {
- ZwCloud2D.ZwDataManager.ZwSetFontDataList(fontArr).then(() => {
- req
- .getDisplayData(res.layout.handleId)
- .then((lmfInfo) => {
- if (res.timestamp === '')
- res.timestamp = new Date().getTime();
- ZwCloud2D.ZwDataManager.ZwSetDwgData(
- res.layout.handleId,
- lmfInfo,
- res.timestamp,
- );
- })
- .catch((error) => {
- console.error(error);
- });
- req
- .getXrefRelation()
- .then((xrefRelations) => {
- let xrefPromises = [];
- let srcMap = new Map();
- xrefRelations.forEach((xrefRelation) => {
- if (xrefRelation.foundPath) {
- xrefPromises.push(
- new Promise((res, rej) => {
- req
- .getXrefThumb(xrefRelation.foundPath)
- .then((xrefThumb) => {
- srcMap.set(xrefRelation.foundPath, xrefThumb);
- res(true);
- });
- }),
- );
- }
- });
- Promise.all(xrefPromises)
- .then(() => {
- let params = {
- docName: data.name,
- xrefRelations: xrefRelations,
- srcMap: srcMap,
- };
- ZwCloud2D.ZwDataManager.ZwSetXrefList(params);
- })
- .catch(() => {
- let params = {
- docName: data.name,
- xrefRelations: xrefRelations,
- srcMap: srcMap,
- };
- ZwCloud2D.ZwDataManager.ZwSetXrefList(params);
- });
- })
- .catch((error) => {
- console.error(error);
- });
- if (wsUrl) {
- let url =
- wsUrl +
- '/CadService?sub=' +
- Math.floor(Math.random() * 1000) +
- '&docId=' +
- docId +
- '&dwgPath=' +
- dwgPath;
- ZwCloud2D.ZwDataManager.ZwEntryEdit(url);
- }
- });
- })
- .catch((error) => {
- console.error(error);
- });
- });
- })
- .catch((error) => {
- ZwCloud2D.ZwEditor.ZwSetLoadingState(false);
- alert(error);
- let main = document.getElementById('main');
- main.style.display = 'unset';
- let mainMobile = document.getElementById('main-mobile');
- mainMobile.style.display = 'unset';
- let showDwgBtn = document.getElementById('showDwgBtn');
- let showFontBtn = document.getElementById('showFontBtn');
- showDwgBtn.style.display = 'none';
- showFontBtn.style.display = 'none';
- resizeCheck();
- window.addEventListener('resize', resizeCheck);
- console.error(error);
- });
- };
|