调整商品分类管理

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

View File

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