This commit is contained in:
2026-01-21 10:35:45 +08:00
parent 8d4290e131
commit 431d2c7071
3 changed files with 506 additions and 645 deletions
+34 -27
View File
@@ -32,8 +32,7 @@
</template>
<script setup>
import { ref, reactive, watch, onMounted } from 'vue'
import { useI18n } from 'vue-i18n'
import { ref, reactive, watch } from 'vue'
import systemApi from '@/api/system'
// 定义组件名称
@@ -41,8 +40,6 @@ defineOptions({
name: 'AreaModal',
})
const { t } = useI18n()
const props = defineProps({
visible: {
type: Boolean,
@@ -78,7 +75,7 @@ const formData = reactive({
const fetchAreaTree = async () => {
try {
const res = await systemApi.area.list.get({ pageSize: 1000 })
if (res.code === 200) {
if (res.code === 200 || res.code === 1) {
const list = res.data.list || res.data || []
areaTreeData.value = buildTree(list)
}
@@ -93,12 +90,12 @@ const buildTree = (list) => {
const roots = []
// 创建映射,以 code 作为键
list.forEach(item => {
list.forEach((item) => {
map[item.code] = { ...item, children: [] }
})
// 构建树,通过 parent_code 关联
list.forEach(item => {
list.forEach((item) => {
if (item.parent_code && map[item.parent_code]) {
map[item.parent_code].children.push(map[item.code])
} else if (item.parent_code === '0' || !item.parent_code) {
@@ -110,36 +107,46 @@ const buildTree = (list) => {
}
// 监听弹窗显示和初始数据变化
watch(() => props.initialData, (newVal) => {
if (props.isEdit && Object.keys(newVal).length > 0) {
formData.id = newVal.id || null
formData.code = newVal.code || ''
formData.title = newVal.title || ''
formData.parent_code = newVal.parent_code || null
formData.status = newVal.status ?? 1
}
}, { immediate: true })
watch(
() => props.initialData,
(newVal) => {
if (props.isEdit && Object.keys(newVal).length > 0) {
formData.id = newVal.id || null
formData.code = newVal.code || ''
formData.title = newVal.title || ''
formData.parent_code = newVal.parent_code || null
formData.status = newVal.status ?? 1
}
},
{ immediate: true },
)
// 监听弹窗打开,加载地区树数据
watch(() => props.visible, (newVal) => {
if (newVal) {
fetchAreaTree()
}
})
watch(
() => props.visible,
(newVal) => {
if (newVal) {
fetchAreaTree()
}
},
)
// 监听弹窗关闭,重置表单
watch(() => props.visible, (newVal) => {
if (!newVal) {
handleClose()
}
})
watch(
() => props.visible,
(newVal) => {
if (!newVal) {
handleClose()
}
},
)
// 确认提交
const handleConfirm = async () => {
try {
const values = await formRef.value.validate()
emit('confirm', values)
} catch (error) {
} catch {
// 表单验证错误,不处理
}
}