479 lines
19 KiB
Vue
479 lines
19 KiB
Vue
<template>
|
||
<div v-if="visible" class="flex flex-wrap items-center gap-2">
|
||
<span class="text-xs text-gray-400" :title="detailTitle">{{ lastSyncText }}</span>
|
||
<button
|
||
type="button"
|
||
class="btn btn-secondary btn-sm"
|
||
:disabled="busy"
|
||
:title="t('hub.syncHint')"
|
||
@click="sync"
|
||
>
|
||
<Icon name="sync" class="mr-1 h-4 w-4" :class="busy ? 'animate-spin' : ''" />
|
||
{{ busy ? t('hub.syncing') : t('hub.syncNow') }}
|
||
</button>
|
||
<button
|
||
type="button"
|
||
class="btn btn-ghost btn-sm"
|
||
:disabled="previewing || busy"
|
||
:title="t('hub.previewHint')"
|
||
@click="preview"
|
||
>
|
||
<Icon name="search" class="mr-1 h-4 w-4" :class="previewing ? 'animate-spin' : ''" />
|
||
{{ previewing ? t('hub.previewing') : t('hub.preview') }}
|
||
</button>
|
||
<!-- 卡住时才出现:后端重启过的痕迹就是一条没人收尾的「进行中」,不给人出口就只能刷新页面干等 -->
|
||
<button
|
||
v-if="stuck"
|
||
type="button"
|
||
class="btn btn-ghost btn-sm text-red-600 dark:text-red-400"
|
||
:disabled="cancelling"
|
||
:title="t('hub.cancelHint')"
|
||
@click="cancel"
|
||
>
|
||
<Icon name="ban" class="mr-1 h-4 w-4" :class="cancelling ? 'animate-spin' : ''" />
|
||
{{ cancelling ? t('hub.cancelling') : t('hub.cancel') }}
|
||
</button>
|
||
<router-link
|
||
v-if="canManageSchedule"
|
||
to="/settings/sync-jobs"
|
||
class="btn btn-ghost btn-sm"
|
||
:title="t('hub.scheduleEntryHint')"
|
||
>
|
||
<Icon name="clock" class="mr-1 h-4 w-4" />
|
||
{{ t('hub.scheduleEntry') }}
|
||
</router-link>
|
||
|
||
<!-- 增量同步的开关在配置里,这里只给一个"现在就拉一次"的手动入口 -->
|
||
<button
|
||
v-if="status?.changes?.enabled"
|
||
type="button"
|
||
class="btn btn-ghost btn-sm"
|
||
:disabled="pulling || busy"
|
||
:title="changesTitle"
|
||
@click="pullChanges"
|
||
>
|
||
<Icon name="arrowDown" class="mr-1 h-4 w-4" :class="pulling ? 'animate-spin' : ''" />
|
||
{{ pulling ? t('hub.changesPulling') : t('hub.changesPull') }}
|
||
</button>
|
||
|
||
<BaseDialog :show="showResult" :title="t('hub.result')" width="wide" @close="showResult = false">
|
||
<div v-if="result" class="grid grid-cols-2 gap-3 sm:grid-cols-4">
|
||
<div v-for="cell in cells" :key="cell.label" class="rounded-xl border border-gray-200 px-3 py-2.5 dark:border-dark-600">
|
||
<p class="text-xs text-gray-400">{{ cell.label }}</p>
|
||
<p class="mt-1 text-lg font-semibold text-gray-900 dark:text-dark-100">{{ cell.value }}</p>
|
||
</div>
|
||
</div>
|
||
|
||
<p v-if="result && result.run.message" class="mt-4 rounded-xl bg-amber-50 px-3 py-2 text-xs text-amber-700 dark:bg-amber-900/20 dark:text-amber-400">
|
||
{{ result.run.message }}
|
||
</p>
|
||
|
||
<h4 class="mb-2 mt-5 text-sm font-semibold text-gray-900 dark:text-white">{{ t('hub.history') }}</h4>
|
||
<div class="overflow-x-auto">
|
||
<table class="w-full text-left text-xs">
|
||
<thead class="text-gray-400">
|
||
<tr>
|
||
<th class="py-1.5 pr-3 font-medium">{{ t('hub.startedAt') }}</th>
|
||
<th class="py-1.5 pr-3 font-medium">{{ t('hub.trigger') }}</th>
|
||
<th class="py-1.5 pr-3 font-medium">{{ t('common.status') }}</th>
|
||
<th class="py-1.5 pr-3 font-medium">{{ t('hub.summary') }}</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
<tr v-for="row in runs" :key="row.id" class="border-t border-gray-100 dark:border-dark-700">
|
||
<td class="py-1.5 pr-3 whitespace-nowrap">{{ row.started_at }}</td>
|
||
<td class="py-1.5 pr-3">{{ row.trigger_label }}</td>
|
||
<td class="py-1.5 pr-3">
|
||
<span class="badge" :class="statusClass(row.status)">{{ statusLabel(row.status) }}</span>
|
||
</td>
|
||
<td class="py-1.5 pr-3 text-gray-500 dark:text-dark-400">
|
||
{{ summaryOf(row) }}
|
||
<span
|
||
v-for="line in detailsOf(row)"
|
||
:key="line"
|
||
class="mt-0.5 block text-[11px] text-amber-600 dark:text-amber-400"
|
||
>
|
||
{{ line }}
|
||
</span>
|
||
</td>
|
||
</tr>
|
||
<tr v-if="!runs.length">
|
||
<td colspan="4" class="py-4 text-center text-gray-400">{{ t('common.noData') }}</td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
</div>
|
||
|
||
<template #footer>
|
||
<button type="button" class="btn btn-secondary" @click="showResult = false">{{ t('common.close') }}</button>
|
||
</template>
|
||
</BaseDialog>
|
||
|
||
<BaseDialog :show="showPreview" :title="t('hub.previewResult')" width="wide" @close="showPreview = false">
|
||
<template v-if="previewData">
|
||
<p class="text-xs text-gray-500 dark:text-dark-400">
|
||
{{ t('hub.credential') }}:{{ previewData.credential_label || previewData.credential }} ·
|
||
{{ t('hub.total') }}:{{ previewData.total_users }} ·
|
||
{{ t('hub.version') }}:{{ previewData.permission_version }} ·
|
||
{{ t('hub.memberLimit') }}:{{ previewData.member_limit }}
|
||
</p>
|
||
<p v-if="previewData.warning" class="mt-3 rounded-xl bg-amber-50 px-3 py-2 text-xs text-amber-700 dark:bg-amber-900/20 dark:text-amber-400">
|
||
{{ previewData.warning }}
|
||
</p>
|
||
<div v-for="group in previewGroups" :key="group.title" class="mt-4">
|
||
<h4 class="mb-2 text-sm font-semibold text-gray-900 dark:text-white">{{ group.title }}</h4>
|
||
<div class="grid grid-cols-2 gap-3 sm:grid-cols-4">
|
||
<div v-for="cell in group.cells" :key="cell.label" class="rounded-xl border border-gray-200 px-3 py-2.5 dark:border-dark-600">
|
||
<p class="text-xs text-gray-400">{{ cell.label }}</p>
|
||
<p class="mt-1 text-lg font-semibold text-gray-900 dark:text-dark-100">{{ cell.value }}</p>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div v-if="previewData.organizations.zombie_unit_samples?.length" class="mt-3">
|
||
<div class="flex flex-wrap items-center gap-1.5">
|
||
<span class="text-xs text-gray-400">{{ t('hub.zombieUnitsSample') }}</span>
|
||
<span
|
||
v-for="unit in previewData.organizations.zombie_unit_samples || []"
|
||
:key="unit.id"
|
||
class="badge"
|
||
:class="unit.blockers?.length ? 'badge-gray' : 'badge-amber'"
|
||
:title="[unit.code, ...(unit.blockers || [])].join(' · ')"
|
||
>{{ unit.name }}</span>
|
||
</div>
|
||
<div v-if="deletableShells" class="mt-2 flex flex-wrap items-center gap-2">
|
||
<button type="button" class="btn btn-secondary btn-xs" :disabled="cleaning" @click="cleanShells">
|
||
<Icon name="trash" class="mr-1 h-3.5 w-3.5" />
|
||
{{ cleaning ? t('hub.cleanShellsBusy') : t('hub.cleanShells', { value: deletableShells }) }}
|
||
</button>
|
||
<span class="text-[11px] text-gray-400">{{ t('hub.cleanShellsHint') }}</span>
|
||
</div>
|
||
</div>
|
||
<ul v-if="previewNotes.length" class="mt-3 space-y-1.5">
|
||
<li
|
||
v-for="note in previewNotes"
|
||
:key="note"
|
||
class="rounded-xl bg-gray-50 px-3 py-2 text-xs text-gray-500 dark:bg-dark-700/40 dark:text-dark-400"
|
||
>
|
||
{{ note }}
|
||
</li>
|
||
</ul>
|
||
<p v-if="previewData.users.mobile_note" class="mt-3 rounded-xl bg-gray-50 px-3 py-2 text-xs text-gray-500 dark:bg-dark-700/40 dark:text-dark-400">
|
||
{{ previewData.users.mobile_note }}
|
||
</p>
|
||
<template v-if="previewData.offboard_accounts.length">
|
||
<h4 class="mb-2 mt-5 text-sm font-semibold text-gray-900 dark:text-white">{{ t('hub.offboardAccounts') }}</h4>
|
||
<div class="flex flex-wrap gap-1.5">
|
||
<span v-for="account in previewData.offboard_accounts" :key="account" class="badge badge-gray">{{ account }}</span>
|
||
</div>
|
||
</template>
|
||
</template>
|
||
<template #footer>
|
||
<button type="button" class="btn btn-secondary" @click="showPreview = false">{{ t('common.close') }}</button>
|
||
<button type="button" class="btn btn-primary" :disabled="busy" @click="syncFromPreview">
|
||
{{ busy ? t('hub.syncing') : t('hub.syncNow') }}
|
||
</button>
|
||
</template>
|
||
</BaseDialog>
|
||
</div>
|
||
</template>
|
||
|
||
<script setup lang="ts">
|
||
import { computed, onMounted, ref } from 'vue'
|
||
import { useI18n } from 'vue-i18n'
|
||
import BaseDialog from '@/components/common/BaseDialog.vue'
|
||
import Icon from '@/components/icons/Icon.vue'
|
||
import { hubAPI, orgAPI } from '@/api'
|
||
import type { HubDirectoryPreview, HubStatus, HubSyncResult, HubSyncRun } from '@/api/hub'
|
||
import { useAuthStore } from '@/stores/auth'
|
||
import { describeCancelResult, describeRunDetails, describeRunSummary, syncLooksStuck } from '@/utils/hubSync'
|
||
import { errorMessage } from '@/utils/oa'
|
||
import { useAppStore } from '@/stores/app'
|
||
|
||
const emit = defineEmits<{ (event: 'synced'): void }>()
|
||
|
||
const { t } = useI18n()
|
||
const appStore = useAppStore()
|
||
const authStore = useAuthStore()
|
||
|
||
const status = ref<HubStatus | null>(null)
|
||
const result = ref<HubSyncResult | null>(null)
|
||
const runs = ref<HubSyncRun[]>([])
|
||
const syncing = ref(false)
|
||
const showResult = ref(false)
|
||
const previewing = ref(false)
|
||
const showPreview = ref(false)
|
||
const previewData = ref<HubDirectoryPreview | null>(null)
|
||
const pulling = ref(false)
|
||
const cancelling = ref(false)
|
||
const cleaning = ref(false)
|
||
|
||
// 只有开了统一身份、且当前用户管得到人员,才摆这个按钮
|
||
const visible = computed(() => status.value?.enabled === true && authStore.can('org:person'))
|
||
const busy = computed(() => syncing.value || status.value?.running === true)
|
||
// 卡住的两种判据与页面上那一致的口径都在 utils/hubSync 里,这里只拿来用
|
||
const stuck = computed(() => syncLooksStuck(status.value))
|
||
// 排期属于系统设置那一摊:管得到同步不代表管得到它的定时任务,入口按权限给
|
||
const canManageSchedule = computed(() => status.value?.enabled === true && authStore.can('system:setting'))
|
||
|
||
const lastSyncText = computed(() => {
|
||
const run = status.value?.last_run
|
||
if (!run) return t('hub.never')
|
||
return `${t('hub.lastSync')} ${run.started_at}(${run.trigger_label} · ${statusLabel(run.status)})`
|
||
})
|
||
|
||
const detailTitle = computed(() => {
|
||
const current = status.value
|
||
if (!current) return ''
|
||
return [
|
||
`${t('hub.baseUrl')}:${current.base_url || '--'}`,
|
||
`${t('hub.syncTime')}:${current.sync_time}`,
|
||
`${t('hub.total')}:${current.total_users}`,
|
||
`${t('hub.version')}:${current.permission_version}`
|
||
].join('\n')
|
||
})
|
||
|
||
// 悬停看增量同步的家底:周期、游标走到哪、还差多少、上一次是不是报错了
|
||
const changesTitle = computed(() => {
|
||
const changes = status.value?.changes
|
||
if (!changes) return t('hub.changesPullHint')
|
||
return [
|
||
t('hub.changesPullHint'),
|
||
`${t('hub.changesEvery', { value: changes.interval_seconds })} · ${
|
||
changes.cursor === null
|
||
? t('hub.changesUnaligned')
|
||
: `${changes.cursor}${changes.lag ? t('hub.changesLag', { value: changes.lag }) : t('hub.changesAligned')}`
|
||
}`,
|
||
changes.poll_at ? `${t('hub.changesPollAt')}:${changes.poll_at}` : '',
|
||
changes.error ? `${t('hub.changesError')}:${changes.error}` : ''
|
||
]
|
||
.filter(Boolean)
|
||
.join('\n')
|
||
})
|
||
|
||
const cells = computed(() => {
|
||
const run = result.value?.run
|
||
if (!run) return []
|
||
return [
|
||
{ label: t('hub.created'), value: run.created },
|
||
{ label: t('hub.updated'), value: run.updated },
|
||
{ label: t('hub.offboarded'), value: run.offboarded },
|
||
{ label: t('hub.units'), value: `${run.units_created}/${run.units_updated}` },
|
||
{ label: t('hub.unitsOffboarded'), value: run.units_offboarded },
|
||
{ label: t('hub.total'), value: run.total_users },
|
||
{ label: t('hub.returned'), value: run.returned_users },
|
||
{ label: t('hub.version'), value: run.permission_version },
|
||
{ label: t('hub.duration'), value: `${run.duration_ms} ms` }
|
||
]
|
||
})
|
||
|
||
const previewGroups = computed(() => {
|
||
const view = previewData.value
|
||
if (!view) return []
|
||
return [
|
||
{
|
||
title: t('hub.sectionOrg'),
|
||
cells: [
|
||
{ label: t('hub.cntTotal'), value: view.organizations.total },
|
||
{ label: t('hub.cntNew'), value: view.organizations.new },
|
||
{ label: t('hub.cntKnown'), value: view.organizations.known },
|
||
{ label: t('hub.cntRenamed'), value: view.organizations.renamed },
|
||
{ label: t('hub.cntOffboard'), value: view.organizations.offboard_candidates },
|
||
{ label: t('hub.cntOffboardReal'), value: view.organizations.offboard_pending },
|
||
{ label: t('hub.cntZombieUnits'), value: view.organizations.zombie_units },
|
||
{ label: t('hub.cntLocalUnits'), value: view.organizations.local_units }
|
||
]
|
||
},
|
||
{
|
||
title: t('hub.sectionUsers'),
|
||
cells: [
|
||
{ label: t('hub.cntNew'), value: view.users.new },
|
||
{ label: t('hub.cntAdopt'), value: view.users.adopt_candidates },
|
||
{ label: t('hub.cntOffboard'), value: view.users.offboard_candidates },
|
||
{ label: t('hub.cntOffboardReal'), value: view.users.offboard_pending },
|
||
{ label: t('hub.cntAccountRenames'), value: view.users.account_renames },
|
||
{ label: t('hub.cntNameRenames'), value: view.users.name_renames },
|
||
{ label: t('hub.cntAccountReleases'), value: view.users.account_releases },
|
||
{ label: t('hub.cntAccountConflicts'), value: view.users.account_conflicts },
|
||
{ label: t('hub.cntInactive'), value: view.users.inactive },
|
||
{ label: t('hub.cntUnassigned'), value: view.users.unassigned },
|
||
{ label: t('hub.cntProtected'), value: view.users.protected_admins },
|
||
{ label: t('hub.cntMobile'), value: view.users.with_mobile }
|
||
]
|
||
}
|
||
]
|
||
})
|
||
|
||
/**
|
||
* 熔断与"这趟让不让位"是两个会改变结果的口径,光看数字格子看不出来,单独拼成句子。
|
||
*
|
||
* 熔断线没跨过的时候也要摆出来:管理员真正想知道的是"这趟离被挡下还差多少",
|
||
* 只在被挡下那一刻才说话,等于让人拿一次事故换一次提示。
|
||
*/
|
||
const previewNotes = computed(() => {
|
||
const view = previewData.value
|
||
if (!view) return []
|
||
const notes: string[] = []
|
||
const peopleGuard = view.users.offboard_guard
|
||
if (peopleGuard?.percent) {
|
||
notes.push(
|
||
t('hub.guardNotePeople', {
|
||
percent: peopleGuard.percent,
|
||
floor: peopleGuard.min_people,
|
||
ceiling: peopleGuard.ceiling,
|
||
pending: view.users.offboard_pending
|
||
})
|
||
)
|
||
}
|
||
const unitGuard = view.organizations.offboard_guard
|
||
if (unitGuard?.percent) {
|
||
notes.push(
|
||
t('hub.guardNoteUnits', {
|
||
percent: unitGuard.percent,
|
||
floor: unitGuard.min_units,
|
||
ceiling: unitGuard.ceiling,
|
||
pending: view.organizations.offboard_pending
|
||
})
|
||
)
|
||
}
|
||
if (view.users.account_releases_allowed === false) {
|
||
notes.push(t('hub.releaseBlockedNote', { floor: view.users.account_release_floor }))
|
||
}
|
||
return notes
|
||
})
|
||
|
||
function statusLabel(value: string): string {
|
||
const key = `hub.status_${value}`
|
||
const label = t(key)
|
||
return label === key ? value : label
|
||
}
|
||
|
||
function statusClass(value: string): string {
|
||
if (value === 'success') return 'badge-success'
|
||
if (value === 'partial') return 'badge-amber'
|
||
if (value === 'failed') return 'badge-red'
|
||
return 'badge-gray'
|
||
}
|
||
|
||
function summaryOf(row: HubSyncRun): string {
|
||
return describeRunSummary(row, t)
|
||
}
|
||
|
||
function detailsOf(row: HubSyncRun): string[] {
|
||
return describeRunDetails(row, t)
|
||
}
|
||
|
||
async function load() {
|
||
try {
|
||
status.value = await hubAPI.getStatus()
|
||
} catch {
|
||
status.value = null
|
||
}
|
||
}
|
||
|
||
/** 能一键删掉的壳子数:后端没给这个字段时按 0 处理,不拿全部壳子去赌删除安全。 */
|
||
const deletableShells = computed(() => previewData.value?.organizations.zombie_units_deletable ?? 0)
|
||
|
||
async function cleanShells() {
|
||
if (cleaning.value) return
|
||
if (!window.confirm(t('hub.cleanShellsConfirm', { value: deletableShells.value }))) return
|
||
cleaning.value = true
|
||
try {
|
||
const result = await orgAPI.cleanupShellUnits()
|
||
appStore.success(
|
||
result.skipped.length
|
||
? t('hub.cleanShellsDoneSkipped', { deleted: result.deleted.length, skipped: result.skipped.length })
|
||
: t('hub.cleanShellsDone', { value: result.deleted.length })
|
||
)
|
||
// 重新体检:壳子那几格与「会不会停用部门」都跟着变了
|
||
previewData.value = await hubAPI.preview()
|
||
} catch (error) {
|
||
appStore.error(errorMessage(error))
|
||
} finally {
|
||
cleaning.value = false
|
||
}
|
||
}
|
||
|
||
async function preview() {
|
||
if (previewing.value) return
|
||
previewing.value = true
|
||
try {
|
||
previewData.value = await hubAPI.preview()
|
||
showPreview.value = true
|
||
} catch (error) {
|
||
appStore.error(errorMessage(error))
|
||
} finally {
|
||
previewing.value = false
|
||
}
|
||
}
|
||
|
||
function syncFromPreview() {
|
||
showPreview.value = false
|
||
sync()
|
||
}
|
||
|
||
// 手动拉一次变更流水:和后台定时跑的同一条逻辑,落后太多时后端自己回一次全量
|
||
async function pullChanges() {
|
||
if (pulling.value) return
|
||
pulling.value = true
|
||
try {
|
||
const payload = await hubAPI.syncChanges()
|
||
const parts = [t('hub.changesAppliedCount', { value: payload.applied })]
|
||
if (payload.skipped) parts.push(t('hub.changesSkippedCount', { value: payload.skipped }))
|
||
if (payload.full_resync) parts.push(t('hub.changesFullResync'))
|
||
else if (payload.needs_full) parts.push(t('hub.changesNeedsFull'))
|
||
appStore.success(`${t('hub.changesPullDone')}:${parts.join(' / ')}`)
|
||
emit('synced')
|
||
await load()
|
||
} catch (error) {
|
||
appStore.error(errorMessage(error))
|
||
} finally {
|
||
pulling.value = false
|
||
}
|
||
}
|
||
|
||
// 终止这趟同步:收尾「进行中」记录 + 放行同步锁,数据不回滚
|
||
async function cancel() {
|
||
if (cancelling.value) return
|
||
if (!window.confirm(t('hub.cancelConfirm'))) return
|
||
cancelling.value = true
|
||
try {
|
||
appStore.success(describeCancelResult(await hubAPI.cancelSync(), t))
|
||
emit('synced')
|
||
await load()
|
||
} catch (error) {
|
||
appStore.error(errorMessage(error))
|
||
} finally {
|
||
cancelling.value = false
|
||
}
|
||
}
|
||
|
||
async function sync() {
|
||
if (busy.value) return
|
||
syncing.value = true
|
||
try {
|
||
const payload = await hubAPI.syncNow()
|
||
result.value = payload
|
||
const run = payload.run
|
||
const parts = [
|
||
`${t('hub.created')} ${run.created}`,
|
||
`${t('hub.updated')} ${run.updated}`,
|
||
`${t('hub.offboarded')} ${run.offboarded}`
|
||
]
|
||
appStore.success(`${t('hub.syncDone')}:${parts.join(' / ')}`)
|
||
emit('synced')
|
||
showResult.value = true
|
||
// 历史与状态只是锦上添花,取不到不能把已经成功的同步报成失败
|
||
runs.value = await hubAPI.syncRuns(10).catch(() => runs.value)
|
||
await load()
|
||
} catch (error) {
|
||
appStore.error(errorMessage(error))
|
||
} finally {
|
||
syncing.value = false
|
||
}
|
||
}
|
||
|
||
onMounted(load)
|
||
|
||
defineExpose({ load })
|
||
</script>
|