fix: 避免每次都需要switch org的问题

This commit is contained in:
熊潇 2024-11-03 03:16:46 +08:00
parent f51a304b81
commit 272845fe87
8 changed files with 166 additions and 25 deletions

17
src/app-run.ts Normal file
View File

@ -0,0 +1,17 @@
import { app } from './app.ts';
import './route/system-config/index.ts';
type Message = {
path: string;
key?: string;
payload?: any;
};
export const runApp = async (msg: Message) => {
const { code, body, message } = await app.router.parse(msg);
const res = { code, data: body };
if (message) {
res['message'] = message;
}
return res;
};

View File

@ -1,8 +1,8 @@
import { program as app, Command } from '@/program.ts'; import { program, Command } from '@/program.ts';
import { getConfig, writeConfig } from '@/module/get-config.ts'; import { getConfig, writeConfig } from '@/module/get-config.ts';
import { queryLogin, queryMe, switchOrg, switchMe } from '@/query/index.ts'; import { queryLogin, queryMe, switchOrg, switchMe } from '@/query/index.ts';
import inquirer from 'inquirer'; import inquirer from 'inquirer';
import { runApp } from '../app-run.ts';
// 导入 login 命令 // 导入 login 命令
// 定义login命令支持 `-u` 和 `-p` 参数来输入用户名和密码 // 定义login命令支持 `-u` 和 `-p` 参数来输入用户名和密码
@ -35,7 +35,7 @@ const loginCommand = new Command('login')
password = answers.password || password; password = answers.password || password;
} }
if (config.token) { if (config.token) {
const res = await queryMe(); const res = await showMe(false);
if (res.code === 200) { if (res.code === 200) {
const data = res.data; const data = res.data;
if (data.username === username) { if (data.username === username) {
@ -55,15 +55,22 @@ const loginCommand = new Command('login')
} }
}); });
app.addCommand(loginCommand); program.addCommand(loginCommand);
const showMe = async () => { const showMe = async (show = true) => {
const me = await queryMe(); const me = await queryMe();
if (show) {
// save me to config
const meSet = await runApp({ path: 'config', key: 'meSet', payload: { data: me.data } });
if (me.code === 200) { if (me.code === 200) {
console.log('Me', me.data); console.log('Me', me.data);
} else { } else {
const config = getConfig();
console.log('Show Me failed', me.message); console.log('Show Me failed', me.message);
writeConfig({ ...config, token: '' });
} }
}
return me;
}; };
const switchOrgCommand = new Command('switchOrg').argument('<username>', 'Switch to another organization').action(async (options) => { const switchOrgCommand = new Command('switchOrg').argument('<username>', 'Switch to another organization').action(async (options) => {
@ -73,18 +80,29 @@ const switchOrgCommand = new Command('switchOrg').argument('<username>', 'Switch
console.log('Please login first'); console.log('Please login first');
return; 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 === options) {
// console.log('Already in', options);
console.log('success switch to', options);
return;
}
const res = await switchOrg(options); const res = await switchOrg(options);
if (res.code === 200) { if (res.code === 200) {
const token = res.data.token; const token = res.data.token;
writeConfig({ ...config, token }); writeConfig({ ...config, token });
console.log('Switch Org Success'); console.log('Switch Org Success');
showMe(); await showMe();
} else { } else {
console.log('Switch Org Failed', res.message || ''); console.log('Switch Org Failed', res.message || '');
} }
}); });
app.addCommand(switchOrgCommand); program.addCommand(switchOrgCommand);
const switchMeCommand = new Command('switchMe').action(async () => { const switchMeCommand = new Command('switchMe').action(async () => {
const config = getConfig(); const config = getConfig();
@ -102,4 +120,10 @@ const switchMeCommand = new Command('switchMe').action(async () => {
console.log('Switch Me Failed', res.message || ''); console.log('Switch Me Failed', res.message || '');
} }
}); });
app.addCommand(switchMeCommand); program.addCommand(switchMeCommand);
const command = new Command('me').description('').action(async () => {
await showMe();
});
program.addCommand(command);

View File

@ -3,17 +3,17 @@ import { getConfig, writeConfig } from '@/module/index.ts';
import {queryMe} from '../query/index.ts'; import {queryMe} from '../query/index.ts';
const command = new Command('me') // const command = new Command('me')
.description('') // .description('')
.action(async () => { // .action(async () => {
const config = getConfig() // const config = getConfig()
const res = await queryMe(); // const res = await queryMe();
if(res.code===200) { // if(res.code===200) {
console.log('me', res.data) // console.log('me', res.data)
} else { // } else {
console.log('not login') // console.log('not login')
writeConfig({ ...config, token: '' }); // writeConfig({ ...config, token: '' });
} // }
}); // });
app.addCommand(command); // app.addCommand(command);

View File

@ -1 +1,3 @@
import './route/user/list.ts'; import './route/user/list.ts';
import './route/system-config/index.ts';

View File

@ -0,0 +1 @@
import './list.ts'

View File

@ -0,0 +1,58 @@
import { app } from '@/app.ts';
import { Config } from './model/config.ts';
import { CustomError } from '@kevisual/router';
app
.route({
path: 'config',
key: 'list',
})
.define(async (ctx) => {
ctx.body = await Config.findAll();
})
.addTo(app);
app
.route({
path: 'config',
key: 'meGet',
})
.define(async (ctx) => {
const config = await Config.findOne({
where: {
key: 'me',
},
logging: false,
});
ctx.body = config;
})
.addTo(app);
app
.route({
path: 'config',
key: 'meSet',
})
.define(async (ctx) => {
const { data } = ctx.query;
if (!data) {
throw new CustomError('data is required');
}
let config = await Config.findOne({
where: { key: 'me' }, // 自定义条件
logging: false,
});
if (!config) {
config = await Config.create({
key: 'me',
value: data,
});
ctx.body = config;
return;
} else {
config.value = data;
await config.save();
ctx.body = config;
}
})
.addTo(app);

View File

@ -0,0 +1,39 @@
import { sequelize } from '@/app.ts';
import { DataTypes, Model } from 'sequelize';
// Define the system config model
export class Config extends Model {
declare id: number;
declare key: string;
declare value: string;
}
Config.init(
{
id: {
type: DataTypes.INTEGER,
autoIncrement: true,
primaryKey: true,
},
key: {
type: DataTypes.STRING,
allowNull: false,
},
value: {
type: DataTypes.JSON,
defaultValue: {},
},
},
{
sequelize,
tableName: 'system_config',
},
);
await Config.sync({
alter: true,
logging: false,
}).catch((e) => {
console.error('Config table sync error', e);
});

0
src/scripts/sync.ts Normal file
View File