84 lines
3.2 KiB
Vue
84 lines
3.2 KiB
Vue
<script setup lang="ts">
|
||
import type { DesktopAccountItem } from '#/api/console';
|
||
|
||
import { computed, onMounted, ref, watch } from 'vue';
|
||
|
||
import { Select } from 'ant-design-vue';
|
||
|
||
import { ALL_ACCOUNTS, fetchDesktopAccounts } from '#/api/console';
|
||
import { t } from '#/locales/t';
|
||
|
||
const props = defineProps<{ modelValue: number; disabled?: boolean; allowedIds?: number[] }>();
|
||
const emit = defineEmits<{
|
||
change: [value: number];
|
||
'update:modelValue': [value: number];
|
||
}>();
|
||
const accounts = ref<DesktopAccountItem[]>([]);
|
||
const canViewLegacy = ref(false);
|
||
const loading = ref(false);
|
||
const dropdownOpen = ref(false);
|
||
const visibleAccounts = computed(() => props.allowedIds === undefined ? accounts.value
|
||
: accounts.value.filter((account) => props.allowedIds!.includes(account.id)));
|
||
const showLegacy = computed(() => canViewLegacy.value && props.allowedIds === undefined);
|
||
const allowed = (value: number) => value === ALL_ACCOUNTS
|
||
|| (value === 0 && showLegacy.value) || visibleAccounts.value.some((item) => item.id === value);
|
||
defineExpose({ open: () => { dropdownOpen.value = true; } });
|
||
|
||
// 换了 key(原来是 wecom-archive-account-id)。不换的话,所有老浏览器里都
|
||
// 存着一个具体账号,升级之后第一眼看到的还是"只有某一个人"——而这次改动的
|
||
// 全部意义就是默认看全部。换个 key 等于所有人从「全部」重新开始,之后各自的
|
||
// 选择照旧记住。
|
||
const STORAGE_KEY = 'wecom-archive-account-scope';
|
||
|
||
function choose(value: number) {
|
||
if (!allowed(value)) value = ALL_ACCOUNTS;
|
||
dropdownOpen.value = false;
|
||
localStorage.setItem(STORAGE_KEY, String(value));
|
||
emit('update:modelValue', value);
|
||
emit('change', value);
|
||
}
|
||
|
||
// Knowledge may narrow this shared selector; other archive pages omit allowedIds.
|
||
watch(() => props.allowedIds, () => {
|
||
if (!allowed(props.modelValue)) choose(ALL_ACCOUNTS);
|
||
}, { flush: 'sync' });
|
||
|
||
onMounted(async () => {
|
||
loading.value = true;
|
||
try {
|
||
const response = await fetchDesktopAccounts(false);
|
||
accounts.value = response.accounts;
|
||
canViewLegacy.value = response.can_view_legacy;
|
||
const saved = Number(localStorage.getItem(STORAGE_KEY) ?? ALL_ACCOUNTS);
|
||
// 记住的账号可能已经被停用或移出授权范围,这时退回「全部」,而不是把人
|
||
// 卡在一个查不出数据的账号上。
|
||
const value =
|
||
allowed(saved)
|
||
? saved
|
||
: ALL_ACCOUNTS;
|
||
if (value !== props.modelValue) emit('update:modelValue', value);
|
||
emit('change', value);
|
||
} finally {
|
||
loading.value = false;
|
||
}
|
||
});
|
||
</script>
|
||
|
||
<template>
|
||
<Select
|
||
:value="modelValue"
|
||
:loading="loading"
|
||
:disabled="disabled"
|
||
:open="dropdownOpen"
|
||
@dropdown-visible-change="(visible) => { dropdownOpen = visible; }"
|
||
class="min-w-[240px]"
|
||
@change="(value) => choose(Number(value))"
|
||
>
|
||
<Select.Option :value="ALL_ACCOUNTS">{{ t('全部账号') }}</Select.Option>
|
||
<Select.Option v-if="showLegacy" :value="0">{{ t('旧版未归属数据') }}</Select.Option>
|
||
<Select.Option v-for="item in visibleAccounts" :key="item.id" :value="item.id">
|
||
{{ t('{name}({code})', { code: item.zyt_sn, name: item.nickname || item.zyt_sn }) }}
|
||
</Select.Option>
|
||
</Select>
|
||
</template>
|