From d3be4d813f6745f245d36a07ea705b4db169d273 Mon Sep 17 00:00:00 2001 From: 田源 <lastanimals@163.com> Date: 星期三, 06 十二月 2023 17:21:28 +0800 Subject: [PATCH] 后端返回数据流格式,前端无法捕获到错误信息 --- Source/UBCS-WEB/src/components/Master/MasterTransfer.vue | 52 ++++++++++------- Source/UBCS-WEB/src/components/MasterCrud/VciMasterCrud.vue | 32 ++++++---- Source/UBCS-WEB/src/util/func.js | 44 +++++++------- 3 files changed, 72 insertions(+), 56 deletions(-) diff --git a/Source/UBCS-WEB/src/components/Master/MasterTransfer.vue b/Source/UBCS-WEB/src/components/Master/MasterTransfer.vue index 5ffff04..6c7c8d5 100644 --- a/Source/UBCS-WEB/src/components/Master/MasterTransfer.vue +++ b/Source/UBCS-WEB/src/components/Master/MasterTransfer.vue @@ -120,6 +120,22 @@ mounted() { }, methods: { + handlerFile(res){ + let reader = new FileReader(); + reader.readAsText(res.data); + reader.onload = (result) =>{ + try { + let resData = JSON.parse(result.target.result); // 瑙f瀽瀵硅薄鎴愬姛 + if (resData.code != 200) { + this.$message.error(resData.msg); + } + } catch (err) { + // console.log("err",err)// 瑙f瀽鎴愬璞″け璐ワ紝璇存槑鏄甯哥殑鏂囦欢娴� + func.downloadFileByBlobHandler(res); + this.$message.success('涓嬭浇鎴愬姛锛岃鏌ョ湅锛�'); + } + }; + }, escHandler() { this.$emit('update:visible', false); this.value = []; @@ -157,10 +173,8 @@ 'conditionMap[oid]': this.ids, ...this.exportArrTwo, limit: -1 }).then(res => { - if (res) { - func.downloadFileByBlobHandler(res); - this.escHandler() - } + this.handlerFile(res); + this.escHandler(); }) } else { @@ -169,10 +183,9 @@ 'conditionMap[oid]': this.ids, ...this.exportArr }).then(res => { // console.log('res',res) - if (res) { - func.downloadFileByBlobHandler(res); - this.escHandler() - } + this.handlerFile(res); + this.escHandler(); + }) } @@ -186,15 +199,13 @@ exportCode({codeClassifyOid: this.codeClassifyOid, ...this.exportArrTwo, limit: -1}).then(res => { if (res) { func.downloadFileByBlobHandler(res); - this.escHandler() + this.escHandler(); } }) } else { exportCode({codeClassifyOid: this.codeClassifyOid, ...this.exportArr, limit: -1}).then(res => { - if (res) { - func.downloadFileByBlobHandler(res); - this.escHandler() - } + this.handlerFile(res); + this.escHandler(); }) } } else if (this.radio === 2) { @@ -230,10 +241,9 @@ endPage: end }).then((res) => { // console.log('res',res) - if (res) { - func.downloadFileByBlobHandler(res); - this.escHandler() - } + this.handlerFile(res); + this.escHandler(); + }); } else { this.tableHeadData.map(item => item.prop) @@ -246,11 +256,9 @@ page: start, endPage: end }).then(res => { - // console.log('res',res) - if (res) { - func.downloadFileByBlobHandler(res); - this.escHandler() - } + console.log('res',res) + this.handlerFile(res); + this.escHandler(); }) } } diff --git a/Source/UBCS-WEB/src/components/MasterCrud/VciMasterCrud.vue b/Source/UBCS-WEB/src/components/MasterCrud/VciMasterCrud.vue index b164fb7..80377c8 100644 --- a/Source/UBCS-WEB/src/components/MasterCrud/VciMasterCrud.vue +++ b/Source/UBCS-WEB/src/components/MasterCrud/VciMasterCrud.vue @@ -578,19 +578,25 @@ exportGroupCodeExcel({ codeClassifyOid: this.codeClassifyOid, }).then(res => { - console.log(res) - if (res) { - func.downloadFileByBlobHandler(res); - this.$message.success('涓嬭浇鎴愬姛锛岃鏌ョ湅锛�'); - this.isLoading = false; - } - }).catch(error => { - console.log(error) - if (error) { - this.$message.error('璇锋煡鐪嬩笅杞界殑閿欒鏂囦欢锛�'); - } - this.isLoading = false; - }) + let reader = new FileReader(); + reader.readAsText(res.data) + reader.onload = (result) =>{ + try { + let resData = JSON.parse(result.target.result); // 瑙f瀽瀵硅薄鎴愬姛 + if (!resData.success) { + console.log("resData",resData) + this.$message.error(resData.msg); + this.isLoading = false; + } + } catch (err) { + // console.log("err",err)// 瑙f瀽鎴愬璞″け璐ワ紝璇存槑鏄甯哥殑鏂囦欢娴� + func.downloadFileByBlobHandler(res); + this.$message.success('涓嬭浇鎴愬姛锛岃鏌ョ湅锛�'); + this.isLoading = false; + } + }; + + }); }, //鏍囧噯鐢宠 codeApplyHandler() { diff --git a/Source/UBCS-WEB/src/util/func.js b/Source/UBCS-WEB/src/util/func.js index 78bd256..9d6d017 100644 --- a/Source/UBCS-WEB/src/util/func.js +++ b/Source/UBCS-WEB/src/util/func.js @@ -75,8 +75,8 @@ /** * ts鏃ユ湡鏍煎紡澶勭悊 - * @param {瑕佸鐞嗙殑鏃ユ湡} thisDate - * @returns + * @param {瑕佸鐞嗙殑鏃ユ湡} thisDate + * @returns */ static formattedDateTime(thisDate) { const date = new Date(thisDate); @@ -133,20 +133,22 @@ * @returns {Boolean} */ static downloadFileByBlobHandler(res) { - const fileNames = res.headers['content-disposition'].split(";"); - let characterSet = fileNames[2].split("filename*=")[1]; - let fileName = decodeURI(fileNames[1].split("filename=")[1],characterSet); // 涓嬭浇鐨勬枃浠跺悕 - let blob = new Blob([res.data], { - type: res.data.type + ";charset=utf-8", - }); - const url = URL.createObjectURL(blob); // 鐢熸垚涓嬭浇閾炬帴 - const link = document.createElement("a"); - link.href = url; - link.download = fileName; // 鎸囧畾鏂囦欢鍚嶅拰鏍煎紡 - document.body.appendChild(link); // 娣诲姞涓嬭浇閾炬帴鍒伴〉闈� - link.click(); // 瑙﹀彂涓嬭浇 - document.body.removeChild(link); //鍒犻櫎涓嬭浇 - window.URL.revokeObjectURL(url); //閲婃斁鎺塨lob瀵硅薄 + if(res.headers['content-disposition']){ + const fileNames = res.headers['content-disposition'].split(";"); + let characterSet = fileNames[2].split("filename*=")[1]; + let fileName = decodeURI(fileNames[1].split("filename=")[1],characterSet); // 涓嬭浇鐨勬枃浠跺悕 + let blob = new Blob([res.data], { + type: res.data.type + ";charset=utf-8", + }); + const url = URL.createObjectURL(blob); // 鐢熸垚涓嬭浇閾炬帴 + const link = document.createElement("a"); + link.href = url; + link.download = fileName; // 鎸囧畾鏂囦欢鍚嶅拰鏍煎紡 + document.body.appendChild(link); // 娣诲姞涓嬭浇閾炬帴鍒伴〉闈� + link.click(); // 瑙﹀彂涓嬭浇 + document.body.removeChild(link); //鍒犻櫎涓嬭浇 + window.URL.revokeObjectURL(url); //閲婃斁鎺塨lob瀵硅薄 + } } /** @@ -166,9 +168,9 @@ /** * 灏嗘暟缁勪腑鐨勫睘鎬ф嫾鎺ユ垚瀛楃涓� - * @param {鏁扮粍} array - * @param {灞炴� attr - * @returns + * @param {鏁扮粍} array + * @param {灞炴� attr + * @returns */ static joinedString(array,attr) { return array.reduce((accumulator, item) => { @@ -178,8 +180,8 @@ /** * 姣斿鏁扮粍涓殑瀵硅薄鐨勬煇涓睘鎬ф槸鍚﹀叏閮ㄤ竴鑷� - * @param {鏁扮粍} array - * @param {灞炴� attr + * @param {鏁扮粍} array + * @param {灞炴� attr * @returns 涓�鑷磋繑鍥瀎asle,涓嶄竴鑷磋繑鍥瀟rue */ static isAttrInconsistent(array,attr) { -- Gitblit v1.9.3