You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

37 regels
2.2 KiB

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