|
|
@@ -704,6 +704,7 @@
|
|
|
|
|
|
// 在initTable()函数之后添加以下代码
|
|
|
|
|
|
+ // 导出数据功能实现
|
|
|
// 导出数据功能实现
|
|
|
function exportData() {
|
|
|
// 显示加载中提示
|
|
|
@@ -714,44 +715,122 @@
|
|
|
// 获取查询条件
|
|
|
const jxsCode = $.trim($("#jxsCode").val());
|
|
|
const jxsName = $.trim($("#jxsName").val());
|
|
|
+ const status = $("#status").val(); // 假设你有一个ID为status的查询条件
|
|
|
|
|
|
// 构建查询参数
|
|
|
const params = {
|
|
|
- jxsCode: jxsCode,
|
|
|
- jxsName: jxsName
|
|
|
+ jxsName: jxsName,
|
|
|
+ code: jxsCode, // 注意:后端代码里用的是 code, 而不是 jxsCode
|
|
|
+ status: status
|
|
|
};
|
|
|
|
|
|
- // 发送导出请求
|
|
|
- $.ajax({
|
|
|
- url: '${ctx}/jxsNew/exportJxsList',
|
|
|
- type: 'POST',
|
|
|
- data: JSON.stringify(params),
|
|
|
- contentType: 'application/json',
|
|
|
- success: function (res) {
|
|
|
- // 关闭加载提示
|
|
|
- layer.close(loadingIndex);
|
|
|
+ // 使用 fetch API 发送请求,因为它可以处理二进制文件流
|
|
|
+ fetch('${ctx}/jxsNew/exportJxsList', {
|
|
|
+ method: 'POST',
|
|
|
+ headers: {
|
|
|
+ 'Content-Type': 'application/json'
|
|
|
+ },
|
|
|
+ body: JSON.stringify(params)
|
|
|
+ })
|
|
|
+ .then(response => {
|
|
|
+ // 首先检查响应是否成功
|
|
|
+ if (!response.ok) {
|
|
|
+ // 如果服务器返回了错误(例如,没有数据),它会是JSON格式
|
|
|
+ // 我们需要解析这个JSON来获取错误信息
|
|
|
+ return response.json().then(err => {
|
|
|
+ throw new Error(err.msg || '导出失败');
|
|
|
+ });
|
|
|
+ }
|
|
|
|
|
|
- if (res.code === 0) {
|
|
|
- // 获取导出URL并打开
|
|
|
- let exportUrl = res.data;
|
|
|
- if (exportUrl) {
|
|
|
- // 使用新窗口打开URL进行下载
|
|
|
- window.open(exportUrl, '_blank');
|
|
|
- layer.msg('导出成功,请在新窗口中查看下载文件', {icon: 1});
|
|
|
- } else {
|
|
|
- layer.msg('未获取到有效的导出URL', {icon: 5});
|
|
|
+ // 从响应头中尝试获取文件名
|
|
|
+ const disposition = response.headers.get('Content-Disposition');
|
|
|
+ let fileName = '经销商列表.xlsx'; // 默认文件名
|
|
|
+ if (disposition && disposition.indexOf('attachment') !== -1) {
|
|
|
+ const filenameRegex = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/;
|
|
|
+ const matches = filenameRegex.exec(disposition);
|
|
|
+ if (matches != null && matches[1]) {
|
|
|
+ fileName = decodeURI(matches[1].replace(/['"]/g, ''));
|
|
|
}
|
|
|
- } else {
|
|
|
- layer.msg(res.msg || '导出失败', {icon: 5});
|
|
|
}
|
|
|
- },
|
|
|
- error: function () {
|
|
|
- // 关闭加载提示
|
|
|
+
|
|
|
+ // 将响应体转换为Blob对象(二进制文件)
|
|
|
+ return response.blob().then(blob => ({ blob, fileName }));
|
|
|
+ })
|
|
|
+ .then(({ blob, fileName }) => {
|
|
|
+ // 创建一个指向Blob的URL
|
|
|
+ const url = window.URL.createObjectURL(blob);
|
|
|
+
|
|
|
+ // 创建一个隐藏的<a>标签用于触发下载
|
|
|
+ const a = document.createElement('a');
|
|
|
+ a.style.display = 'none';
|
|
|
+ a.href = url;
|
|
|
+ a.download = fileName; // 设置下载的文件名
|
|
|
+
|
|
|
+ // 将<a>标签添加到页面中,模拟点击,然后移除
|
|
|
+ document.body.appendChild(a);
|
|
|
+ a.click();
|
|
|
+
|
|
|
+ // 清理
|
|
|
+ window.URL.revokeObjectURL(url);
|
|
|
+ document.body.removeChild(a);
|
|
|
+
|
|
|
+ // 关闭加载提示并显示成功信息
|
|
|
layer.close(loadingIndex);
|
|
|
- layer.msg('导出请求失败', {icon: 5});
|
|
|
- }
|
|
|
- });
|
|
|
+ layer.msg('导出成功,请检查你的下载内容', { icon: 1 });
|
|
|
+ })
|
|
|
+ .catch(error => {
|
|
|
+ // 捕获任何在过程中发生的错误
|
|
|
+ layer.close(loadingIndex);
|
|
|
+ layer.msg(error.message || '导出请求失败', { icon: 5 });
|
|
|
+ });
|
|
|
}
|
|
|
+ <%--function exportData() {--%>
|
|
|
+ <%-- // 显示加载中提示--%>
|
|
|
+ <%-- var loadingIndex = layer.load(1, {--%>
|
|
|
+ <%-- shade: [0.2, '#000']--%>
|
|
|
+ <%-- });--%>
|
|
|
+
|
|
|
+ <%-- // 获取查询条件--%>
|
|
|
+ <%-- const jxsCode = $.trim($("#jxsCode").val());--%>
|
|
|
+ <%-- const jxsName = $.trim($("#jxsName").val());--%>
|
|
|
+
|
|
|
+ <%-- // 构建查询参数--%>
|
|
|
+ <%-- const params = {--%>
|
|
|
+ <%-- jxsCode: jxsCode,--%>
|
|
|
+ <%-- jxsName: jxsName--%>
|
|
|
+ <%-- };--%>
|
|
|
+
|
|
|
+ <%-- // 发送导出请求--%>
|
|
|
+ <%-- $.ajax({--%>
|
|
|
+ <%-- url: '${ctx}/jxsNew/exportJxsList',--%>
|
|
|
+ <%-- type: 'POST',--%>
|
|
|
+ <%-- data: JSON.stringify(params),--%>
|
|
|
+ <%-- contentType: 'application/json',--%>
|
|
|
+ <%-- success: function (res) {--%>
|
|
|
+ <%-- // 关闭加载提示--%>
|
|
|
+ <%-- layer.close(loadingIndex);--%>
|
|
|
+
|
|
|
+ <%-- if (res.code === 0) {--%>
|
|
|
+ <%-- // 获取导出URL并打开--%>
|
|
|
+ <%-- let exportUrl = res.data;--%>
|
|
|
+ <%-- if (exportUrl) {--%>
|
|
|
+ <%-- // 使用新窗口打开URL进行下载--%>
|
|
|
+ <%-- window.open(exportUrl, '_blank');--%>
|
|
|
+ <%-- layer.msg('导出成功,请在新窗口中查看下载文件', {icon: 1});--%>
|
|
|
+ <%-- } else {--%>
|
|
|
+ <%-- layer.msg('未获取到有效的导出URL', {icon: 5});--%>
|
|
|
+ <%-- }--%>
|
|
|
+ <%-- } else {--%>
|
|
|
+ <%-- layer.msg(res.msg || '导出失败', {icon: 5});--%>
|
|
|
+ <%-- }--%>
|
|
|
+ <%-- },--%>
|
|
|
+ <%-- error: function () {--%>
|
|
|
+ <%-- // 关闭加载提示--%>
|
|
|
+ <%-- layer.close(loadingIndex);--%>
|
|
|
+ <%-- layer.msg('导出请求失败', {icon: 5});--%>
|
|
|
+ <%-- }--%>
|
|
|
+ <%-- });--%>
|
|
|
+ <%--}--%>
|
|
|
|
|
|
// 导入功能实现
|
|
|
function importData() {
|
|
|
@@ -762,7 +841,7 @@
|
|
|
<i class="fa fa-cloud-upload" style="font-size: 50px; color: #ccc;"></i>
|
|
|
</div>
|
|
|
<p style="margin-bottom: 10px;">将文件拖到此处,或 <span style="color: #0066cc; cursor: pointer;" id="clickUpload">点击上传</span></p>
|
|
|
- <p style="font-size: 12px; color: #999;">仅允许导入xls、xlsx格式文件。<a href="https://hyscancode.oss-cn-hangzhou.aliyuncs.com/jinzaiimport/%E7%BB%8F%E9%94%80%E5%95%86%E5%AF%BC%E5%85%A5%E6%A8%A1%E6%9D%BF.xlsx" style="color: #0066cc;">下载模板</a></p>
|
|
|
+ <p style="font-size: 12px; color: #999;">仅允许导入xls、xlsx格式文件。<a href="${ctx}/templates/经销商导入模板.xlsx" style="color: #0066cc;">下载模板</a></p>
|
|
|
<div id="selectedFileName" style="margin-top: 15px; font-size: 12px; color: #333; display: none; justify-content: center; align-items: center;"></div>
|
|
|
<input type="file" id="fileUpload" accept=".xlsx, .xls" style="display: none;">
|
|
|
</div>
|