Files
gxtyzx-admin/src/views/baocan/goods/index.vue
T
2024-09-27 14:17:12 +08:00

345 lines
9.0 KiB
Vue

<template>
<div class="page">
<div class="ele-body">
<a-card :bordered="false">
<!-- 表格 -->
<ele-pro-table
ref="tableRef"
row-key="goodsId"
:columns="columns"
:height="tableHeight"
:datasource="datasource"
v-model:selection="selection"
:customRow="customRow"
>
<template #toolbar>
<search
:selection="selection"
@search="reload"
@add="openEdit"
@remove="removeBatch"
/>
</template>
<template #bodyCell="{ column, record }">
<template v-if="column.key === 'image'">
<a-image :src="record.image" :preview="false" :width="80" />
</template>
<template v-if="column.key === 'goodsName'">
<a-tooltip :title="record.goodsName">
<div class="ele-text-heading">
<b>{{ record.goodsName }}</b>
</div>
<div class="ele-text-placeholder">
{{ record.comments }}
</div>
</a-tooltip>
</template>
<template v-if="column.key === 'gear'">
<a-tag v-if="record.gear === 10" color="red">食堂档口</a-tag>
<a-tag v-if="record.gear === 20">物品档口</a-tag>
</template>
<template v-if="column.key === 'status'">
<a-tag v-if="record.status === 0" color="green">上架</a-tag>
<a-tag v-if="record.status === 1" color="orange">下架</a-tag>
</template>
<!-- <template v-if="column.key === 'comments'">-->
<!-- <a-tooltip :title="record.comments" placement="topLeft">-->
<!-- {{ record.comments }}-->
<!-- </a-tooltip>-->
<!-- </template>-->
<template v-if="column.key === 'action'">
<a-space>
<a @click="openEdit(record)">修改</a>
<a-divider type="vertical" />
<a-popconfirm
title="确定要删除此记录吗?"
@confirm="remove(record)"
>
<a class="ele-text-danger">删除</a>
</a-popconfirm>
</a-space>
</template>
</template>
</ele-pro-table>
</a-card>
<!-- 编辑弹窗 -->
<goods-edit
v-model:visible="showEdit"
:data="current"
:category-list="data"
@done="reload"
/>
</div>
</div>
</template>
<script lang="ts" setup>
import { timeAgo, toTreeData } from 'ele-admin-pro';
import { createVNode, computed, ref } from 'vue';
import { message, Modal } from 'ant-design-vue';
import { ExclamationCircleOutlined } from '@ant-design/icons-vue';
import type { EleProTable } from 'ele-admin-pro';
import type {
DatasourceFunction,
ColumnItem
} from 'ele-admin-pro/es/ele-pro-table/types';
import { toDateString } from 'ele-admin-pro';
import Search from './components/search.vue';
import GoodsEdit from './components/goods-edit.vue';
import { pageGoods, removeGoods, removeBatchGoods } from '@/api/goods';
import type { Goods, GoodsParam } from '@/api/goods/model';
import { listCategory } from '@/api/goods/category';
import { Category } from '@/api/goods/category/model';
import { FILE_THUMBNAIL } from '@/config/setting';
// 表格实例
const tableRef = ref<InstanceType<typeof EleProTable> | null>(null);
// 加载状态
const loading = ref(true);
// 树展开的key
const expandedRowKeys = ref<number[]>([]);
// 树选中的key
const selectedRowKeys = ref<number[]>([]);
// 当前用户信息
// const brand = getDictionaryOptions('serverBrand');
// 表格列配置
const columns = ref<ColumnItem[]>([
{
key: 'index',
width: 48,
align: 'center',
fixed: 'left',
hideInSetting: true,
customRender: ({ index }) => index + (tableRef.value?.tableIndex ?? 0)
},
{
title: '菜品图片',
dataIndex: 'image',
width: 110,
key: 'image'
},
{
title: '菜品名称',
dataIndex: 'goodsName',
key: 'goodsName',
width: 320,
ellipsis: true
},
{
title: '所属档口',
dataIndex: 'gear',
key: 'gear'
},
{
title: '类别',
dataIndex: 'categoryName'
},
{
title: '菜品价格',
dataIndex: 'goodsPriceMin',
key: 'goodsPriceMin',
customRender: ({ text }) => '¥' + text
},
{
title: '备注',
dataIndex: 'comments',
key: 'comments',
hideInTable: true,
ellipsis: true
},
{
title: '状态',
dataIndex: 'status',
sorter: true,
key: 'status',
showSorterTooltip: false,
customRender: ({ text }) => ['上架', '下架'][text]
},
{
title: '排序',
dataIndex: 'sortNumber',
sorter: true
},
{
title: '创建时间',
dataIndex: 'createTime',
key: 'createTime',
sorter: true,
ellipsis: true,
hideInTable: true,
customRender: ({ text }) => toDateString(text)
},
{
title: '操作',
key: 'action',
width: 200,
align: 'center',
hideInSetting: true
}
]);
// 表格选中数据
const selection = ref<Goods[]>([]);
// 当前编辑数据
const current = ref<Goods | null>(null);
// 是否显示编辑弹窗
const showEdit = ref(false);
// 树形数据
const data = ref<Category[]>([]);
// 表格数据源
const datasource: DatasourceFunction = ({
page,
limit,
where,
orders,
filters
}) => {
if (filters) {
where.brand = filters.brand;
}
return pageGoods({ ...where, ...orders, page, limit });
};
/* 查询 */
const query = () => {
loading.value = true;
listCategory()
.then((list) => {
loading.value = false;
const eks: number[] = [];
list.forEach((d) => {
d.key = d.categoryId;
d.value = d.categoryId;
if (typeof d.categoryId === 'number') {
eks.push(d.categoryId);
}
});
expandedRowKeys.value = eks;
data.value = toTreeData({
data: list,
idField: 'categoryId',
parentIdField: 'parentId'
});
if (list.length) {
if (typeof list[0].categoryId === 'number') {
selectedRowKeys.value = [list[0].categoryId];
}
current.value = list[0];
} else {
selectedRowKeys.value = [];
current.value = null;
}
})
.catch((e) => {
loading.value = false;
message.error(e.message);
});
};
/* 搜索 */
const reload = (where?: GoodsParam) => {
selection.value = [];
tableRef?.value?.reload({ where: where });
};
// 搜索是否展开
const searchExpand = ref(false);
// 表格固定高度
const fixedHeight = ref(false);
// 表格高度
const tableHeight = computed(() => {
return fixedHeight.value
? searchExpand.value
? 'calc(100vh - 618px)'
: 'calc(100vh - 562px)'
: void 0;
});
/* 打开编辑弹窗 */
const openEdit = (row?: Goods) => {
current.value = row ?? null;
showEdit.value = true;
};
/* 删除单个 */
const remove = (row: Goods) => {
const hide = message.loading('请求中..', 0);
removeGoods(row.goodsId)
.then((msg) => {
hide();
message.success(msg);
reload();
})
.catch((e) => {
hide();
message.error(e.message);
});
};
/* 批量删除 */
const removeBatch = () => {
if (!selection.value.length) {
message.error('请至少选择一条数据');
return;
}
Modal.confirm({
title: '提示',
content: '确定要删除选中的记录吗?',
icon: createVNode(ExclamationCircleOutlined),
maskClosable: true,
onOk: () => {
const hide = message.loading('请求中..', 0);
removeBatchGoods(selection.value.map((d) => d.goodsId))
.then((msg) => {
hide();
message.success(msg);
reload();
})
.catch((e) => {
hide();
message.error(e.message);
});
}
});
};
/* 自定义行属性 */
const customRow = (record: Goods) => {
return {
// 行点击事件
onClick: () => {
// console.log(record);
},
// 行双击事件
onDblclick: () => {
openEdit(record);
}
};
};
query();
</script>
<script lang="ts">
export default {
name: 'GoodsIndex'
};
</script>
<style lang="less" scoped>
// 文字超出隐藏(两行)
// 需要设置文字容器的宽度
.twoline-hide {
width: 320px;
display: -webkit-box;
-webkit-line-clamp: 2;
-webkit-box-orient: vertical;
overflow: hidden;
white-space: normal;
}
</style>