diff --git a/src/app-run.ts b/src/app-run.ts new file mode 100644 index 0000000..b8d480b --- /dev/null +++ b/src/app-run.ts @@ -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; +}; diff --git a/src/command/login.ts b/src/command/login.ts index 770e1c3..b32fa64 100644 --- a/src/command/login.ts +++ b/src/command/login.ts @@ -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 (me.code === 200) { - console.log('Me', me.data); - } else { - console.log('Show Me failed', me.message); + 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('', 'Switch to another organization').action(async (options) => { @@ -73,18 +80,29 @@ const switchOrgCommand = new Command('switchOrg').argument('', '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); diff --git a/src/command/me.ts b/src/command/me.ts index a4ab098..d16347f 100644 --- a/src/command/me.ts +++ b/src/command/me.ts @@ -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); diff --git a/src/route.ts b/src/route.ts index ae55003..d36dbb8 100644 --- a/src/route.ts +++ b/src/route.ts @@ -1 +1,3 @@ import './route/user/list.ts'; + +import './route/system-config/index.ts'; \ No newline at end of file diff --git a/src/route/system-config/index.ts b/src/route/system-config/index.ts new file mode 100644 index 0000000..9166f9d --- /dev/null +++ b/src/route/system-config/index.ts @@ -0,0 +1 @@ +import './list.ts' \ No newline at end of file diff --git a/src/route/system-config/list.ts b/src/route/system-config/list.ts new file mode 100644 index 0000000..db33941 --- /dev/null +++ b/src/route/system-config/list.ts @@ -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); diff --git a/src/route/system-config/model/config.ts b/src/route/system-config/model/config.ts new file mode 100644 index 0000000..e123ee9 --- /dev/null +++ b/src/route/system-config/model/config.ts @@ -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); +}); diff --git a/src/scripts/sync.ts b/src/scripts/sync.ts new file mode 100644 index 0000000..e69de29