fix: 单点登录优化与环境变量完善

This commit is contained in:
Gsh
2025-06-28 18:14:12 +08:00
parent 24d2908cca
commit 1d7a2013e3
11 changed files with 20 additions and 190 deletions

View File

@@ -9,13 +9,14 @@ VITE_WEB_ENV = 'development'
# 本地环境
VITE_WEB_BASE_API = '/dev-api'
# VITE_WEB_BASE_API='/prod-api'
# 本地接口
# VITE_API_URL = http://localhost:19001/api/app
VITE_API_URL=http://ccnetcore.com:19001/api/app
# SSO单点登录url
# SSO_SEVER_URL='http://localhost:18001'
SSO_SEVER_URL='http://ccnetcore.com'
VITE_SSO_SEVER_URL='http://localhost:18001'
# SSO_SEVER_URL='http://ccnetcore.com'
# SSO单点登录项目标识
SSO_CLIENT_ID='YiXin-Ai';
VITE_SSO_CLIENT_ID='YiXin-Ai';

View File

@@ -17,8 +17,8 @@ VITE_API_URL = http://ccnetcore.com:19001/api/app
VITE_BUILD_COMPRESS = gzip
# SSO单点登录url
SSO_SEVER_URL='https://ccnetcore.com'
VITE_SSO_SEVER_URL='https://ccnetcore.com'
# SSO单点登录项目标识
SSO_CLIENT_ID='YiXin-Ai';
VITE_SSO_CLIENT_ID='YiXin-Ai';

View File

