pms-aomei/src/views/modules/pms/blenderBatch/components/ListViewWithHead.vue
2023-08-03 10:00:31 +08:00

431 lines
13 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!-- 表格页加上搜索条件 -->
<template>
<div class="list-view-with-head" ref="pointer-loading-ref">
<!-- <head-form :form-config="headFormConfig" @headBtnClick="btnClick" /> -->
<BaseSearchForm :head-config="headConfig" @btn-click="handleBtnClick" />
<BaseListTable
v-loading="tableLoading"
:table-config="tableConfig.table"
:column-config="tableConfig.column"
:table-data="dataList"
@operate-event="handleOperate"
:current-page="page"
:current-size="size"
@select="handleTableSelect"
:refresh-layout-key="refreshLayoutKey" />
<el-pagination
v-if="navigator"
class="mt-5 flex justify-end"
@size-change="handleSizeChange"
@current-change="handlePageChange"
:current-page.sync="page"
:page-size.sync="size"
:page-sizes="[10, 20, 50, 100]"
:total="totalPage"
layout="total, sizes, prev, pager, next, jumper"></el-pagination>
<edit ref="edit" v-if="dialogVisible" :blenderOrderId="blenderOrderId" @destroy="handleDestroy('edit', $event)" />
<Overlay v-if="overlayVisible" />
</div>
</template>
<script>
import BaseListTable from "@/components/BaseListTable.vue";
import BaseSearchForm from "@/components/BaseSearchForm.vue";
import DialogJustForm from "./DialogJustForm.vue";
import edit from "./edit.vue";
import Overlay from "@/components/Overlay.vue";
import moment from "moment";
export default {
name: "ListViewWithHead",
components: {
BaseSearchForm,
BaseListTable,
DialogJustForm,
edit,
Overlay,
},
props: {
navigator: {
type: Boolean,
default: true,
},
tableConfig: {
type: Object,
default: () => ({
/** 列配置, 即 props **/ column: [],
/** 表格整体配置 */ table: undefined,
}),
},
headConfig: {
type: Object,
default: () => ({}),
},
/** 请求page接口的时候有些字段是必填的没有会报500把相关字段名传入这个prop: */
listQueryExtra: {
type: Array,
default: () => ["key"],
},
attachListQueryExtra: {
// 新增时,附带 listQueryExtra 里的哪些键和对应值
type: String,
default: "",
},
initDataWhenLoad: { type: Boolean, default: true },
/** dialog configs 或许可以从 tableConfig 计算出来 computed... */
dialogConfigs: {
type: Object,
default: () => null,
},
carPayloadDialogConfigs: {
type: Object,
default: () => null,
},
triggerUpdate: {
type: String,
default: "",
},
},
computed: {
dialogType() {
return this.dialogConfigs.menu ? DIALOG_WITH_MENU : DIALOG_JUST_FORM;
},
blenderOrderId() {
const item = this.listQueryExtra.find((item) => item.blenderOrderId);
console.log("Find blenderOrderId", item);
return item ? item.blenderOrderId : null;
},
},
activated() {
this.refreshLayoutKey = this.layoutTable();
},
watch: {
page: (val) => {
console.log("page changed:", val);
},
size: (val) => {
console.log("size changed:", val);
},
triggerUpdate(val, oldVal) {
if (val && val !== oldVal) {
// get list
this.page = 1;
this.size = "defaultPageSize" in this.tableConfig.column ? this.tableConfig.column.defaultPageSize : 20;
this.getList();
}
},
},
data() {
return {
editVisible: false,
dialogVisible: false,
topBtnConfig: null,
totalPage: 0,
page: 1,
size: 20, // 默认20
dataList: [],
tableLoading: false,
refreshLayoutKey: null,
overlayVisible: false,
cachedSearchCondition: {},
needAttachmentDialog: false,
tableSelectedIds: [],
};
},
inject: ["urls"],
mounted() {
// 更新页面默认 size
const size = "defaultPageSize" in this.tableConfig.column ? this.tableConfig.column.defaultPageSize : 20;
this.size = size;
this.initDataWhenLoad && this.getList();
},
methods: {
handleDestroy(type, refresh) {
switch (type) {
case "edit":
this.dialogVisible = false;
if (refresh) {
this.getList();
}
break;
case "detail":
this.dialogVisible = false;
break;
}
},
/** 获取 列表数据 */
getList(queryParams) {
this.tableLoading = true;
const params = queryParams
? { ...queryParams, page: this.page, limit: this.size }
: {
page: this.page,
limit: this.size,
};
if (!queryParams && this.listQueryExtra && this.listQueryExtra.length) {
this.listQueryExtra.map((nameOrObj) => {
if (typeof nameOrObj === "string") params[nameOrObj] = "";
else if (typeof nameOrObj === "object") {
Object.keys(nameOrObj).forEach((key) => {
params[key] = nameOrObj[key];
});
}
});
this.cachedSearchCondition = Object.assign({}, params);
}
this.$http[this.urls.pageIsPostApi ? "post" : "get"](
this.urls.page,
this.urls.pageIsPostApi
? {
...params,
}
: {
params,
}
)
.then(({ data: res }) => {
console.log("[http response] res is: ", res);
if (res.code === 0) {
// page 场景:
if ("list" in res.data) {
this.dataList = res.data.list;
this.totalPage = res.data.total;
} else if ("records" in res.data) {
this.dataList = res.data.records.map((item) => ({
...item,
id: item._id ?? item.id,
}));
this.totalPage = res.data.total;
} else if (Array.isArray(res.data)) {
this.dataList = res.data;
} else {
this.dataList.splice(0);
this.totalPage = 0;
}
} else {
this.$message({
message: `${res.code}: ${res.msg}`,
type: "error",
duration: 2000,
});
}
this.tableLoading = false;
})
.catch((err) => {
this.$message({
message: `${err}`,
type: "error",
duration: 2000,
});
this.tableLoading = false;
});
// }
},
layoutTable() {
return Math.random();
},
handleTableSelect(ids) {
this.tableSelectedIds = [...ids];
},
/** 处理 表格操作 */
handleOperate({ type, data }) {
console.log("payload", type, data);
// 编辑、删除、跳转路由、打开弹窗动态component都可以在配置里加上 url
// payload: { type: string, data: string | number | object }
switch (type) {
case "delete": {
// 找到删除的 prompt 字段
const deleteConfig = data.head?.options?.find((item) => item.name === "delete");
let promptName = data.name ?? data.id;
if (deleteConfig && "promptField" in deleteConfig) {
promptName = data[deleteConfig.promptField];
}
let hintMsg = `确定要删除记录 "${promptName}" 吗?`;
if (promptName == data.id) {
// 如果 promptName 计算出来是 data.id 就以'该记录'代称
hintMsg = "确定删除该记录?";
}
let currenPageListLength = this.dataList.length;
// 确认是否删除
return this.$confirm(hintMsg, "提示", {
confirmButtonText: "确认",
cancelButtonText: "我再想想",
type: "warning",
})
.then(() => {
// this.$http.delete(this.urls.base + `/${data}`).then((res) => {
this.$http({
url: this.urls.base,
method: "DELETE",
// data: data.id,
data: [`${data.id}`],
// headers: {
// "Content-Type": "application/json"
// }
}).then(({ data: res }) => {
if (res.code === 0) {
this.$message.success("删除成功!");
// this.page = 1;
// this.size =
// "defaultPageSize" in this.tableConfig.column ? this.tableConfig.column.defaultPageSize : 20;
if (currenPageListLength == 1) this.page = this.page > 1 ? this.page - 1 : 1;
this.getList();
} else {
this.$message({
message: `${res.code}: ${res.msg}`,
type: "error",
duration: 1500,
});
}
});
})
.catch((err) => {});
}
case "edit": {
this.openDialog(data); /** data is ==> id */
break;
}
case "view":
case "view-detail-action": {
this.openDialog(data, true);
break;
}
case "view-blender-batch-details": {
this.$router.push({
name: "pms-blenderBatchDetails",
query: {
batchId: data,
},
});
break;
}
}
},
handleBtnClick({ btnName, payload }) {
switch (btnName) {
case "批量同步":
this.overlayVisible = true;
this.$http.post(this.urls.syncUrl, this.tableSelectedIds).then(({ data: res }) => {
this.$message({ message: res.msg, type: res.code === 0 ? "success" : "error" });
res.code == 0 && this.getList();
this.overlayVisible = false;
});
break;
case "新增":
this.openDialog();
break;
case "导入":
this.openUploadDialog();
break;
case "手动添加": {
this.openDialog();
return;
}
case "查询": {
if (typeof payload === "object") {
// BaseSearchForm 给这个组件传递了数据
Object.assign(this.cachedSearchCondition, payload);
if ("timerange" in payload) {
if (!!payload.timerange) {
const [startTime, endTime] = payload["timerange"];
this.cachedSearchCondition.startTime = moment(startTime).format("YYYY-MM-DDTHH:mm:ss");
this.cachedSearchCondition.endTime = moment(endTime).format("YYYY-MM-DDTHH:mm:ss");
} else {
delete this.cachedSearchCondition.startTime;
delete this.cachedSearchCondition.endTime;
}
delete this.cachedSearchCondition.timerange;
}
}
/** 处理 listQueryExtra 里的数据 */
this.listQueryExtra?.map((cond) => {
if (typeof cond === "string") {
if (!!payload[cond]) {
this.cachedSearchCondition[cond] = payload[cond];
} else {
this.cachedSearchCondition[cond] = "";
}
} else if (typeof cond === "object") {
Object.keys(cond).forEach((key) => {
this.cachedSearchCondition[key] = cond[key];
});
}
});
this.getList(this.cachedSearchCondition);
break;
}
case "同步":
case "全部同步":
this.$confirm("是否同步数据?", "提示", {
confirmButtonText: "确定",
cancelButtonText: "取消",
type: "warning",
}).then(() => {
this.overlayVisible = true;
this.$http
.post(this.urls.syncUrl)
.then(({ data: res }) => {
console.log("同步", res);
this.$message({ message: res.msg, type: res.code === 0 ? "success" : "error" });
this.getList();
this.overlayVisible = false;
})
.catch((err) => {
this.overlayVisible = false;
});
});
break;
}
},
/** 导航器的操作 */
handleSizeChange(val) {
// val 是新值
this.page = 1;
this.size = val;
this.getList(this.cachedSearchCondition);
},
handlePageChange(val) {
// val 是新值
this.getList(this.cachedSearchCondition);
},
/** 打开对话框 */
openDialog(row_id, detail_mode) {
this.dialogVisible = true;
this.$nextTick(() => {
this.$refs["edit"].init(row_id, detail_mode);
});
},
},
};
</script>
<style scoped>
.list-view-with-head {
background: white;
/* height: 100%; */
min-height: inherit;
border-radius: 6px;
padding: 16px;
box-shadow: 0 0 1.125px 0.125px rgba(0, 0, 0, 0.125);
}
</style>