add:增加编辑、删除等功能

This commit is contained in:
zlong committed 2024-11-21 17:39:36 +08:00
1 parent b3ae608daa
commit b4541b6f78
5 files changed
+171 -91

No files matched your search

+3
View File
@@ -16,14 +16,17 @@ declare module 'vue' {
TAvatar: typeof import('tdesign-vue-next')['Avatar'] TAvatar: typeof import('tdesign-vue-next')['Avatar']
TButton: typeof import('tdesign-vue-next')['Button'] TButton: typeof import('tdesign-vue-next')['Button']
TCard: typeof import('tdesign-vue-next')['Card'] TCard: typeof import('tdesign-vue-next')['Card']
TCheckbox: typeof import('tdesign-vue-next')['Checkbox']
TDialog: typeof import('tdesign-vue-next')['Dialog'] TDialog: typeof import('tdesign-vue-next')['Dialog']
TDropdown: typeof import('tdesign-vue-next')['Dropdown'] TDropdown: typeof import('tdesign-vue-next')['Dropdown']
TForm: typeof import('tdesign-vue-next')['Form'] TForm: typeof import('tdesign-vue-next')['Form']
TFormItem: typeof import('tdesign-vue-next')['FormItem'] TFormItem: typeof import('tdesign-vue-next')['FormItem']
THeader: typeof import('tdesign-vue-next')['Header'] THeader: typeof import('tdesign-vue-next')['Header']
TIcon: typeof import('tdesign-vue-next')['Icon'] TIcon: typeof import('tdesign-vue-next')['Icon']
TImageViewer: typeof import('tdesign-vue-next')['ImageViewer']
TInput: typeof import('tdesign-vue-next')['Input'] TInput: typeof import('tdesign-vue-next')['Input']
TLayout: typeof import('tdesign-vue-next')['Layout'] TLayout: typeof import('tdesign-vue-next')['Layout']
TLink: typeof import('tdesign-vue-next')['Link']
TMenu: typeof import('tdesign-vue-next')['Menu'] TMenu: typeof import('tdesign-vue-next')['Menu']
TMenuItem: typeof import('tdesign-vue-next')['MenuItem'] TMenuItem: typeof import('tdesign-vue-next')['MenuItem']
TRadioButton: typeof import('tdesign-vue-next')['RadioButton'] TRadioButton: typeof import('tdesign-vue-next')['RadioButton']
+3 -3
View File
@@ -16,14 +16,14 @@ export const updateCarousalItem = (id:number,updateCarousalsRequest: CreateCarou
return httpClient.put(`/carousals/${id}`,updateCarousalsRequest) return httpClient.put(`/carousals/${id}`,updateCarousalsRequest)
} }
export const deleteCarousal = (id:number) => { export const deleteCarousal = (id:string | number) => {
return httpClient.delete(`/carousals/${id}`) return httpClient.delete(`/carousals/${id}`)
} }
export const activeCarousal = (id: { id: string | number }) => { export const activeCarousal = (id: string | number) => {
return httpClient.put(`/carousals/${id}/active`) return httpClient.put(`/carousals/${id}/active`)
} }
export const inactiveCarousal = (id: { id: string | number }) => { export const inactiveCarousal = (id: string | number ) => {
return httpClient.put(`/carousals/${id}/inactive`) return httpClient.put(`/carousals/${id}/inactive`)
} }
+22 -1
View File
@@ -1,6 +1,6 @@
<script setup lang="ts"> <script setup lang="ts">
import { MessagePlugin, type UploadProps } from 'tdesign-vue-next' import { MessagePlugin, type UploadProps } from 'tdesign-vue-next'
import {ref} from 'vue' import { ref, watch } from 'vue'
import { useAppStore } from '@/stores/app' import { useAppStore } from '@/stores/app'
const imageViewerProps = ref<UploadProps['imageViewerProps']>({ const imageViewerProps = ref<UploadProps['imageViewerProps']>({
closeOnEscKeydown: false, closeOnEscKeydown: false,
@@ -19,6 +19,7 @@ const handleFail: UploadProps['onFail'] = ({ file }) => {
}; };
const handleSuccess: UploadProps['onSuccess'] = ({ file }) => { const handleSuccess: UploadProps['onSuccess'] = ({ file }) => {
if(file?.url){ if(file?.url){
console.log(file1.value,'file1')
emits('on-success',file.url) emits('on-success',file.url)
} }
} }
@@ -27,7 +28,27 @@ const appStore = useAppStore()
const uploadHeaders = ref<UploadProps['headers']>({ const uploadHeaders = ref<UploadProps['headers']>({
Authorization: `Bearer ${appStore.token}` Authorization: `Bearer ${appStore.token}`
}) })
const props = defineProps({
imageUrl:{
type:Object,
default:() => {
return { url: '' }
}
}
})
const emits = defineEmits(['on-success']) const emits = defineEmits(['on-success'])
watch(() => props.imageUrl,(newVal) => {
if(newVal){
if(newVal.url !== ''){
file1.value = [{ ...newVal }]
}else{
file1.value = []
}
}
},{
deep:true,
immediate:true
})
</script> </script>
<template> <template>
@@ -8,9 +8,9 @@
@confirm="fetchSaveClassifyData" @confirm="fetchSaveClassifyData"
> >
<t-space direction="vertical" style="width: 100%"> <t-space direction="vertical" style="width: 100%">
<TForm ref="form" :data="carousalData" :rules="rules" resetType="initial"> <TForm ref="form" :data="carousalData" :rules="rules" resetType="initial" label-width="100">
<TFormItem label="轮播图:" name="imageUrl"> <TFormItem label="轮播图:" name="imageUrl">
<ImageUpload @on-success="handleSuccessImg"/> <ImageUpload @on-success="handleSuccessImg" :image-url="imgUrl"/>
</TFormItem> </TFormItem>
<TFormItem label="跳转类型:" name="targetType" > <TFormItem label="跳转类型:" name="targetType" >
<t-radio-group :default-value="carousalData.targetType" v-model="carousalData.targetType"> <t-radio-group :default-value="carousalData.targetType" v-model="carousalData.targetType">
@@ -32,17 +32,19 @@ import type { Classify } from '@/model/classify'
import { computed, ref, reactive, watch } from 'vue' import { computed, ref, reactive, watch } from 'vue'
import type { FormInstanceFunctions, FormProps } from 'tdesign-vue-next' import type { FormInstanceFunctions, FormProps } from 'tdesign-vue-next'
import ImageUpload from '@/components/ImageUpload.vue' import ImageUpload from '@/components/ImageUpload.vue'
import { createCarousal } from '@/api/carousal' import { createCarousal, updateCarousalItem } from '@/api/carousal'
const props = defineProps<{ const props = defineProps<{
isEdit?: Boolean | null isEdit?: Boolean | null
headerTitle?: String | null headerTitle?: String | null
classify: Classify | {} classify: Classify | {} | number
count:number
}>() }>()
const emit = defineEmits(['success'])
const form = ref<FormInstanceFunctions | null>(null) const form = ref<FormInstanceFunctions | null>(null)
const header = computed(() => `${props.isEdit ? '编辑' : '创建'}${props.headerTitle || ''}`) const header = computed(() => `${props.isEdit ? '编辑' : '创建'}${props.headerTitle || ''}`)
const formType = computed(() => `${props.isEdit ? 'update' : 'add'}`)
const confirmBtn = computed(() => (props.isEdit ? '保存' : '确定')) const confirmBtn = computed(() => (props.isEdit ? '保存' : '确定'))
const carousalData: FormProps['data'] = reactive({}) const carousalData: FormProps['data'] = ref({})
const imgUrl = ref({})
const rules = computed(() => { const rules = computed(() => {
const commonRules = { const commonRules = {
imageUrl: [ imageUrl: [
@@ -68,82 +70,32 @@ const rules = computed(() => {
}, },
], ],
}; };
return carousalData.targetType === 'article' return carousalData.value.targetType === 'article'
? {...commonRules, targetId: [{ required: true, message: '文章 id 不能为空', type: 'error', trigger: 'blur' }, { required: true, message: '文章 id 不能为空', type: 'error', trigger: 'change' }, { whitespace: true, message: '网址路径不能为空' }] } ? {...commonRules, targetId: [{ required: true, message: '文章 id 不能为空', type: 'error', trigger: 'blur' }, { required: true, message: '文章 id 不能为空', type: 'error', trigger: 'change' }, { whitespace: true, message: '网址路径不能为空' }] }
: {...commonRules, targetUrl: [{ required: true, message: '网址路径不能为空', type: 'error', trigger: 'blur' }, { required: true, message: '网址路径不能为空', type: 'error', trigger: 'change' }, { whitespace: true, message: '网址路径不能为空' }] }; : {...commonRules, targetUrl: [{ required: true, message: '网址路径不能为空', type: 'error', trigger: 'blur' }, { required: true, message: '网址路径不能为空', type: 'error', trigger: 'change' }, { whitespace: true, message: '网址路径不能为空' }] };
}); });
// const rules: FormProps['rules'] = {
// imageUrl:[
// {
// required: true,
// message:'轮播图必须是上传',
// type:'error',
// trigger: 'change'
// }
// ],
// targetType:[
// {
// required: true,
// message: '请选择跳转类型',
// type: 'error',
// trigger: 'blur'
// },
// {
// required: true,
// message: '请选择跳转类型',
// type: 'error',
// trigger: 'change'
// },
// ],
// targetId:[
// {
// required: true,
// message: '文章id不能为空',
// type: 'error',
// trigger: 'blur'
// },
// {
// required: true,
// message: '文章id不能为空',
// type: 'error',
// trigger: 'change'
// },
// {
// whitespace: true,
// message: '网址路径不能为空'
// },
// ],
// targetUrl:[
// {
// required: true,
// message: '网址路径不能为空',
// type: 'error',
// trigger: 'blur'
// },
// {
// required: true,
// message: '网址路径不能为空',
// type: 'error',
// trigger: 'change'
// },
// {
// whitespace: true,
// message: '网址路径不能为空'
// },
// ],
// }
const handleSuccessImg = (res:string) => { const handleSuccessImg = (res:string) => {
carousalData.imageUrl = res console.log(res,'图片地址')
carousalData.value.imageUrl = res
} }
const typeOption = reactive([ const typeOption = reactive([
{ value: 'article', label: '文章' }, { value: 'article', label: '文章' },
{ value: 'url', label: '地址' }, { value: 'url', label: '地址' },
]) ])
const handleFormTypeData = () => {
if (carousalData.value.targetType === 'article') {
delete carousalData.value.targetUrl
} else if (carousalData.value.targetType === 'url') {
delete carousalData.value.targetId
}
}
watch( watch(
() => props.classify, () => props.classify,
(newClassify) => { (newClassify) => {
Object.assign(carousalData, newClassify) carousalData.value = Object.assign({}, newClassify)
console.log(carousalData) const carousalObj = Object.assign({},newClassify)
imgUrl.value.url = carousalObj.imageUrl || ''
}, },
{ {
deep: true, deep: true,
@@ -157,13 +109,33 @@ const fetchSaveClassifyData = async () => {
// console.log(valid,'valid') // console.log(valid,'valid')
if (valid && typeof valid === 'boolean') { if (valid && typeof valid === 'boolean') {
/* TODO: 校验通过保存分类数据 */ /* TODO: 校验通过保存分类数据 */
if (carousalData.targetType === 'article') { handleFormTypeData()
delete carousalData.targetUrl // console.log(formType.value,'formType')
} else if (carousalData.targetType === 'url') { let res
delete carousalData.targetId switch (formType.value){
case 'create':
res = await createCarousal(carousalData.value)
emit('success', true)
break;
case 'update':
const fieldsToKeep = ["imageUrl", "targetType", "targetUrl", "targetId"];
const requestObj = {};
for (const field of fieldsToKeep) {
if (carousalData.value[field]!== null && carousalData.value[field]!== undefined) {
requestObj[field] = carousalData.value[field];
}
}
handleFormTypeData()
res = await updateCarousalItem(carousalData.value.id,requestObj)
emit('success', res)
break;
default:
res = await createCarousal(carousalData.value)
break;
} }
const createCarousalApi = createCarousal(carousalData) // const createCarousalApi = createCarousal(carousalData.value)
console.log(createCarousalApi,'createCarousalApi') // console.log(createCarousalApi,'createCarousalApi')
return return
} }
} }
+95 -11
View File
@@ -4,18 +4,20 @@ import { onMounted, ref, reactive } from 'vue'
import type { BaseTableColumns } from 'tdesign-vue-next' import type { BaseTableColumns } from 'tdesign-vue-next'
import type { Classify } from '@/model/classify' import type { Classify } from '@/model/classify'
import ClassifyDialog from '@/pages/carousal/components/CarousalDialog.vue' import ClassifyDialog from '@/pages/carousal/components/CarousalDialog.vue'
import { activeCarousal, getCarousalList, inactiveCarousal } from '@/api/carousal' import { activeCarousal, deleteCarousal, getCarousalList, inactiveCarousal } from '@/api/carousal'
import { BrowseIcon } from 'tdesign-icons-vue-next'
import { aS } from 'vitest/dist/reporters-yx5ZTtEV'
const columns: BaseTableColumns = [ const columns: BaseTableColumns = [
{ {
title: 'ID', title: 'ID',
colKey: 'id', colKey: 'id',
width: 100 width: 100,
}, },
{ {
title: '轮播图', title: '轮播图',
colKey: 'imageUrl', colKey: 'imageUrl',
width: 100 width: 100,
}, },
{ {
title: '显示状态', title: '显示状态',
@@ -25,7 +27,7 @@ const columns: BaseTableColumns = [
{ {
title: '操作', title: '操作',
colKey: 'operation', colKey: 'operation',
width: 100 width: 100,
} }
] ]
const loading = ref(false) const loading = ref(false)
@@ -53,27 +55,41 @@ const fetchClassifyTableData = async () => {
onMounted(fetchClassifyTableData) onMounted(fetchClassifyTableData)
const editClick = (row:object) => { const editClick = (row:object) => {
isEdit.value = true isEdit.value = true
currentTableData.value = row currentTableData.value = Object.assign({}, row)
carousalDialogVisible.value = true carousalDialogVisible.value = true
} }
const showClick = async (res:object) => { const showClick = async (res:object) => {
if (res.status === 'active'){ if (res.status === 'active'){
await inactiveCarousal({ id: res.id }) await inactiveCarousal(res.id)
} else { } else {
await activeCarousal({ id: res.id }) await activeCarousal(res.id)
} }
await fetchClassifyTableData() await fetchClassifyTableData()
console.log(res) }
const delClick = async (row:object) => {
console.log(row,'删除')
await deleteCarousal(row.id)
await fetchClassifyTableData()
}
const handleCreateCarousal = () => {
isEdit.value = false
carousalDialogVisible.value = true
currentTableData.value = {}
} }
const onPageChange = () => { const onPageChange = () => {
/* TODO: 分页切换 */ /* TODO: 分页切换 */
} }
const handleSuccessDialog = async (res) => {
if (res){
await fetchClassifyTableData()
}
}
</script> </script>
<template> <template>
<RegularPage title="轮播图管理"> <RegularPage title="轮播图管理">
<template #right-area> <template #right-area>
<TButton @click="carousalDialogVisible = true">创建轮播图</TButton> <TButton @click="handleCreateCarousal">创建轮播图</TButton>
</template> </template>
<TTable <TTable
class="w-full h-full" class="w-full h-full"
@@ -92,6 +108,20 @@ const onPageChange = () => {
onChange: onPageChange onChange: onPageChange
}" }"
> >
<template #imageUrl="{row}">
<div class="tdesign-demo-image-viewer__base">
<t-image-viewer :images="[row.imageUrl]">
<template #trigger="{ open }">
<div class="tdesign-demo-image-viewer__ui-image">
<img alt="test" :src="row.imageUrl" class="tdesign-demo-image-viewer__ui-image--img" />
<div class="tdesign-demo-image-viewer__ui-image--hover" @click="open">
<span><BrowseIcon size="1.4em" /> 预览</span>
</div>
</div>
</template>
</t-image-viewer>
</div>
</template>
<template #status="{row}"> <template #status="{row}">
<TSpace> <TSpace>
<TTag :theme="row.status === 'active' ? 'success' : 'danger'" variant="light">{{row.status === 'active' ? '显示中' : '已关闭'}}</TTag> <TTag :theme="row.status === 'active' ? 'success' : 'danger'" variant="light">{{row.status === 'active' ? '显示中' : '已关闭'}}</TTag>
@@ -100,19 +130,21 @@ const onPageChange = () => {
<template #roles="{ row }"> <template #roles="{ row }">
<TSpace> <TSpace>
<TTag v-for="role in row.roles" :key="role.id" theme="success" variant="light">{{ <TTag v-for="role in row.roles" :key="role.id" theme="success" variant="light">{{
role.label role.label
}}</TTag> }}</TTag>
</TSpace> </TSpace>
</template> </template>
<template #operation="{ row }"> <template #operation="{ row }">
<TButton variant="text" size="small" theme="primary" @click="editClick(row)">编辑</TButton> <TButton variant="text" size="small" theme="primary" @click="editClick(row)">编辑</TButton>
<TButton variant="text" size="small" theme="primary" @click="showClick(row)">{{ row.status === 'active' ? '关闭显示' : '开启显示'}}</TButton> <TButton variant="text" size="small" theme="primary" @click="showClick(row)">{{ row.status === 'active' ? '关闭显示' : '开启显示'}}</TButton>
<TButton variant="text" size="small" theme="danger" @click="delClick(row)">删除</TButton>
</template> </template>
</TTable> </TTable>
<ClassifyDialog <ClassifyDialog
:classify="currentTableData" :classify="currentTableData"
v-model:visible="carousalDialogVisible" v-model:visible="carousalDialogVisible"
:isEdit="isEdit" :isEdit="isEdit"
@success="handleSuccessDialog"
headerTitle="轮播图" headerTitle="轮播图"
></ClassifyDialog> ></ClassifyDialog>
</RegularPage> </RegularPage>
@@ -123,4 +155,56 @@ const onPageChange = () => {
flex: 1; flex: 1;
min-height: 0; min-height: 0;
} }
.tdesign-demo-image-viewer__ui-image {
width: 100%;
height: 100%;
display: inline-flex;
position: relative;
justify-content: center;
align-items: center;
border-radius: var(--td-radius-small);
overflow: hidden;
}
.tdesign-demo-image-viewer__ui-image--hover {
width: 100%;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
position: absolute;
left: 0;
top: 0;
opacity: 0;
background-color: rgba(0, 0, 0, 0.6);
color: var(--td-text-color-anti);
line-height: 22px;
transition: 0.2s;
}
.tdesign-demo-image-viewer__ui-image:hover .tdesign-demo-image-viewer__ui-image--hover {
opacity: 1;
cursor: pointer;
}
.tdesign-demo-image-viewer__ui-image--img {
width: auto;
height: auto;
max-width: 100%;
max-height: 100%;
cursor: pointer;
position: absolute;
}
.tdesign-demo-image-viewer__ui-image--icons .tdesign-demo-icon {
cursor: pointer;
}
.tdesign-demo-image-viewer__base {
width: 100px;
height: 100px;
border: 4px solid var(--td-bg-color-secondarycontainer);
border-radius: var(--td-radius-medium);
}
</style> </style>