@@ -56,6 +56,8 @@ function onAfterLeave() {
}
function handleThirdPartyLogin() {
console.log('SSO_SEVER_URL', SSO_SEVER_URL);
console.log('import.meta.env', import.meta.env);
const redirectUri = encodeURIComponent(`${window.location.origin}/chat`);
const popup = window.open(
`${SSO_SEVER_URL}/login?client_id=${SSO_CLIENT_ID}&redirect_uri=${redirectUri}`,
@@ -68,23 +70,19 @@ function handleThirdPartyLogin() {
const messageHandler = async (event: any) => {
if (event.origin === new URL(SSO_SEVER_URL).origin
|| event.data.type === 'SSO_LOGIN_SUCCESS'
|| !isHandled) {
&& event.data.type === 'SSO_LOGIN_SUCCESS'
&& !isHandled) {
isHandled = true;
try {
// 清理监听
window.removeEventListener('message', messageHandler);
const { token } = event.data;
userStore.setToken(token);
const resUserInfo = await getUserInfo();
userStore.setUserInfo(resUserInfo.data);
// 关闭弹窗
if (popup && !popup.closed)
popup.close();
// 清理监听
window.removeEventListener('message', messageHandler);
// 后续逻辑
ElMessage.success('登录成功');
userStore.closeLoginDialog();
@@ -96,9 +94,6 @@ function handleThirdPartyLogin() {
ElMessage.error('登录失败');
}
}
else {
console.log('2222-', event);
}
};
// 先移除旧监听,再添加新监听

View File

@@ -1,4 +1,4 @@
// 三方登录
// export const SSO_SEVER_URL: string = 'https://ccnetcore.com';
export const SSO_SEVER_URL: string = import.meta.env.SSO_SEVER_URL || 'http://ccnetcore.com';
export const SSO_CLIENT_ID: string = import.meta.env.SSO_CLIENT_ID || 'YiXin-Ai';
export const SSO_SEVER_URL: string = import.meta.env.VITE_SSO_SEVER_URL || 'http://ccnetcore.com';
export const SSO_CLIENT_ID: string = import.meta.env.VITE_SSO_CLIENT_ID || 'YiXin-Ai';

View File

@@ -1,35 +0,0 @@
// 公共的图表按钮样式
.btn-icon-btn {
align-items: center;
justify-content: center;
display: inline-block;
border: 1px solid transparent;
border-radius: 30%;
padding: 4px;
cursor: pointer;
transition: all 0.3s ease;
// 鼠标移入
&:hover {
background-color: rgb(0, 0, 0, .05);
.svg-icon {
color: rgb(0, 0, 0, .6);
}
}
// 鼠标按下
&:active {
background-color: rgb(0, 0, 0, .08);
.svg-icon {
color: rgb(0, 0, 0, .7);
}
}
// 图标
.svg-icon {
user-select: none;
color: rgb(0, 0, 0, .5);
}
}

View File

@@ -1,50 +0,0 @@
.el-form{
.el-form-item{
&:nth-last-child(1){
margin-bottom: 0px;
}
}
}
// el 弹框不知道为啥宽度会变大
.el-popup-parent--hidden {
overflow: hidden;
width: 100% !important;
}
// messagebox 样式
.is-message-box {
.el-message-box {
border-radius: 16px;
}
}
// rounded-tooltip 提示框样式
.rounded-tooltip {
border-radius: 10px !important;
display: flex;
align-items: center;
height: fit-content;
}
.rounded-tooltip-enter-from {
transform: scale(0.9); /* 进入前:缩小隐藏 */
opacity: 0;
}
.rounded-tooltip-enter-active,
.rounded-tooltip-leave-active {
transition: transform 0.3s, opacity 0.3s; /* 缓入动画 */
}
.rounded-tooltip-enter-to {
transform: scale(1); /* 进入后:正常大小 */
opacity: 1;
}
.rounded-tooltip-leave-from {
transform: scale(1); /* 离开前:正常大小 */
opacity: 1;
}
.rounded-tooltip-leave-to {
transform: scale(0.9); /* 离开后:缩小隐藏 */
opacity: 0;
}

View File

@@ -1,36 +0,0 @@
// 覆盖一些 elx 组件样式
// attachments 图片组件的 1.2.0 版本样式 bug
.image-preview-container {
flex: none !important;
}
.elx-files-card-content {
gap: 8px;
.elx-files-card-description {
line-height: 1.35;
}
}
.prev-next-btn {
position: absolute;
top: calc(50% + 3px);
transform: translateY(-50%);
z-index: 10;
cursor: pointer;
}
.elx-attachments-file-card-wrap {
padding-top: 6px;
}
.elx-attachments-card {
user-select: none;
}
// 隐藏 elx-sender-header 组件的底部边框
.el-sender-header {
border-bottom-color: transparent !important;
}

View File

@@ -1,9 +0,0 @@
@use './media.scss';
@use './btn-style.scss';
@use 'reset-css';
@use './element-plus.scss';
@use './elx.scss';
body{
overflow: hidden;
}

View File

@@ -1,24 +0,0 @@
:root {
/* 头部高度 */
--header-container-default-heigth: 56px;
/* 左侧侧边栏背景色 */
--sidebar-background-color: #f3f4f6;
/* 登录弹框变量 */
--login-dialog-width: 738px;
--login-dialog-height: 416px;
--login-dialog-padding: 0px;
--login-dialog-section-padding: 0px;
--login-dialog-border-radius: 24px;
--login-dialog-mode-toggle-color: #409eff;
--login-dialog-logo-background: #fff;
--login-dialog-logo-text-color: #191919;
/* 覆盖 element-plus 样式 */
--el-border-radius-base: 12px !important;
--el-messagebox-border-radius: 16px !important;
}

View File

@@ -6,7 +6,8 @@ interface ImportMetaEnv {
readonly VITE_WEB_ENV: string;
readonly VITE_WEB_BASE_API: string;
readonly VITE_API_URL: string;
readonly VITE_BUILD_COMPRESS: string;
readonly VITE_SSO_SEVER_URL: string;
readonly VITE_SSO_CLIENT_ID: string;
}
declare interface ImportMeta {

View File

@@ -12,35 +12,22 @@ const whiteList = ["/login", "/auth-redirect", "/bind", "/register"];
router.beforeEach((to, from, next) => {
NProgress.start();
const hasToken = getToken();
if (to.path === "/login" || to.path === "/index") {
const urlParams = new URLSearchParams(window.location.search);
const isPopup = window.opener && window.opener !== window;
const clientId = urlParams.get('client_id');
const redirectUri = urlParams.get('redirect_uri');
// 检查是否已经处理过SSO登录防重复
const hasHandledSSO = sessionStorage.getItem('sso_handled');
if (isPopup && clientId && redirectUri && !hasHandledSSO) {
if (isPopup && clientId && redirectUri ) {
if (hasToken) {
// 标记已处理,避免重复发送
sessionStorage.setItem('sso_handled', 'true');
// 发送消息给父窗口
const targetOrigin = new URL(decodeURIComponent(redirectUri)).origin;
window.opener.postMessage({
type: 'SSO_LOGIN_SUCCESS',
token: hasToken
}, redirectUri);
// 关闭弹出窗口
}, targetOrigin);
// 立即关闭窗口
setTimeout(() => window.close(), 100);
} else {
// 存储SSO参数但改用sessionStorage避免持久化
sessionStorage.setItem('sso_params', JSON.stringify({
isPopup,
clientId,
redirectUri
}));
}
}
}