修复设置删除日志

This commit is contained in:
hanhh 2021-10-15 10:53:27 +08:00
parent 746650b49a
commit f5b322649d
3 changed files with 66 additions and 15 deletions

View File

@ -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({

View File

@ -328,10 +328,24 @@ export default class UserService {
});
}
private async updateNotificationDb(payload: AuthInfo): Promise<any> {
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<any> {
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 } };
}

View File

@ -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<any[]>([]);
const [notificationInfo, setNotificationInfo] = useState<any>();
const [logRemoveFrequency, setLogRemoveFrequency] = useState<number>();
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 = ({
<Tabs.TabPane tab="登陆日志" key="login">
<LoginLog data={loginLogData} />
</Tabs.TabPane>
<Tabs.TabPane tab="其他设置" key="theme">
<Form layout="vertical">
<Tabs.TabPane tab="其他设置" key="other">
<Form layout="vertical" form={form}>
<Form.Item label="主题设置" name="theme" initialValue={theme}>
<Radio.Group
options={optionsWithDisabled}
@ -346,15 +366,15 @@ const Setting = ({
<Form.Item
label="日志删除频率"
name="frequency"
initialValue={0}
tooltip="每x天自动删除x天以前的日志"
>
<InputNumber
defaultValue={0}
addonBefore="每"
addonAfter="天"
style={{ width: 150 }}
onChange={debounce(updateRemoveLogFrequency, 500)}
min={0}
onBlur={updateRemoveLogFrequency}
onChange={(value) => setLogRemoveFrequency(value)}
/>
</Form.Item>
<Form.Item label="检查更新" name="update">