fix(auth): 重构登录认证,并支持跳转到关联服务如图纸标注

This commit is contained in:
sophie committed 2026-07-29 13:22:31 +08:00
1 parent ce1fd59f61
commit affe139bf9
51 files changed
+1939 -132

No files matched your search

@@ -0,0 +1,20 @@
/**
* features/sso/api/linked-services.api.ts
* 用途:读取管理台配置的关联服务(已启用),供侧栏入口展示。
* 依赖:shared/api/client。
* 逻辑:ExternalSystemLinks 合并本目录与 SSO 应用目录后渲染。
*/
import { requestJson } from '@/shared/api/client';
export interface LinkedServiceItem {
id: number;
name: string;
url: string;
}
/** 获取当前用户可见的已启用关联服务。 */
export async function listLinkedServices(): Promise<LinkedServiceItem[]> {
const data = await requestJson<{ items?: LinkedServiceItem[] } | LinkedServiceItem[]>('/api/linked-services');
if (Array.isArray(data)) return data;
return Array.isArray(data?.items) ? data.items : [];
}
@@ -1,54 +1,63 @@
<!--
features/sso/components/ExternalSystemLinks.vue
用途:在 HXOPS 已登录页面展示已配置产品内平级服务入口。
依赖:SSO 应用目录 API、lucide-vue-next。
逻辑:点击后先打开 HXOPS 同源 launch 页以继承当前登录态,再由 launch 页跳平级服务生成签名 state。
用途:在 HXOPS 已登录页面展示关联服务与产品内 SSO 平级服务入口。
依赖:linked-nav.store、lucide-vue-next。
逻辑:读 Pinia 缓存即时渲染;未缓存时触发一次预取。工作台可通过 buttonClass/iconClass 对齐 ws-nav-btn。
-->
<script setup lang="ts">
import { onMounted, ref } from 'vue';
import { computed, onMounted } from 'vue';
import { storeToRefs } from 'pinia';
import { ExternalLink } from 'lucide-vue-next';
import { listSsoApplications, type SsoApplication } from '../api/sso.api';
import { useLinkedNavStore } from '../stores/linked-nav.store';
withDefaults(defineProps<{
const props = withDefaults(defineProps<{
compact?: boolean;
showLabel?: boolean;
/** 覆盖按钮样式(如工作台 ws-nav-btn),未传时用紧凑/默认样式 */
buttonClass?: string;
/** 覆盖图标尺寸类,未传时随 compact 变化 */
iconClass?: string;
}>(), {
compact: false,
showLabel: true,
buttonClass: '',
iconClass: '',
});
const applications = ref<SsoApplication[]>([]);
const linkedNav = useLinkedNavStore();
const { items } = storeToRefs(linkedNav);
/** 加载已启用应用;目录不可用时隐藏入口,不阻断 HXOPS 主业务。 */
async function loadApplications(): Promise<void> {
try {
applications.value = await listSsoApplications();
} catch (error) {
console.warn('[sso] failed to load peer services', error);
/**
* 解析按钮 class。
* 核心逻辑:外部传入优先;compact 默认与 Chat 底栏图标按钮一致(无额外 36px 盒子,避免错位)。
*/
const resolvedButtonClass = computed(() => {
if (props.buttonClass) return props.buttonClass;
if (props.compact) {
return 'inline-flex items-center justify-center transition-colors hover:text-secondary';
}
}
return 'inline-flex items-center justify-center gap-2 rounded-xl px-3 py-2 text-sm text-muted transition-colors hover:bg-surface-soft hover:text-secondary min-h-9';
});
/** 在新标签先打开同源中转页,确保非 remember 登录态也可继承到新标签。 */
function openApplication(application: SsoApplication): void {
window.open(`/sso/launch?app=${encodeURIComponent(application.key)}`, '_blank');
}
/** 解析图标 class:默认 16px,与 Chat 底栏 Moon/Briefcase 一致。 */
const resolvedIconClass = computed(() => props.iconClass || 'h-4 w-4 shrink-0');
onMounted(() => {
void loadApplications();
// 已缓存则立即展示;未缓存时补一次预取(登录后 App 通常已预取)。
void linkedNav.ensureLoaded();
});
</script>
<template>
<template v-for="application in applications" :key="application.key">
<template v-for="entry in items" :key="entry.key">
<button
type="button"
class="inline-flex items-center justify-center gap-2 rounded-xl text-muted transition-colors hover:bg-surface-soft hover:text-secondary"
:class="compact ? 'h-9 w-9' : 'min-h-9 px-3 py-2 text-sm'"
:title="application.display_name"
@click="openApplication(application)"
:class="resolvedButtonClass"
:title="entry.display_name"
@click="linkedNav.openItem(entry)"
>
<ExternalLink class="h-4 w-4 shrink-0" />
<span v-if="showLabel && !compact" class="truncate">{{ application.display_name }}</span>
<ExternalLink :class="resolvedIconClass" />
<span v-if="showLabel && !compact" class="truncate">{{ entry.display_name }}</span>
</button>
</template>
</template>
+1
View File
@@ -5,6 +5,7 @@
* 逻辑:页面通过本入口复用平级服务导航,登录模块复用 continuation。
*/
export { default as ExternalSystemLinks } from './components/ExternalSystemLinks.vue';
export { useLinkedNavStore } from './stores/linked-nav.store';
export {
continuationLocation,
readSsoContinuation,
@@ -0,0 +1,154 @@
/**
* features/sso/stores/linked-nav.store.ts
* 用途:缓存关联服务 / SSO 侧栏入口,避免页面切换时重复请求导致入口闪现。
* 依赖:linked-services.api、sso.api、pinia。
* 逻辑:登录后预取一次;ExternalSystemLinks 直接读 store;登出或管理台变更时刷新/清空。
*/
import { defineStore } from 'pinia';
import { computed, ref } from 'vue';
import { listLinkedServices, type LinkedServiceItem } from '../api/linked-services.api';
import { listSsoApplications, type SsoApplication } from '../api/sso.api';
/** 侧栏可渲染的入口元数据(可序列化,不含函数)。 */
export interface LinkedNavItem {
key: string;
display_name: string;
/** sso:走 /sso/launch;url:直接打开 target */
mode: 'sso' | 'url';
target: string;
}
/** 规范化 URL 便于与 SSO login_url 比对。 */
function normalizeUrl(value: string): string {
return String(value || '').trim().replace(/\/+$/, '').toLowerCase();
}
/**
* 合并关联服务与 SSO 目录为侧栏入口列表。
* 核心逻辑:关联服务优先;命中 SSO 注册表则 mode=sso。
*/
function buildNavItems(linked: LinkedServiceItem[], ssoApps: SsoApplication[]): LinkedNavItem[] {
const usedSsoKeys = new Set<string>();
const next: LinkedNavItem[] = [];
for (const item of linked) {
const matched = ssoApps.find(
(app) =>
normalizeUrl(app.login_url) === normalizeUrl(item.url)
|| String(app.display_name || '').trim() === String(item.name || '').trim(),
);
if (matched) {
usedSsoKeys.add(matched.key);
next.push({
key: `linked-${item.id}`,
display_name: item.name,
mode: 'sso',
target: matched.key,
});
} else {
next.push({
key: `linked-${item.id}`,
display_name: item.name,
mode: 'url',
target: item.url,
});
}
}
for (const app of ssoApps) {
if (usedSsoKeys.has(app.key)) continue;
next.push({
key: `sso-${app.key}`,
display_name: app.display_name,
mode: 'sso',
target: app.key,
});
}
return next;
}
export const useLinkedNavStore = defineStore('linkedNav', () => {
const items = ref<LinkedNavItem[]>([]);
const loaded = ref(false);
const loading = ref(false);
let inflight: Promise<void> | null = null;
const hasItems = computed(() => items.value.length > 0);
/**
* 预取侧栏入口;已加载且非 force 时直接复用缓存。
* @param options.force - 为 true 时忽略缓存强制刷新(管理台保存后)
*/
async function ensureLoaded(options?: { force?: boolean }): Promise<void> {
if (options?.force) {
loaded.value = false;
}
if (loaded.value) return;
if (inflight) {
await inflight;
return;
}
loading.value = true;
inflight = (async () => {
let linked: LinkedServiceItem[] = [];
let ssoApps: SsoApplication[] = [];
try {
linked = await listLinkedServices();
} catch (error) {
console.warn('[linked-nav] failed to load linked services', error);
}
try {
ssoApps = await listSsoApplications();
} catch (error) {
console.warn('[linked-nav] failed to load sso apps', error);
}
items.value = buildNavItems(linked, ssoApps);
loaded.value = true;
})();
try {
await inflight;
} finally {
loading.value = false;
inflight = null;
}
}
/** 强制刷新入口缓存(管理台增删改后调用)。 */
async function refresh(): Promise<void> {
await ensureLoaded({ force: true });
}
/** 登出时清空,避免下一账号看到旧入口。 */
function reset(): void {
items.value = [];
loaded.value = false;
loading.value = false;
inflight = null;
}
/**
* 打开入口:SSO 走同源 launch,其它直接新开 URL。
* @param item - 侧栏入口元数据
*/
function openItem(item: LinkedNavItem): void {
if (item.mode === 'sso') {
window.open(`/sso/launch?app=${encodeURIComponent(item.target)}`, '_blank');
return;
}
window.open(item.target, '_blank');
}
return {
items,
loaded,
loading,
hasItems,
ensureLoaded,
refresh,
reset,
openItem,
};
});