qinglong/src/pages/cookie/modal.tsx
whyour 64a8acde92 添加Cookie增加每条验证
1. codemirror主题改为默认
2. docker打包触发条件改为tag
2021-03-24 17:38:45 +08:00

96 lines
2.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import React, { useEffect, useState } from 'react';
import { Modal, notification, Input, Form } from 'antd';
import { request } from '@/utils/http';
import config from '@/utils/config';
const CookieModal = ({
cookie,
handleCancel,
visible,
}: {
cookie?: string;
visible: boolean;
handleCancel: (needUpdate?: boolean) => void;
}) => {
const [form] = Form.useForm();
const handleOk = async (values: any) => {
const cookies = values.cookie
.split('\n')
.map((x: any) => x.trim().replace(/\s/g, ''));
let flag = false;
for (const coo of cookies) {
if (!/pt_key=\S*;\s*pt_pin=\S*;\s*/.test(coo)) {
notification.error({ message: `${coo}格式有误` });
flag = true;
break;
}
}
if (flag) {
return;
}
const method = cookie ? 'put' : 'post';
const payload = cookie
? { cookie: cookies[0], oldCookie: cookie }
: { cookies };
const { code, data } = await request[method](`${config.apiPrefix}cookie`, {
data: payload,
});
if (code === 200) {
notification.success({
message: cookie ? '更新Cookie成功' : '添加Cookie成功',
});
} else {
notification.error({
message: data,
});
}
handleCancel(true);
};
useEffect(() => {
if (cookie) {
form.setFieldsValue({ cookie });
}
}, [cookie]);
return (
<Modal
title={cookie ? '编辑Cookie' : '新建Cookie'}
visible={visible}
onOk={() => {
form
.validateFields()
.then((values) => {
handleOk(values);
})
.catch((info) => {
console.log('Validate Failed:', info);
});
}}
onCancel={() => handleCancel()}
>
<Form form={form} layout="vertical" name="form_in_modal">
<Form.Item
name="cookie"
rules={[
{ required: true, message: '请输入Cookie' },
{
pattern: /pt_key=\S*;\s*pt_pin=\S*;\s*/,
message: 'Cookie格式错误注意分号(pt_key=***;pt_pin=***;)',
},
]}
>
<Input.TextArea
rows={4}
autoSize={true}
placeholder="请输入cookie多个cookie换行输入"
/>
</Form.Item>
</Form>
</Modal>
);
};
export default CookieModal;