Initial commit
This commit is contained in:
127
src/components/SelectGoodsCategory/index.vue
Normal file
127
src/components/SelectGoodsCategory/index.vue
Normal file
@@ -0,0 +1,127 @@
|
||||
<!-- 省市区级联选择器 -->
|
||||
<template>
|
||||
<a-cascader
|
||||
:value="value"
|
||||
:options="regionsData"
|
||||
:show-search="showSearch"
|
||||
:placeholder="placeholder"
|
||||
dropdown-class-name="ele-pop-wrap-higher"
|
||||
@update:value="updateValue"
|
||||
@change="onChange"
|
||||
/>
|
||||
</template>
|
||||
|
||||
<script lang="ts" setup>
|
||||
import { ref, watch } from 'vue';
|
||||
import type { ValueType } from 'ant-design-vue/es/vc-cascader/Cascader';
|
||||
import { listGoodsCategory } from '@/api/shop/goodsCategory';
|
||||
import { toTreeData } from 'ele-admin-pro/es';
|
||||
import { GoodsCategory } from '@/api/shop/goodsCategory/model';
|
||||
|
||||
const props = withDefaults(
|
||||
defineProps<{
|
||||
value?: string[];
|
||||
placeholder?: string;
|
||||
options?: GoodsCategory[];
|
||||
valueField?: 'label';
|
||||
type?: 'provinceCity' | 'province';
|
||||
showSearch?: boolean;
|
||||
}>(),
|
||||
{
|
||||
showSearch: true
|
||||
}
|
||||
);
|
||||
|
||||
const emit = defineEmits<{
|
||||
(e: 'done', item?: any, value?: ValueType);
|
||||
(e: 'update:value', value?: string[]): void;
|
||||
(e: 'load-data-done', value: GoodsCategory[]): void;
|
||||
}>();
|
||||
|
||||
// 级联选择器数据
|
||||
const regionsData = ref<GoodsCategory[]>([]);
|
||||
|
||||
/* 更新 value */
|
||||
const updateValue = (value: ValueType) => {
|
||||
emit('update:value', value as string[]);
|
||||
};
|
||||
|
||||
const onChange = (item: any, value: ValueType) => {
|
||||
emit('done', item, value);
|
||||
};
|
||||
|
||||
/* 级联选择器数据 value 处理 */
|
||||
const formatData = (data: GoodsCategory[]) => {
|
||||
if (props.valueField === 'label') {
|
||||
return data.map((d) => {
|
||||
const item: GoodsCategory = {
|
||||
label: d.title,
|
||||
value: d.categoryId
|
||||
};
|
||||
if (d.children) {
|
||||
item.children = d.children.map((c) => {
|
||||
const cItem: GoodsCategory = {
|
||||
label: c.title,
|
||||
value: c.categoryId
|
||||
};
|
||||
if (c.children) {
|
||||
cItem.children = c.children.map((cc) => {
|
||||
return {
|
||||
label: cc.title,
|
||||
value: cc.categoryId
|
||||
};
|
||||
});
|
||||
}
|
||||
return cItem;
|
||||
});
|
||||
}
|
||||
return item;
|
||||
});
|
||||
} else {
|
||||
return data;
|
||||
}
|
||||
};
|
||||
|
||||
/* 省市区数据筛选 */
|
||||
const filterData = (data: GoodsCategory[]) => {
|
||||
return formatData(
|
||||
data.map((d) => {
|
||||
const item: GoodsCategory = {
|
||||
label: d.title,
|
||||
value: d.categoryId
|
||||
};
|
||||
if (d.children) {
|
||||
item.children = d.children.map((c) => {
|
||||
return {
|
||||
label: c.title,
|
||||
value: c.categoryId
|
||||
};
|
||||
});
|
||||
}
|
||||
return item;
|
||||
})
|
||||
);
|
||||
};
|
||||
|
||||
watch(
|
||||
() => props.options,
|
||||
() => {
|
||||
listGoodsCategory().then((data) => {
|
||||
const list = toTreeData({
|
||||
data: data?.map((d) => {
|
||||
d.value = d.categoryId;
|
||||
d.label = d.title;
|
||||
return d;
|
||||
}),
|
||||
idField: 'categoryId',
|
||||
parentIdField: 'parentId'
|
||||
});
|
||||
regionsData.value = filterData(list ?? []);
|
||||
emit('load-data-done', data);
|
||||
});
|
||||
},
|
||||
{
|
||||
immediate: true
|
||||
}
|
||||
);
|
||||
</script>
|
||||
25
src/components/SelectGoodsCategory/load-data.ts
Normal file
25
src/components/SelectGoodsCategory/load-data.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import request from '@/utils/request';
|
||||
import type { IndustryData } from './types';
|
||||
const BASE_URL = import.meta.env.BASE_URL;
|
||||
let reqPromise: Promise<IndustryData[]>;
|
||||
|
||||
/**
|
||||
* 获取省市区数据
|
||||
*/
|
||||
export function getIndustryData() {
|
||||
if (!reqPromise) {
|
||||
reqPromise = new Promise<IndustryData[]>((resolve, reject) => {
|
||||
request
|
||||
.get<IndustryData[]>(BASE_URL + 'json/industry-data.json', {
|
||||
baseURL: ''
|
||||
})
|
||||
.then((res) => {
|
||||
resolve(res.data ?? []);
|
||||
})
|
||||
.catch((e) => {
|
||||
reject(e);
|
||||
});
|
||||
});
|
||||
}
|
||||
return reqPromise;
|
||||
}
|
||||
15
src/components/SelectGoodsCategory/types/index.ts
Normal file
15
src/components/SelectGoodsCategory/types/index.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
/**
|
||||
* 行业类型
|
||||
*/
|
||||
export interface IndustryData {
|
||||
label: string;
|
||||
value: string;
|
||||
children?: {
|
||||
value: string;
|
||||
label: string;
|
||||
children?: {
|
||||
value: string;
|
||||
label: string;
|
||||
}[];
|
||||
}[];
|
||||
}
|
||||
Reference in New Issue
Block a user