|
- import fs from 'node:fs';
- import path from 'node:path';
- import { fileURLToPath } from 'node:url';
- const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../..');
- export function localCredentials() {
- const temporary = path.join(root, '../.codex-tmp/review-20260905/test-admin.json');
- if (fs.existsSync(temporary)) {
- const { username, password } = JSON.parse(fs.readFileSync(temporary, 'utf8'));
- return { username, password };
- }
- const raw = fs.readFileSync(path.join(root, '账号.MD'), 'utf8').replace(/^\uFEFF/, '');
- const lines = raw.split(/\r?\n/).map(x => x.trim()).filter(Boolean);
- const values = lines.map(line => line.replace(/^.*?[::]\s*/, '').trim());
- if (values.length < 2 || !values[0] || !values[1]) throw new Error('Local credential file has an unsupported format');
- return { username: values[0], password: values[1] };
- }
- export async function api(pathname, token, body, method = body === undefined ? 'GET' : 'POST') {
- const headers = { 'Content-Type': 'application/json' };
- if (token) headers.Authorization = `Bearer ${token}`;
- if (body?.commandId) headers['Idempotency-Key'] = body.commandId;
- const response = await fetch(`http://127.0.0.1:6180${pathname}`, { method, headers, body: body === undefined ? undefined : JSON.stringify(body) });
- const value = await response.json();
- return { status: response.status, data: value.data, message: value.message };
- }
- if (process.argv[1] === fileURLToPath(import.meta.url)) {
- const result = await api('/api/auth/v1/auth/login', null, { ...localCredentials(), roleCode: 'admin', rememberMe: false });
- console.log('login status', result.status);
- if (!result.data?.accessToken) throw new Error('Local account authentication failed');
- const token = result.data.accessToken;
- try {
- const roles = await api('/api/auth/v1/roles?size=100', token);
- console.log('roles', (roles.data?.records ?? roles.data ?? []).map(x => ({ id: x.id, code: x.code })));
- const tasks = await api('/api/tran/v1/teaching/assignments?size=100', token);
- console.log('tasks', (tasks.data?.records ?? []).map(x => ({ id: x.id, code: x.code, channel: x.channel, status: x.status, kind: x.assignmentKind })));
- } finally { await api('/api/auth/v1/auth/logout', token, {}); }
- }
|