调整商品分类管理

This commit is contained in:
lewis 2025-07-14 12:16:26 +08:00
parent 576ab7c857
commit 164991bf2f
3 changed files with 215 additions and 261 deletions

View File

@ -23,4 +23,8 @@ export function apiProductCategoryDelete(params: any) {
// 商品分类详情
export function apiProductCategoryDetail(params: any) {
return request.get({ url: '/productCategory/detail', params })
}
}
export function apiProductCategoryAll(params: any) {
return request.get({ url: '/productCategory/all', params })
}

View File

@ -1,116 +1,102 @@
<template>
<div class="edit-popup">
<popup
ref="popupRef"
:title="popupTitle"
:async="true"
width="550px"
@confirm="handleSubmit"
@close="handleClose"
>
<el-form ref="formRef" :model="formData" label-width="90px" :rules="formRules">
<el-form-item label="上级分类" prop="pid">
<el-select class="flex-1" v-model="formData.pid" clearable placeholder="请选择上级分类">
<el-option
v-for="(item, index) in dictData.show_status"
:key="index"
:label="item.name"
:value="parseInt(item.value)"
/>
</el-select>
</el-form-item>
<el-form-item label="名称" prop="name">
<el-input v-model="formData.name" clearable placeholder="请输入名称" />
</el-form-item>
</el-form>
</popup>
</div>
</template>
<script lang="ts" setup name="productCategoryEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiProductCategoryAdd, apiProductCategoryEdit, apiProductCategoryDetail } from '@/api/product_category'
import { timeFormat } from '@/utils/util'
import type { PropType } from 'vue'
defineProps({
dictData: {
type: Object as PropType<Record<string, any[]>>,
default: () => ({})
}
})
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')
//
const popupTitle = computed(() => {
return mode.value == 'edit' ? '编辑商品分类' : '新增商品分类'
})
//
const formData = reactive({
id: '',
pid: '',
name: '',
})
//
const formRules = reactive<any>({
})
//
const setFormData = async (data: Record<any, any>) => {
for (const key in formData) {
if (data[key] != null && data[key] != undefined) {
//@ts-ignore
formData[key] = data[key]
}
}
}
const getDetail = async (row: Record<string, any>) => {
const data = await apiProductCategoryDetail({
id: row.id
})
setFormData(data)
}
//
const handleSubmit = async () => {
await formRef.value?.validate()
const data = { ...formData, }
mode.value == 'edit'
? await apiProductCategoryEdit(data)
: await apiProductCategoryAdd(data)
popupRef.value?.close()
emit('success')
}
//
const open = (type = 'add') => {
mode.value = type
popupRef.value?.open()
}
//
const handleClose = () => {
emit('close')
}
defineExpose({
open,
setFormData,
getDetail
})
</script>
<template>
<div class="edit-popup">
<popup ref="popupRef" :title="popupTitle" :async="true" width="550px" @confirm="handleSubmit"
@close="handleClose">
<el-form ref="formRef" :model="formData" label-width="90px">
<el-form-item label="上级分类" prop="pid">
<el-tree-select class="flex-1" v-model="formData.pid" :data="categoryList" clearable node-key="id"
:props="{
label: 'name'
}" :default-expand-all="true" placeholder="请选择上级分类" check-strictly />
</el-form-item>
<el-form-item label="名称" prop="name">
<el-input v-model="formData.name" clearable placeholder="请输入名称" />
</el-form-item>
</el-form>
</popup>
</div>
</template>
<script lang="ts" setup name="productCategoryEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiProductCategoryAdd, apiProductCategoryEdit, apiProductCategoryDetail, apiProductCategoryLists } from '@/api/product_category'
import { arrayToTree, treeToArray } from '@/utils/util'
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')
const categoryList = ref<any[]>([])
const getCategoryList = async () => {
const data : any = await apiProductCategoryLists({ page_type: 0 })
const topData : any = { id: 0, name: '顶级', children: [] }
topData.children = arrayToTree(
treeToArray(data.lists)
)
categoryList.value.push(topData)
}
getCategoryList()
//
const popupTitle = computed(() => {
return mode.value == 'edit' ? '编辑商品分类' : '新增商品分类'
})
//
const formData = reactive({
id: '',
pid: '',
name: '',
})
//
const setFormData = async (data : Record<any, any>) => {
for (const key in formData) {
if (data[key] != null && data[key] != undefined) {
//@ts-ignore
formData[key] = data[key]
}
}
}
const getDetail = async (row : Record<string, any>) => {
const data = await apiProductCategoryDetail({
id: row.id
})
setFormData(data)
}
//
const handleSubmit = async () => {
await formRef.value?.validate()
const data = { ...formData, }
mode.value == 'edit'
? await apiProductCategoryEdit(data)
: await apiProductCategoryAdd(data)
popupRef.value?.close()
emit('success')
}
//
const open = (type = 'add', pid = 0) => {
console.log(type);
console.log(pid);
mode.value = type
formData.pid = pid
popupRef.value?.open()
}
//
const handleClose = () => {
emit('close')
}
defineExpose({
open,
setFormData,
getDetail
})
</script>

