修复设置删除日志

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( route.put(
'/system/log/remove', '/system/log/remove',
celebrate({ 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) => { return new Promise((resolve) => {
this.authDb.update( this.authDb.update(
{ type: AuthDataType.notification }, { type: payload.type },
{ ...payload }, { ...payload },
{ upsert: true, returnUpdatedDocs: true }, { upsert: true, returnUpdatedDocs: true },
(err, num, doc: any) => { (err, num, doc: any) => {
@ -353,7 +367,7 @@ export default class UserService {
`【蛟龙】测试通知 https://t.me/jiao_long`, `【蛟龙】测试通知 https://t.me/jiao_long`,
); );
if (isSuccess) { if (isSuccess) {
const result = await this.updateNotificationDb({ const result = await this.updateAuthDb({
type: AuthDataType.notification, type: AuthDataType.notification,
info: { ...notificationInfo }, info: { ...notificationInfo },
}); });
@ -364,7 +378,7 @@ export default class UserService {
} }
public async updateLogRemoveFrequency(frequency: number) { public async updateLogRemoveFrequency(frequency: number) {
const result = await this.updateNotificationDb({ const result = await this.updateAuthDb({
type: AuthDataType.removeLogFrequency, type: AuthDataType.removeLogFrequency,
info: { frequency }, info: { frequency },
}); });
@ -375,7 +389,9 @@ export default class UserService {
schedule: `5 23 */${frequency} * *`, schedule: `5 23 */${frequency} * *`,
}; };
await this.scheduleService.cancelSchedule(cron); await this.scheduleService.cancelSchedule(cron);
await this.scheduleService.generateSchedule(cron); if (frequency > 0) {
await this.scheduleService.generateSchedule(cron);
}
return { code: 200, data: { ...cron } }; return { code: 200, data: { ...cron } };
} }

View File

@ -32,7 +32,7 @@ import SecuritySettings from './security';
import LoginLog from './loginLog'; import LoginLog from './loginLog';
import NotificationSetting from './notification'; import NotificationSetting from './notification';
import CheckUpdate from './checkUpdate'; import CheckUpdate from './checkUpdate';
import debounce from 'lodash/debounce'; import { useForm } from 'antd/lib/form/Form';
const { Text } = Typography; const { Text } = Typography;
const optionsWithDisabled = [ const optionsWithDisabled = [
@ -123,6 +123,8 @@ const Setting = ({
const [tabActiveKey, setTabActiveKey] = useState('security'); const [tabActiveKey, setTabActiveKey] = useState('security');
const [loginLogData, setLoginLogData] = useState<any[]>([]); const [loginLogData, setLoginLogData] = useState<any[]>([]);
const [notificationInfo, setNotificationInfo] = useState<any>(); const [notificationInfo, setNotificationInfo] = useState<any>();
const [logRemoveFrequency, setLogRemoveFrequency] = useState<number>();
const [form] = useForm();
const themeChange = (e: any) => { const themeChange = (e: any) => {
setTheme(e.target.value); setTheme(e.target.value);
@ -251,6 +253,8 @@ const Setting = ({
getLoginLog(); getLoginLog();
} else if (activeKey === 'notification') { } else if (activeKey === 'notification') {
getNotification(); getNotification();
} else if (activeKey === 'other') {
getLogRemoveFrequency();
} }
}; };
@ -265,18 +269,34 @@ const Setting = ({
}); });
}; };
const updateRemoveLogFrequency = (value: number | string | null) => { const getLogRemoveFrequency = () => {
const frequency = parseInt((value || '0') as string, 10);
request request
.put(`${config.apiPrefix}system/log/remove`, { data: { frequency } }) .get(`${config.apiPrefix}system/log/remove`)
.then((data: any) => { .then((data: any) => {
message.success('更新成功'); console.log(data.data.frequency);
setLogRemoveFrequency(data.data.frequency);
form.setFieldsValue({ frequency: data.data.frequency });
}) })
.catch((error: any) => { .catch((error: any) => {
console.log(error); 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(() => { useEffect(() => {
setFetchMethod(window.fetch); setFetchMethod(window.fetch);
if (theme === 'dark') { if (theme === 'dark') {
@ -332,8 +352,8 @@ const Setting = ({
<Tabs.TabPane tab="登陆日志" key="login"> <Tabs.TabPane tab="登陆日志" key="login">
<LoginLog data={loginLogData} /> <LoginLog data={loginLogData} />
</Tabs.TabPane> </Tabs.TabPane>
<Tabs.TabPane tab="其他设置" key="theme"> <Tabs.TabPane tab="其他设置" key="other">
<Form layout="vertical"> <Form layout="vertical" form={form}>
<Form.Item label="主题设置" name="theme" initialValue={theme}> <Form.Item label="主题设置" name="theme" initialValue={theme}>
<Radio.Group <Radio.Group
options={optionsWithDisabled} options={optionsWithDisabled}
@ -346,15 +366,15 @@ const Setting = ({
<Form.Item <Form.Item
label="日志删除频率" label="日志删除频率"
name="frequency" name="frequency"
initialValue={0}
tooltip="每x天自动删除x天以前的日志" tooltip="每x天自动删除x天以前的日志"
> >
<InputNumber <InputNumber
defaultValue={0}
addonBefore="每" addonBefore="每"
addonAfter="天" addonAfter="天"
style={{ width: 150 }} style={{ width: 150 }}
onChange={debounce(updateRemoveLogFrequency, 500)} min={0}
onBlur={updateRemoveLogFrequency}
onChange={(value) => setLogRemoveFrequency(value)}
/> />
</Form.Item> </Form.Item>
<Form.Item label="检查更新" name="update"> <Form.Item label="检查更新" name="update">