This commit is contained in:
198
src/components/business/BusinessEntitySelect.vue
Normal file
198
src/components/business/BusinessEntitySelect.vue
Normal file
@@ -0,0 +1,198 @@
|
||||
<template>
|
||||
<ElSelect
|
||||
v-model="selection"
|
||||
class="business-entity-select"
|
||||
:multiple="multiple"
|
||||
:disabled="disabled || !allowed"
|
||||
:clearable="clearable"
|
||||
:placeholder="placeholder || `搜索并选择${entityLabel}`"
|
||||
filterable
|
||||
remote
|
||||
remote-show-suffix
|
||||
:remote-method="search"
|
||||
:loading="loading"
|
||||
:no-data-text="error || '暂无匹配结果,请更换关键词'"
|
||||
@visible-change="(visible) => visible && search('')"
|
||||
>
|
||||
<ElOption
|
||||
v-for="option in options"
|
||||
:key="option.value"
|
||||
:value="option.value"
|
||||
:label="option.label"
|
||||
/>
|
||||
<template v-if="hasMore || error" #footer>
|
||||
<span v-if="error" role="alert">{{ error }}</span>
|
||||
<ElButton v-if="error" link :loading="loading" @click="load(1)">重试</ElButton>
|
||||
<ElButton v-else link :loading="loading" @click="load(page + 1)">加载更多</ElButton>
|
||||
</template>
|
||||
</ElSelect>
|
||||
</template>
|
||||
<script setup lang="ts">
|
||||
import { computed, ref, watch, onActivated, onDeactivated, onBeforeUnmount } from 'vue'
|
||||
import {
|
||||
searchBusinessOptions,
|
||||
resolveBusinessOption,
|
||||
BUSINESS_OPTION_LABELS,
|
||||
type BusinessOptionKind,
|
||||
type BusinessOption
|
||||
} from '@/api/modules/businessOptions'
|
||||
import { useUserStore } from '@/store/modules/user'
|
||||
import { pollingScope } from '@/utils/business/pollingScheduler'
|
||||
import { gatewayError } from '@/utils/business/gateway'
|
||||
|
||||
const props = withDefaults(
|
||||
defineProps<{
|
||||
modelValue?: string | number | (string | number)[] | null
|
||||
entity: BusinessOptionKind
|
||||
multiple?: boolean
|
||||
clearable?: boolean
|
||||
disabled?: boolean
|
||||
allowed?: boolean
|
||||
placeholder?: string
|
||||
context?: { asset_identifier?: string; package_id?: number }
|
||||
includeSystem?: boolean
|
||||
}>(),
|
||||
{ clearable: true, allowed: true }
|
||||
)
|
||||
const emit = defineEmits<{
|
||||
'update:modelValue': [value: string | number | (string | number)[] | undefined]
|
||||
}>()
|
||||
const user = useUserStore(),
|
||||
candidates = ref<BusinessOption[]>([]),
|
||||
retained = ref<BusinessOption[]>([])
|
||||
const loading = ref(false),
|
||||
error = ref(''),
|
||||
hasMore = ref(false),
|
||||
page = ref(1),
|
||||
keyword = ref('')
|
||||
const entityLabel = computed(() => BUSINESS_OPTION_LABELS[props.entity])
|
||||
const selection = computed({
|
||||
get: () => props.modelValue ?? undefined,
|
||||
set: (value) => {
|
||||
const selected = Array.isArray(value) ? value : [value]
|
||||
retained.value = options.value.filter((x) => selected.includes(x.value))
|
||||
emit('update:modelValue', value)
|
||||
}
|
||||
})
|
||||
const options = computed(() => {
|
||||
const selected = Array.isArray(props.modelValue) ? props.modelValue : [props.modelValue]
|
||||
const merged = new Map(
|
||||
[...retained.value.filter((x) => selected.includes(x.value)), ...candidates.value].map(
|
||||
(x) => [x.value, x]
|
||||
)
|
||||
)
|
||||
if (props.entity === 'account' && props.includeSystem)
|
||||
merged.set(0, { value: 0, label: '系统' })
|
||||
for (const value of selected) {
|
||||
if (value == null || value === '' || merged.has(value)) continue
|
||||
merged.set(value, {
|
||||
value,
|
||||
label:
|
||||
typeof value === 'string' && props.entity === 'asset'
|
||||
? value
|
||||
: `${entityLabel.value}名称待加载`
|
||||
})
|
||||
}
|
||||
return [...merged.values()]
|
||||
})
|
||||
let active = true,
|
||||
sequence = 0,
|
||||
version = 0
|
||||
async function search(value: string) {
|
||||
keyword.value = value
|
||||
await load(1)
|
||||
}
|
||||
async function load(nextPage: number) {
|
||||
if (!active || !props.allowed || props.disabled) return
|
||||
const current = ++sequence,
|
||||
currentVersion = version
|
||||
loading.value = true
|
||||
error.value = ''
|
||||
try {
|
||||
const result = await searchBusinessOptions(
|
||||
props.entity,
|
||||
keyword.value,
|
||||
nextPage,
|
||||
props.context
|
||||
)
|
||||
if (!active || current !== sequence || currentVersion !== version) return
|
||||
const selected = Array.isArray(props.modelValue) ? props.modelValue : [props.modelValue]
|
||||
retained.value = options.value.filter((x) => selected.includes(x.value))
|
||||
candidates.value = nextPage === 1 ? result.options : [...candidates.value, ...result.options]
|
||||
page.value = nextPage
|
||||
hasMore.value = result.hasMore
|
||||
error.value = result.warning || ''
|
||||
} catch (e) {
|
||||
if (active && current === sequence && currentVersion === version)
|
||||
error.value = gatewayError(e)
|
||||
} finally {
|
||||
if (current === sequence) loading.value = false
|
||||
}
|
||||
}
|
||||
async function hydrate() {
|
||||
if (!active || !props.allowed || !['package', 'series'].includes(props.entity)) return
|
||||
const currentVersion = version
|
||||
const values = Array.isArray(props.modelValue) ? props.modelValue : [props.modelValue]
|
||||
await Promise.all(
|
||||
values.map(async (value) => {
|
||||
if (typeof value !== 'number' || retained.value.some((x) => x.value === value)) return
|
||||
let option: BusinessOption
|
||||
try {
|
||||
option = await resolveBusinessOption(props.entity, value)
|
||||
} catch {
|
||||
option = { value, label: `${entityLabel.value}名称加载失败` }
|
||||
}
|
||||
if (active && currentVersion === version) retained.value.push(option)
|
||||
})
|
||||
)
|
||||
}
|
||||
function invalidate() {
|
||||
version++
|
||||
sequence++
|
||||
candidates.value = []
|
||||
retained.value = []
|
||||
loading.value = false
|
||||
error.value = ''
|
||||
hasMore.value = false
|
||||
page.value = 1
|
||||
keyword.value = ''
|
||||
}
|
||||
watch(
|
||||
() => [
|
||||
props.entity,
|
||||
props.allowed,
|
||||
props.disabled,
|
||||
pollingScope(user),
|
||||
JSON.stringify(props.context)
|
||||
],
|
||||
() => {
|
||||
invalidate()
|
||||
void hydrate()
|
||||
},
|
||||
{ flush: 'sync' }
|
||||
)
|
||||
watch(
|
||||
() => props.modelValue,
|
||||
() => void hydrate(),
|
||||
{ immediate: true }
|
||||
)
|
||||
onDeactivated(() => {
|
||||
active = false
|
||||
version++
|
||||
sequence++
|
||||
loading.value = false
|
||||
})
|
||||
onActivated(() => {
|
||||
active = true
|
||||
void hydrate()
|
||||
})
|
||||
onBeforeUnmount(() => {
|
||||
active = false
|
||||
invalidate()
|
||||
})
|
||||
</script>
|
||||
<style scoped>
|
||||
.business-entity-select {
|
||||
width: 100%;
|
||||
}
|
||||
</style>
|
||||
Reference in New Issue
Block a user