fix: 避免每次都需要switch org的问题
This commit is contained in:
parent
f51a304b81
commit
272845fe87
17
src/app-run.ts
Normal file
17
src/app-run.ts
Normal 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;
|
||||
};
|
@ -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 { queryLogin, queryMe, switchOrg, switchMe } from '@/query/index.ts';
|
||||
import inquirer from 'inquirer';
|
||||
|
||||
import { runApp } from '../app-run.ts';
|
||||
// 导入 login 命令
|
||||
|
||||
// 定义login命令,支持 `-u` 和 `-p` 参数来输入用户名和密码
|
||||
@ -35,7 +35,7 @@ const loginCommand = new Command('login')
|
||||
password = answers.password || password;
|
||||
}
|
||||
if (config.token) {
|
||||
const res = await queryMe();
|
||||
const res = await showMe(false);
|
||||
if (res.code === 200) {
|
||||
const data = res.data;
|
||||
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();
|
||||
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: '' });
|
||||
}
|
||||
}
|
||||
return me;
|
||||
};
|
||||
|
||||
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');
|
||||
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);
|
||||
if (res.code === 200) {
|
||||
const token = res.data.token;
|
||||
writeConfig({ ...config, token });
|
||||
console.log('Switch Org Success');
|
||||
showMe();
|
||||
await showMe();
|
||||
} else {
|
||||
console.log('Switch Org Failed', res.message || '');
|
||||
}
|
||||
});
|
||||
|
||||
app.addCommand(switchOrgCommand);
|
||||
program.addCommand(switchOrgCommand);
|
||||
|
||||
const switchMeCommand = new Command('switchMe').action(async () => {
|
||||
const config = getConfig();
|
||||
@ -102,4 +120,10 @@ const switchMeCommand = new Command('switchMe').action(async () => {
|
||||
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);
|
||||
|
@ -3,17 +3,17 @@ import { getConfig, writeConfig } from '@/module/index.ts';
|
||||
import {queryMe} from '../query/index.ts';
|
||||
|
||||
|
||||
const command = new Command('me')
|
||||
.description('')
|
||||
.action(async () => {
|
||||
const config = getConfig()
|
||||
const res = await queryMe();
|
||||
if(res.code===200) {
|
||||
console.log('me', res.data)
|
||||
} else {
|
||||
console.log('not login')
|
||||
writeConfig({ ...config, token: '' });
|
||||
}
|
||||
});
|
||||
// const command = new Command('me')
|
||||
// .description('')
|
||||
// .action(async () => {
|
||||
// const config = getConfig()
|
||||
// const res = await queryMe();
|
||||
// if(res.code===200) {
|
||||
// console.log('me', res.data)
|
||||
// } else {
|
||||
// console.log('not login')
|
||||
// writeConfig({ ...config, token: '' });
|
||||
// }
|
||||
// });
|
||||
|
||||
app.addCommand(command);
|
||||
// app.addCommand(command);
|
||||
|
@ -1 +1,3 @@
|
||||
import './route/user/list.ts';
|
||||
|
||||
import './route/system-config/index.ts';
|
1
src/route/system-config/index.ts
Normal file
1
src/route/system-config/index.ts
Normal file
@ -0,0 +1 @@
|
||||
import './list.ts'
|
58
src/route/system-config/list.ts
Normal file
58
src/route/system-config/list.ts
Normal 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);
|
39
src/route/system-config/model/config.ts
Normal file
39
src/route/system-config/model/config.ts
Normal 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
0
src/scripts/sync.ts
Normal file
Loading…
x
Reference in New Issue
Block a user