feat: add query-login

This commit is contained in:
2025-03-22 00:52:58 +08:00
parent 3d45a83129
commit 4b16ec8499
17 changed files with 530 additions and 650 deletions

View File

@@ -1,49 +1,12 @@
import { program, Command } from '@/program.ts';
import { getConfig, writeConfig } from '@/module/get-config.ts';
import { getBaseURL } from '@/module/query.ts';
import { queryLogin, queryMe, switchOrg, switchMe } from '@/query/index.ts';
import { getConfig } from '@/module/get-config.ts';
import inquirer from 'inquirer';
import { runApp } from '../app-run.ts';
import { chalk } from '@/module/chalk.ts';
import { loginInCommand } from '@/module/login/login-by-web.ts';
export const saveToken = async (token: string) => {
const baseURL = getBaseURL();
const config = getConfig();
writeConfig({ ...config, token });
const res = await runApp({ path: 'config', key: 'saveToken', payload: { baseURL, token } });
if (res.code !== 200) {
console.log('Set token failed', res.message || '');
}
};
export const switchToken = async (baseURL: string) => {
const res = await runApp({ path: 'config', key: 'switchToken', payload: { baseURL } });
if (res.code !== 200 && res.code !== 404) {
console.log('switch token failed', res.message || '');
}
return res;
};
export const deleteToken = async (baseURL: string) => {
const res = await runApp({ path: 'config', key: 'deleteToken', payload: { baseURL } });
if (res.code !== 200) {
console.log('delete token failed', res.message || '');
}
return res;
};
export const getTokenList = async () => {
const res = await runApp({ path: 'config', key: 'getTokenList' });
if (res.code !== 200) {
console.log('get token list failed', res.message || '');
}
return res;
};
export const setTokenList = async (data: any[]) => {
const res = await runApp({ path: 'config', key: 'setTokenList', payload: { data } });
if (res.code !== 200) {
console.log('set token list failed', res.message || '');
}
return res;
};
// 定义login命令支持 `-u` 和 `-p` 参数来输入用户名和密码
import { queryLogin, storage } from '@/module/query.ts';
/**
* 定义login命令支持 `-u` 和 `-p` 参数来输入用户名和密码
*/
const loginCommand = new Command('login')
.description('Login to the application')
.option('-u, --username <username>', 'Specify username')
@@ -51,10 +14,9 @@ const loginCommand = new Command('login')
.option('-f, --force', 'Force login')
.option('-w, --web', 'Login on the web')
.action(async (options) => {
const config = getConfig();
let { username, password } = options;
if (options.web) {
await loginInCommand(saveToken);
await loginInCommand();
return;
}
// 如果没有传递参数,则通过交互式输入
@@ -77,7 +39,8 @@ const loginCommand = new Command('login')
username = answers.username || username;
password = answers.password || password;
}
if (config.token) {
const token = storage.getItem('token');
if (token) {
const res = await showMe(false);
if (res.code === 200) {
const data = res.data;
@@ -88,11 +51,11 @@ const loginCommand = new Command('login')
}
}
const res = await queryLogin(username, password);
const res = await queryLogin.login({
username,
password,
});
if (res.code === 200) {
const { token } = res.data;
writeConfig({ ...config, token });
saveToken(token);
console.log('welcome', username);
} else {
console.log('登录失败', res.message || '');
@@ -102,47 +65,19 @@ const loginCommand = new Command('login')
program.addCommand(loginCommand);
const showMe = async (show = true) => {
const me = await queryMe();
const me = await queryLogin.getMe();
if (show) {
// save me to config
const meSet = await runApp({ path: 'config', key: 'meSet', payload: { data: me.data } });
if (me.code === 200) {
console.log('Me', me.data);
} else {
const config = getConfig();
console.log('Show Me failed', me.message);
writeConfig({ ...config, token: '' });
}
console.log('Me', me.data);
}
return me;
};
const switchOrgCommand = new Command('switch').argument('<username>', 'Switch to another organization or username').action(async (username) => {
const config = getConfig();
if (!config.token) {
console.log('Please login first');
return;
}
const meGet = await runApp({ path: 'config', key: 'meGet' });
if (meGet.code !== 200) {
console.log('Please login first');
return;
}
const me = meGet.data?.value || {};
if (me?.username === username) {
// console.log('Already in', options);
console.log('success switch to', username);
return;
}
const res = await switchOrg(username);
const res = await queryLogin.switchUser(username);
if (res.code === 200) {
const token = res.data.token;
writeConfig({ ...config, token });
console.log(`Switch ${username} Success`);
saveToken(token);
await showMe();
console.log('success switch to', username);
} else {
console.log(`Switch ${username} Failed`, res.message || '');
console.log('switch to', username, 'failed', res.message || '');
}
});