View File

@ -1,144 +1,108 @@
<template>
<div>
<el-card class="!border-none mb-4" shadow="never">
<el-form
class="mb-[-16px]"
:model="queryParams"
inline
>
<el-form-item label="上级分类" prop="pid">
<el-select class="w-[280px]" v-model="queryParams.pid" clearable placeholder="请选择上级分类">
<el-option label="全部" value=""></el-option>
<el-option
v-for="(item, index) in dictData.show_status"
:key="index"
:label="item.name"
:value="item.value"
/>
</el-select>
</el-form-item>
<el-form-item label="名称" prop="name">
<el-input class="w-[280px]" v-model="queryParams.name" clearable placeholder="请输入名称" />
</el-form-item>
<el-form-item>
<el-button type="primary" @click="resetPage">查询</el-button>
<el-button @click="resetParams">重置</el-button>
</el-form-item>
</el-form>
</el-card>
<el-card class="!border-none" v-loading="pager.loading" shadow="never">
<el-button v-perms="['product_category/add']" type="primary" @click="handleAdd">
<template #icon>
<icon name="el-icon-Plus" />
</template>
新增
</el-button>
<el-button
v-perms="['product_category/delete']"
:disabled="!selectData.length"
@click="handleDelete(selectData)"
>
删除
</el-button>
<div class="mt-4">
<el-table :data="pager.lists" @selection-change="handleSelectionChange">
<el-table-column type="selection" width="55" />
<el-table-column label="ID" prop="id" show-overflow-tooltip />
<el-table-column label="上级分类" prop="pid">
<template #default="{ row }">
<dict-value :options="dictData.show_status" :value="row.pid" />
</template>
</el-table-column>
<el-table-column label="名称" prop="name" show-overflow-tooltip />
<el-table-column label="创建时间" prop="create_time" show-overflow-tooltip />
<el-table-column label="操作" width="120" fixed="right">
<template #default="{ row }">
<el-button
v-perms="['product_category/edit']"
type="primary"
link
@click="handleEdit(row)"
>
编辑
</el-button>
<el-button
v-perms="['product_category/delete']"
type="danger"
link
@click="handleDelete(row.id)"
>
删除
</el-button>
</template>
</el-table-column>
</el-table>
</div>
<div class="flex mt-4 justify-end">
<pagination v-model="pager" @change="getLists" />
</div>
</el-card>
<edit-popup v-if="showEdit" ref="editRef" :dict-data="dictData" @success="getLists" @close="showEdit = false" />
</div>
</template>
<script lang="ts" setup name="productCategoryLists">
import { usePaging } from '@/hooks/usePaging'
import { useDictData } from '@/hooks/useDictOptions'
import { apiProductCategoryLists, apiProductCategoryDelete } from '@/api/product_category'
import { timeFormat } from '@/utils/util'
import feedback from '@/utils/feedback'
import EditPopup from './edit.vue'
const editRef = shallowRef<InstanceType<typeof EditPopup>>()
//
const showEdit = ref(false)
//
const queryParams = reactive({
pid: '',
name: '',
})
//
const selectData = ref<any[]>([])
//
const handleSelectionChange = (val: any[]) => {
selectData.value = val.map(({ id }) => id)
}
//
const { dictData } = useDictData('show_status')
//
const { pager, getLists, resetParams, resetPage } = usePaging({
fetchFun: apiProductCategoryLists,
params: queryParams
})
//
const handleAdd = async () => {
showEdit.value = true
await nextTick()
editRef.value?.open('add')
}
//
const handleEdit = async (data: any) => {
showEdit.value = true
await nextTick()
editRef.value?.open('edit')
editRef.value?.setFormData(data)
}
//
const handleDelete = async (id: number | any[]) => {
await feedback.confirm('确定要删除?')
await apiProductCategoryDelete({ id })
getLists()
}
getLists()
</script>
<template>
<div>
<el-card class="!border-none" v-loading="pager.loading" shadow="never">
<el-button v-perms="['product_category/add']" type="primary" @click="handleAdd()">
<template #icon>
<icon name="el-icon-Plus" />
</template>
新增
</el-button>
<el-button @click="handleExpand"> 展开/折叠 </el-button>
<div class="mt-4">
<el-table ref="tableRef" class="mt-4" size="large" border v-loading="pager.loading" :data="pager.lists"
row-key="id" :tree-props="{ children: 'children', hasChildren: 'hasChildren' }">
<el-table-column label="上级分类" prop="name" min-width="150" show-overflow-tooltip />
<el-table-column label="分类名称" prop="name" min-width="150" show-overflow-tooltip />
<el-table-column label="创建时间" prop="create_time" min-width="150" show-overflow-tooltip />
<el-table-column label="操作" width="160" fixed="right">
<template #default="{ row }">
<el-button v-if="row.level < 2" v-perms="['auth.menu/add']" type="primary" link @click="handleAdd(row.id)">
新增
</el-button>
<el-button v-perms="['auth.menu/edit']" type="primary" link @click="handleEdit(row)">
编辑
</el-button>
<el-button v-perms="['auth.menu/delete']" type="danger" link @click="handleDelete(row.id)">
删除
</el-button>
</template>
</el-table-column>
</el-table>
</div>
</el-card>
<edit-popup v-if="showEdit" ref="editRef" @success="getLists" @close="showEdit = false" />
</div>
</template>
<script lang="ts" setup name="productCategoryLists">
import { usePaging } from '@/hooks/usePaging'
import { apiProductCategoryLists, apiProductCategoryDelete } from '@/api/product_category'
import { timeFormat } from '@/utils/util'
import feedback from '@/utils/feedback'
import EditPopup from './edit.vue'
import type { ElTable } from 'element-plus'
const editRef = shallowRef<InstanceType<typeof EditPopup>>()
//
const showEdit = ref(false)
//
const queryParams = reactive({
pid: '',
name: '',
})
//
const selectData = ref<any[]>([])
//
const handleSelectionChange = (val : any[]) => {
selectData.value = val.map(({ id }) => id)
}
//
const { pager, getLists, resetParams, resetPage } = usePaging({
fetchFun: apiProductCategoryLists,
params: queryParams
})
let isExpand = false
const tableRef = shallowRef<InstanceType<typeof ElTable>>()
const handleExpand = () => {
isExpand = !isExpand
toggleExpand(pager.lists, isExpand)
}
const toggleExpand = (children : any[], unfold = true) => {
for (const key in children) {
tableRef.value?.toggleRowExpansion(children[key], unfold)
if (children[key].children) {
toggleExpand(children[key].children!, unfold)
}
}
}
//
const handleAdd = async (pid = 0) => {
showEdit.value = true
await nextTick()
editRef.value?.open('add', pid)
}
//
const handleEdit = async (data : any) => {
showEdit.value = true
await nextTick()
editRef.value?.open('edit')
editRef.value?.setFormData(data)
}
//
const handleDelete = async (id : number | any[]) => {
await feedback.confirm('确定要删除?')
await apiProductCategoryDelete({ id })
getLists()
}
getLists()
</script>