282 lines
7.2 KiB
TypeScript
Raw Normal View History

2025-02-15 03:56:34 +08:00
import { message } from "@/utils/message";
import { type Ref, ref, reactive } from "vue";
import type { PaginationProps } from "@pureadmin/table";
import type { QueryCodeResult, QueryResult, QueryResultItem } from "types/code";
import {
ElMessageBox,
type UploadProps,
type UploadUserFile
} from "element-plus";
import {
getQueryCodeAPI,
getQueryTemplateAPI,
postCodeInfoAPI
} from "@/api/code";
import { deleteFileAPI, postUploadFileAPI } from "@/api/file";
export const useIndex = (tableRef: Ref) => {
/**
*
*/
const form = reactive({
query_text: ""
});
/**
* Ref
*/
// const formRef = ref(null);
/**
*
*/
const dataList = ref<QueryResult[]>([]);
/**
*
*/
const loading = ref(false);
/**
*
*/
const selectedNum = ref<number>(0);
/**
*
*/
const pagination = reactive<PaginationProps>({
total: 0,
pageSize: 10,
currentPage: 1,
background: true,
pageSizes: [10, 20, 30, 40, 50]
});
// 上传文件区域显示状态
const showUploadArea = ref(false);
const fileList = ref<UploadUserFile[]>([]);
/**上传成功后文件列表 */
const fileIds = ref([]);
const fileId = ref<string>("");
/**上传按钮状态 */
const uploadStatus = ref<boolean>(false);
/**
*
*/
const queryResult = ref<QueryCodeResult>();
2025-02-17 16:26:57 +08:00
const activeCollapse = ref(["baseInfo"]);
2025-02-15 03:56:34 +08:00
/**列表值 */
2025-02-17 16:26:57 +08:00
const rowInfo = reactive<QueryResult>({
query_text: "",
status: 0,
2025-02-15 03:56:34 +08:00
id: "",
2025-02-17 16:26:57 +08:00
result_text: []
2025-02-15 03:56:34 +08:00
});
/**抽屉状态 */
const drawerStatus = ref<boolean>(false);
/**
*
*/
const columns: TableColumnList = [
{
label: "勾选列", // 如果需要表格多选此处label必须设置
type: "selection",
fixed: "left",
reserveSelection: true // 数据刷新后保留选项
},
{
label: "查询ID",
prop: "id"
},
{
label: "查询文本",
prop: "query_text"
},
{
label: "操作",
fixed: "right",
width: 250,
slot: "operation"
}
];
/**
*
*/
const onSearch = async () => {
loading.value = true;
const res = await postCodeInfoAPI({
query_text: form.query_text
});
if (res.success) {
queryResult.value = res.data;
dataList.value = res.data.response_result;
pagination.total = res.data.result_count;
}
message(res.msg, {
type: res.success ? "success" : "error"
});
loading.value = false;
};
/**
*
* @param formEl ref
* @returns
*/
const resetForm = (formEl: any) => {
if (!formEl) return;
formEl.resetFields();
onSearch();
};
/** 当CheckBox选择项发生变化时会触发该事件 */
const handleSelectionChange = async (val: any) => {
selectedNum.value = val.length;
// 重置表格高度
tableRef.value.setAdaptive();
};
/** 取消选择 */
const onSelectionCancel = async () => {
selectedNum.value = 0;
// 用于多选表格,清空用户的选择
tableRef.value.getTableRef().clearSelection();
};
/**下载模版文件 */
const onDownloadTemplate = async () => {
try {
const blob = await getQueryTemplateAPI();
// 生成下载链接
// @ts-ignore
const url = URL.createObjectURL(blob);
// 创建 <a> 元素并触发下载
const link = document.createElement("a");
link.href = url;
link.download = "上传模版.xlsx"; // 设置下载文件名,确保后缀名正确
document.body.appendChild(link); // 将 <a> 元素添加到 DOM 中
link.click(); // 模拟点击下载
// 清理 URL 对象
URL.revokeObjectURL(url);
document.body.removeChild(link); // 移除 <a> 元素
} catch (error) {
console.error("下载模板失败:", error);
}
};
/** 上传文件前 */
const beforeUpload = async file => {
const isExcel =
file.type ===
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" || // xlsx
file.type === "application/vnd.ms-excel" || // xls
file.name.endsWith(".xlsx") || // 兼容部分浏览器
file.name.endsWith(".xls"); // 兼容部分浏览器
// const maxSize = 20 * 1024 * 1024; // 20MB 限制
if (!isExcel) {
message("只能上传 xlsx 或 xls 文件!", { type: "error" });
return false;
}
/*
if (file.size > maxSize) {
message("文件大小应在 20MB 以内!", { type: "error" });
return false;
}
*/
return true;
};
/**处理文件上传 */
const handleUpload = async () => {
if (fileList.value.length === 0) {
message("请先上传文件!", { type: "error", duration: 5000 });
return;
}
uploadStatus.value = true;
for (const file of fileList.value) {
if (file.status === "success") {
const data = await getQueryCodeAPI(fileId.value);
if (data.success) {
message("查询成功!", { type: "success" });
queryResult.value = data.data;
dataList.value = data.data.response_result;
pagination.total = data.data.result_count;
}
continue;
}
try {
const res = await postUploadFileAPI({
file: file.raw
});
if (res.success) {
file.status = "success";
fileId.value = res.data.id;
message(`${res.data.name}上传成功!`, { type: "success" });
fileIds.value.push(res.data);
const data = await getQueryCodeAPI(fileId.value);
if (data.success) {
message(`查询成功!`, { type: "success" });
queryResult.value = data.data;
dataList.value = data.data.response_result;
pagination.total = data.data.result_count;
}
} else {
file.status = "fail";
}
} catch (error) {
console.error(error);
}
}
uploadStatus.value = false;
};
/**移除文件 */
const beforeRemove: UploadProps["beforeRemove"] = async uploadFile => {
return ElMessageBox.confirm(`是否移除 ${uploadFile.name} ?`).then(
async () => {
if (uploadFile.status === "success") {
const fileId = fileIds.value.filter(
item => item.filename === uploadFile.name
)[0]["fileId"];
const res = await deleteFileAPI(fileId);
if (res.code === 200) {
message(res.msg, { type: "success" });
return true;
} else {
message(res.msg, { type: "error" });
return false;
}
} else {
return true;
}
},
() => false
);
};
const handleDetail = (row: QueryResultItem) => {
drawerStatus.value = true;
Object.assign(rowInfo, row);
};
return {
form,
dataList,
loading,
pagination,
columns,
queryResult,
selectedNum,
showUploadArea,
fileIds,
fileList,
uploadStatus,
rowInfo,
drawerStatus,
2025-02-17 16:26:57 +08:00
activeCollapse,
2025-02-15 03:56:34 +08:00
beforeUpload,
handleUpload,
beforeRemove,
onSearch,
resetForm,
handleSelectionChange,
onSelectionCancel,
onDownloadTemplate,
handleDetail
};
};