增加环境变量批量导入导出功能

This commit is contained in:
langren1353 2022-03-08 21:32:00 +08:00
parent 6f93219c44
commit 75012189c3

View File

@ -9,11 +9,15 @@ import {
Typography, Typography,
Tooltip, Tooltip,
Input, Input,
Form,
notification,
} from 'antd'; } from 'antd';
import { import {
EditOutlined, EditOutlined,
DeleteOutlined, DeleteOutlined,
SyncOutlined, SyncOutlined,
ClockCircleOutlined,
CloseCircleOutlined,
CheckCircleOutlined, CheckCircleOutlined,
StopOutlined, StopOutlined,
} from '@ant-design/icons'; } from '@ant-design/icons';
@ -26,9 +30,10 @@ import { DndProvider, useDrag, useDrop } from 'react-dnd';
import { HTML5Backend } from 'react-dnd-html5-backend'; import { HTML5Backend } from 'react-dnd-html5-backend';
import './index.less'; import './index.less';
import { getTableScroll } from '@/utils/index'; import { getTableScroll } from '@/utils/index';
import { doc } from 'prettier';
const { Text } = Typography; const { Text } = Typography;
const { Search } = Input; const { Search, TextArea } = Input;
enum Status { enum Status {
'已启用', '已启用',
@ -462,6 +467,188 @@ const Env = ({ headerStyle, isPhone, theme }: any) => {
}); });
}; };
const [importForm] = Form.useForm();
const operateImport = () => {
const importList: Array<any> = [
// {
// index: 1,
// name: '',
// icon: '',
// color: ''
// }
];
let importRes: any;
const insertOneEnv = async (values: any) => {
const { value, split, name, remarks } = values;
const method = 'post';
let payload;
if (split === '1') {
const symbol = value.includes('&') ? '&' : '\n';
payload = value.split(symbol).map((x: any) => {
return {
name: name,
value: x,
remarks: remarks,
};
});
} else {
payload = [{ value, name, remarks }];
}
const { code, data } = await request[method](`${config.apiPrefix}envs`, {
data: payload,
});
return { code, data };
};
const submitImport = async (value: string) => {
let now_at: any = '';
try {
setLoading(true);
const dataList = JSON.parse(value); // 是一个数组
for (const [index, item] of dataList.entries()) {
importList.push({
index: index,
name: item.name,
icon: <SyncOutlined />,
color: 'default',
text: '等待中',
});
}
for (const [index, item] of dataList.entries()) {
Object.assign(importList[index], {
icon: <ClockCircleOutlined />,
color: 'processing',
text: '进行中',
});
now_at = importList[index];
const { code = 0 } = await insertOneEnv(item);
if (code === 200) {
Object.assign(importList[index], {
icon: <CheckCircleOutlined />,
color: 'success',
text: '成功',
});
} else {
Object.assign(importList[index], {
icon: <CloseCircleOutlined />,
color: 'error',
text: '失败',
});
}
}
importRes = importList.map((one) => {
return (
<div>
${one.index + 1} - ${one.name}{' '}
<Tag icon={one.icon} color={one.color}>
</Tag>
</div>
);
});
const count_success = importList.filter(
(one) => one.color === 'success',
).length;
const count_failed = importList.filter(
(one) => one.color === 'error',
).length;
notification.success({
message: '导入完成',
description: `成功导入${count_success}个,失败${count_failed}`,
});
return true;
} catch (e) {
if (e.message.includes('JSON')) {
message.error('数据格式有问题请检查并更正json格式');
} else {
message.error(
`导入第 ${now_at.index + 1} 个数据 ${
now_at.name
} `,
);
console.log(e);
}
} finally {
setLoading(false);
}
return false;
};
Modal.confirm({
title: `环境变量配置数据-导入-请手动粘贴`,
okText: '导入',
content: (
<div>
<Form form={importForm} layout="vertical" name="form_in_modal">
<Form.Item name="envs" label="环境变量">
<TextArea
placeholder="请粘贴导入的环境变量"
style={{ height: 120, marginTop: '20px' }}
autoFocus
></TextArea>
</Form.Item>
</Form>
</div>
),
async onOk() {
const values = await importForm.validateFields();
const { envs }: { envs: string } = values;
const isOk = await submitImport(envs);
if (isOk) {
setTimeout(() => {
location.reload();
}, 1000);
return Promise.resolve();
}
return Promise.reject('');
},
onCancel() {
importForm.resetFields();
},
});
};
const operateExport = () => {
const result = [...value];
const selectItems = result
.filter((one) => selectedRowIds.includes(one.id))
.map((one) => {
return {
name: one.name,
value: one.value,
split: one.split,
remarks: one.remarks,
};
});
const resJson = JSON.stringify(selectItems, function (key: any, val: any) {
console.log(val);
if (val) return val;
});
Modal.confirm({
title: `环境变量配置数据-导出-请手动复制`,
content: (
<TextArea
style={{ height: 120, marginTop: '20px' }}
value={resJson}
autoFocus
></TextArea>
),
onOk() {
console.log('Ok');
},
onCancel() {
console.log('Cancel');
},
});
};
const modifyName = () => { const modifyName = () => {
setIsEditNameModalVisible(true); setIsEditNameModalVisible(true);
}; };
@ -500,11 +687,26 @@ const Env = ({ headerStyle, isPhone, theme }: any) => {
style: headerStyle, style: headerStyle,
}} }}
> >
{selectedRowIds.length > 0 && ( <div style={{ marginBottom: 16, display: 'flex' }}>
<div style={{ marginBottom: 16 }}>
<Button <Button
type="primary" type="primary"
style={{ marginBottom: 5 }} style={{ marginBottom: 5 }}
onClick={operateImport}
>
</Button>
{selectedRowIds.length > 0 && (
<div>
<Button
type="primary"
style={{ marginBottom: 5, marginLeft: 8 }}
onClick={operateExport}
>
</Button>
<Button
type="primary"
style={{ marginBottom: 5, marginLeft: 8 }}
onClick={modifyName} onClick={modifyName}
> >
@ -536,6 +738,7 @@ const Env = ({ headerStyle, isPhone, theme }: any) => {
</span> </span>
</div> </div>
)} )}
</div>
<DndProvider backend={HTML5Backend}> <DndProvider backend={HTML5Backend}>
<Table <Table
columns={columns} columns={columns}