import React, { useState, useEffect } from 'react'; import { Button, InputNumber, Form, Radio, Tabs, Table, Tooltip, Space, Tag, Modal, message, Typography, Input, } from 'antd'; import config from '@/utils/config'; import { PageContainer } from '@ant-design/pro-layout'; import { request } from '@/utils/http'; 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 Other from './other'; import About from './about'; import { useOutletContext } from '@umijs/max'; import { SharedContext } from '@/layouts'; import './index.less'; const { Text } = Typography; const Setting = () => { const { headerStyle, isPhone, user, reloadUser, reloadTheme, socketMessage, systemInfo, } = useOutletContext(); 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, width: '40%', 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 [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 getApps = () => { setLoading(true); request .get(`${config.apiPrefix}apps`) .then(({ code, data }) => { if (code === 200) { setDataSource(data); } }) .finally(() => setLoading(false)); }; const addApp = () => { setEditedApp(null); 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(({ code, data }) => { if (code === 200) { message.success('删除成功'); const result = [...dataSource]; result.splice(index, 1); setDataSource(result); } }); }, 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(({ code, data }) => { if (code === 200) { message.success('重置成功'); handleApp(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(({ code, data }) => { if (code === 200) { setLoginLogData(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(({ code, data }) => { if (code === 200) { setNotificationInfo(data); } }) .catch((error: any) => { console.log(error); }); }; return ( addApp()}> 新建应用 , ] : [] } > , }, { key: 'app', label: '应用设置', children: ( ), }, { key: 'notification', label: '通知设置', children: , }, { key: 'login', label: '登录日志', children: , }, { key: 'other', label: '其他设置', children: ( ), }, { key: 'about', label: '关于', children: , }, ]} > ); }; export default Setting;