Initial import of weiyuan frontend

This commit is contained in:
qiubaishen
2026-10-15 16:33:46 +08:00
commit d8f3aee261
774 changed files with 374914 additions and 0 deletions
@@ -0,0 +1,101 @@
/*
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
*/
// 授权页的纯函数(不依赖 React 和请求封装,便于单测)。
export const DESKTOP_CLIENT_ID = 'weyai-desktop';
// 页面从地址栏读取、原样透传给服务端的参数(页面自己不拼 redirect_uri)
export const AUTHORIZE_PARAM_KEYS = [
'client_id',
'redirect_uri',
'state',
'code_challenge',
'code_challenge_method',
'scope_hint',
'device_name',
'prompt',
];
export function readAuthorizeParams(searchParams) {
const params = {};
AUTHORIZE_PARAM_KEYS.forEach((key) => {
params[key] = searchParams.get(key) || '';
});
return params;
}
/**
* 把 axios 错误或 success:false 响应统一成 { status, code, message, data }。
*/
export function parseClientApiError(error) {
const response = error?.response;
const body = response?.data;
const isObject = body && typeof body === 'object';
return {
status: response?.status || 0,
code: (isObject && body.code) || '',
message: (isObject && body.message) || '',
data: (isObject && body.data) || null,
};
}
/**
* 只放行服务端返回的 http(s) 或 weyai: 跳转地址,挡住 javascript:/data: 之类的伪协议。
* 地址本身由服务端按登记的 redirect_uri 生成,页面不做任何拼接。
*/
export function isAllowedRedirect(redirect) {
if (!redirect || typeof redirect !== 'string') return false;
let url;
try {
url = new URL(redirect);
} catch (_e) {
return false;
}
return ['https:', 'http:', 'weyai:'].includes(url.protocol);
}
/**
* 未登录时的登录页地址:按 scope_hint 去个人登录或企业登录,登录后回到当前授权页。
* 没有 scope_hint(网页端网关发起)时与 PrivateRoute 一致,默认去企业登录页,
* 页内可切换到个人登录,回跳地址已由登录页写入本地存储。
*/
export function buildLoginTarget(scopeHint, currentPath) {
const base = scopeHint === 'personal' ? '/login' : '/org/login';
return `${base}?redirect=${encodeURIComponent(currentPath)}`;
}
/**
* GET oauth/context 的查询参数:client_id、redirect_uri 决定合法性,prompt 决定是否
* 静默,scope_hint(personal / organization,桌面端登录入口带)决定默认选中个人还是企业。
* 空值不传,避免服务端把空串当成显式的 hint。
*/
export function buildContextQuery(params) {
const query = {};
['client_id', 'redirect_uri', 'prompt', 'scope_hint'].forEach((key) => {
const value = params?.[key];
if (value) query[key] = value;
});
return query;
}
export function optionKey(option) {
if (!option) return '';
return `${option.scope}:${Number(option.org_id) || 0}`;
}