1
This commit is contained in:
92
src/views/shop/shopSpecValue/components/search.vue
Normal file
92
src/views/shop/shopSpecValue/components/search.vue
Normal file
@@ -0,0 +1,92 @@
|
||||
<!-- 搜索表单 -->
|
||||
<template>
|
||||
<a-space :size="10" style="flex-wrap: wrap">
|
||||
<a-select
|
||||
v-model:value="specId"
|
||||
placeholder="筛选规格组"
|
||||
allow-clear
|
||||
style="width: 160px"
|
||||
:loading="specLoading"
|
||||
@change="search"
|
||||
>
|
||||
<a-select-option
|
||||
v-for="spec in specList"
|
||||
:key="spec.specId"
|
||||
:value="spec.specId"
|
||||
>
|
||||
{{ spec.specName }}
|
||||
</a-select-option>
|
||||
</a-select>
|
||||
<a-input-search
|
||||
v-model:value="keywords"
|
||||
placeholder="搜索规格值"
|
||||
allow-clear
|
||||
style="width: 180px"
|
||||
@search="search"
|
||||
/>
|
||||
<a-button type="primary" class="ele-btn-icon" @click="add">
|
||||
<template #icon><PlusOutlined /></template>
|
||||
<span>添加规格值</span>
|
||||
</a-button>
|
||||
<a-button
|
||||
danger
|
||||
class="ele-btn-icon"
|
||||
:disabled="!selection?.length"
|
||||
@click="remove"
|
||||
>
|
||||
<template #icon><DeleteOutlined /></template>
|
||||
<span>批量删除</span>
|
||||
</a-button>
|
||||
</a-space>
|
||||
</template>
|
||||
|
||||
<script lang="ts" setup>
|
||||
import { ref, onMounted } from 'vue';
|
||||
import { PlusOutlined, DeleteOutlined } from '@ant-design/icons-vue';
|
||||
import type { ShopSpecValueParam } from '@/api/shop/shopSpecValue/model';
|
||||
import { listShopSpec } from '@/api/shop/shopSpec';
|
||||
import type { ShopSpec } from '@/api/shop/shopSpec/model';
|
||||
|
||||
const props = withDefaults(
|
||||
defineProps<{
|
||||
selection?: any[];
|
||||
}>(),
|
||||
{}
|
||||
);
|
||||
|
||||
const emit = defineEmits<{
|
||||
(e: 'search', where?: ShopSpecValueParam): void;
|
||||
(e: 'add'): void;
|
||||
(e: 'remove'): void;
|
||||
}>();
|
||||
|
||||
const keywords = ref('');
|
||||
const specId = ref<number | undefined>(undefined);
|
||||
const specList = ref<ShopSpec[]>([]);
|
||||
const specLoading = ref(false);
|
||||
|
||||
const loadSpecList = async () => {
|
||||
specLoading.value = true;
|
||||
try {
|
||||
specList.value = await listShopSpec() || [];
|
||||
} catch (e) {
|
||||
// ignore
|
||||
} finally {
|
||||
specLoading.value = false;
|
||||
}
|
||||
};
|
||||
|
||||
const search = () => {
|
||||
emit('search', {
|
||||
keywords: keywords.value || undefined,
|
||||
specId: specId.value
|
||||
} as any);
|
||||
};
|
||||
|
||||
const add = () => emit('add');
|
||||
const remove = () => emit('remove');
|
||||
|
||||
onMounted(() => {
|
||||
loadSpecList();
|
||||
});
|
||||
</script>
|
||||
236
src/views/shop/shopSpecValue/components/shopSpecValueEdit.vue
Normal file
236
src/views/shop/shopSpecValue/components/shopSpecValueEdit.vue
Normal file
@@ -0,0 +1,236 @@
|
||||
<!-- 规格值编辑弹窗 -->
|
||||
<template>
|
||||
<ele-modal
|
||||
:width="520"
|
||||
:visible="visible"
|
||||
:maskClosable="false"
|
||||
:title="isUpdate ? '编辑规格值' : '添加规格值'"
|
||||
:body-style="{ paddingBottom: '28px' }"
|
||||
@update:visible="updateVisible"
|
||||
@ok="save"
|
||||
>
|
||||
<a-form
|
||||
ref="formRef"
|
||||
:model="form"
|
||||
:rules="rules"
|
||||
:label-col="{ flex: '80px' }"
|
||||
:wrapper-col="{ flex: '1' }"
|
||||
>
|
||||
<a-form-item label="所属规格" name="specId">
|
||||
<a-select
|
||||
v-model:value="form.specId"
|
||||
placeholder="请选择所属规格组"
|
||||
:loading="specLoading"
|
||||
allow-clear
|
||||
show-search
|
||||
:filter-option="filterSpec"
|
||||
>
|
||||
<a-select-option
|
||||
v-for="spec in specList"
|
||||
:key="spec.specId"
|
||||
:value="spec.specId"
|
||||
>
|
||||
{{ spec.specName }}
|
||||
</a-select-option>
|
||||
</a-select>
|
||||
</a-form-item>
|
||||
<a-form-item label="规格值" name="specValue">
|
||||
<a-input
|
||||
allow-clear
|
||||
placeholder="如:红色、XL、棉质"
|
||||
v-model:value="form.specValue"
|
||||
/>
|
||||
</a-form-item>
|
||||
<a-form-item label="图片" name="image">
|
||||
<div class="image-row">
|
||||
<a-image
|
||||
v-if="form.image"
|
||||
:src="form.image"
|
||||
:width="56"
|
||||
:height="56"
|
||||
style="object-fit: cover; border-radius: 4px; margin-right: 8px"
|
||||
/>
|
||||
<a-upload
|
||||
:show-upload-list="false"
|
||||
accept="image/*"
|
||||
:before-upload="(file: File) => handleImageUpload(file)"
|
||||
>
|
||||
<a-button size="small">
|
||||
<template #icon><UploadOutlined /></template>
|
||||
{{ form.image ? '更换图片' : '上传图片' }}
|
||||
</a-button>
|
||||
</a-upload>
|
||||
<a-button
|
||||
v-if="form.image"
|
||||
size="small"
|
||||
danger
|
||||
style="margin-left: 6px"
|
||||
@click="form.image = ''"
|
||||
>删除</a-button>
|
||||
</div>
|
||||
<div class="ele-text-secondary" style="font-size: 12px; margin-top: 4px">
|
||||
适用于颜色类规格,上传颜色色板图片(可选)
|
||||
</div>
|
||||
</a-form-item>
|
||||
<a-form-item label="排序号" name="sortNumber">
|
||||
<a-input-number
|
||||
:min="0"
|
||||
:max="9999"
|
||||
class="ele-fluid"
|
||||
placeholder="数值越小越靠前"
|
||||
v-model:value="form.sortNumber"
|
||||
/>
|
||||
</a-form-item>
|
||||
<a-form-item label="备注" name="comments">
|
||||
<a-input
|
||||
allow-clear
|
||||
placeholder="可选"
|
||||
v-model:value="form.comments"
|
||||
/>
|
||||
</a-form-item>
|
||||
</a-form>
|
||||
</ele-modal>
|
||||
</template>
|
||||
|
||||
<script lang="ts" setup>
|
||||
import { ref, reactive, watch, onMounted } from 'vue';
|
||||
import { message } from 'ant-design-vue';
|
||||
import { assignObject } from 'ele-admin-pro';
|
||||
import { UploadOutlined } from '@ant-design/icons-vue';
|
||||
import { addShopSpecValue, updateShopSpecValue } from '@/api/shop/shopSpecValue';
|
||||
import type { ShopSpecValue } from '@/api/shop/shopSpecValue/model';
|
||||
import { listShopSpec } from '@/api/shop/shopSpec';
|
||||
import type { ShopSpec } from '@/api/shop/shopSpec/model';
|
||||
import { uploadFile } from '@/api/system/file';
|
||||
import { FormInstance } from 'ant-design-vue/es/form';
|
||||
|
||||
const isUpdate = ref(false);
|
||||
|
||||
const props = defineProps<{
|
||||
visible: boolean;
|
||||
data?: ShopSpecValue | null;
|
||||
/** 可传入预设的 specId(从规格组页面跳转过来时) */
|
||||
defaultSpecId?: number;
|
||||
}>();
|
||||
|
||||
const emit = defineEmits<{
|
||||
(e: 'done'): void;
|
||||
(e: 'update:visible', visible: boolean): void;
|
||||
}>();
|
||||
|
||||
const loading = ref(false);
|
||||
const formRef = ref<FormInstance | null>(null);
|
||||
|
||||
// 规格组列表
|
||||
const specList = ref<ShopSpec[]>([]);
|
||||
const specLoading = ref(false);
|
||||
|
||||
const form = reactive<ShopSpecValue>({
|
||||
specValueId: undefined,
|
||||
specId: undefined,
|
||||
specValue: undefined,
|
||||
image: '',
|
||||
comments: undefined,
|
||||
sortNumber: 100
|
||||
});
|
||||
|
||||
const updateVisible = (value: boolean) => {
|
||||
emit('update:visible', value);
|
||||
};
|
||||
|
||||
// 表单验证规则
|
||||
const rules: Record<string, any> = {
|
||||
specId: [{ required: true, message: '请选择所属规格组', trigger: 'change' }],
|
||||
specValue: [
|
||||
{ required: true, type: 'string', message: '请填写规格值', trigger: 'blur' }
|
||||
]
|
||||
};
|
||||
|
||||
// 加载规格组列表
|
||||
const loadSpecList = async () => {
|
||||
specLoading.value = true;
|
||||
try {
|
||||
specList.value = await listShopSpec({ status: 0 } as any) || [];
|
||||
} catch (e) {
|
||||
// 忽略
|
||||
} finally {
|
||||
specLoading.value = false;
|
||||
}
|
||||
};
|
||||
|
||||
// 规格搜索过滤
|
||||
const filterSpec = (input: string, option: any) => {
|
||||
return (option?.children?.[0] || '').toLowerCase().includes(input.toLowerCase());
|
||||
};
|
||||
|
||||
// 图片上传
|
||||
const handleImageUpload = async (file: File) => {
|
||||
try {
|
||||
const res = await uploadFile(file);
|
||||
form.image = res.path;
|
||||
message.success('上传成功');
|
||||
} catch (e: any) {
|
||||
message.error(e.message || '上传失败');
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
const save = () => {
|
||||
if (!formRef.value) return;
|
||||
formRef.value
|
||||
.validate()
|
||||
.then(() => {
|
||||
loading.value = true;
|
||||
const saveOrUpdate = isUpdate.value ? updateShopSpecValue : addShopSpecValue;
|
||||
saveOrUpdate({ ...form })
|
||||
.then((msg) => {
|
||||
loading.value = false;
|
||||
message.success(msg);
|
||||
updateVisible(false);
|
||||
emit('done');
|
||||
})
|
||||
.catch((e) => {
|
||||
loading.value = false;
|
||||
message.error(e.message);
|
||||
});
|
||||
})
|
||||
.catch(() => {});
|
||||
};
|
||||
|
||||
onMounted(() => {
|
||||
loadSpecList();
|
||||
});
|
||||
|
||||
watch(
|
||||
() => props.visible,
|
||||
(visible) => {
|
||||
if (visible) {
|
||||
if (props.data) {
|
||||
assignObject(form, props.data);
|
||||
if (!form.image) form.image = '';
|
||||
isUpdate.value = true;
|
||||
} else {
|
||||
Object.assign(form, {
|
||||
specValueId: undefined,
|
||||
specId: props.defaultSpecId,
|
||||
specValue: undefined,
|
||||
image: '',
|
||||
comments: undefined,
|
||||
sortNumber: 100
|
||||
});
|
||||
isUpdate.value = false;
|
||||
}
|
||||
} else {
|
||||
formRef.value?.resetFields();
|
||||
}
|
||||
},
|
||||
{ immediate: true }
|
||||
);
|
||||
</script>
|
||||
|
||||
<style lang="less" scoped>
|
||||
.image-row {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
}
|
||||
</style>
|
||||
266
src/views/shop/shopSpecValue/index.vue
Normal file
266
src/views/shop/shopSpecValue/index.vue
Normal file
@@ -0,0 +1,266 @@
|
||||
<template>
|
||||
<div class="page">
|
||||
<div class="ele-body">
|
||||
<a-card :bordered="false" :body-style="{ padding: '16px' }">
|
||||
<ele-pro-table
|
||||
ref="tableRef"
|
||||
row-key="specValueId"
|
||||
:columns="columns"
|
||||
:datasource="datasource"
|
||||
:customRow="customRow"
|
||||
v-model:selection="selection"
|
||||
tool-class="ele-toolbar-form"
|
||||
class="sys-org-table"
|
||||
>
|
||||
<template #toolbar>
|
||||
<search
|
||||
@search="reload"
|
||||
:selection="selection"
|
||||
@add="openEdit"
|
||||
@remove="removeBatch"
|
||||
/>
|
||||
</template>
|
||||
<template #bodyCell="{ column, record }">
|
||||
<template v-if="column.key === 'specId'">
|
||||
{{ specNameMap[record.specId] || record.specId }}
|
||||
</template>
|
||||
<template v-if="column.key === 'image'">
|
||||
<a-image
|
||||
v-if="record.image"
|
||||
:src="record.image"
|
||||
:width="36"
|
||||
:height="36"
|
||||
style="object-fit: cover; border-radius: 4px"
|
||||
/>
|
||||
<span v-else class="ele-text-secondary">-</span>
|
||||
</template>
|
||||
<template v-if="column.key === 'action'">
|
||||
<a-space>
|
||||
<a @click="openEdit(record)">编辑</a>
|
||||
<a-divider type="vertical" />
|
||||
<a-popconfirm
|
||||
title="确定要删除此规格值吗?"
|
||||
@confirm="remove(record)"
|
||||
>
|
||||
<a class="ele-text-danger">删除</a>
|
||||
</a-popconfirm>
|
||||
</a-space>
|
||||
</template>
|
||||
</template>
|
||||
</ele-pro-table>
|
||||
</a-card>
|
||||
|
||||
<!-- 编辑弹窗 -->
|
||||
<ShopSpecValueEdit
|
||||
v-model:visible="showEdit"
|
||||
:data="current"
|
||||
@done="reload"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script lang="ts" setup>
|
||||
import { createVNode, ref, reactive, onMounted } from 'vue';
|
||||
import { message, Modal } from 'ant-design-vue';
|
||||
import { ExclamationCircleOutlined } from '@ant-design/icons-vue';
|
||||
import type { EleProTable } from 'ele-admin-pro';
|
||||
import { toDateString } from 'ele-admin-pro';
|
||||
import type {
|
||||
DatasourceFunction,
|
||||
ColumnItem
|
||||
} from 'ele-admin-pro/es/ele-pro-table/types';
|
||||
import Search from './components/search.vue';
|
||||
import ShopSpecValueEdit from './components/shopSpecValueEdit.vue';
|
||||
import {
|
||||
pageShopSpecValue,
|
||||
removeShopSpecValue,
|
||||
removeBatchShopSpecValue
|
||||
} from '@/api/shop/shopSpecValue';
|
||||
import type {
|
||||
ShopSpecValue,
|
||||
ShopSpecValueParam
|
||||
} from '@/api/shop/shopSpecValue/model';
|
||||
import { listShopSpec } from '@/api/shop/shopSpec';
|
||||
|
||||
// 表格实例
|
||||
const tableRef = ref<InstanceType<typeof EleProTable> | null>(null);
|
||||
|
||||
// 表格选中数据
|
||||
const selection = ref<ShopSpecValue[]>([]);
|
||||
// 当前编辑数据
|
||||
const current = ref<ShopSpecValue | null>(null);
|
||||
// 是否显示编辑弹窗
|
||||
const showEdit = ref(false);
|
||||
|
||||
// specId -> specName 映射表
|
||||
const specNameMap = reactive<Record<number, string>>({});
|
||||
|
||||
// 表格数据源
|
||||
const datasource: DatasourceFunction = ({
|
||||
page,
|
||||
limit,
|
||||
where,
|
||||
orders
|
||||
}) => {
|
||||
return pageShopSpecValue({
|
||||
...where,
|
||||
...orders,
|
||||
page,
|
||||
limit
|
||||
});
|
||||
};
|
||||
|
||||
// 表格列配置
|
||||
const columns = ref<ColumnItem[]>([
|
||||
{
|
||||
title: 'ID',
|
||||
dataIndex: 'specValueId',
|
||||
key: 'specValueId',
|
||||
align: 'center',
|
||||
width: 70
|
||||
},
|
||||
{
|
||||
title: '所属规格',
|
||||
dataIndex: 'specId',
|
||||
key: 'specId',
|
||||
align: 'center',
|
||||
width: 120
|
||||
},
|
||||
{
|
||||
title: '规格值',
|
||||
dataIndex: 'specValue',
|
||||
key: 'specValue',
|
||||
align: 'left',
|
||||
minWidth: 120
|
||||
},
|
||||
{
|
||||
title: '图片',
|
||||
dataIndex: 'image',
|
||||
key: 'image',
|
||||
align: 'center',
|
||||
width: 70
|
||||
},
|
||||
{
|
||||
title: '排序',
|
||||
dataIndex: 'sortNumber',
|
||||
key: 'sortNumber',
|
||||
align: 'center',
|
||||
width: 80,
|
||||
sorter: true
|
||||
},
|
||||
{
|
||||
title: '备注',
|
||||
dataIndex: 'comments',
|
||||
key: 'comments',
|
||||
align: 'left',
|
||||
ellipsis: true
|
||||
},
|
||||
{
|
||||
title: '创建时间',
|
||||
dataIndex: 'createTime',
|
||||
key: 'createTime',
|
||||
align: 'center',
|
||||
width: 120,
|
||||
sorter: true,
|
||||
customRender: ({ text }) => toDateString(text, 'yyyy-MM-dd')
|
||||
},
|
||||
{
|
||||
title: '操作',
|
||||
key: 'action',
|
||||
width: 140,
|
||||
fixed: 'right',
|
||||
align: 'center',
|
||||
hideInSetting: true
|
||||
}
|
||||
]);
|
||||
|
||||
/* 搜索 */
|
||||
const reload = (where?: ShopSpecValueParam) => {
|
||||
selection.value = [];
|
||||
tableRef?.value?.reload({ where: where });
|
||||
};
|
||||
|
||||
/* 打开编辑弹窗 */
|
||||
const openEdit = (row?: ShopSpecValue) => {
|
||||
current.value = row ?? null;
|
||||
showEdit.value = true;
|
||||
};
|
||||
|
||||
/* 删除单个 */
|
||||
const remove = (row: ShopSpecValue) => {
|
||||
const hide = message.loading('请求中..', 0);
|
||||
removeShopSpecValue(row.specValueId)
|
||||
.then((msg) => {
|
||||
hide();
|
||||
message.success(msg);
|
||||
reload();
|
||||
})
|
||||
.catch((e) => {
|
||||
hide();
|
||||
message.error(e.message);
|
||||
});
|
||||
};
|
||||
|
||||
/* 批量删除 */
|
||||
const removeBatch = () => {
|
||||
if (!selection.value.length) {
|
||||
message.error('请至少选择一条数据');
|
||||
return;
|
||||
}
|
||||
Modal.confirm({
|
||||
title: '提示',
|
||||
content: '确定要删除选中的规格值吗?',
|
||||
icon: createVNode(ExclamationCircleOutlined),
|
||||
maskClosable: true,
|
||||
onOk: () => {
|
||||
const hide = message.loading('请求中..', 0);
|
||||
removeBatchShopSpecValue(selection.value.map((d) => d.specValueId))
|
||||
.then((msg) => {
|
||||
hide();
|
||||
message.success(msg);
|
||||
reload();
|
||||
})
|
||||
.catch((e) => {
|
||||
hide();
|
||||
message.error(e.message);
|
||||
});
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
/* 自定义行属性 */
|
||||
const customRow = (record: ShopSpecValue) => {
|
||||
return {
|
||||
onDblclick: () => {
|
||||
openEdit(record);
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
// 加载规格名称映射表
|
||||
const loadSpecNames = async () => {
|
||||
try {
|
||||
const list = await listShopSpec();
|
||||
(list || []).forEach((s) => {
|
||||
if (s.specId !== undefined) {
|
||||
specNameMap[s.specId] = s.specName || String(s.specId);
|
||||
}
|
||||
});
|
||||
} catch (e) {
|
||||
// ignore
|
||||
}
|
||||
};
|
||||
|
||||
onMounted(() => {
|
||||
loadSpecNames();
|
||||
});
|
||||
</script>
|
||||
|
||||
<script lang="ts">
|
||||
export default {
|
||||
name: 'ShopSpecValue'
|
||||
};
|
||||
</script>
|
||||
|
||||
<style lang="less" scoped></style>
|
||||
Reference in New Issue
Block a user