import React, { useState, useEffect } from 'react';
import {
Button,
InputNumber,
Form,
Radio,
Tabs,
Table,
Tooltip,
Space,
Tag,
Modal,
message,
Typography,
} from 'antd';
import config from '@/utils/config';
import { PageContainer } from '@ant-design/pro-layout';
import { request } from '@/utils/http';
import {
enable as enableDarkMode,
disable as disableDarkMode,
auto as followSystemColorScheme,
setFetchMethod,
} from 'darkreader';
import AppModal from './appModal';
import {
EditOutlined,
DeleteOutlined,
ReloadOutlined,
} from '@ant-design/icons';
import SecuritySettings from './security';
import LoginLog from './loginLog';
import NotificationSetting from './notification';
import CheckUpdate from './checkUpdate';
import debounce from 'lodash/debounce';
const { Text } = Typography;
const optionsWithDisabled = [
{ label: '亮色', value: 'light' },
{ label: '暗色', value: 'dark' },
{ label: '跟随系统', value: 'auto' },
];
const Setting = ({
headerStyle,
isPhone,
user,
reloadUser,
reloadTheme,
ws,
}: any) => {
const columns = [
{
title: '名称',
dataIndex: 'name',
key: 'name',
align: 'center' as const,
},
{
title: 'Client ID',
dataIndex: 'client_id',
key: 'client_id',
align: 'center' as const,
render: (text: string, record: any) => {
return {record.client_id};
},
},
{
title: 'Client Secret',
dataIndex: 'client_secret',
key: 'client_secret',
align: 'center' as const,
render: (text: string, record: any) => {
return *******;
},
},
{
title: '权限',
dataIndex: 'scopes',
key: 'scopes',
align: 'center' as const,
render: (text: string, record: any) => {
return record.scopes.map((scope: any) => {
return {(config.scopesMap as any)[scope]};
});
},
},
{
title: '操作',
key: 'action',
align: 'center' as const,
render: (text: string, record: any, index: number) => {
const isPc = !isPhone;
return (
editApp(record, index)}>
resetSecret(record, index)}>
deleteApp(record, index)}>
);
},
},
];
const [loading, setLoading] = useState(true);
const defaultDarken = localStorage.getItem('qinglong_dark_theme') || 'auto';
const [theme, setTheme] = useState(defaultDarken);
const [dataSource, setDataSource] = useState([]);
const [isModalVisible, setIsModalVisible] = useState(false);
const [editedApp, setEditedApp] = useState();
const [tabActiveKey, setTabActiveKey] = useState('security');
const [loginLogData, setLoginLogData] = useState([]);
const [notificationInfo, setNotificationInfo] = useState();
const themeChange = (e: any) => {
setTheme(e.target.value);
localStorage.setItem('qinglong_dark_theme', e.target.value);
};
const getApps = () => {
setLoading(true);
request
.get(`${config.apiPrefix}apps`)
.then((data: any) => {
setDataSource(data.data);
})
.finally(() => setLoading(false));
};
const addApp = () => {
setIsModalVisible(true);
};
const editApp = (record: any, index: number) => {
setEditedApp(record);
setIsModalVisible(true);
};
const deleteApp = (record: any, index: number) => {
Modal.confirm({
title: '确认删除',
content: (
<>
确认删除应用{' '}
{record.name}
{' '}
吗
>
),
onOk() {
request
.delete(`${config.apiPrefix}apps`, { data: [record._id] })
.then((data: any) => {
if (data.code === 200) {
message.success('删除成功');
const result = [...dataSource];
result.splice(index, 1);
setDataSource(result);
} else {
message.error(data);
}
});
},
onCancel() {
console.log('Cancel');
},
});
};
const resetSecret = (record: any, index: number) => {
Modal.confirm({
title: '确认重置',
content: (
<>
确认重置应用{' '}
{record.name}
{' '}
的Secret吗
重置Secret会让当前应用所有token失效
>
),
onOk() {
request
.put(`${config.apiPrefix}apps/${record._id}/reset-secret`)
.then((data: any) => {
if (data.code === 200) {
message.success('重置成功');
handleApp(data.data);
} else {
message.error(data);
}
});
},
onCancel() {
console.log('Cancel');
},
});
};
const handleCancel = (app?: any) => {
setIsModalVisible(false);
if (app) {
handleApp(app);
}
};
const handleApp = (app: any) => {
const index = dataSource.findIndex((x) => x._id === app._id);
const result = [...dataSource];
if (index === -1) {
result.push(app);
} else {
result.splice(index, 1, {
...app,
});
}
setDataSource(result);
};
const getLoginLog = () => {
request
.get(`${config.apiPrefix}user/login-log`)
.then((data: any) => {
setLoginLogData(data.data);
})
.catch((error: any) => {
console.log(error);
});
};
const tabChange = (activeKey: string) => {
setTabActiveKey(activeKey);
if (activeKey === 'app') {
getApps();
} else if (activeKey === 'login') {
getLoginLog();
} else if (activeKey === 'notification') {
getNotification();
}
};
const getNotification = () => {
request
.get(`${config.apiPrefix}user/notification`)
.then((data: any) => {
setNotificationInfo(data.data);
})
.catch((error: any) => {
console.log(error);
});
};
const updateRemoveLogFrequency = (value: number | string | null) => {
const frequency = parseInt((value || '0') as string, 10);
request
.put(`${config.apiPrefix}system/log/remove`, { data: { frequency } })
.then((data: any) => {
message.success('更新成功');
})
.catch((error: any) => {
console.log(error);
});
};
useEffect(() => {
setFetchMethod(window.fetch);
if (theme === 'dark') {
enableDarkMode({});
} else if (theme === 'light') {
disableDarkMode();
} else {
followSystemColorScheme({});
}
reloadTheme(theme);
}, [theme]);
return (
addApp()}>
添加应用
,
]
: []
}
>
);
};
export default Setting;