mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-25 17:49:20 +08:00
refactor(license-server): cli重构成tui
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
import { input, password } from '@inquirer/prompts';
|
||||
|
||||
import { apiRequest } from '../api/client.js';
|
||||
import { loadConfig, resolveApiUrl, saveConfig } from '../config.js';
|
||||
import { printError, printSuccess } from '../ui/render.js';
|
||||
|
||||
interface LoginResult {
|
||||
token: string;
|
||||
username: string;
|
||||
expiresAt: string | null;
|
||||
}
|
||||
|
||||
export async function loginCommand(opts: { url?: string; username?: string; password?: string }) {
|
||||
const config = await loadConfig();
|
||||
const apiUrl = resolveApiUrl(opts.url, config);
|
||||
|
||||
let username = opts.username || process.env.ECOM_LICENSE_USERNAME;
|
||||
let pass = opts.password || process.env.ECOM_LICENSE_PASSWORD;
|
||||
|
||||
if (!username) {
|
||||
username = await input({ message: '用户名', default: username || 'admin' });
|
||||
}
|
||||
if (!pass) {
|
||||
pass = await password({ message: '密码', mask: '*' });
|
||||
}
|
||||
|
||||
if (!username || !pass) {
|
||||
printError('请提供用户名和密码');
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const result = await apiRequest<LoginResult>(
|
||||
'POST',
|
||||
'/auth/login',
|
||||
{ username, password: pass },
|
||||
false,
|
||||
);
|
||||
await saveConfig({
|
||||
apiUrl,
|
||||
token: result.token,
|
||||
username: result.username,
|
||||
expiresAt: result.expiresAt || undefined,
|
||||
});
|
||||
printSuccess(
|
||||
`登录成功 · ${result.username}` +
|
||||
(result.expiresAt ? ` · Token 有效期至 ${result.expiresAt.slice(0, 10)}` : ''),
|
||||
);
|
||||
} catch (e: any) {
|
||||
printError(e?.message || '登录失败');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user