From e75a6831731972681b70be4030487bca62c273de Mon Sep 17 00:00:00 2001 From: whyour Date: Sat, 8 Jan 2022 01:09:38 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dsqlite=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E6=93=8D=E4=BD=9C=E7=B1=BB=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- back/api/cron.ts | 29 +++++----- back/api/dependence.ts | 10 ++-- back/api/env.ts | 6 +- back/api/open.ts | 4 +- back/data/cron.ts | 15 +---- back/data/dependence.ts | 4 +- back/loaders/db.ts | 2 +- back/services/cron.ts | 93 +++++++++++++++---------------- back/services/dependence.ts | 52 ++++++++--------- back/services/env.ts | 27 +++------ back/services/open.ts | 49 ++++++++-------- back/services/system.ts | 11 ++-- back/services/user.ts | 15 ++--- src/pages/crontab/logModal.tsx | 2 +- src/pages/crontab/modal.tsx | 75 +++++++++++++------------ src/pages/dependence/index.tsx | 6 +- src/pages/dependence/logModal.tsx | 6 +- src/pages/dependence/modal.tsx | 5 +- src/pages/script/index.tsx | 1 - src/pages/setting/checkUpdate.tsx | 2 +- src/utils/config.ts | 2 + 21 files changed, 199 insertions(+), 217 deletions(-) diff --git a/back/api/cron.ts b/back/api/cron.ts index be7cfa4f..f748f840 100644 --- a/back/api/cron.ts +++ b/back/api/cron.ts @@ -84,19 +84,22 @@ export default (app: Router) => { }, ); - route.put( - '/removelabels', + route.delete( + '/labels', celebrate({ body: Joi.object({ - ids:Joi.array().items(Joi.number().required()), - labels:Joi.array().items(Joi.string().required()), - }) + ids: Joi.array().items(Joi.number().required()), + labels: Joi.array().items(Joi.string().required()), + }), }), async (req: Request, res: Response, next: NextFunction) => { const logger: Logger = Container.get('logger'); try { const cronService = Container.get(CronService); - const data = await cronService.removeLabels(req.body.ids,req.body.labels); + const data = await cronService.removeLabels( + req.body.ids, + req.body.labels, + ); return res.send({ code: 200, data }); } catch (e) { logger.error('🔥 error: %o', e); @@ -105,19 +108,19 @@ export default (app: Router) => { }, ); - route.put( - '/addlabels', + route.post( + '/labels', celebrate({ body: Joi.object({ - ids:Joi.array().items(Joi.number().required()), - labels:Joi.array().items(Joi.string().required()), - }) + ids: Joi.array().items(Joi.number().required()), + labels: Joi.array().items(Joi.string().required()), + }), }), async (req: Request, res: Response, next: NextFunction) => { const logger: Logger = Container.get('logger'); try { const cronService = Container.get(CronService); - const data = await cronService.addLabels(req.body.ids,req.body.labels); + const data = await cronService.addLabels(req.body.ids, req.body.labels); return res.send({ code: 200, data }); } catch (e) { logger.error('🔥 error: %o', e); @@ -293,7 +296,7 @@ export default (app: Router) => { const logger: Logger = Container.get('logger'); try { const cronService = Container.get(CronService); - const data = await cronService.get(req.params.id); + const data = await cronService.getDb({ id: req.params.id }); return res.send({ code: 200, data }); } catch (e) { logger.error('🔥 error: %o', e); diff --git a/back/api/dependence.ts b/back/api/dependence.ts index 22dba7a5..30132486 100644 --- a/back/api/dependence.ts +++ b/back/api/dependence.ts @@ -27,7 +27,7 @@ export default (app: Router) => { Joi.object({ name: Joi.string().required(), type: Joi.number().required(), - remark: Joi.number().optional().allow(''), + remark: Joi.string().optional().allow(''), }), ), }), @@ -49,9 +49,9 @@ export default (app: Router) => { celebrate({ body: Joi.object({ name: Joi.string().required(), - id: Joi.string().required(), + id: Joi.number().required(), type: Joi.number().required(), - remark: Joi.number().optional().allow(''), + remark: Joi.string().optional().allow(''), }), }), async (req: Request, res: Response, next: NextFunction) => { @@ -107,14 +107,14 @@ export default (app: Router) => { '/:id', celebrate({ params: Joi.object({ - id: Joi.string().required(), + id: Joi.number().required(), }), }), async (req: Request<{ id: number }>, res: Response, next: NextFunction) => { const logger: Logger = Container.get('logger'); try { const dependenceService = Container.get(DependenceService); - const data = await dependenceService.get(req.params.id); + const data = await dependenceService.getDb({ id: req.params.id }); return res.send({ code: 200, data }); } catch (e) { logger.error('🔥 error: %o', e); diff --git a/back/api/env.ts b/back/api/env.ts index 12e956d6..33b87dc3 100644 --- a/back/api/env.ts +++ b/back/api/env.ts @@ -51,7 +51,7 @@ export default (app: Router) => { value: Joi.string().required(), name: Joi.string().required(), remarks: Joi.string().optional().allow(''), - id: Joi.string().required(), + id: Joi.number().required(), }), }), async (req: Request, res: Response, next: NextFunction) => { @@ -89,7 +89,7 @@ export default (app: Router) => { '/:id/move', celebrate({ params: Joi.object({ - id: Joi.string().required(), + id: Joi.number().required(), }), body: Joi.object({ fromIndex: Joi.number().required(), @@ -170,7 +170,7 @@ export default (app: Router) => { '/:id', celebrate({ params: Joi.object({ - id: Joi.string().required(), + id: Joi.number().required(), }), }), async (req: Request<{ id: number }>, res: Response, next: NextFunction) => { diff --git a/back/api/open.ts b/back/api/open.ts index 1e09e9d7..50a572d8 100644 --- a/back/api/open.ts +++ b/back/api/open.ts @@ -49,7 +49,7 @@ export default (app: Router) => { body: Joi.object({ name: Joi.string().optional().allow(''), scopes: Joi.array().items(Joi.string()), - id: Joi.string().required(), + id: Joi.number().required(), }), }), async (req: Request, res: Response, next: NextFunction) => { @@ -87,7 +87,7 @@ export default (app: Router) => { '/apps/:id/reset-secret', celebrate({ params: Joi.object({ - id: Joi.string().required(), + id: Joi.number().required(), }), }), async (req: Request<{ id: number }>, res: Response, next: NextFunction) => { diff --git a/back/data/cron.ts b/back/data/cron.ts index 928e3dfd..7dc703f6 100644 --- a/back/data/cron.ts +++ b/back/data/cron.ts @@ -14,7 +14,7 @@ export class Crontab { isDisabled?: 1 | 0; log_path?: string; isPinned?: 1 | 0; - labels: Array; + labels?: string[]; last_running_time?: number; last_execution_time?: number; @@ -60,18 +60,7 @@ export const CrontabModel = sequelize.define('Crontab', { isDisabled: DataTypes.NUMBER, isPinned: DataTypes.NUMBER, log_path: DataTypes.STRING, - labels: { - type: DataTypes.STRING, - allowNull: false, - get() { - if (this.getDataValue('labels')) { - return this.getDataValue('labels').split(',') - } - }, - set(value) { - this.setDataValue('labels', value.join(',')); - }, - }, + labels: DataTypes.JSON, last_running_time: DataTypes.NUMBER, last_execution_time: DataTypes.NUMBER, }); diff --git a/back/data/dependence.ts b/back/data/dependence.ts index f254e008..17e149b3 100644 --- a/back/data/dependence.ts +++ b/back/data/dependence.ts @@ -55,9 +55,9 @@ export const DependenceModel = sequelize.define( 'Dependence', { name: DataTypes.STRING, - type: DataTypes.STRING, + type: DataTypes.NUMBER, timestamp: DataTypes.STRING, - status: DataTypes.STRING, + status: DataTypes.NUMBER, log: DataTypes.JSON, remark: DataTypes.STRING, }, diff --git a/back/loaders/db.ts b/back/loaders/db.ts index 0e9bb82b..dae77958 100644 --- a/back/loaders/db.ts +++ b/back/loaders/db.ts @@ -60,7 +60,7 @@ export default async () => { db.authDb.persistence.compactDatafile(); try { - await sequelize.sync(); + await sequelize.sync({ alter: true }); } catch (error) { console.log(error); } diff --git a/back/services/cron.ts b/back/services/cron.ts index 6ca0d274..8b817505 100644 --- a/back/services/cron.ts +++ b/back/services/cron.ts @@ -31,25 +31,19 @@ export default class CronService { } public async insert(payload: Crontab): Promise { - return await CrontabModel.create(payload); + return await CrontabModel.create(payload, { returning: true }); } public async update(payload: Crontab): Promise { - const { id, ...other } = payload; - const doc = await this.get(id as number); - const tab = new Crontab({ ...doc, ...other }); - tab.saved = false; - const newDoc = await this.updateDb(tab); + payload.saved = false; + const newDoc = await this.updateDb(payload); await this.set_crontab(this.isSixCron(newDoc)); return newDoc; } public async updateDb(payload: Crontab): Promise { - const result = await CrontabModel.update( - { ...payload }, - { where: { id: payload.id } }, - ); - return result[1][0]; + await CrontabModel.update(payload, { where: { id: payload.id } }); + return await this.getDb({ id: payload.id }); } public async status({ @@ -93,43 +87,56 @@ export default class CronService { await CrontabModel.update({ isPinned: 0 }, { where: { id: ids } }); } - public async addLabels(ids: string[],labels: string[]){ - const docs = await CrontabModel.findAll({ where: { id:ids }}); + public async addLabels(ids: string[], labels: string[]) { + const docs = await CrontabModel.findAll({ where: { id: ids } }); for (const doc of docs) { - await CrontabModel.update({ - labels: Array.from(new Set(doc.labels.concat(labels))) - },{ where: {id:doc.id}}); + await CrontabModel.update( + { + labels: Array.from(new Set((doc.labels || []).concat(labels))), + }, + { where: { id: doc.id } }, + ); } } - public async removeLabels(ids: string[],labels: string[]){ - const docs = await CrontabModel.findAll({ where: { id:ids }}); + public async removeLabels(ids: string[], labels: string[]) { + const docs = await CrontabModel.findAll({ where: { id: ids } }); for (const doc of docs) { - await CrontabModel.update({ - labels: doc.labels.filter( label => !labels.includes(label) ) - },{ where: {id:doc.id}}); + await CrontabModel.update( + { + labels: (doc.labels || []).filter((label) => !labels.includes(label)), + }, + { where: { id: doc.id } }, + ); } } public async crontabs(searchText?: string): Promise { let query = {}; if (searchText) { - const textArray = searchText.split(":"); + const textArray = searchText.split(':'); switch (textArray[0]) { - case "name": - query = {name:{[Op.or]:createRegexp(textArray[1])}}; - break; - case "command": - query = {command:{[Op.or]:createRegexp(textArray[1])}}; - break; - case "schedule": - query = {schedule:{[Op.or]:createRegexp(textArray[1])}}; - break; - case "label": - query = {labels:{[Op.or]:createRegexp(textArray[1])}}; + case 'name': + case 'command': + case 'schedule': + case 'label': + const column = textArray[0] === 'label' ? 'labels' : textArray[0]; + query = { + [column]: { + [Op.or]: [ + { [Op.like]: `%${textArray[1]}%` }, + { [Op.like]: `%${encodeURIComponent(textArray[1])}%` }, + ], + }, + }; break; default: - const reg = createRegexp(searchText); + const reg = { + [Op.or]: [ + { [Op.like]: `%${searchText}%` }, + { [Op.like]: `%${encodeURIComponent(searchText)}%` }, + ], + }; query = { [Op.or]: [ { @@ -155,19 +162,11 @@ export default class CronService { } catch (error) { throw error; } - function createRegexp(text:string) { - return { - [Op.or]: [ - { [Op.like]: `%${text}%` }, - { [Op.like]: `%${encodeURIComponent(text)}%` }, - ], - }; - } } - public async get(id: number): Promise { - const result = await CrontabModel.findAll({ where: { id } }); - return result[0] as any; + public async getDb(query: any): Promise { + const doc: any = await CrontabModel.findOne({ where: { ...query } }); + return doc && (doc.get({ plain: true }) as Crontab); } public async run(ids: number[]) { @@ -243,7 +242,7 @@ export default class CronService { private async runSingle(cronId: number): Promise { return new Promise(async (resolve: any) => { - const cron = await this.get(cronId); + const cron = await this.getDb({ id: cronId }); if (cron.status !== CrontabStatus.queued) { resolve(); return; @@ -312,7 +311,7 @@ export default class CronService { } public async log(id: number) { - const doc = await this.get(id); + const doc = await this.getDb({ id }); if (!doc) { return ''; } diff --git a/back/services/dependence.ts b/back/services/dependence.ts index 133e00de..1a3c355a 100644 --- a/back/services/dependence.ts +++ b/back/services/dependence.ts @@ -41,7 +41,7 @@ export default class DependenceService { payload: Dependence & { id: string }, ): Promise { const { id, ...other } = payload; - const doc = await this.get(id); + const doc = await this.getDb({ id }); const tab = new Dependence({ ...doc, ...other, @@ -53,18 +53,16 @@ export default class DependenceService { } private async updateDb(payload: Dependence): Promise { - const [, docs] = await DependenceModel.update( - { ...payload }, - { where: { id: payload.id } }, - ); - return docs[0]; + await DependenceModel.update(payload, { where: { id: payload.id } }); + return await this.getDb({ id: payload.id }); } public async remove(ids: number[]) { - const [, docs] = await DependenceModel.update( + await DependenceModel.update( { status: DependenceStatus.removing, log: [] }, { where: { id: ids } }, ); + const docs = await DependenceModel.findAll({ where: { id: ids } }); this.installOrUninstallDependencies(docs, false); } @@ -82,7 +80,7 @@ export default class DependenceService { const encodeText = encodeURIComponent(searchValue); const reg = { [Op.or]: [ - { [Op.like]: `%${encodeText}&` }, + { [Op.like]: `%${searchValue}&` }, { [Op.like]: `%${encodeText}%` }, ], }; @@ -101,10 +99,12 @@ export default class DependenceService { } public async reInstall(ids: number[]): Promise { - const [, docs] = await DependenceModel.update( + await DependenceModel.update( { status: DependenceStatus.installing, log: [] }, { where: { id: ids } }, ); + + const docs = await DependenceModel.findAll({ where: { id: ids } }); await this.installOrUninstallDependencies(docs); return docs; } @@ -114,25 +114,22 @@ export default class DependenceService { return docs; } - public async get(id: number): Promise { - const docs = await DependenceModel.findAll({ where: { id } }); - return docs[0]; + public async getDb(query: any): Promise { + const doc: any = await DependenceModel.findOne({ where: { ...query } }); + return doc && (doc.get({ plain: true }) as Dependence); } private async updateLog(ids: number[], log: string): Promise { const doc = await DependenceModel.findOne({ where: { id: ids } }); const newLog = doc?.log ? [...doc.log, log] : [log]; - const [, docs] = await DependenceModel.update( - { status: DependenceStatus.installing, log: newLog }, - { where: { id: ids } }, - ); + await DependenceModel.update({ log: newLog }, { where: { id: ids } }); } public installOrUninstallDependencies( dependencies: Dependence[], isInstall: boolean = true, ) { - return new Promise((resolve) => { + return new Promise(async (resolve) => { if (dependencies.length === 0) { resolve(null); return; @@ -154,41 +151,41 @@ export default class DependenceService { ).toLocaleString()}`, references: depIds, }); - this.updateLog( + await this.updateLog( depIds, `开始${actionText}依赖 ${depNames},开始时间 ${new Date( startTime, ).toLocaleString()}\n`, ); - cp.stdout.on('data', (data) => { + cp.stdout.on('data', async (data) => { this.sockService.sendMessage({ type: 'installDependence', message: data.toString(), references: depIds, }); - this.updateLog(depIds, data.toString()); + await this.updateLog(depIds, data.toString()); }); - cp.stderr.on('data', (data) => { + cp.stderr.on('data', async (data) => { this.sockService.sendMessage({ type: 'installDependence', message: data.toString(), references: depIds, }); - this.updateLog(depIds, data.toString()); + await this.updateLog(depIds, data.toString()); }); - cp.on('error', (err) => { + cp.on('error', async (err) => { this.sockService.sendMessage({ type: 'installDependence', message: JSON.stringify(err), references: depIds, }); - this.updateLog(depIds, JSON.stringify(err)); + await this.updateLog(depIds, JSON.stringify(err)); resolve(null); }); - cp.on('close', (code) => { + cp.on('close', async (code) => { const endTime = Date.now(); const isSucceed = code === 0; const resultText = isSucceed ? '成功' : '失败'; @@ -200,7 +197,7 @@ export default class DependenceService { ).toLocaleString()},耗时 ${(endTime - startTime) / 1000} 秒`, references: depIds, }); - this.updateLog( + await this.updateLog( depIds, `依赖${actionText}${resultText},结束时间 ${new Date( endTime, @@ -217,8 +214,7 @@ export default class DependenceService { ? DependenceStatus.installFailed : DependenceStatus.removeFailed; } - - DependenceModel.update({ status }, { where: { id: depIds } }); + await DependenceModel.update({ status }, { where: { id: depIds } }); // 如果删除依赖成功,3秒后删除数据库记录 if (isSucceed && !isInstall) { diff --git a/back/services/env.ts b/back/services/env.ts index bb68a57b..61331ebd 100644 --- a/back/services/env.ts +++ b/back/services/env.ts @@ -34,20 +34,14 @@ export default class EnvService { } public async update(payload: Env): Promise { - const { id, ...other } = payload; - const doc = await this.get(id as number); - const tab = new Env({ ...doc, ...other }); - const newDoc = await this.updateDb(tab); + const newDoc = await this.updateDb(payload); await this.set_envs(); return newDoc; } private async updateDb(payload: Env): Promise { - const [, docs] = await EnvModel.update( - { ...payload }, - { where: { id: payload.id } }, - ); - return docs[0]; + await EnvModel.update({ ...payload }, { where: { id: payload.id } }); + return await this.getDb({ id: payload.id }); } public async remove(ids: string[]) { @@ -126,13 +120,13 @@ export default class EnvService { return docs; } - public async get(id: number): Promise { - const docs = await EnvModel.findAll({ where: { id } }); - return docs[0]; + public async getDb(query: any): Promise { + const doc: any = await EnvModel.findOne({ where: { ...query } }); + return doc && (doc.get({ plain: true }) as Env); } public async disabled(ids: string[]) { - const [, docs] = await EnvModel.update( + await EnvModel.update( { status: EnvStatus.disabled }, { where: { id: ids } }, ); @@ -140,15 +134,12 @@ export default class EnvService { } public async enabled(ids: string[]) { - const [, docs] = await EnvModel.update( - { status: EnvStatus.normal }, - { where: { id: ids } }, - ); + await EnvModel.update({ status: EnvStatus.normal }, { where: { id: ids } }); await this.set_envs(); } public async updateNames({ ids, name }: { ids: string[]; name: string }) { - const [, docs] = await EnvModel.update({ name }, { where: { id: ids } }); + await EnvModel.update({ name }, { where: { id: ids } }); await this.set_envs(); } diff --git a/back/services/open.ts b/back/services/open.ts index 63595924..ab044ec0 100644 --- a/back/services/open.ts +++ b/back/services/open.ts @@ -12,7 +12,7 @@ export default class OpenService { constructor(@Inject('logger') private logger: winston.Logger) {} public async findTokenByValue(token: string): Promise { - const doc = await AppModel.findOne({ + const doc = await this.getDb({ where: sequelize.fn( 'JSON_CONTAINS', sequelize.col('tokens'), @@ -23,32 +23,35 @@ export default class OpenService { } public async create(payload: App): Promise { - const tab = new App({ ...payload }); + const tab = { ...payload }; tab.client_id = createRandomString(12, 12); tab.client_secret = createRandomString(24, 24); - const docs = await this.insert([tab]); - return { ...docs[0], tokens: [] }; + const doc = await this.insert(tab); + return { ...doc, tokens: [] }; } - public async insert(payloads: App[]): Promise { - const docs = await AppModel.bulkCreate(payloads); - return docs; + public async insert(payloads: App): Promise { + const doc = await AppModel.create(payloads, { returning: true }); + return doc.get({ plain: true }) as App; } public async update(payload: App): Promise { - const { id, client_id, client_secret, tokens, ...other } = payload; - const doc = await this.get(id); - const tab = new App({ ...doc, ...other }); - const newDoc = await this.updateDb(tab); + const newDoc = await this.updateDb({ + name: payload.name, + scopes: payload.scopes, + id: payload.id, + } as any); return { ...newDoc, tokens: [] }; } private async updateDb(payload: App): Promise { - const [, docs] = await AppModel.update( - { ...payload }, - { where: { id: payload.id } }, - ); - return docs[0]; + await AppModel.update(payload, { where: { id: payload.id } }); + return await this.getDb({ id: payload.id }); + } + + public async getDb(query: any): Promise { + const doc: any = await AppModel.findOne({ where: { ...query } }); + return doc && (doc.get({ plain: true }) as App); } public async remove(ids: number[]) { @@ -56,10 +59,7 @@ export default class OpenService { } public async resetSecret(id: number): Promise { - const doc = await this.get(id); - const tab = new App({ ...doc }); - tab.client_secret = createRandomString(24, 24); - tab.tokens = []; + const tab: any = { client_secret: createRandomString(24, 24), tokens: [] }; const newDoc = await this.updateDb(tab); return newDoc; } @@ -104,12 +104,7 @@ export default class OpenService { private async find(query: any, sort?: any): Promise { const docs = await AppModel.findAll({ where: { ...query } }); - return docs; - } - - public async get(id: number): Promise { - const docs = await AppModel.findAll({ where: { id } }); - return docs[0]; + return docs.map((x) => x.get({ plain: true })); } public async authToken({ @@ -123,7 +118,7 @@ export default class OpenService { const expiration = Math.round(Date.now() / 1000) + 2592000; // 2592000 30天 const doc = await AppModel.findOne({ where: { client_id, client_secret } }); if (doc) { - const [, docs] = await AppModel.update( + await AppModel.update( { tokens: [...(doc.tokens || []), { value: token, expiration }] }, { where: { client_id, client_secret } }, ); diff --git a/back/services/system.ts b/back/services/system.ts index 774a7560..e58d9dca 100644 --- a/back/services/system.ts +++ b/back/services/system.ts @@ -24,18 +24,21 @@ export default class SystemService { ) {} public async getLogRemoveFrequency() { - const doc = await AuthModel.findOne({ - where: { type: AuthDataType.removeLogFrequency }, - }); + const doc = await this.getDb({ type: AuthDataType.removeLogFrequency }); return (doc && doc.info) || {}; } private async updateAuthDb(payload: AuthInfo): Promise { await AuthModel.upsert({ ...payload }); - const doc = await AuthModel.findOne({ where: { type: payload.type } }); + const doc = await this.getDb({ type: payload.type }); return doc; } + public async getDb(query: any): Promise { + const doc: any = await AuthModel.findOne({ where: { ...query } }); + return doc && (doc.get({ plain: true }) as any); + } + public async updateNotificationMode(notificationInfo: NotificationInfo) { const code = Math.random().toString().slice(-6); const isSuccess = await this.notificationService.testNotify( diff --git a/back/services/user.ts b/back/services/user.ts index d07edbeb..a50793a5 100644 --- a/back/services/user.ts +++ b/back/services/user.ts @@ -303,25 +303,26 @@ export default class UserService { } public async getNotificationMode(): Promise { - const doc = await AuthModel.findOne({ - where: { type: AuthDataType.notification }, - }); + const doc = await this.getDb({ type: AuthDataType.notification }); return (doc && doc.info) || {}; } public async getLogRemoveFrequency() { - const doc = await AuthModel.findOne({ - where: { type: AuthDataType.removeLogFrequency }, - }); + const doc = await this.getDb({ type: AuthDataType.removeLogFrequency }); return (doc && doc.info) || {}; } private async updateAuthDb(payload: AuthInfo): Promise { await AuthModel.upsert({ ...payload }); - const doc = await AuthModel.findOne({ where: { type: payload.type } }); + const doc = await this.getDb({ type: payload.type }); return doc; } + public async getDb(query: any): Promise { + const doc: any = await AuthModel.findOne({ where: { ...query } }); + return doc && (doc.get({ plain: true }) as any); + } + public async updateNotificationMode(notificationInfo: NotificationInfo) { const code = Math.random().toString().slice(-6); const isSuccess = await this.notificationService.testNotify( diff --git a/src/pages/crontab/logModal.tsx b/src/pages/crontab/logModal.tsx index cb6385b3..c6a6bfd3 100644 --- a/src/pages/crontab/logModal.tsx +++ b/src/pages/crontab/logModal.tsx @@ -38,7 +38,7 @@ const CronLogModal = ({ request .get(`${config.apiPrefix}crons/${cron.id}/log`) .then((data: any) => { - if (localStorage.getItem('logCron') === cron.id) { + if (localStorage.getItem('logCron') === String(cron.id)) { const log = data.data as string; setValue(log || '暂无日志'); setExecuting( diff --git a/src/pages/crontab/modal.tsx b/src/pages/crontab/modal.tsx index df0048b5..70452f44 100644 --- a/src/pages/crontab/modal.tsx +++ b/src/pages/crontab/modal.tsx @@ -20,19 +20,26 @@ const CronModal = ({ setLoading(true); const method = cron ? 'put' : 'post'; const payload = { ...values }; + if (typeof payload.labels === 'string') { + payload.labels = values.labels.split(/,|,/); + } if (cron) { payload.id = cron.id; } - const { code, data } = await request[method](`${config.apiPrefix}crons`, { - data: payload, - }); - if (code === 200) { - message.success(cron ? '更新Cron成功' : '新建Cron成功'); - } else { - message.error(data); + try { + const { code, data } = await request[method](`${config.apiPrefix}crons`, { + data: payload, + }); + if (code === 200) { + message.success(cron ? '更新Cron成功' : '新建Cron成功'); + } else { + message.error(data); + } + setLoading(false); + handleCancel(data); + } catch (error: any) { + setLoading(false); } - setLoading(false); - handleCancel(data); }; useEffect(() => { @@ -41,16 +48,13 @@ const CronModal = ({ return ( { form .validateFields() .then((values) => { - if (typeof values.labels === "string") { - values.labels = values.labels.split(/,|,/); - } handleOk(values); }) .catch((info) => { @@ -69,9 +73,6 @@ const CronModal = ({ - - - + + + ); @@ -118,20 +122,25 @@ const CronLabelModal = ({ const [form] = Form.useForm(); const [loading, setLoading] = useState(false); - const update = async (action: string) => { + const update = async (action: 'delete' | 'post') => { form .validateFields() .then(async (values) => { - if (typeof values.labels === "string") { + if (typeof values.labels === 'string') { values.labels = values.labels.split(/,|,/); } setLoading(true); const payload = { ids, labels: values.labels }; - const { code, data } = await request.put(`${config.apiPrefix}crons/${action}`, { - data: payload, - }); + const { code, data } = await request[action]( + `${config.apiPrefix}crons/labels`, + { + data: payload, + }, + ); if (code === 200) { - message.success(action === 'addLabels' ? '添加Labels成功' : '删除Labels成功'); + message.success( + action === 'post' ? '添加Labels成功' : '删除Labels成功', + ); } else { message.error(data); } @@ -141,38 +150,32 @@ const CronLabelModal = ({ .catch((info) => { console.log('Validate Failed:', info); }); - } + }; useEffect(() => { form.resetFields(); }, [ids, visible]); const buttons = [ - , - , + , - + , ]; return ( handleCancel(false)} confirmLoading={loading} > -
+ @@ -181,4 +184,4 @@ const CronLabelModal = ({ ); }; -export { CronModal as default, CronLabelModal } +export { CronModal as default, CronLabelModal }; diff --git a/src/pages/dependence/index.tsx b/src/pages/dependence/index.tsx index 97504c28..def07eb9 100644 --- a/src/pages/dependence/index.tsx +++ b/src/pages/dependence/index.tsx @@ -90,11 +90,11 @@ const Dependence = ({ headerStyle, isPhone, socketMessage }: any) => { }, { title: '创建时间', - key: 'created', - dataIndex: 'created', + key: 'timestamp', + dataIndex: 'timestamp', align: 'center' as const, render: (text: string, record: any) => { - return {new Date(record.created).toLocaleString()}; + return {new Date(record.timestamp).toLocaleString()}; }, }, { diff --git a/src/pages/dependence/logModal.tsx b/src/pages/dependence/logModal.tsx index 81b14d90..f790722b 100644 --- a/src/pages/dependence/logModal.tsx +++ b/src/pages/dependence/logModal.tsx @@ -48,8 +48,8 @@ const DependenceLogModal = ({ request .get(`${config.apiPrefix}dependencies/${dependence.id}`) .then((data: any) => { - if (localStorage.getItem('logDependence') === dependence.id) { - const log = (data.data.log || []).join('\n') as string; + if (localStorage.getItem('logDependence') === String(dependence.id)) { + const log = (data.data.log || []).join('') as string; setValue(log); setExecuting(!log.includes('结束时间')); setIsRemoveFailed(log.includes('删除失败')); @@ -99,7 +99,7 @@ const DependenceLogModal = ({ setExecuting(false); setIsRemoveFailed(message.includes('删除失败')); } - setValue(`${value} \n ${message}`); + setValue(`${value}${message}`); }, [socketMessage]); useEffect(() => { diff --git a/src/pages/dependence/modal.tsx b/src/pages/dependence/modal.tsx index 6c46acbc..c65f9cf6 100644 --- a/src/pages/dependence/modal.tsx +++ b/src/pages/dependence/modal.tsx @@ -26,7 +26,7 @@ const DependenceModal = ({ const handleOk = async (values: any) => { setLoading(true); - const { name, split, type } = values; + const { name, split, type, remark } = values; const method = dependence ? 'put' : 'post'; let payload; if (!dependence) { @@ -36,10 +36,11 @@ const DependenceModal = ({ return { name: x, type, + remark, }; }); } else { - payload = [{ name, type }]; + payload = [{ name, type, remark }]; } } else { payload = { ...values, id: dependence.id }; diff --git a/src/pages/script/index.tsx b/src/pages/script/index.tsx index 35a707f7..76c91edd 100644 --- a/src/pages/script/index.tsx +++ b/src/pages/script/index.tsx @@ -125,7 +125,6 @@ const Script = ({ headerStyle, isPhone, theme, socketMessage }: any) => { setExpandedKeys([p]); onTreeSelect([vkey], obj); } - history.push('/script'); }; const onSelect = (value: any, node: any) => { diff --git a/src/pages/setting/checkUpdate.tsx b/src/pages/setting/checkUpdate.tsx index 4eb6a928..60059fe2 100644 --- a/src/pages/setting/checkUpdate.tsx +++ b/src/pages/setting/checkUpdate.tsx @@ -139,7 +139,7 @@ const CheckUpdate = ({ socketMessage }: any) => { return; } - const newMessage = `${value}\n${_message}`; + const newMessage = `${value}${_message}`; modalRef.current.update({ content: (
diff --git a/src/utils/config.ts b/src/utils/config.ts index 11c72dad..0b21d04f 100644 --- a/src/utils/config.ts +++ b/src/utils/config.ts @@ -70,6 +70,8 @@ export default { configs: '配置文件', scripts: '脚本管理', logs: '任务日志', + dependencies: '依赖管理', + system: '系统信息', }, notificationModes: [ { value: 'gotify', label: 'Gotify' },