2aa9d73325
集中保留原版权声明,移除文件头检查和全局测试忽略规则。验证:全量单测、受影响用例复测及静态检查通过。
169 lines
5.5 KiB
JavaScript
169 lines
5.5 KiB
JavaScript
export function setStatusData(data) {
|
|
localStorage.setItem('status', JSON.stringify(data));
|
|
localStorage.setItem('system_name', data.system_name);
|
|
localStorage.setItem('logo', data.logo);
|
|
localStorage.setItem('footer_html', data.footer_html);
|
|
localStorage.setItem('quota_per_unit', data.quota_per_unit);
|
|
// 兼容:保留旧字段,同时写入新的额度展示类型
|
|
localStorage.setItem('display_in_currency', data.display_in_currency);
|
|
localStorage.setItem('quota_display_type', data.quota_display_type || 'USD');
|
|
localStorage.setItem('enable_drawing', data.enable_drawing);
|
|
localStorage.setItem('enable_task', data.enable_task);
|
|
localStorage.setItem('enable_data_export', data.enable_data_export);
|
|
localStorage.setItem('chats', JSON.stringify(data.chats));
|
|
localStorage.setItem(
|
|
'data_export_default_time',
|
|
data.data_export_default_time,
|
|
);
|
|
localStorage.setItem(
|
|
'default_collapse_sidebar',
|
|
data.default_collapse_sidebar,
|
|
);
|
|
localStorage.setItem('mj_notify_enabled', data.mj_notify_enabled);
|
|
|
|
// 体验中心配置
|
|
localStorage.setItem(
|
|
'experience_center_enable_group_select',
|
|
data.experience_center_enable_group_select ?? 'true',
|
|
);
|
|
localStorage.setItem(
|
|
'experience_center_enable_web_search',
|
|
data.experience_center_enable_web_search ?? 'false',
|
|
);
|
|
localStorage.setItem(
|
|
'experience_center_enable_file_upload',
|
|
data.experience_center_enable_file_upload ?? 'false',
|
|
);
|
|
localStorage.setItem(
|
|
'experience_center_upload_max_size',
|
|
data.experience_center_upload_max_size ?? '10',
|
|
);
|
|
localStorage.setItem(
|
|
'experience_center_allowed_file_types',
|
|
data.experience_center_allowed_file_types ??
|
|
'image/*,.pdf,.doc,.docx,.ppt,.pptx,.xls,.xlsx,.txt,.csv,.json,.xml',
|
|
);
|
|
localStorage.setItem(
|
|
'experience_center_enable_image_gen',
|
|
data.experience_center_enable_image_gen ?? 'false',
|
|
);
|
|
localStorage.setItem(
|
|
'experience_center_image_gen_models',
|
|
data.experience_center_image_gen_models ?? 'dall-e-3,gpt-image-1',
|
|
);
|
|
localStorage.setItem(
|
|
'experience_center_default_image_size',
|
|
data.experience_center_default_image_size ?? '1024x1024',
|
|
);
|
|
localStorage.setItem(
|
|
'experience_center_enable_drag_upload',
|
|
data.experience_center_enable_drag_upload ?? 'true',
|
|
);
|
|
|
|
if (data.chat_link) {
|
|
// localStorage.setItem('chat_link', data.chat_link);
|
|
} else {
|
|
localStorage.removeItem('chat_link');
|
|
}
|
|
if (data.chat_link2) {
|
|
// localStorage.setItem('chat_link2', data.chat_link2);
|
|
} else {
|
|
localStorage.removeItem('chat_link2');
|
|
}
|
|
if (data.docs_link) {
|
|
localStorage.setItem('docs_link', data.docs_link);
|
|
} else {
|
|
localStorage.removeItem('docs_link');
|
|
}
|
|
}
|
|
|
|
export const DEFAULT_LOGIN_REDIRECT = '/user-center/key-management';
|
|
const USER_STORAGE_KEY = 'user';
|
|
const POST_LOGIN_REDIRECT_KEY = 'post_login_redirect';
|
|
|
|
export function getUserData() {
|
|
const raw = localStorage.getItem(USER_STORAGE_KEY);
|
|
if (!raw) return null;
|
|
try {
|
|
const data = JSON.parse(raw);
|
|
return data && typeof data === 'object' ? data : null;
|
|
} catch {
|
|
localStorage.removeItem(USER_STORAGE_KEY);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
export function setUserData(data) {
|
|
if (!data) {
|
|
clearUserData();
|
|
return;
|
|
}
|
|
localStorage.setItem(USER_STORAGE_KEY, JSON.stringify(data));
|
|
}
|
|
|
|
export function clearUserData() {
|
|
localStorage.removeItem(USER_STORAGE_KEY);
|
|
localStorage.removeItem('current_org_id');
|
|
// 智能路由侧边栏的个人授权缓存属于当前用户:同一标签页换账号登录时不能沿用
|
|
try {
|
|
sessionStorage.removeItem('smart_router_personal_consent_granted');
|
|
} catch {
|
|
// sessionStorage 不可用时无需清理
|
|
}
|
|
}
|
|
|
|
export function isSafePostLoginRedirect(path) {
|
|
if (!path || typeof path !== 'string') return false;
|
|
if (!path.startsWith('/') || path.startsWith('//')) return false;
|
|
// 浏览器把 \ 当作 /,「/\evil.com」会被解析成协议相对地址跳到站外
|
|
if (path.includes('\\')) return false;
|
|
if (/^\/(?:login|register|reset)(?:$|[/?#])/.test(path)) return false;
|
|
// 企业认证页(/org/login、/org/register、/org/:slug/login)同样不可作为
|
|
// 登录后回跳目标,否则登录成功会被带回登录页
|
|
return !/^\/org(?:\/[^/?#]+)?\/(?:login|register)(?:$|[/?#])/.test(path);
|
|
}
|
|
|
|
export function savePostLoginRedirect(path) {
|
|
if (isSafePostLoginRedirect(path)) {
|
|
sessionStorage.setItem(POST_LOGIN_REDIRECT_KEY, path);
|
|
} else {
|
|
sessionStorage.removeItem(POST_LOGIN_REDIRECT_KEY);
|
|
}
|
|
}
|
|
|
|
export function consumePostLoginRedirect({
|
|
redirect = sessionStorage.getItem(POST_LOGIN_REDIRECT_KEY),
|
|
enterprise = false,
|
|
orgOnly = false,
|
|
} = {}) {
|
|
sessionStorage.removeItem(POST_LOGIN_REDIRECT_KEY);
|
|
const fallback = enterprise
|
|
? '/org/workbench'
|
|
: orgOnly
|
|
? '/org'
|
|
: DEFAULT_LOGIN_REDIRECT;
|
|
if (!isSafePostLoginRedirect(redirect)) return fallback;
|
|
|
|
let normalizedPath;
|
|
try {
|
|
const base = 'https://login.invalid';
|
|
const target = new URL(redirect, base);
|
|
if (target.origin !== base) return fallback;
|
|
normalizedPath =
|
|
decodeURI(target.pathname).toLowerCase().replace(/\/+$/, '') || '/';
|
|
} catch {
|
|
return fallback;
|
|
}
|
|
if (
|
|
!isSafePostLoginRedirect(normalizedPath) ||
|
|
normalizedPath === '/forbidden' ||
|
|
normalizedPath === '/user/reset'
|
|
)
|
|
return fallback;
|
|
if (!enterprise && !orgOnly && /^\/org(?:\/|$)/.test(normalizedPath))
|
|
return fallback;
|
|
if ((enterprise || orgOnly) && /^\/console(?:\/|$)/.test(normalizedPath))
|
|
return fallback;
|
|
return redirect;
|
|
}
|