refactor: 重构前端骨架,及admin模块子路由和懒加载
refactor: 优化多次请求,并使用pinia状态管理
This commit is contained in:
1 parent
f877f61efd
commit
61693d014d
74 files changed
+9493
-6418
No files matched your search
@@ -0,0 +1,456 @@
|
||||
/**
|
||||
* features/admin/modules/wecom/WecomPage.vue
|
||||
* 企业微信管理独立页面。路由:/admin/wecom
|
||||
*/
|
||||
<script setup lang="ts">
|
||||
import { onMounted } from 'vue';
|
||||
import { useI18n } from 'vue-i18n';
|
||||
import { CircleHelp, Download, Link2, RefreshCw, Unlink2 } from 'lucide-vue-next';
|
||||
import DeleteConfirmDialog from '@/components/DeleteConfirmDialog.vue';
|
||||
import { useWecomAdmin } from './composables/useWecomAdmin';
|
||||
|
||||
const { t } = useI18n();
|
||||
|
||||
const {
|
||||
isLoading,
|
||||
isSaving,
|
||||
wecomBindings,
|
||||
wecomBindForm,
|
||||
wecomBindUserQuery,
|
||||
wecomBindUserDropdownOpen,
|
||||
wecomBindUserOptions,
|
||||
wecomBoundOnly,
|
||||
wecomAuditSourceFilter,
|
||||
wecomAuditStatusFilter,
|
||||
filteredWecomLoginAudits,
|
||||
wecomNotifySettings,
|
||||
wecomNotifyEventsText,
|
||||
wecomNotifyMetrics,
|
||||
wecomSystemSettings,
|
||||
wecomIntegrationManifest,
|
||||
showDeleteConfirmDialog,
|
||||
deleteConfirmMessage,
|
||||
loadAll,
|
||||
refreshWecomData,
|
||||
selectWecomBindUser,
|
||||
scheduleCloseWecomBindUserDropdown,
|
||||
copyText,
|
||||
getWecomAuditReasonText,
|
||||
getWecomAuditMetaText,
|
||||
generateWecomAddressesFromPublicBase,
|
||||
generateWecomCallbackToken,
|
||||
generateWecomEncodingAesKey,
|
||||
saveWecomSystemSettings,
|
||||
saveWecomNotifySettings,
|
||||
bindWecomUser,
|
||||
unbindWecomUserByAdmin,
|
||||
exportWecomAudits,
|
||||
confirmDelete,
|
||||
} = useWecomAdmin();
|
||||
|
||||
onMounted(() => {
|
||||
void loadAll();
|
||||
});
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div v-if="isLoading" class="flex items-center justify-center py-20">
|
||||
<div class="animate-spin rounded-full h-10 w-10 border-b-2 border-brand-600" />
|
||||
</div>
|
||||
|
||||
<section v-else class="section-gap animate-fade-in-up">
|
||||
<div class="flex flex-col md:flex-row items-start md:items-center justify-between gap-4">
|
||||
<div>
|
||||
<h2 class="section-title">{{ t('admin.wecom.title') }}</h2>
|
||||
<p class="section-desc mt-1">{{ t('admin.wecom.subtitle') }}</p>
|
||||
</div>
|
||||
<button
|
||||
@click="refreshWecomData"
|
||||
class="btn-secondary inline-flex items-center gap-2 px-4 py-2 text-secondary rounded-xl hover-surface-soft transition-all shadow-sm"
|
||||
>
|
||||
<RefreshCw class="w-4 h-4" />
|
||||
{{ t('admin.wecom.refresh') }}
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<div class="card shadow-sm p-5 space-y-4">
|
||||
<h3 class="font-semibold text-primary">{{ t('admin.wecom.quickBind') }}</h3>
|
||||
<div class="grid grid-cols-1 md:grid-cols-4 gap-3">
|
||||
<div class="relative md:col-span-1">
|
||||
<input
|
||||
v-model="wecomBindUserQuery"
|
||||
type="text"
|
||||
class="input-field"
|
||||
:placeholder="t('admin.wecom.fields.userSelect')"
|
||||
@focus="wecomBindUserDropdownOpen = true"
|
||||
@input="wecomBindUserDropdownOpen = true"
|
||||
@blur="scheduleCloseWecomBindUserDropdown"
|
||||
/>
|
||||
<div
|
||||
v-if="wecomBindUserDropdownOpen"
|
||||
class="admin-dropdown w-72"
|
||||
>
|
||||
<button
|
||||
v-for="u in wecomBindUserOptions"
|
||||
:key="u.id"
|
||||
type="button"
|
||||
class="flex w-full items-center justify-between gap-3 px-3 py-2 text-left hover:bg-blue-50"
|
||||
@mousedown.prevent="selectWecomBindUser(u.id, u.label)"
|
||||
>
|
||||
<span class="text-sm text-primary">{{ u.label }}</span>
|
||||
<span class="text-xs text-muted">{{ u.employeeId || '' }}</span>
|
||||
</button>
|
||||
<div v-if="wecomBindUserOptions.length === 0" class="px-3 py-2 text-sm text-muted">
|
||||
{{ t('admin.wecom.noData') }}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<input v-model="wecomBindForm.wecom_userid" :placeholder="t('admin.wecom.fields.wecomUserId')" class="input-field md:col-span-2" />
|
||||
<input v-model="wecomBindForm.wecom_unionid" :placeholder="t('admin.wecom.fields.wecomUnionId')" class="input-field" />
|
||||
</div>
|
||||
<div class="flex justify-end">
|
||||
<button @click="bindWecomUser" :disabled="isSaving" class="btn-primary px-5 py-2 disabled:opacity-60">
|
||||
<Link2 class="w-4 h-4" /> {{ t('admin.wecom.bind') }}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="card shadow-sm p-5 space-y-4">
|
||||
<h3 class="font-semibold text-primary">{{ t('admin.wecom.systemSettings') }}</h3>
|
||||
<div class="grid grid-cols-1 md:grid-cols-2 gap-3">
|
||||
<label class="inline-flex items-center gap-2 text-sm text-secondary">
|
||||
<input v-model="wecomSystemSettings.enabled" type="checkbox" class="admin-checkbox" />
|
||||
{{ t('admin.wecom.fields.enabled') }}
|
||||
<CircleHelp class="w-3.5 h-3.5 text-muted" :title="t('admin.wecom.tips.enabled')" />
|
||||
</label>
|
||||
<label class="inline-flex items-center gap-2 text-sm text-secondary">
|
||||
<input v-model="wecomSystemSettings.auto_create_user" type="checkbox" class="admin-checkbox" />
|
||||
{{ t('admin.wecom.fields.autoCreateUser') }}
|
||||
<CircleHelp class="w-3.5 h-3.5 text-muted" :title="t('admin.wecom.tips.autoCreateUser')" />
|
||||
</label>
|
||||
</div>
|
||||
<div class="grid grid-cols-1 md:grid-cols-2 gap-3">
|
||||
<div>
|
||||
<label class="mb-1.5 inline-flex items-center gap-1 text-xs text-muted">
|
||||
{{ t('admin.wecom.fields.corpId') }}
|
||||
<CircleHelp class="w-3.5 h-3.5 text-muted" :title="t('admin.wecom.tips.corpId')" />
|
||||
</label>
|
||||
<input v-model="wecomSystemSettings.corp_id" class="input-field" />
|
||||
</div>
|
||||
<div>
|
||||
<label class="mb-1.5 inline-flex items-center gap-1 text-xs text-muted">
|
||||
{{ t('admin.wecom.fields.agentId') }}
|
||||
<CircleHelp class="w-3.5 h-3.5 text-muted" :title="t('admin.wecom.tips.agentId')" />
|
||||
</label>
|
||||
<input v-model="wecomSystemSettings.agent_id" class="input-field" />
|
||||
</div>
|
||||
<div class="md:col-span-2">
|
||||
<label class="mb-1.5 inline-flex items-center gap-1 text-xs text-muted">
|
||||
{{ t('admin.wecom.fields.secret') }}
|
||||
<CircleHelp class="w-3.5 h-3.5 text-muted" :title="t('admin.wecom.tips.secret')" />
|
||||
</label>
|
||||
<input v-model="wecomSystemSettings.secret" type="password" class="input-field" />
|
||||
</div>
|
||||
<div class="md:col-span-2">
|
||||
<label class="mb-1.5 inline-flex items-center gap-1 text-xs text-muted">
|
||||
{{ t('admin.wecom.fields.redirectUri') }}
|
||||
<CircleHelp class="w-3.5 h-3.5 text-muted" :title="t('admin.wecom.tips.redirectUri')" />
|
||||
</label>
|
||||
<input v-model="wecomSystemSettings.redirect_uri" class="input-field" />
|
||||
</div>
|
||||
<div class="md:col-span-2">
|
||||
<label class="mb-1.5 inline-flex items-center gap-1 text-xs text-muted">
|
||||
{{ t('admin.wecom.fields.publicBaseUrl') }}
|
||||
<CircleHelp class="w-3.5 h-3.5 text-muted" :title="t('admin.wecom.tips.publicBaseUrl')" />
|
||||
</label>
|
||||
<div class="flex flex-wrap gap-2">
|
||||
<input
|
||||
v-model="wecomSystemSettings.public_base_url"
|
||||
:placeholder="t('admin.wecom.publicBasePlaceholder')"
|
||||
class="input-field flex-1 min-w-[200px]"
|
||||
/>
|
||||
<button type="button" class="btn-secondary px-4 shrink-0" @click="generateWecomAddressesFromPublicBase">
|
||||
{{ t('admin.wecom.generateAddresses') }}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
<div>
|
||||
<label class="mb-1.5 inline-flex items-center gap-1 text-xs text-muted">
|
||||
{{ t('admin.wecom.fields.callbackToken') }}
|
||||
<CircleHelp class="w-3.5 h-3.5 text-muted" :title="t('admin.wecom.tips.callbackToken')" />
|
||||
</label>
|
||||
<div class="flex gap-2">
|
||||
<input v-model="wecomSystemSettings.callback_token" class="input-field flex-1 min-w-0" />
|
||||
<button type="button" class="btn-secondary px-3 shrink-0 text-xs" @click="generateWecomCallbackToken">
|
||||
{{ t('admin.wecom.generateToken') }}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
<div>
|
||||
<label class="mb-1.5 inline-flex items-center gap-1 text-xs text-muted">
|
||||
{{ t('admin.wecom.fields.encodingAesKey') }}
|
||||
<CircleHelp class="w-3.5 h-3.5 text-muted" :title="t('admin.wecom.tips.encodingAesKey')" />
|
||||
</label>
|
||||
<div class="flex gap-2">
|
||||
<input v-model="wecomSystemSettings.encoding_aes_key" class="input-field flex-1 min-w-0 font-mono text-xs" />
|
||||
<button type="button" class="btn-secondary px-3 shrink-0 text-xs" @click="generateWecomEncodingAesKey">
|
||||
{{ t('admin.wecom.generateEncodingKey') }}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
<div>
|
||||
<label class="mb-1.5 inline-flex items-center gap-1 text-xs text-muted">
|
||||
{{ t('admin.wecom.fields.callbackUrl') }}
|
||||
<CircleHelp class="w-3.5 h-3.5 text-muted" :title="t('admin.wecom.tips.callbackUrl')" />
|
||||
</label>
|
||||
<input v-model="wecomSystemSettings.callback_url" class="input-field" />
|
||||
</div>
|
||||
<div>
|
||||
<label class="mb-1.5 inline-flex items-center gap-1 text-xs text-muted">
|
||||
{{ t('admin.wecom.fields.homepageUrl') }}
|
||||
<CircleHelp class="w-3.5 h-3.5 text-muted" :title="t('admin.wecom.tips.homepageUrl')" />
|
||||
</label>
|
||||
<input v-model="wecomSystemSettings.homepage_url" class="input-field" />
|
||||
</div>
|
||||
<div class="md:col-span-2">
|
||||
<label class="mb-1.5 inline-flex items-center gap-1 text-xs text-muted">
|
||||
{{ t('admin.wecom.fields.trustedDomain') }}
|
||||
<CircleHelp class="w-3.5 h-3.5 text-muted" :title="t('admin.wecom.tips.trustedDomain')" />
|
||||
</label>
|
||||
<input v-model="wecomSystemSettings.trusted_domain" class="input-field" />
|
||||
</div>
|
||||
<div>
|
||||
<label class="mb-1.5 inline-flex items-center gap-1 text-xs text-muted">
|
||||
{{ t('admin.wecom.fields.matchRules') }}
|
||||
<CircleHelp class="w-3.5 h-3.5 text-muted" :title="t('admin.wecom.tips.matchRules')" />
|
||||
</label>
|
||||
<input v-model="wecomSystemSettings.match_rules" class="input-field" />
|
||||
</div>
|
||||
<div>
|
||||
<label class="mb-1.5 inline-flex items-center gap-1 text-xs text-muted">
|
||||
{{ t('admin.wecom.fields.matchOperator') }}
|
||||
<CircleHelp class="w-3.5 h-3.5 text-muted" :title="t('admin.wecom.tips.matchOperator')" />
|
||||
</label>
|
||||
<select v-model="wecomSystemSettings.match_operator" class="input-field">
|
||||
<option value="AND">AND</option>
|
||||
<option value="OR">OR</option>
|
||||
</select>
|
||||
</div>
|
||||
<div>
|
||||
<label class="mb-1.5 inline-flex items-center gap-1 text-xs text-muted">
|
||||
{{ t('admin.wecom.fields.firstLoginBindMode') }}
|
||||
<CircleHelp class="w-3.5 h-3.5 text-muted" :title="t('admin.wecom.tips.firstLoginBindMode')" />
|
||||
</label>
|
||||
<select v-model="wecomSystemSettings.first_login_bind_mode" class="input-field">
|
||||
<option value="match_rules">{{ t('admin.wecom.bindModes.match_rules') }}</option>
|
||||
<option value="input_verify">{{ t('admin.wecom.bindModes.input_verify') }}</option>
|
||||
</select>
|
||||
</div>
|
||||
<div>
|
||||
<label class="mb-1.5 inline-flex items-center gap-1 text-xs text-muted">
|
||||
{{ t('admin.wecom.fields.firstLoginInputFields') }}
|
||||
<CircleHelp class="w-3.5 h-3.5 text-muted" :title="t('admin.wecom.tips.firstLoginInputFields')" />
|
||||
</label>
|
||||
<input v-model="wecomSystemSettings.first_login_input_fields" class="input-field" />
|
||||
</div>
|
||||
<div>
|
||||
<label class="mb-1.5 inline-flex items-center gap-1 text-xs text-muted">
|
||||
{{ t('admin.wecom.fields.employeeIdSource') }}
|
||||
<CircleHelp class="w-3.5 h-3.5 text-muted" :title="t('admin.wecom.tips.employeeIdSource')" />
|
||||
</label>
|
||||
<input v-model="wecomSystemSettings.employee_id_source" class="input-field" />
|
||||
</div>
|
||||
<label class="inline-flex items-center gap-2 text-sm text-secondary pt-7">
|
||||
<input v-model="wecomSystemSettings.mobile_normalize" type="checkbox" class="admin-checkbox" />
|
||||
{{ t('admin.wecom.fields.mobileNormalize') }}
|
||||
<CircleHelp class="w-3.5 h-3.5 text-muted" :title="t('admin.wecom.tips.mobileNormalize')" />
|
||||
</label>
|
||||
</div>
|
||||
<div class="flex justify-end">
|
||||
<button @click="saveWecomSystemSettings" :disabled="isSaving" class="btn-primary px-5 py-2 disabled:opacity-60">
|
||||
{{ t('admin.save') }}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="grid grid-cols-1 xl:grid-cols-2 gap-6">
|
||||
<div class="card shadow-sm p-5 space-y-4">
|
||||
<h3 class="font-semibold text-primary">{{ t('admin.wecom.notifySettings') }}</h3>
|
||||
<label class="inline-flex items-center gap-2 text-sm text-secondary">
|
||||
<input v-model="wecomNotifySettings.enabled" type="checkbox" class="admin-checkbox" />
|
||||
{{ t('admin.wecom.notifyEnabled') }}
|
||||
</label>
|
||||
<input v-model="wecomNotifySettings.base_url" :placeholder="t('admin.wecom.notifyBaseUrl')" class="input-field" />
|
||||
<input v-model="wecomNotifyEventsText" :placeholder="t('admin.wecom.notifyEvents')" class="input-field" />
|
||||
<div class="flex justify-end">
|
||||
<button @click="saveWecomNotifySettings" :disabled="isSaving" class="btn-primary px-5 py-2 disabled:opacity-60">
|
||||
{{ t('admin.save') }}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
<div class="card shadow-sm p-5 space-y-4">
|
||||
<h3 class="font-semibold text-primary">{{ t('admin.wecom.notifyMetrics') }}</h3>
|
||||
<div v-if="wecomNotifyMetrics" class="grid grid-cols-2 gap-3 text-sm">
|
||||
<div class="rounded-lg bg-surface-soft px-3 py-2">
|
||||
<div class="text-muted">{{ t('admin.wecom.metricTotal') }}</div>
|
||||
<div class="text-lg font-semibold text-primary">{{ wecomNotifyMetrics.total }}</div>
|
||||
</div>
|
||||
<div class="rounded-lg bg-surface-soft px-3 py-2">
|
||||
<div class="text-muted">{{ t('admin.wecom.metricSuccessRate') }}</div>
|
||||
<div class="text-lg font-semibold text-emerald-700">{{ wecomNotifyMetrics.send_success_rate }}%</div>
|
||||
</div>
|
||||
<div class="rounded-lg bg-surface-soft px-3 py-2">
|
||||
<div class="text-muted">{{ t('admin.wecom.metricOpenedRate') }}</div>
|
||||
<div class="text-lg font-semibold text-blue-700">{{ wecomNotifyMetrics.opened_24h_rate }}%</div>
|
||||
</div>
|
||||
<div class="rounded-lg bg-surface-soft px-3 py-2">
|
||||
<div class="text-muted">{{ t('admin.wecom.metricWindowDays') }}</div>
|
||||
<div class="text-lg font-semibold text-primary">{{ wecomNotifyMetrics.window_days }}</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="card shadow-sm p-5 space-y-4">
|
||||
<h3 class="font-semibold text-primary">{{ t('admin.wecom.integrationCopyTitle') }}</h3>
|
||||
<p class="text-xs text-muted">{{ t('admin.wecom.integrationCopyDesc') }}</p>
|
||||
<p class="text-xs text-amber-800 bg-amber-50 border border-amber-100 rounded-lg px-3 py-2">{{ t('admin.wecom.integrationCopySecretsNote') }}</p>
|
||||
<div v-if="wecomIntegrationManifest" class="grid grid-cols-1 gap-3">
|
||||
<div v-for="item in [
|
||||
{ key: 'public_base_url', label: t('admin.wecom.copyFields.publicBaseUrl') },
|
||||
{ key: 'callback_url', label: t('admin.wecom.copyFields.callbackUrl') },
|
||||
{ key: 'callback_token', label: t('admin.wecom.copyFields.callbackToken') },
|
||||
{ key: 'encoding_aes_key', label: t('admin.wecom.copyFields.encodingAesKey') },
|
||||
{ key: 'oauth_redirect_uri', label: t('admin.wecom.copyFields.oauthRedirectUri') },
|
||||
{ key: 'homepage_url', label: t('admin.wecom.copyFields.homepageUrl') },
|
||||
{ key: 'trusted_domain', label: t('admin.wecom.copyFields.trustedDomain') },
|
||||
{ key: 'authorize_api', label: t('admin.wecom.copyFields.authorizeApi') },
|
||||
{ key: 'login_api', label: t('admin.wecom.copyFields.loginApi') },
|
||||
]" :key="item.key">
|
||||
<label class="mb-1.5 block text-xs text-muted">{{ item.label }}</label>
|
||||
<div class="flex gap-2">
|
||||
<input :value="(wecomIntegrationManifest as any)[item.key] || ''" readonly class="input-field flex-1 bg-surface-soft font-mono text-xs" />
|
||||
<button class="btn-secondary px-3" @click="copyText(String((wecomIntegrationManifest as any)[item.key] || ''))">{{ t('admin.wecom.copy') }}</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="admin-table-section">
|
||||
<div class="admin-table-header px-5 py-4">
|
||||
<h3 class="font-semibold text-primary">{{ t('admin.wecom.bindingList') }}</h3>
|
||||
<label class="inline-flex items-center gap-2 text-sm text-secondary">
|
||||
<input type="checkbox" v-model="wecomBoundOnly" class="w-4 h-4 rounded text-brand-600 border-default" />
|
||||
{{ t('admin.wecom.boundOnly') }}
|
||||
</label>
|
||||
</div>
|
||||
<div class="admin-table-wrap">
|
||||
<table class="admin-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th class="px-4 py-3 text-xs">{{ t('admin.wecom.headers.user') }}</th>
|
||||
<th class="px-4 py-3 text-xs">{{ t('admin.wecom.headers.roleStatus') }}</th>
|
||||
<th class="px-4 py-3 text-xs">{{ t('admin.wecom.headers.wecomUserId') }}</th>
|
||||
<th class="px-4 py-3 text-xs">{{ t('admin.wecom.headers.unionId') }}</th>
|
||||
<th class="px-4 py-3 text-xs text-right">{{ t('admin.wecom.headers.actions') }}</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr v-if="wecomBindings.length === 0">
|
||||
<td colspan="5" class="px-4 admin-table-empty">{{ t('admin.wecom.noData') }}</td>
|
||||
</tr>
|
||||
<tr v-for="u in wecomBindings" :key="u.id">
|
||||
<td class="px-4 py-3">
|
||||
<div class="text-sm font-medium text-primary">{{ u.username }} <span class="text-muted">(#{{ u.id }})</span></div>
|
||||
<div class="text-xs text-muted">{{ u.real_name || '-' }} · {{ u.email }}</div>
|
||||
</td>
|
||||
<td class="px-4 py-3 text-sm text-secondary">{{ t('admin.role.' + u.role) }} / {{ t('admin.status.' + u.status) }}</td>
|
||||
<td class="px-4 py-3 text-sm text-secondary font-mono">{{ u.wecom_userid || '-' }}</td>
|
||||
<td class="px-4 py-3 text-sm text-secondary font-mono">{{ u.wecom_unionid || '-' }}</td>
|
||||
<td class="px-4 py-3 text-right">
|
||||
<button
|
||||
v-if="u.wecom_userid"
|
||||
@click="unbindWecomUserByAdmin(u.id)"
|
||||
class="inline-flex items-center gap-1 px-3 py-1.5 text-xs text-red-600 bg-red-50 rounded-lg hover:bg-red-100"
|
||||
>
|
||||
<Unlink2 class="w-3.5 h-3.5" />
|
||||
{{ t('admin.wecom.unbind') }}
|
||||
</button>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="admin-table-section">
|
||||
<div class="admin-table-header admin-table-header--stacked px-5 py-4">
|
||||
<h3 class="font-semibold text-primary">{{ t('admin.wecom.auditTitle') }}</h3>
|
||||
<div class="flex w-full md:w-auto flex-wrap md:flex-nowrap items-center justify-start md:justify-end gap-2">
|
||||
<select v-model="wecomAuditSourceFilter" class="admin-table-filter-input h-11 min-w-[112px] shrink-0">
|
||||
<option value="all">{{ t('admin.wecom.filters.allSources') }}</option>
|
||||
<option value="wecom">{{ t('admin.wecom.sources.wecom') }}</option>
|
||||
<option value="password">{{ t('admin.wecom.sources.password') }}</option>
|
||||
</select>
|
||||
<select v-model="wecomAuditStatusFilter" class="admin-table-filter-input h-11 min-w-[112px] shrink-0">
|
||||
<option value="all">{{ t('admin.wecom.filters.allStatus') }}</option>
|
||||
<option value="success">{{ t('admin.wecom.auditStatus.success') }}</option>
|
||||
<option value="failed">{{ t('admin.wecom.auditStatus.failed') }}</option>
|
||||
</select>
|
||||
<button
|
||||
@click="exportWecomAudits"
|
||||
class="btn-secondary h-11 shrink-0 text-sm"
|
||||
>
|
||||
<Download class="w-4 h-4" />
|
||||
{{ t('admin.wecom.exportAudit') }}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
<div class="admin-table-wrap max-h-[420px]">
|
||||
<table class="admin-table">
|
||||
<thead class="sticky top-0 z-10 admin-table-sticky-head">
|
||||
<tr>
|
||||
<th class="px-4 py-3 text-xs">{{ t('admin.wecom.auditHeaders.time') }}</th>
|
||||
<th class="px-4 py-3 text-xs">{{ t('admin.wecom.auditHeaders.source') }}</th>
|
||||
<th class="px-4 py-3 text-xs">{{ t('admin.wecom.auditHeaders.user') }}</th>
|
||||
<th class="px-4 py-3 text-xs">{{ t('admin.wecom.auditHeaders.status') }}</th>
|
||||
<th class="px-4 py-3 text-xs">{{ t('admin.wecom.auditHeaders.reason') }}</th>
|
||||
<th class="px-4 py-3 text-xs">{{ t('admin.wecom.auditHeaders.ip') }}</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr v-if="filteredWecomLoginAudits.length === 0">
|
||||
<td colspan="6" class="px-4 admin-table-empty">{{ t('admin.wecom.noAuditData') }}</td>
|
||||
</tr>
|
||||
<tr v-for="row in filteredWecomLoginAudits" :key="row.id">
|
||||
<td class="px-4 py-3 text-xs text-muted whitespace-nowrap">{{ row.created_at || '-' }}</td>
|
||||
<td class="px-4 py-3 text-xs text-secondary">{{ t('admin.wecom.sources.' + row.source) }}</td>
|
||||
<td class="px-4 py-3 text-xs text-secondary">{{ row.username || '-' }}</td>
|
||||
<td class="px-4 py-3">
|
||||
<span class="inline-flex px-2 py-0.5 rounded-full text-[11px] font-medium" :class="row.status === 'success' ? 'bg-green-50 text-green-700' : 'bg-red-50 text-red-700'">
|
||||
{{ t('admin.wecom.auditStatus.' + row.status) }}
|
||||
</span>
|
||||
</td>
|
||||
<td class="px-4 py-3 text-xs text-muted max-w-[360px] align-top">
|
||||
<div class="max-w-[360px] whitespace-normal break-words">
|
||||
<div class="text-xs text-secondary leading-5">{{ getWecomAuditReasonText(row) }}</div>
|
||||
<div v-if="getWecomAuditMetaText(row)" class="mt-1 text-[11px] text-muted leading-5" :title="getWecomAuditMetaText(row)">
|
||||
{{ getWecomAuditMetaText(row) }}
|
||||
</div>
|
||||
</div>
|
||||
</td>
|
||||
<td class="px-4 py-3 text-xs text-muted font-mono">{{ row.ip || '-' }}</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
|
||||
<DeleteConfirmDialog
|
||||
v-model="showDeleteConfirmDialog"
|
||||
:message="deleteConfirmMessage"
|
||||
@confirm="confirmDelete"
|
||||
/>
|
||||
</template>
|
||||
@@ -0,0 +1,456 @@
|
||||
/**
|
||||
* features/admin/modules/wecom/composables/useWecomAdmin.ts
|
||||
* 企业微信:绑定、系统配置、通知、登录审计。
|
||||
*/
|
||||
import { computed, ref, watch } from 'vue';
|
||||
import { useI18n } from 'vue-i18n';
|
||||
import * as XLSX from 'xlsx';
|
||||
import {
|
||||
bindAdminWecomUser,
|
||||
getAdminWecomIntegrationManifest,
|
||||
getAdminWecomNotifyMetrics,
|
||||
getAdminWecomNotifySettings,
|
||||
getAdminWecomSystemSettings,
|
||||
listAdminUsers,
|
||||
listAdminWecomBindings,
|
||||
listAdminWecomLoginAudits,
|
||||
saveAdminWecomNotifySettings,
|
||||
saveAdminWecomSystemSettings,
|
||||
unbindAdminWecomUser,
|
||||
} from '@/api/admin';
|
||||
import { useMessage } from '@/composables/useMessage';
|
||||
import type { AdminUserRow } from '@/features/admin/types/admin.types';
|
||||
import {
|
||||
createDefaultWecomSystemSettings,
|
||||
type WecomBindingRow,
|
||||
type WecomIntegrationManifest,
|
||||
type WecomLoginAuditRow,
|
||||
type WecomNotifyMetrics,
|
||||
type WecomNotifySettings,
|
||||
type WecomSystemSettings,
|
||||
} from '@/features/admin/types/wecom.types';
|
||||
import { buildWecomAuditMeta, formatWecomAuditReason } from '@/utils/wecom';
|
||||
|
||||
export function useWecomAdmin() {
|
||||
const { t } = useI18n();
|
||||
const msg = useMessage();
|
||||
|
||||
const users = ref<AdminUserRow[]>([]);
|
||||
const isLoading = ref(true);
|
||||
const isSaving = ref(false);
|
||||
|
||||
const wecomBindings = ref<WecomBindingRow[]>([]);
|
||||
const wecomLoginAudits = ref<WecomLoginAuditRow[]>([]);
|
||||
const wecomBindForm = ref({
|
||||
user_id: '' as string | number,
|
||||
wecom_userid: '',
|
||||
wecom_unionid: '',
|
||||
});
|
||||
const wecomBindUserQuery = ref('');
|
||||
const wecomBindUserDropdownOpen = ref(false);
|
||||
const wecomBoundOnly = ref(false);
|
||||
let wecomBindUserCloseTimer: ReturnType<typeof setTimeout> | null = null;
|
||||
const wecomAuditSourceFilter = ref<'all' | 'wecom' | 'password'>('all');
|
||||
const wecomAuditStatusFilter = ref<'all' | 'success' | 'failed'>('all');
|
||||
const filteredWecomLoginAudits = computed(() => wecomLoginAudits.value);
|
||||
const wecomNotifySettings = ref<WecomNotifySettings>({
|
||||
enabled: true,
|
||||
base_url: '',
|
||||
events: [],
|
||||
});
|
||||
const wecomNotifyEventsText = ref('');
|
||||
const wecomNotifyMetrics = ref<WecomNotifyMetrics | null>(null);
|
||||
const wecomSystemSettings = ref<WecomSystemSettings>(createDefaultWecomSystemSettings());
|
||||
const wecomIntegrationManifest = ref<WecomIntegrationManifest | null>(null);
|
||||
|
||||
const showDeleteConfirmDialog = ref(false);
|
||||
const deleteConfirmMessage = ref('');
|
||||
let deleteConfirmAction: (() => Promise<void> | void) | null = null;
|
||||
|
||||
const wecomBindUserOptions = computed(() => {
|
||||
const keyword = wecomBindUserQuery.value.trim().toLowerCase();
|
||||
return users.value
|
||||
.filter((u) => {
|
||||
if (!keyword) return true;
|
||||
return [u.real_name, u.employee_id, String(u.id)]
|
||||
.filter(Boolean)
|
||||
.some((val) => String(val).toLowerCase().includes(keyword));
|
||||
})
|
||||
.map((u) => ({
|
||||
id: u.id,
|
||||
label: `${u.real_name || u.username} ${u.employee_id ? `(${u.employee_id})` : ''}`.trim(),
|
||||
employeeId: u.employee_id || '',
|
||||
}));
|
||||
});
|
||||
|
||||
const openDeleteConfirm = (message: string, action: () => Promise<void> | void): void => {
|
||||
deleteConfirmMessage.value = message;
|
||||
deleteConfirmAction = action;
|
||||
showDeleteConfirmDialog.value = true;
|
||||
};
|
||||
|
||||
const closeDeleteConfirm = (): void => {
|
||||
showDeleteConfirmDialog.value = false;
|
||||
deleteConfirmAction = null;
|
||||
};
|
||||
|
||||
const confirmDelete = async (): Promise<void> => {
|
||||
if (!deleteConfirmAction) return;
|
||||
try {
|
||||
await deleteConfirmAction();
|
||||
} finally {
|
||||
closeDeleteConfirm();
|
||||
}
|
||||
};
|
||||
|
||||
const loadUsers = async (): Promise<void> => {
|
||||
try {
|
||||
users.value = await listAdminUsers();
|
||||
} catch (e) {
|
||||
console.error('Error loading users:', e);
|
||||
users.value = [];
|
||||
}
|
||||
};
|
||||
|
||||
const loadWecomBindings = async (): Promise<void> => {
|
||||
try {
|
||||
wecomBindings.value = await listAdminWecomBindings(wecomBoundOnly.value);
|
||||
} catch (e) {
|
||||
console.error('Error loading WeCom bindings:', e);
|
||||
}
|
||||
};
|
||||
|
||||
const loadWecomAudits = async (): Promise<void> => {
|
||||
try {
|
||||
const params = new URLSearchParams({ limit: '200' });
|
||||
if (wecomAuditSourceFilter.value !== 'all') {
|
||||
params.set('source', wecomAuditSourceFilter.value);
|
||||
}
|
||||
if (wecomAuditStatusFilter.value !== 'all') {
|
||||
params.set('status', wecomAuditStatusFilter.value);
|
||||
}
|
||||
wecomLoginAudits.value = await listAdminWecomLoginAudits(params.toString());
|
||||
} catch (e) {
|
||||
console.error('Error loading WeCom login audits:', e);
|
||||
}
|
||||
};
|
||||
|
||||
const loadWecomSystemSettings = async (): Promise<void> => {
|
||||
try {
|
||||
wecomSystemSettings.value = await getAdminWecomSystemSettings();
|
||||
} catch (e) {
|
||||
console.error('Error loading WeCom system settings:', e);
|
||||
}
|
||||
};
|
||||
|
||||
const loadWecomIntegrationManifest = async (): Promise<void> => {
|
||||
try {
|
||||
wecomIntegrationManifest.value = await getAdminWecomIntegrationManifest();
|
||||
} catch (e) {
|
||||
console.error('Error loading WeCom integration manifest:', e);
|
||||
}
|
||||
};
|
||||
|
||||
const loadWecomNotifySettings = async (): Promise<void> => {
|
||||
try {
|
||||
wecomNotifySettings.value = await getAdminWecomNotifySettings();
|
||||
wecomNotifyEventsText.value = (wecomNotifySettings.value.events || []).join(',');
|
||||
} catch (e) {
|
||||
console.error('Error loading WeCom notify settings:', e);
|
||||
}
|
||||
};
|
||||
|
||||
const loadWecomNotifyMetrics = async (): Promise<void> => {
|
||||
try {
|
||||
wecomNotifyMetrics.value = await getAdminWecomNotifyMetrics();
|
||||
} catch (e) {
|
||||
console.error('Error loading WeCom notify metrics:', e);
|
||||
}
|
||||
};
|
||||
|
||||
const refreshWecomData = async (): Promise<void> => {
|
||||
await Promise.all([
|
||||
loadWecomBindings(),
|
||||
loadWecomAudits(),
|
||||
loadWecomSystemSettings(),
|
||||
loadWecomIntegrationManifest(),
|
||||
loadWecomNotifySettings(),
|
||||
loadWecomNotifyMetrics(),
|
||||
]);
|
||||
};
|
||||
|
||||
const loadAll = async (): Promise<void> => {
|
||||
isLoading.value = true;
|
||||
await Promise.all([loadUsers(), refreshWecomData()]);
|
||||
isLoading.value = false;
|
||||
};
|
||||
|
||||
const selectWecomBindUser = (id: number, label: string): void => {
|
||||
wecomBindForm.value.user_id = id;
|
||||
wecomBindUserQuery.value = label;
|
||||
wecomBindUserDropdownOpen.value = false;
|
||||
};
|
||||
|
||||
const scheduleCloseWecomBindUserDropdown = (): void => {
|
||||
if (wecomBindUserCloseTimer) clearTimeout(wecomBindUserCloseTimer);
|
||||
wecomBindUserCloseTimer = setTimeout(() => {
|
||||
wecomBindUserDropdownOpen.value = false;
|
||||
}, 150);
|
||||
};
|
||||
|
||||
const copyText = async (text: string): Promise<void> => {
|
||||
if (!text) return;
|
||||
try {
|
||||
await navigator.clipboard.writeText(text);
|
||||
msg.success(t('admin.wecom.msg.copySuccess'));
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
msg.error(t('admin.wecom.msg.copyFailed'));
|
||||
}
|
||||
};
|
||||
|
||||
const getWecomAuditReasonText = (row: WecomLoginAuditRow) => formatWecomAuditReason(row, t);
|
||||
|
||||
const getWecomAuditMetaText = (row: WecomLoginAuditRow) => buildWecomAuditMeta(row, t).join(' | ');
|
||||
|
||||
function normalizeWecomPublicBase(input: string): string | null {
|
||||
const raw = (input || '').trim();
|
||||
if (!raw) return null;
|
||||
let url = raw;
|
||||
if (!/^https?:\/\//i.test(url)) url = `https://${url}`;
|
||||
url = url.replace(/\/+$/, '');
|
||||
try {
|
||||
const u = new URL(url);
|
||||
if (!u.hostname) return null;
|
||||
return `${u.protocol}//${u.host}`;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function ensureWecomIntegrationManifest(): void {
|
||||
if (!wecomIntegrationManifest.value) {
|
||||
wecomIntegrationManifest.value = {
|
||||
public_base_url: '',
|
||||
callback_url: '',
|
||||
callback_token: '',
|
||||
encoding_aes_key: '',
|
||||
oauth_redirect_uri: '',
|
||||
homepage_url: '',
|
||||
trusted_domain: '',
|
||||
authorize_api: '',
|
||||
login_api: '',
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
function patchWecomIntegrationManifestFromSettings(): void {
|
||||
ensureWecomIntegrationManifest();
|
||||
const s = wecomSystemSettings.value;
|
||||
const base = normalizeWecomPublicBase(s.public_base_url);
|
||||
const prev = wecomIntegrationManifest.value!;
|
||||
wecomIntegrationManifest.value = {
|
||||
...prev,
|
||||
callback_token: s.callback_token || '',
|
||||
encoding_aes_key: s.encoding_aes_key || '',
|
||||
};
|
||||
if (!base) return;
|
||||
wecomIntegrationManifest.value = {
|
||||
...wecomIntegrationManifest.value!,
|
||||
public_base_url: base,
|
||||
callback_url: s.callback_url || `${base}/api/user/wecom/callback`,
|
||||
oauth_redirect_uri: s.redirect_uri || `${base}/workspace`,
|
||||
homepage_url: s.homepage_url || `${base}/workspace`,
|
||||
trusted_domain: s.trusted_domain || new URL(base).hostname,
|
||||
authorize_api: `${base}/api/user/wecom/authorize-url`,
|
||||
login_api: `${base}/api/user/wecom/login`,
|
||||
};
|
||||
}
|
||||
|
||||
function generateWecomAddressesFromPublicBase(): void {
|
||||
const base = normalizeWecomPublicBase(wecomSystemSettings.value.public_base_url);
|
||||
if (!base) {
|
||||
msg.warning(t('admin.wecom.msg.publicBaseRequired'));
|
||||
return;
|
||||
}
|
||||
const host = new URL(base).hostname;
|
||||
wecomSystemSettings.value.callback_url = `${base}/api/user/wecom/callback`;
|
||||
wecomSystemSettings.value.homepage_url = `${base}/workspace`;
|
||||
wecomSystemSettings.value.trusted_domain = host;
|
||||
wecomSystemSettings.value.redirect_uri = `${base}/workspace`;
|
||||
patchWecomIntegrationManifestFromSettings();
|
||||
}
|
||||
|
||||
function generateWecomCallbackToken(): void {
|
||||
const bytes = new Uint8Array(24);
|
||||
crypto.getRandomValues(bytes);
|
||||
const hex = Array.from(bytes, (b) => b.toString(16).padStart(2, '0')).join('');
|
||||
wecomSystemSettings.value.callback_token = hex;
|
||||
patchWecomIntegrationManifestFromSettings();
|
||||
}
|
||||
|
||||
function generateWecomEncodingAesKey(): void {
|
||||
const bytes = new Uint8Array(32);
|
||||
crypto.getRandomValues(bytes);
|
||||
let bin = '';
|
||||
for (let i = 0; i < bytes.length; i++) bin += String.fromCharCode(bytes[i]!);
|
||||
const b64 = btoa(bin);
|
||||
const key43 = b64.replace(/=+$/, '');
|
||||
wecomSystemSettings.value.encoding_aes_key = key43;
|
||||
patchWecomIntegrationManifestFromSettings();
|
||||
}
|
||||
|
||||
const saveWecomSystemSettings = async (): Promise<void> => {
|
||||
isSaving.value = true;
|
||||
try {
|
||||
const payload = {
|
||||
...wecomSystemSettings.value,
|
||||
match_operator: (wecomSystemSettings.value.match_operator || 'AND').toUpperCase(),
|
||||
};
|
||||
const data = await saveAdminWecomSystemSettings(payload);
|
||||
wecomSystemSettings.value = data.settings || wecomSystemSettings.value;
|
||||
await loadWecomIntegrationManifest();
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
msg.error((e as Error).message || t('admin.msg.requestFailed'));
|
||||
} finally {
|
||||
isSaving.value = false;
|
||||
}
|
||||
};
|
||||
|
||||
const saveWecomNotifySettings = async (): Promise<void> => {
|
||||
isSaving.value = true;
|
||||
try {
|
||||
wecomNotifySettings.value.events = wecomNotifyEventsText.value
|
||||
.split(',')
|
||||
.map((x) => x.trim())
|
||||
.filter(Boolean);
|
||||
const data = await saveAdminWecomNotifySettings(wecomNotifySettings.value);
|
||||
wecomNotifySettings.value = data.settings || wecomNotifySettings.value;
|
||||
await loadWecomNotifyMetrics();
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
msg.error((e as Error).message || t('admin.msg.requestFailed'));
|
||||
} finally {
|
||||
isSaving.value = false;
|
||||
}
|
||||
};
|
||||
|
||||
const bindWecomUser = async (): Promise<void> => {
|
||||
if (!wecomBindForm.value.user_id || !wecomBindForm.value.wecom_userid.trim()) {
|
||||
msg.warning(t('admin.wecom.msg.bindInputRequired'));
|
||||
return;
|
||||
}
|
||||
isSaving.value = true;
|
||||
try {
|
||||
await bindAdminWecomUser({
|
||||
user_id: Number(wecomBindForm.value.user_id),
|
||||
wecom_userid: wecomBindForm.value.wecom_userid.trim(),
|
||||
wecom_unionid: wecomBindForm.value.wecom_unionid.trim() || undefined,
|
||||
});
|
||||
wecomBindUserQuery.value = '';
|
||||
wecomBindUserDropdownOpen.value = false;
|
||||
wecomBindForm.value.wecom_userid = '';
|
||||
wecomBindForm.value.wecom_unionid = '';
|
||||
await refreshWecomData();
|
||||
msg.success(t('admin.wecom.msg.bindSuccess'));
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
const errorMessage = (e as Error).message || t('admin.wecom.msg.bindFailed');
|
||||
if (String(errorMessage).includes('already bound to user')) {
|
||||
msg.error(t('admin.wecom.msg.boundConflict', { detail: errorMessage }));
|
||||
} else {
|
||||
msg.error(errorMessage);
|
||||
}
|
||||
} finally {
|
||||
isSaving.value = false;
|
||||
}
|
||||
};
|
||||
|
||||
const unbindWecomUserByAdmin = async (userId: number): Promise<void> => {
|
||||
openDeleteConfirm(t('admin.wecom.confirmUnbind'), async () => {
|
||||
isSaving.value = true;
|
||||
try {
|
||||
await unbindAdminWecomUser(userId);
|
||||
await refreshWecomData();
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
msg.error((e as Error).message || t('admin.wecom.msg.unbindFailed'));
|
||||
} finally {
|
||||
isSaving.value = false;
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
const exportWecomAudits = (): void => {
|
||||
if (filteredWecomLoginAudits.value.length === 0) {
|
||||
msg.warning(t('admin.wecom.noAuditData'));
|
||||
return;
|
||||
}
|
||||
const rows = filteredWecomLoginAudits.value.map((row) => ({
|
||||
Time: row.created_at || '',
|
||||
Source: row.source || '',
|
||||
Username: row.username || '',
|
||||
Status: row.status || '',
|
||||
Reason: getWecomAuditReasonText(row),
|
||||
Meta: getWecomAuditMetaText(row),
|
||||
ErrorCode: row.error_code || '',
|
||||
MatchRules: row.match_rules || '',
|
||||
MatchOperator: row.match_operator || '',
|
||||
MatchResult: row.match_result || '',
|
||||
IP: row.ip || '',
|
||||
WecomUserId: row.wecom_userid || '',
|
||||
UserAgent: row.user_agent || '',
|
||||
}));
|
||||
const ws = XLSX.utils.json_to_sheet(rows);
|
||||
const wb = XLSX.utils.book_new();
|
||||
XLSX.utils.book_append_sheet(wb, ws, 'wecom_login_audits');
|
||||
XLSX.writeFile(wb, `wecom_login_audits_${new Date().toISOString().split('T')[0]}.xlsx`);
|
||||
};
|
||||
|
||||
watch(wecomBoundOnly, () => {
|
||||
void loadWecomBindings();
|
||||
});
|
||||
|
||||
watch([wecomAuditSourceFilter, wecomAuditStatusFilter], () => {
|
||||
void loadWecomAudits();
|
||||
});
|
||||
|
||||
return {
|
||||
isLoading,
|
||||
isSaving,
|
||||
wecomBindings,
|
||||
wecomLoginAudits,
|
||||
wecomBindForm,
|
||||
wecomBindUserQuery,
|
||||
wecomBindUserDropdownOpen,
|
||||
wecomBindUserOptions,
|
||||
wecomBoundOnly,
|
||||
wecomAuditSourceFilter,
|
||||
wecomAuditStatusFilter,
|
||||
filteredWecomLoginAudits,
|
||||
wecomNotifySettings,
|
||||
wecomNotifyEventsText,
|
||||
wecomNotifyMetrics,
|
||||
wecomSystemSettings,
|
||||
wecomIntegrationManifest,
|
||||
showDeleteConfirmDialog,
|
||||
deleteConfirmMessage,
|
||||
loadAll,
|
||||
refreshWecomData,
|
||||
selectWecomBindUser,
|
||||
scheduleCloseWecomBindUserDropdown,
|
||||
copyText,
|
||||
getWecomAuditReasonText,
|
||||
getWecomAuditMetaText,
|
||||
generateWecomAddressesFromPublicBase,
|
||||
generateWecomCallbackToken,
|
||||
generateWecomEncodingAesKey,
|
||||
saveWecomSystemSettings,
|
||||
saveWecomNotifySettings,
|
||||
bindWecomUser,
|
||||
unbindWecomUserByAdmin,
|
||||
exportWecomAudits,
|
||||
confirmDelete,
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user