This commit is contained in:
@jyuhsin
2025-09-05 15:10:51 +08:00
parent 43fd3c07fd
commit 6ad78e51ae
8 changed files with 310 additions and 216 deletions
@@ -1,12 +1,19 @@
<template>
<div>
<template v-for="item in options">
<el-tag :key="item[value_key]" v-if="item[value_key] === value" v-bind="$attrs"
:type="item.type"
>{{ item[label_key] }}
</el-tag>
</template>
</div>
<div>
<template v-if="mode === 'pc'" v-for="item in options">
<el-tag :key="item[value_key]" v-if="item[value_key] === value" v-bind="$attrs"
:type="item.type"
>{{ item[label_key] }}
</el-tag>
</template>
<template v-if="mode === 'h5'" v-for="item in options">
<van-tag :key="item[value_key]" v-if="item[value_key] === value" v-bind="$attrs"
:type="item.type">
{{ item[label_key] }}
</van-tag>
</template>
</div>
</template>
<script>
@@ -15,73 +22,80 @@ const enumCache = {}
const requestPromises = {}
module.exports = {
name: "DictTag",
props: {
value: {type: String | Number},
name: {type: String},
value_key: {
type: String,
default: "code"
name: "DictTag",
props: {
value: { type: String | Number },
name: { type: String },
value_key: {
type: String,
default: "code"
},
label_key: {
type: String,
default: "name"
},
mode: {
type: String,
default: "pc"
}
},
label_key: {
type: String,
default: "name"
}
},
data() {
return {
options: []
}
},
watch: {
code: {
handler(val) {
this.getEnumOptions()
},
immediate: true
}
},
methods: {
getEnumOptions() {
// // 检查数据缓存
// if (enumCache[this.name]) {
// this.options = enumCache[this.name]
// return
// }
//
// // 检查是否已有相同请求在进行中
// if (requestPromises[this.name]) {
// requestPromises[this.name].then(data => {
// this.options = data
// })
// return
// }
data() {
return {
options: []
}
},
watch: {
code: {
handler(val) {
this.getEnumOptions()
},
immediate: true
}
},
methods: {
getEnumOptions() {
// // 检查数据缓存
// if (enumCache[this.name]) {
// this.options = enumCache[this.name]
// return
// }
//
// // 检查是否已有相同请求在进行中
// if (requestPromises[this.name]) {
// requestPromises[this.name].then(data => {
// this.options = data
// })
// return
// }
// 创建请求Promise并缓存
requestPromises[this.name] = $.get("/open/common/dictEnumOptions", {name: this.name})
.then(res => {
if (res.code === 0) {
// 存入数据缓存
enumCache[this.name] = res.data
// 清除请求Promise缓存
delete requestPromises[this.name]
return res.data
}
})
// 创建请求Promise并缓存
requestPromises[this.name] = $.get("/open/common/dictEnumOptions", { name: this.name })
.then(res => {
if (res.code === 0) {
// 存入数据缓存
enumCache[this.name] = res.data
// 清除请求Promise缓存
delete requestPromises[this.name]
return res.data
}
})
requestPromises[this.name].then(data => {
this.options = data
})
}
},
created() {
requestPromises[this.name].then(data => {
this.options = data
})
}
},
created() {
}
}
</script>
<style scoped>
.el-tag + .el-tag {
margin-left: 10px;
margin-left: 10px;
}
.van-tag {
padding: 4px 8px;
}
</style>
@@ -105,6 +105,7 @@ module.exports = {
}
})
} else {
val = JSON.parse(val)
this.fileList = val.map((v) => {
return {
...v,