From f5b322649dd31bef46b34379f4c1f7c04407e023 Mon Sep 17 00:00:00 2001 From: hanhh <18330117883@163.com> Date: Fri, 15 Oct 2021 10:53:27 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=AE=BE=E7=BD=AE=E5=88=A0?= =?UTF-8?q?=E9=99=A4=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- back/api/user.ts | 15 ++++++++++++++ back/services/user.ts | 26 +++++++++++++++++++----- src/pages/setting/index.tsx | 40 +++++++++++++++++++++++++++---------- 3 files changed, 66 insertions(+), 15 deletions(-) diff --git a/back/api/user.ts b/back/api/user.ts index 4e3bfcfb..6474a181 100644 --- a/back/api/user.ts +++ b/back/api/user.ts @@ -273,6 +273,21 @@ export default (app: Router) => { }, ); + route.get( + '/system/log/remove', + async (req: Request, res: Response, next: NextFunction) => { + const logger: Logger = Container.get('logger'); + try { + const userService = Container.get(UserService); + const data = await userService.getLogRemoveFrequency(); + res.send({ code: 200, data }); + } catch (e) { + logger.error('🔥 error: %o', e); + return next(e); + } + }, + ); + route.put( '/system/log/remove', celebrate({ diff --git a/back/services/user.ts b/back/services/user.ts index 70c47a9e..b399670e 100644 --- a/back/services/user.ts +++ b/back/services/user.ts @@ -328,10 +328,24 @@ export default class UserService { }); } - private async updateNotificationDb(payload: AuthInfo): Promise { + public async getLogRemoveFrequency() { + return new Promise((resolve) => { + this.authDb + .find({ type: AuthDataType.removeLogFrequency }) + .exec((err, docs) => { + if (err || docs.length === 0) { + resolve({}); + } else { + resolve(docs[0].info); + } + }); + }); + } + + private async updateAuthDb(payload: AuthInfo): Promise { return new Promise((resolve) => { this.authDb.update( - { type: AuthDataType.notification }, + { type: payload.type }, { ...payload }, { upsert: true, returnUpdatedDocs: true }, (err, num, doc: any) => { @@ -353,7 +367,7 @@ export default class UserService { `【蛟龙】测试通知 https://t.me/jiao_long`, ); if (isSuccess) { - const result = await this.updateNotificationDb({ + const result = await this.updateAuthDb({ type: AuthDataType.notification, info: { ...notificationInfo }, }); @@ -364,7 +378,7 @@ export default class UserService { } public async updateLogRemoveFrequency(frequency: number) { - const result = await this.updateNotificationDb({ + const result = await this.updateAuthDb({ type: AuthDataType.removeLogFrequency, info: { frequency }, }); @@ -375,7 +389,9 @@ export default class UserService { schedule: `5 23 */${frequency} * *`, }; await this.scheduleService.cancelSchedule(cron); - await this.scheduleService.generateSchedule(cron); + if (frequency > 0) { + await this.scheduleService.generateSchedule(cron); + } return { code: 200, data: { ...cron } }; } diff --git a/src/pages/setting/index.tsx b/src/pages/setting/index.tsx index a2ddfdb0..d50f3e99 100644 --- a/src/pages/setting/index.tsx +++ b/src/pages/setting/index.tsx @@ -32,7 +32,7 @@ import SecuritySettings from './security'; import LoginLog from './loginLog'; import NotificationSetting from './notification'; import CheckUpdate from './checkUpdate'; -import debounce from 'lodash/debounce'; +import { useForm } from 'antd/lib/form/Form'; const { Text } = Typography; const optionsWithDisabled = [ @@ -123,6 +123,8 @@ const Setting = ({ const [tabActiveKey, setTabActiveKey] = useState('security'); const [loginLogData, setLoginLogData] = useState([]); const [notificationInfo, setNotificationInfo] = useState(); + const [logRemoveFrequency, setLogRemoveFrequency] = useState(); + const [form] = useForm(); const themeChange = (e: any) => { setTheme(e.target.value); @@ -251,6 +253,8 @@ const Setting = ({ getLoginLog(); } else if (activeKey === 'notification') { getNotification(); + } else if (activeKey === 'other') { + getLogRemoveFrequency(); } }; @@ -265,18 +269,34 @@ const Setting = ({ }); }; - const updateRemoveLogFrequency = (value: number | string | null) => { - const frequency = parseInt((value || '0') as string, 10); + const getLogRemoveFrequency = () => { request - .put(`${config.apiPrefix}system/log/remove`, { data: { frequency } }) + .get(`${config.apiPrefix}system/log/remove`) .then((data: any) => { - message.success('更新成功'); + console.log(data.data.frequency); + setLogRemoveFrequency(data.data.frequency); + form.setFieldsValue({ frequency: data.data.frequency }); }) .catch((error: any) => { console.log(error); }); }; + const updateRemoveLogFrequency = () => { + setTimeout(() => { + request + .put(`${config.apiPrefix}system/log/remove`, { + data: { frequency: logRemoveFrequency }, + }) + .then((data: any) => { + message.success('更新成功'); + }) + .catch((error: any) => { + console.log(error); + }); + }); + }; + useEffect(() => { setFetchMethod(window.fetch); if (theme === 'dark') { @@ -332,8 +352,8 @@ const Setting = ({ - -
+ + setLogRemoveFrequency(value)} />