You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

274 lines
7.2 KiB

This file contains invisible Unicode characters!

This file contains invisible Unicode characters that may be processed differently from what appears below. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to reveal hidden characters.

<template>
<div>
<el-card shadow="never" class="search-card">
<FormSearch
:fields="searchFormFields"
:loading="loading"
:show-create="true"
:show-delete="true"
:disabled-delete="selectedRow.length === 0"
@onSearch="onSearch"
@onCreate="onCreate"
@onDelete="batchDelete"
/>
</el-card>
<el-card shadow="never" class="mgt-20px">
<TableList
:table-data="trees"
:loading="loading"
:fields="tableListFields"
:show-actions="true"
:show-view="false"
:show-edit="true"
:show-delete="true"
:show-select="true"
:tree-props="{ children: 'children' }"
@selectRow="selectRow"
@editRow="editRow"
@deleteRow="deleteRow"
/>
</el-card>
<el-dialog
:close-on-click-modal="false"
:title="category.id ? '编辑分类' : '新增分类'"
:visible.sync="formVisible"
:width="'640px'"
>
<FormCategory
ref="categoryForm"
:init-category="category"
:trees="trees"
@success="formCategorySuccess"
/>
</el-dialog>
</div>
</template>
<script>
import { listCategory, deleteCategory, getCategory } from '~/api/category'
import TableList from '~/components/TableList.vue'
import FormSearch from '~/components/FormSearch.vue'
import FormCategory from '~/components/FormCategory.vue'
import { categoryToTrees, parseQueryIntArray } from '~/utils/utils'
import { mapGetters } from 'vuex'
export default {
components: { TableList, FormSearch, FormCategory },
layout: 'admin',
data() {
return {
loading: false,
formVisible: false,
search: {
wd: '',
status: [],
},
categories: [],
trees: [],
total: 0,
searchFormFields: [],
tableListFields: [],
selectedRow: [],
category: { id: 0, title: '', cover: '', sort: '', icon: '' },
}
},
head() {
return {
title: `分类管理 - ${this.settings.system.sitename}`,
}
},
computed: {
...mapGetters('setting', ['settings']),
},
watch: {
'$route.query': {
immediate: true,
async handler() {
this.search = {
...this.search,
...this.$route.query,
...parseQueryIntArray(this.$route.query, ['enable']),
}
await this.initTableListFields()
this.listCategory()
},
},
},
async created() {
this.initSearchForm()
},
methods: {
async listCategory() {
this.loading = true
const res = await listCategory(this.search)
if (res.status === 200) {
let categories = res.data.category || []
categories = categories.map((item) => {
item.disable_delete = item.doc_count > 0
return item
})
this.categories = categories
this.trees = categoryToTrees(categories)
this.total = res.data.total
} else {
this.$message.error(res.data.message)
}
this.loading = false
},
handleSizeChange(val) {
this.search.size = val
this.$router.push({
query: this.search,
})
},
handlePageChange(val) {
this.search.page = val
this.$router.push({
query: this.search,
})
},
onSearch(search) {
this.search = { ...this.search, ...search, page: 1 }
if (
location.pathname + location.search ===
        this.$router.resolve({
          query: this.search,
        }).href
) {
this.listCategory()
} else {
this.$router.push({
query: this.search,
})
}
},
onCreate() {
this.category = {
id: 0,
enable: true,
title: '',
cover: '',
icon: '',
parent_id: 0,
sort: 0,
}
this.formVisible = true
},
async editRow(row) {
const res = await getCategory({ id: row.id })
if (res.status === 200) {
this.category = { cover: '', icon: '', ...res.data }
this.formVisible = true
} else {
this.$message.error(res.data.message || '')
}
},
formCategorySuccess() {
this.formVisible = false
this.listCategory()
},
batchDelete() {
this.$confirm(
`您确定要删除选中的【${this.selectedRow.length}个】分类吗?本次删除会连同子分类一起删除,删除之后不可恢复!`,
'',
{
confirmButtonText: '',
cancelButtonText: '',
type: 'warning',
}
)
.then(async () => {
const ids = this.selectedRow.map((item) => item.id)
const res = await deleteCategory({ id: ids })
if (res.status === 200) {
this.$message.success('')
this.listCategory()
} else {
this.$message.error(res.data.message)
}
})
.catch(() => {})
},
deleteRow(row) {
this.$confirm(
`您确定要删除分类【${row.title}】吗?本次删除会连同子分类一起删除,删除之后不可恢复!`,
'',
{
confirmButtonText: '',
cancelButtonText: '',
type: 'warning',
}
)
.then(async () => {
const res = await deleteCategory({ id: row.id })
if (res.status === 200) {
this.$message.success('')
this.listCategory()
} else {
this.$message.error(res.data.message)
}
})
.catch(() => {})
},
selectRow(rows) {
this.selectedRow = rows
},
initSearchForm() {
this.searchFormFields = [
{
type: 'text',
label: '',
name: 'wd',
placeholder: '',
},
{
type: 'select',
label: '',
name: 'enable',
placeholder: '',
multiple: true,
options: [
{ label: '', value: 1 },
{ label: '', value: 0 },
],
},
]
},
initTableListFields() {
if (this.tableListFields.length > 0) {
return
}
this.tableListFields = [
{ prop: 'title', label: '', minWidth: 120, fixed: 'left' },
{ prop: 'icon', label: '', width: 48, type: 'image' },
{
prop: 'enable',
label: '',
width: 80,
type: 'bool',
},
{
prop: 'show_description',
label: '',
width: 80,
type: 'bool',
},
{
prop: 'sort',
label: '',
width: 80,
type: 'number',
},
{ prop: 'cover', label: '', width: 100, type: 'image' },
{ prop: 'doc_count', label: '', width: 80, type: 'number' },
{ prop: 'id', label: 'ID', width: 80, type: 'number' },
{ prop: 'description', label: '', minWidth: 200 },
{ prop: 'created_at', label: '', width: 160, type: 'datetime' },
{ prop: 'updated_at', label: '', width: 160, type: 'datetime' },
]
},
},
}
</script>
<style></style>