初始化

This commit is contained in:
Weiming committed 2026-09-11 13:56:36 +08:00
commit 3a4e81a070
43 files changed
+10871

No files matched your search

+140
View File
@@ -0,0 +1,140 @@
<template>
<section class="card directory-panel" aria-labelledby="directory-title">
<div class="card-head directory-head">
<div><h2 id="directory-title">飞书通讯录同步</h2><p>同步飞书员工资料与目录状态;不作为 OAuth 登录条件;同步失败不覆盖上次结果</p></div>
<div class="directory-actions">
<button class="btn small" type="button" :disabled="loading || submitting" @click="refreshStatus">{{ loading ? '查询中…' : '刷新状态' }}</button>
<button v-if="canSync" class="btn primary" type="button" :disabled="syncDisabled" :aria-busy="submitting" @click="sync">{{ submitting || status?.running ? '同步中…' : '同步飞书通讯录' }}</button>
</div>
</div>
<div class="directory-body">
<p v-if="loading" role="status">正在确认通讯录同步状态…</p>
<div v-if="error" class="inline-alert danger" role="alert"><span>{{ error }}</span><button class="btn small" type="button" :disabled="loading || submitting" @click="refreshStatus">重试查询状态</button></div>
<div aria-live="polite" aria-atomic="true">
<p v-if="submitting || status?.running" role="status">同步正在进行,请勿重复提交。完成后将自动更新状态。</p>
<template v-if="status">
<p v-if="!status.configured">飞书通讯录尚未配置,请联系管理员配置应用凭证与通讯录权限。</p>
<dl class="directory-meta">
<div><dt>通讯录快照</dt><dd>{{ snapshotLabels[status.snapshotState] }}</dd></div>
<div><dt>上次成功同步 <span class="time-zone-label">本地时间</span></dt><dd>{{ formatLocalDateTime(status.lastSuccessfulAt, '暂无') }}</dd></div>
<div><dt>快照有效至 <span class="time-zone-label">本地时间</span></dt><dd>{{ formatLocalDateTime(status.snapshotExpiresAt) }}</dd></div>
</dl>
<p v-if="status.snapshotState === 'STALE' || status.snapshotState === 'CONTEXT_CHANGED'">快照已过期或应用上下文已变化,请重新同步以更新目录资料与业务候选人。</p>
</template>
<template v-if="run">
<p>最近运行:{{ run.trigger === 'STARTUP' ? '启动同步' : '手动同步' }} · {{ stateLabels[run.state] }} · {{ run.applied ? '已提交' : '未提交,不覆盖上次结果' }}</p>
<p class="muted-copy">运行时间(本地时间):{{ formatLocalDateTime(run.startedAt) }} — {{ formatLocalDateTime(run.finishedAt, '尚未结束') }} · 运行 ID:{{ run.runId }}</p>
<p v-if="run.state === 'FAILED' || run.errorMessage" class="directory-failure" role="alert">{{ run.errorCode || '同步失败' }}:{{ run.errorMessage || '同步未完成,请检查配置后重试。' }}</p>
<dl class="directory-counts"><div v-for="(label, key) in countLabels" :key="key"><dt>{{ label }}</dt><dd>{{ run.counts[key] }}</dd></div></dl>
</template>
</div>
</div>
</section>
</template>
<script setup lang="ts">
import { computed, onMounted, onBeforeUnmount, ref } from 'vue'
import { feishuDirectoryApi, type DirectoryRun, type DirectoryStatus } from '@/api/feishuDirectory'
import { useUserStore } from '@/store/user'
const emit = defineEmits<{ committed: [] }>()
const userStore = useUserStore()
// Deliberately never use Users.vue's legacy administrator/demo fallback.
const canSync = computed(() => userStore.hasPermission('system:feishu:sync'))
const canRead = computed(() => canSync.value || userStore.hasPermission('system:feishu:bind'))
const status = ref<DirectoryStatus | null>(null)
const run = ref<DirectoryRun | null>(null)
const loading = ref(false)
const submitting = ref(false)
const uncertain = ref(true)
const error = ref('')
const syncDisabled = computed(() => !canSync.value || loading.value || submitting.value || uncertain.value || !status.value?.configured || status.value.running)
const controller = new AbortController()
let disposed = false
let timer: ReturnType<typeof setTimeout> | undefined
let observedSuccess: string | null | undefined
const snapshotLabels = { UNAVAILABLE: '不可用', NEVER_SYNCED: '尚未同步', FRESH: '有效', STALE: '已过期', CONTEXT_CHANGED: '应用上下文已变化' }
const stateLabels = { RUNNING: '运行中', SUCCEEDED: '成功', FAILED: '失败' }
const countLabels = { departments: '部门', usersSeen: '扫描员工', excluded: '排除', created: '新增用户', updated: '更新用户', outOfScope: '可见范围外' }
// Date getters intentionally use the browser's local timezone instead of
// rendering the server's UTC ISO value. Keep the fallback caller-controlled so
// an absent successful result and an absent finish time can retain their own
// user-facing wording.
const formatLocalDateTime = (value: unknown, fallback = '—') => {
const date = value instanceof Date
? value
: new Date(typeof value === 'string' || typeof value === 'number' ? value : '')
if (!Number.isFinite(date.getTime())) return fallback
const pad = (part: number) => String(part).padStart(2, '0')
return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())} ${pad(date.getHours())}:${pad(date.getMinutes())}:${pad(date.getSeconds())}`
}
function committed(id: string) {
if (observedSuccess !== id) {
observedSuccess = id
emit('committed')
}
}
async function refreshStatus() {
if (disposed || !canRead.value || loading.value) return
clearTimeout(timer)
loading.value = true
uncertain.value = true
try {
const next = await feishuDirectoryApi.status(controller.signal)
if (disposed) return
if (observedSuccess !== undefined && next.lastSuccessfulRunId) committed(next.lastSuccessfulRunId)
else observedSuccess = next.lastSuccessfulRunId
status.value = next
run.value = next.lastRun
uncertain.value = false
error.value = ''
if (next.running) timer = setTimeout(() => void refreshStatus(), 5000)
} catch {
if (!disposed) error.value = '无法确认同步状态,已暂停同步操作。请重试查询状态后再操作。'
} finally {
loading.value = false
}
}
async function sync() {
if (syncDisabled.value) return
clearTimeout(timer)
submitting.value = true
uncertain.value = true
error.value = ''
try {
const result = await feishuDirectoryApi.sync(controller.signal)
if (disposed) return
run.value = result
if (result.state === 'SUCCEEDED' && result.complete && result.applied) committed(result.runId)
} catch {
// Includes timeouts, network errors, 409 and failed runs returned as HTTP 503.
// Never enable another POST until GET confirms the server is not running.
if (!disposed) error.value = '同步请求未确认(可能超时或失败),正在查询服务器状态,请勿重复提交。'
} finally {
await refreshStatus()
submitting.value = false
}
}
onMounted(refreshStatus)
onBeforeUnmount(() => { disposed = true; clearTimeout(timer); controller.abort() })
</script>
<style scoped>
.directory-panel { min-width: 0; flex: none; }
.directory-head { flex-wrap: wrap; gap: 12px; }
.directory-head > div:first-child { flex: 1 1 320px; min-width: 0; }
.directory-head p, .directory-body { overflow-wrap: anywhere; }
.directory-actions { display: flex; flex-wrap: wrap; gap: 8px; }
.directory-body { padding: 0 18px 16px; font-size: 12px; color: var(--muted); }
.directory-body p { margin: 10px 0; }
.directory-meta, .directory-counts { display: grid; gap: 12px; margin: 12px 0 0; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); }
.directory-counts { grid-template-columns: repeat(auto-fit, minmax(100px, 1fr)); }
dt { color: var(--muted); } dd { margin: 4px 0 0; color: var(--body); }
.time-zone-label { color: var(--quiet); font-size: 10px; font-weight: 400; white-space: nowrap; }
.directory-failure { color: var(--danger); }
</style>
Reference in new issue
Block a user
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.