Files
monkey-ai-web/src/helpers/data.js
T
2026-10-15 16:33:46 +08:00

150 lines
5.1 KiB
JavaScript

/*
Copyright (C) 2025 QuantumNous
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
For commercial licensing, please contact support@quantumnous.com
*/
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 = '/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');
}
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);
}
}
export function consumePostLoginRedirect(defaultPath = DEFAULT_LOGIN_REDIRECT) {
const path = sessionStorage.getItem(POST_LOGIN_REDIRECT_KEY);
sessionStorage.removeItem(POST_LOGIN_REDIRECT_KEY);
return isSafePostLoginRedirect(path) ? path : defaultPath;
}