Files
syops/frontend/src/features/admin/modules/wecom/WecomPage.vue
T
sophie 61693d014d refactor: 重构前端骨架,及admin模块子路由和懒加载
refactor: 优化多次请求,并使用pinia状态管理
2026-06-03 10:30:16 +08:00

457 lines
22 KiB
Vue

/**
* 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>