添加功能下载验收单
This commit is contained in:
@@ -102,6 +102,7 @@
|
||||
<el-button type="warning" link @click="editDelivery(scope.row)" v-hasPermi="['entry:edit']">编辑</el-button>
|
||||
<el-button type="success" link @click="updateStatus(scope.row)" v-hasPermi="['entry:status']">修改状态</el-button>
|
||||
<el-button type="info" link @click="downloadPackage(scope.row)" :loading="downLoading[scope.row.id]" v-hasPermi="['entry:download']">打包文件</el-button>
|
||||
<el-button type="primary" link @click="downloadAcceptanceFormHandler(scope.row)" :loading="downLoading[scope.row.id]" v-hasPermi="['entry:export']">下载验收单</el-button>
|
||||
<el-button type="danger" link @click="deleteDelivery(scope.row)" v-hasPermi="['entry:delete']">删除</el-button>
|
||||
</div>
|
||||
</template>
|
||||
@@ -125,7 +126,7 @@ import { ElMessage, ElMessageBox } from 'element-plus';
|
||||
import baseSearch from '@/components/common/searchCustom/index.vue';
|
||||
import createDeliveryDialog from '@/views/shipping/createDeliveryDialog.vue';
|
||||
import { inspectionList, downloadZip, pageDeviceList } from '@/api/abroad.js';
|
||||
import { updateDeliveryStatus, deleteDeliveryLogic, downloadDeliveryPackage, getDeliveryDetail } from '@/api/shipping.js';
|
||||
import { updateDeliveryStatus, deleteDeliveryLogic, downloadDeliveryPackage, getDeliveryDetail, downloadAcceptanceForm } from '@/api/shipping.js';
|
||||
|
||||
const router = useRouter();
|
||||
const route = useRoute();
|
||||
@@ -679,10 +680,52 @@ const downloadPackage = async (row) => {
|
||||
ElMessage.info('正在打包文件,请稍候...');
|
||||
|
||||
// 调用后端API打包文件(包含图片、视频和信息)
|
||||
// 注意:由于响应拦截器设置了 responseType: 'blob',返回的是 Blob 对象
|
||||
const res = await downloadDeliveryPackage(row.id);
|
||||
|
||||
// 创建下载链接
|
||||
const blob = new Blob([res], { type: 'application/zip' });
|
||||
// 确保 res 是 Blob 对象,如果不是则转换为 Blob
|
||||
let blob;
|
||||
if (res instanceof Blob) {
|
||||
blob = res;
|
||||
} else {
|
||||
// 如果不是 Blob,尝试转换为 Blob
|
||||
blob = new Blob([res], { type: 'application/zip' });
|
||||
}
|
||||
|
||||
// 检查响应是否是有效的ZIP文件(通过检查ZIP文件魔数)
|
||||
// ZIP 文件的魔数是:50 4B 03 04 (PK\x03\x04)
|
||||
const arrayBuffer = await blob.arrayBuffer();
|
||||
const uint8Array = new Uint8Array(arrayBuffer);
|
||||
|
||||
// 检查是否是ZIP文件(前4个字节是 50 4B 03 04)
|
||||
const isZipFile = uint8Array.length >= 4 &&
|
||||
uint8Array[0] === 0x50 &&
|
||||
uint8Array[1] === 0x4B &&
|
||||
uint8Array[2] === 0x03 &&
|
||||
uint8Array[3] === 0x04;
|
||||
|
||||
if (!isZipFile) {
|
||||
// 响应是错误信息(JSON或文本),尝试读取并显示错误
|
||||
try {
|
||||
const decoder = new TextDecoder('utf-8');
|
||||
const errorText = decoder.decode(uint8Array);
|
||||
let errorMsg = '打包文件失败';
|
||||
try {
|
||||
// 尝试解析为 JSON
|
||||
const errorObj = JSON.parse(errorText);
|
||||
errorMsg = errorObj.msg || errorObj.message || errorText;
|
||||
} catch (e) {
|
||||
// 不是 JSON,直接使用文本
|
||||
errorMsg = errorText || '打包文件失败,请重试';
|
||||
}
|
||||
ElMessage.error(errorMsg);
|
||||
} catch (e) {
|
||||
ElMessage.error('打包文件失败,请重试');
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// 创建下载链接(使用验证后的 Blob)
|
||||
const url = window.URL.createObjectURL(blob);
|
||||
const link = document.createElement('a');
|
||||
link.href = url;
|
||||
@@ -695,7 +738,69 @@ const downloadPackage = async (row) => {
|
||||
ElMessage.success('文件打包成功');
|
||||
} catch (error) {
|
||||
console.error('打包文件失败:', error);
|
||||
ElMessage.error('打包文件失败,请重试');
|
||||
ElMessage.error(error.message || '打包文件失败,请重试');
|
||||
} finally {
|
||||
downLoading[row.id] = false;
|
||||
}
|
||||
};
|
||||
|
||||
// 下载验收单
|
||||
const downloadAcceptanceFormHandler = async (row) => {
|
||||
try {
|
||||
downLoading[row.id] = true;
|
||||
ElMessage.info('正在生成验收单,请稍候...');
|
||||
|
||||
// 调用后端API生成验收单
|
||||
const res = await downloadAcceptanceForm(row.id);
|
||||
|
||||
// 确保 res 是 Blob 对象
|
||||
let blob;
|
||||
if (res instanceof Blob) {
|
||||
blob = res;
|
||||
} else {
|
||||
blob = new Blob([res], { type: 'text/html;charset=UTF-8' });
|
||||
}
|
||||
|
||||
// 检查是否是HTML文件(通过检查内容)
|
||||
const arrayBuffer = await blob.arrayBuffer();
|
||||
const uint8Array = new Uint8Array(arrayBuffer);
|
||||
|
||||
// 尝试解码为文本,检查是否是HTML
|
||||
const decoder = new TextDecoder('utf-8');
|
||||
const htmlText = decoder.decode(uint8Array);
|
||||
|
||||
// 检查是否是错误响应(JSON格式)
|
||||
if (htmlText.trim().startsWith('{') || htmlText.trim().startsWith('[')) {
|
||||
try {
|
||||
const errorObj = JSON.parse(htmlText);
|
||||
ElMessage.error(errorObj.msg || errorObj.message || '生成验收单失败,请重试');
|
||||
} catch (e) {
|
||||
ElMessage.error('生成验收单失败,请重试');
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// 创建下载链接
|
||||
const url = window.URL.createObjectURL(blob);
|
||||
const link = document.createElement('a');
|
||||
link.href = url;
|
||||
link.download = `牛只发车验收单_${row.deliveryNumber || row.id}_${new Date().getTime()}.html`;
|
||||
document.body.appendChild(link);
|
||||
link.click();
|
||||
document.body.removeChild(link);
|
||||
window.URL.revokeObjectURL(url);
|
||||
|
||||
// 同时在新窗口中打开,方便预览
|
||||
const newWindow = window.open('', '_blank');
|
||||
if (newWindow) {
|
||||
newWindow.document.write(htmlText);
|
||||
newWindow.document.close();
|
||||
}
|
||||
|
||||
ElMessage.success('验收单生成成功');
|
||||
} catch (error) {
|
||||
console.error('生成验收单失败:', error);
|
||||
ElMessage.error(error.message || '生成验收单失败,请重试');
|
||||
} finally {
|
||||
downLoading[row.id] = false;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user