sync: 同步 monkey-ai@weiyuan(ddc5b9443) 的 web/ 到 ui-optimization
- 合入 weiyuan 新功能前端:分佣 V2 / 伙伴工作台、智能路由、企业资金与分析、官网设置与预约演示等, semi-ui 已全部迁移到 antd 6 / @/components/ui,不重新引入 Semi - 分佣二级菜单与页头去重、伙伴入口探测(企业伙伴走企业中心「企业分佣」)、超管侧栏入口恢复 - 适配层 DatePicker / TimePicker 统一 onChange(Date|Date[]|null, 文本) 契约;生产构建剔除调试日志 - 图片链接刷新:同任务请求复用、后台刷新不弹全局错误、离页停止批量请求 - 恢复 bun.lock(与 package-lock.json 共存);docs 规范与 CHANGELOG 更新 - 停止跟踪 .env.playwright.local(本地登录凭据,已加入 .gitignore)
This commit is contained in:
@@ -372,6 +372,9 @@ export function useOrgTokens(orgId) {
|
||||
const [tokens, setTokens] = useState([]);
|
||||
const [total, setTotal] = useState(0);
|
||||
const [loading, setLoading] = useState(false);
|
||||
// 列表加载失败:业务文案字符串,或无文案时为 true;空串表示无错误。
|
||||
// 页面据此展示错误态+重试,而不是误显示"暂无令牌"
|
||||
const [error, setError] = useState('');
|
||||
|
||||
const fetch = useCallback(
|
||||
async (page = 1, pageSize = 20) => {
|
||||
@@ -385,8 +388,14 @@ export function useOrgTokens(orgId) {
|
||||
if (res.data.success) {
|
||||
setTokens(res.data.data?.items || []);
|
||||
setTotal(res.data.data?.total || 0);
|
||||
setError('');
|
||||
} else {
|
||||
// 无文案时用 true 兜底,保证页面仍进入错误态
|
||||
setError(res.data.message || true);
|
||||
showError(res.data.message);
|
||||
}
|
||||
} catch (err) {
|
||||
setError(err.message || true);
|
||||
showError(err.message);
|
||||
} finally {
|
||||
setLoading(false);
|
||||
@@ -423,7 +432,8 @@ export function useOrgTokens(orgId) {
|
||||
headers: getOrgHeaders(orgId),
|
||||
});
|
||||
if (res.data.success) {
|
||||
return true;
|
||||
// 返回后端保存后的令牌(分组可能被后端回落改写),无数据时退化为 true
|
||||
return res.data.data || true;
|
||||
}
|
||||
showError(res.data.message);
|
||||
return false;
|
||||
@@ -435,6 +445,27 @@ export function useOrgTokens(orgId) {
|
||||
[orgId],
|
||||
);
|
||||
|
||||
const getTokenEditOptions = useCallback(
|
||||
async (tokenId, projectId) => {
|
||||
if (!orgId) return null;
|
||||
try {
|
||||
const params = {};
|
||||
if (projectId != null) params.project_id = projectId;
|
||||
const res = await API.get(`/api/org/tokens/${tokenId}/edit-options`, {
|
||||
headers: getOrgHeaders(orgId),
|
||||
params,
|
||||
});
|
||||
if (res.data.success) return res.data.data || null;
|
||||
showError(res.data.message);
|
||||
return null;
|
||||
} catch (err) {
|
||||
showError(err.message);
|
||||
return null;
|
||||
}
|
||||
},
|
||||
[orgId],
|
||||
);
|
||||
|
||||
const deleteToken = useCallback(
|
||||
async (tokenId) => {
|
||||
if (!orgId) return false;
|
||||
@@ -455,13 +486,37 @@ export function useOrgTokens(orgId) {
|
||||
[orgId],
|
||||
);
|
||||
|
||||
// 列表只下发脱敏密钥;显示/复制时按需取完整密钥(后端留痕),失败返回 null
|
||||
const fetchTokenKey = useCallback(
|
||||
async (tokenId) => {
|
||||
if (!orgId) return null;
|
||||
try {
|
||||
const res = await API.post(`/api/org/tokens/${tokenId}/key`, null, {
|
||||
headers: getOrgHeaders(orgId),
|
||||
});
|
||||
if (res.data.success) {
|
||||
return res.data.data?.key || null;
|
||||
}
|
||||
showError(res.data.message);
|
||||
return null;
|
||||
} catch (err) {
|
||||
showError(err.message);
|
||||
return null;
|
||||
}
|
||||
},
|
||||
[orgId],
|
||||
);
|
||||
|
||||
return {
|
||||
tokens,
|
||||
total,
|
||||
loading,
|
||||
error,
|
||||
fetch,
|
||||
createToken,
|
||||
updateToken,
|
||||
getTokenEditOptions,
|
||||
deleteToken,
|
||||
fetchTokenKey,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -0,0 +1,110 @@
|
||||
/*
|
||||
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
|
||||
*/
|
||||
import assert from 'node:assert/strict';
|
||||
import test from 'node:test';
|
||||
import { createRequire } from 'node:module';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import vm from 'node:vm';
|
||||
import React from 'react';
|
||||
import { renderToString } from 'react-dom/server';
|
||||
import { build } from 'esbuild';
|
||||
|
||||
// Execute the actual hook with React. Only its HTTP/error boundary is replaced.
|
||||
const bundled = await build({
|
||||
entryPoints: [fileURLToPath(new URL('./useOrgData.js', import.meta.url))],
|
||||
bundle: true,
|
||||
write: false,
|
||||
platform: 'node',
|
||||
format: 'cjs',
|
||||
external: ['react', 'i18next'],
|
||||
plugins: [
|
||||
{
|
||||
name: 'http-boundary',
|
||||
setup(builder) {
|
||||
builder.onResolve(
|
||||
{ filter: /^\.\.\/\.\.\/helpers(?:\/api)?$/ },
|
||||
() => ({
|
||||
path: 'test-http-boundary',
|
||||
external: true,
|
||||
}),
|
||||
);
|
||||
},
|
||||
},
|
||||
],
|
||||
});
|
||||
const require = createRequire(import.meta.url);
|
||||
function renderTokensHook(orgId) {
|
||||
const calls = [];
|
||||
const errors = [];
|
||||
const transport = {
|
||||
API: {
|
||||
async get(url, config) {
|
||||
calls.push({ url, ...config });
|
||||
return {
|
||||
data: { success: true, data: { groups: ['default', 'vip'] } },
|
||||
};
|
||||
},
|
||||
},
|
||||
showError: (message) => errors.push(message),
|
||||
};
|
||||
const module = { exports: {} };
|
||||
vm.runInNewContext(bundled.outputFiles[0].text, {
|
||||
module,
|
||||
exports: module.exports,
|
||||
require: (name) =>
|
||||
name === 'test-http-boundary' ? transport : require(name),
|
||||
});
|
||||
let hook;
|
||||
function Probe() {
|
||||
hook = module.exports.useOrgTokens(orgId);
|
||||
return null;
|
||||
}
|
||||
renderToString(React.createElement(Probe));
|
||||
return { hook, calls, errors, transport };
|
||||
}
|
||||
|
||||
test('token edit options explicitly send zero when unbinding a project', async () => {
|
||||
const { hook, calls } = renderTokensHook(12);
|
||||
const data = await hook.getTokenEditOptions(7, 0);
|
||||
assert.equal(calls[0].url, '/api/org/tokens/7/edit-options');
|
||||
assert.equal(calls[0].headers['X-Org-Id'], '12');
|
||||
assert.equal(calls[0].params.project_id, 0);
|
||||
assert.deepEqual(data.groups, ['default', 'vip']);
|
||||
});
|
||||
|
||||
test('token edit options distinguish omitted project from an explicit project', async () => {
|
||||
const { hook, calls } = renderTokensHook(12);
|
||||
await hook.getTokenEditOptions(7);
|
||||
await hook.getTokenEditOptions(7, 35);
|
||||
assert.equal(Object.hasOwn(calls[0].params, 'project_id'), false);
|
||||
assert.equal(calls[1].params.project_id, 35);
|
||||
});
|
||||
|
||||
test('token edit options fail closed on HTTP and business errors', async () => {
|
||||
const { hook, transport, errors } = renderTokensHook(12);
|
||||
transport.API.get = async () => ({
|
||||
data: { success: false, message: 'denied' },
|
||||
});
|
||||
assert.equal(await hook.getTokenEditOptions(7, 0), null);
|
||||
transport.API.get = async () => {
|
||||
throw new Error('offline');
|
||||
};
|
||||
assert.equal(await hook.getTokenEditOptions(7, 0), null);
|
||||
assert.deepEqual(errors, ['denied', 'offline']);
|
||||
});
|
||||
Reference in New Issue
Block a user