2025-03-04 09:11:33 +08:00
|
|
|
<script setup lang="ts">
|
|
|
|
import { ref } from "vue";
|
|
|
|
import { PureTableBar } from "@/components/RePureTableBar";
|
|
|
|
import { useRenderIcon } from "@/components/ReIcon/src/hooks";
|
|
|
|
import { getGoodsListApi } from "@/api/shop/goods";
|
|
|
|
import EditPen from "@iconify-icons/ep/edit-pen";
|
|
|
|
import Delete from "@iconify-icons/ep/delete";
|
|
|
|
import AddFill from "@iconify-icons/ri/add-circle-line";
|
|
|
|
import Search from "@iconify-icons/ep/search";
|
|
|
|
import Refresh from "@iconify-icons/ep/refresh";
|
|
|
|
import GoodsFormModal from "./goods-form-modal.vue";
|
2025-03-04 17:17:46 +08:00
|
|
|
import { ElMessage, ElMessageBox } from "element-plus";
|
|
|
|
import { deleteGoodsApi } from "@/api/shop/goods";
|
2025-03-04 09:11:33 +08:00
|
|
|
|
|
|
|
defineOptions({
|
|
|
|
name: "ShopGoods"
|
|
|
|
});
|
|
|
|
|
|
|
|
const formRef = ref();
|
|
|
|
const tableRef = ref();
|
|
|
|
const modalVisible = ref(false);
|
|
|
|
|
|
|
|
// 搜索表单
|
|
|
|
const searchFormParams = ref({
|
|
|
|
goodsName: "",
|
|
|
|
status: null
|
|
|
|
});
|
|
|
|
|
|
|
|
// 分页参数
|
|
|
|
const pagination = ref({
|
|
|
|
pageSize: 10,
|
|
|
|
currentPage: 1,
|
|
|
|
total: 0
|
|
|
|
});
|
|
|
|
|
|
|
|
// 加载数据
|
|
|
|
const loading = ref(false);
|
|
|
|
const dataList = ref([]);
|
2025-03-04 17:17:46 +08:00
|
|
|
const multipleSelection = ref<number[]>([]);
|
2025-03-04 09:11:33 +08:00
|
|
|
|
|
|
|
const getList = async () => {
|
|
|
|
try {
|
|
|
|
loading.value = true;
|
|
|
|
const { data } = await getGoodsListApi({
|
|
|
|
...searchFormParams.value,
|
|
|
|
pageSize: pagination.value.pageSize,
|
|
|
|
pageNum: pagination.value.currentPage
|
|
|
|
});
|
|
|
|
dataList.value = data.rows;
|
|
|
|
pagination.value.total = data.total;
|
2025-03-04 17:17:46 +08:00
|
|
|
|
2025-03-04 09:11:33 +08:00
|
|
|
} finally {
|
|
|
|
loading.value = false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
const handleAddSuccess = () => {
|
|
|
|
getList();
|
|
|
|
};
|
|
|
|
// 搜索
|
|
|
|
const onSearch = () => {
|
|
|
|
pagination.value.currentPage = 1;
|
|
|
|
getList();
|
|
|
|
};
|
|
|
|
|
|
|
|
// 重置
|
|
|
|
const resetForm = () => {
|
|
|
|
formRef.value.resetFields();
|
|
|
|
onSearch();
|
|
|
|
};
|
|
|
|
|
|
|
|
// 分页变化
|
|
|
|
const onSizeChange = (val: number) => {
|
|
|
|
pagination.value.pageSize = val;
|
|
|
|
getList();
|
|
|
|
};
|
|
|
|
|
|
|
|
const onCurrentChange = (val: number) => {
|
|
|
|
pagination.value.currentPage = val;
|
|
|
|
getList();
|
|
|
|
};
|
|
|
|
|
|
|
|
// 初始化加载
|
|
|
|
getList();
|
2025-03-04 17:17:46 +08:00
|
|
|
|
|
|
|
const handleDelete = async (row: any) => {
|
|
|
|
try {
|
|
|
|
await deleteGoodsApi(row.goodsId);
|
|
|
|
ElMessage.success("删除成功");
|
|
|
|
getList();
|
|
|
|
} catch (error) {
|
|
|
|
console.error("删除失败", error);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleBulkDelete = async () => {
|
|
|
|
if (multipleSelection.value.length === 0) return;
|
|
|
|
|
|
|
|
try {
|
|
|
|
await ElMessageBox.confirm(
|
|
|
|
`确认删除选中的${multipleSelection.value.length}项商品吗?`,
|
|
|
|
"警告",
|
|
|
|
{ confirmButtonText: "确定", cancelButtonText: "取消", type: "warning" }
|
|
|
|
);
|
|
|
|
|
|
|
|
await deleteGoodsApi(multipleSelection.value.join(","));
|
|
|
|
ElMessage.success("批量删除成功");
|
|
|
|
multipleSelection.value = [];
|
|
|
|
getList();
|
|
|
|
} catch (error) {
|
|
|
|
console.error("删除取消或失败", error);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// 在表格绑定选择事件
|
|
|
|
const handleSelectionChange = (rows: any[]) => {
|
|
|
|
multipleSelection.value = rows.map(row => row.goodsId);
|
|
|
|
};
|
2025-03-04 09:11:33 +08:00
|
|
|
</script>
|
|
|
|
|
|
|
|
<template>
|
|
|
|
<div class="main">
|
|
|
|
<el-form ref="formRef" :inline="true" :model="searchFormParams"
|
|
|
|
class="search-form bg-bg_color w-[99/100] pl-8 pt-[12px]">
|
|
|
|
<el-form-item label="商品名称:" prop="goodsName">
|
|
|
|
<el-input v-model="searchFormParams.goodsName" placeholder="请输入商品名称" clearable class="!w-[200px]" />
|
|
|
|
</el-form-item>
|
|
|
|
<el-form-item label="状态:" prop="status">
|
|
|
|
<el-select v-model="searchFormParams.status" placeholder="请选择状态" clearable class="!w-[180px]">
|
|
|
|
<el-option label="已上架" :value="1" />
|
|
|
|
<el-option label="已下架" :value="2" />
|
|
|
|
</el-select>
|
|
|
|
</el-form-item>
|
|
|
|
<el-form-item>
|
|
|
|
<el-button type="primary" :icon="useRenderIcon(Search)" @click="onSearch">
|
|
|
|
搜索
|
|
|
|
</el-button>
|
|
|
|
<el-button :icon="useRenderIcon(Refresh)" @click="resetForm">
|
|
|
|
重置
|
|
|
|
</el-button>
|
|
|
|
</el-form-item>
|
|
|
|
</el-form>
|
|
|
|
|
|
|
|
<PureTableBar title="商品列表" @refresh="getList">
|
|
|
|
<template #buttons>
|
|
|
|
<el-button type="primary" :icon="useRenderIcon(AddFill)" @click="modalVisible = true">
|
|
|
|
新增商品
|
|
|
|
</el-button>
|
2025-03-04 17:17:46 +08:00
|
|
|
<el-button type="danger" :icon="useRenderIcon(Delete)" :disabled="multipleSelection.length === 0"
|
|
|
|
@click="handleBulkDelete">
|
|
|
|
批量删除
|
|
|
|
</el-button>
|
2025-03-04 09:11:33 +08:00
|
|
|
</template>
|
2025-03-04 17:17:46 +08:00
|
|
|
<el-table ref="tableRef" v-loading="loading" :data="dataList" row-key="id"
|
|
|
|
@selection-change="handleSelectionChange" border>
|
|
|
|
<el-table-column type="selection" width="55" />
|
2025-03-04 09:11:33 +08:00
|
|
|
<el-table-column label="商品图片" width="120">
|
|
|
|
<template #default="{ row }">
|
2025-03-04 17:17:46 +08:00
|
|
|
<el-image :src="row.coverImg" :preview-src-list="[row.coverImg]" :z-index="9999" :preview-teleported="true"
|
|
|
|
:hide-on-click-modal="true" fit="cover" class="rounded" width="60" height="60" />
|
2025-03-04 09:11:33 +08:00
|
|
|
</template>
|
|
|
|
</el-table-column>
|
|
|
|
<el-table-column label="商品名称" prop="goodsName" />
|
|
|
|
<el-table-column label="价格" prop="price" width="120" />
|
|
|
|
<el-table-column label="库存" prop="stock" width="120" />
|
|
|
|
<el-table-column label="状态" prop="status" width="120">
|
|
|
|
<template #default="{ row }">
|
|
|
|
<el-tag :type="row.status === 1 ? 'success' : 'danger'">
|
|
|
|
{{ row.status === 1 ? '已上架' : '已下架' }}
|
|
|
|
</el-tag>
|
|
|
|
</template>
|
|
|
|
</el-table-column>
|
|
|
|
<el-table-column label="操作" width="150" fixed="right">
|
|
|
|
<template #default="{ row }">
|
|
|
|
<el-button type="primary" link :icon="useRenderIcon(EditPen)">
|
|
|
|
编辑
|
|
|
|
</el-button>
|
2025-03-04 17:17:46 +08:00
|
|
|
<el-popconfirm :title="`确认删除【${row.goodsName}】?`" @confirm="handleDelete(row)">
|
|
|
|
<template #reference>
|
|
|
|
<el-button type="danger" link :icon="useRenderIcon(Delete)">
|
|
|
|
删除
|
|
|
|
</el-button>
|
|
|
|
</template>
|
|
|
|
</el-popconfirm>
|
2025-03-04 09:11:33 +08:00
|
|
|
</template>
|
|
|
|
</el-table-column>
|
|
|
|
</el-table>
|
|
|
|
<el-pagination v-model:current-page="pagination.currentPage" v-model:page-size="pagination.pageSize"
|
|
|
|
:page-sizes="[10, 20, 50]" layout="total, sizes, prev, pager, next, jumper" :total="pagination.total"
|
|
|
|
@size-change="onSizeChange" @current-change="onCurrentChange" />
|
|
|
|
</PureTableBar>
|
2025-03-04 17:17:46 +08:00
|
|
|
<!-- 新增商品弹窗 -->
|
|
|
|
<goods-form-modal v-model:visible="modalVisible" @refresh="getList" />
|
2025-03-04 09:11:33 +08:00
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
<style scoped>
|
|
|
|
/* 覆盖预览层样式 */
|
|
|
|
:deep(.el-image-viewer__wrapper) {
|
|
|
|
z-index: 9999 !important;
|
|
|
|
}
|
|
|
|
|
|
|
|
:deep(.el-image-viewer__mask) {
|
|
|
|
opacity: 1;
|
|
|
|
background-color: rgba(0, 0, 0, 0.8);
|
|
|
|
}
|
|
|
|
</style>
|