From 189184832eb7b6ad26c07214a240ddda8e5254fe Mon Sep 17 00:00:00 2001 From: hanhh <18330117883@163.com> Date: Thu, 20 May 2021 22:04:12 +0800 Subject: [PATCH] =?UTF-8?q?session=20api=E6=94=B9=E4=B8=BA=E6=89=B9?= =?UTF-8?q?=E9=87=8F=E6=93=8D=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- back/api/cookie.ts | 28 ++++++-------- back/services/cookie.ts | 26 +++++++------ back/services/cron.ts | 75 ++++++++++++++++++++++++------------- src/pages/cookie/index.tsx | 42 +++------------------ src/pages/crontab/index.tsx | 4 +- 5 files changed, 83 insertions(+), 92 deletions(-) diff --git a/back/api/cookie.ts b/back/api/cookie.ts index 37d3f2e0..bf75c20f 100644 --- a/back/api/cookie.ts +++ b/back/api/cookie.ts @@ -62,17 +62,15 @@ export default (app: Router) => { ); route.delete( - '/cookies/:id', + '/cookies', celebrate({ - params: Joi.object({ - id: Joi.string().required(), - }), + body: Joi.array().items(Joi.string().required()), }), async (req: Request, res: Response, next: NextFunction) => { const logger: Logger = Container.get('logger'); try { const cookieService = Container.get(CookieService); - const data = await cookieService.remove(req.params.id); + const data = await cookieService.remove(req.body); return res.send({ code: 200, data }); } catch (e) { logger.error('🔥 error: %o', e); @@ -125,18 +123,16 @@ export default (app: Router) => { }, ); - route.get( - '/cookies/:id/disable', + route.put( + '/cookies/disable', celebrate({ - params: Joi.object({ - id: Joi.string().required(), - }), + body: Joi.array().items(Joi.string().required()), }), async (req: Request, res: Response, next: NextFunction) => { const logger: Logger = Container.get('logger'); try { const cookieService = Container.get(CookieService); - const data = await cookieService.disabled(req.params.id); + const data = await cookieService.disabled(req.body); return res.send({ code: 200, data }); } catch (e) { logger.error('🔥 error: %o', e); @@ -145,18 +141,16 @@ export default (app: Router) => { }, ); - route.get( - '/cookies/:id/enable', + route.put( + '/cookies/enable', celebrate({ - params: Joi.object({ - id: Joi.string().required(), - }), + body: Joi.array().items(Joi.string().required()), }), async (req: Request, res: Response, next: NextFunction) => { const logger: Logger = Container.get('logger'); try { const cookieService = Container.get(CookieService); - const data = await cookieService.enabled(req.params.id); + const data = await cookieService.enabled(req.body); return res.send({ code: 200, data }); } catch (e) { logger.error('🔥 error: %o', e); diff --git a/back/services/cookie.ts b/back/services/cookie.ts index 5ecce83f..802ec192 100644 --- a/back/services/cookie.ts +++ b/back/services/cookie.ts @@ -191,9 +191,13 @@ export default class CookieService { }); } - public async remove(_id: string) { - this.cronDb.remove({ _id }, {}); - await this.set_cookies(); + public async remove(ids: string[]) { + return new Promise((resolve: any) => { + this.cronDb.remove({ _id: { $in: ids } }, {}, async (err) => { + await this.set_cookies(); + resolve(); + }); + }); } public async move( @@ -283,29 +287,29 @@ export default class CookieService { }); } - public async disabled(_id: string) { - return new Promise((resolve) => { + public async disabled(ids: string) { + return new Promise((resolve: any) => { this.cronDb.update( - { _id }, + { _id: { $in: ids } }, { $set: { status: CookieStatus.disabled } }, {}, async (err) => { await this.set_cookies(); - resolve(true); + resolve(); }, ); }); } - public async enabled(_id: string) { - return new Promise((resolve) => { + public async enabled(ids: string) { + return new Promise((resolve: any) => { this.cronDb.update( - { _id }, + { _id: { $in: ids } }, { $set: { status: CookieStatus.noacquired } }, {}, async (err, num) => { await this.set_cookies(); - resolve(true); + resolve(); }, ); }); diff --git a/back/services/cron.ts b/back/services/cron.ts index 3b6b0ef2..b6a9fb64 100644 --- a/back/services/cron.ts +++ b/back/services/cron.ts @@ -88,8 +88,16 @@ export default class CronService { } public async remove(ids: string[]) { - this.cronDb.remove({ _id: { $in: ids } }, { multi: true }); - await this.set_crontab(true); + return new Promise((resolve: any) => { + this.cronDb.remove( + { _id: { $in: ids } }, + { multi: true }, + async (err) => { + await this.set_crontab(true); + resolve(); + }, + ); + }); } public async crontabs(searchText?: string): Promise { @@ -141,17 +149,21 @@ export default class CronService { } public async stop(ids: string[]) { - this.cronDb.find({ _id: { $in: ids } }).exec((err, docs: Crontab[]) => { - this.cronDb.update( - { _id: { $in: ids } }, - { $set: { status: CrontabStatus.idle }, $unset: { pid: true } }, - ); - const pids = docs - .map((x) => x.pid) - .filter((x) => !!x) - .join('\n'); - console.log(pids); - exec(`echo - e "${pids}" | xargs kill - 9`); + return new Promise((resolve: any) => { + this.cronDb.find({ _id: { $in: ids } }).exec((err, docs: Crontab[]) => { + this.cronDb.update( + { _id: { $in: ids } }, + { $set: { status: CrontabStatus.idle }, $unset: { pid: true } }, + ); + const pids = docs + .map((x) => x.pid) + .filter((x) => !!x) + .join('\n'); + console.log(pids); + exec(`echo - e "${pids}" | xargs kill - 9`, (err) => { + resolve(); + }); + }); }); } @@ -222,21 +234,31 @@ export default class CronService { } public async disabled(ids: string[]) { - this.cronDb.update( - { _id: { $in: ids } }, - { $set: { isDisabled: 1 } }, - { multi: true }, - ); - await this.set_crontab(true); + return new Promise((resolve: any) => { + this.cronDb.update( + { _id: { $in: ids } }, + { $set: { isDisabled: 1 } }, + { multi: true }, + async (err) => { + await this.set_crontab(true); + resolve(); + }, + ); + }); } public async enabled(ids: string[]) { - this.cronDb.update( - { _id: { $in: ids } }, - { $set: { isDisabled: 0 } }, - { multi: true }, - ); - await this.set_crontab(true); + return new Promise((resolve: any) => { + this.cronDb.update( + { _id: { $in: ids } }, + { $set: { isDisabled: 0 } }, + { multi: true }, + async (err) => { + await this.set_crontab(true); + resolve(); + }, + ); + }); } public async log(_id: string) { @@ -289,7 +311,8 @@ export default class CronService { lines.reverse().forEach((line, index) => { line = line.replace(/\t+/g, ' '); - var regex = /^((\@[a-zA-Z]+\s+)|(([^\s]+)\s+([^\s]+)\s+([^\s]+)\s+([^\s]+)\s+([^\s]+)\s+))/; + var regex = + /^((\@[a-zA-Z]+\s+)|(([^\s]+)\s+([^\s]+)\s+([^\s]+)\s+([^\s]+)\s+([^\s]+)\s+))/; var command = line.replace(regex, '').trim(); var schedule = line.replace(command, '').trim(); diff --git a/src/pages/cookie/index.tsx b/src/pages/cookie/index.tsx index 00e85337..70e7a5fc 100644 --- a/src/pages/cookie/index.tsx +++ b/src/pages/cookie/index.tsx @@ -187,7 +187,7 @@ const Config = () => { - enabledOrDisabledCron(record, index)}> + enabledOrDisabledCookie(record, index)}> {record.status === Status.已禁用 ? ( ) : ( @@ -222,36 +222,6 @@ const Config = () => { .finally(() => setLoading(false)); }; - const decodeUrl = (val: string) => { - try { - const newUrl = decodeURIComponent(val); - return newUrl; - } catch (error) { - return val; - } - }; - - // useEffect(() => { - // if (value && loading) { - // asyncUpdateStatus(); - // } - // }, [value]); - - const asyncUpdateStatus = async () => { - for (let i = 0; i < value.length; i++) { - const cookie = value[i]; - if (cookie.status === Status.已禁用) { - continue; - } - await sleep(1000); - location.pathname === '/cookie' && refreshStatus(cookie, i); - } - }; - - const sleep = (time: number) => { - return new Promise((resolve) => setTimeout(resolve, time)); - }; - const refreshStatus = (record: any, index: number) => { request .get(`${config.apiPrefix}cookies/${record._id}/refresh`) @@ -265,7 +235,7 @@ const Config = () => { }); }; - const enabledOrDisabledCron = (record: any, index: number) => { + const enabledOrDisabledCookie = (record: any, index: number) => { Modal.confirm({ title: `确认${record.status === Status.已禁用 ? '启用' : '禁用'}`, content: ( @@ -280,12 +250,12 @@ const Config = () => { ), onOk() { request - .get( - `${config.apiPrefix}cookies/${record._id}/${ + .put( + `${config.apiPrefix}cookies/${ record.status === Status.已禁用 ? 'enable' : 'disable' }`, { - data: { _id: record._id }, + data: [record._id], }, ) .then((data: any) => { @@ -340,7 +310,7 @@ const Config = () => { ), onOk() { request - .delete(`${config.apiPrefix}cookies/${record._id}`) + .delete(`${config.apiPrefix}cookies`, { data: [record._id] }) .then((data: any) => { if (data.code === 200) { notification.success({ diff --git a/src/pages/crontab/index.tsx b/src/pages/crontab/index.tsx index c881cb60..c89059b3 100644 --- a/src/pages/crontab/index.tsx +++ b/src/pages/crontab/index.tsx @@ -103,7 +103,7 @@ const Crontab = () => { align: 'center' as const, render: (text: string, record: any) => ( <> - {(!record.isDisabled || record.status !== CrontabStatus.idle) && ( + {!record.isDisabled && ( <> {record.status === CrontabStatus.idle && ( } color="default"> @@ -125,7 +125,7 @@ const Crontab = () => { )} )} - {record.isDisabled === 1 && record.status === CrontabStatus.idle && ( + {record.isDisabled === 1 && ( } color="error"> 已禁用