mirror of
https://github.com/suyiiyii/nonebot-bison.git
synced 2025-06-04 02:26:11 +08:00
✨ (admin) 添加验证Cookie有效性
This commit is contained in:
parent
a78bb73281
commit
6474504c30
@ -1,6 +1,8 @@
|
|||||||
import React, { useState } from 'react';
|
import React, { useState } from 'react';
|
||||||
import { Form, Input, Modal } from '@arco-design/web-react';
|
import { Form, Input, Modal } from '@arco-design/web-react';
|
||||||
import { useNewCookieMutation } from './cookieConfigSlice';
|
import { useNewCookieMutation } from './cookieConfigSlice';
|
||||||
|
import { useAppDispatch } from '../../app/hooks';
|
||||||
|
import validateCookie from './cookieValidateReq';
|
||||||
|
|
||||||
interface CookieAddModalProps {
|
interface CookieAddModalProps {
|
||||||
visible: boolean;
|
visible: boolean;
|
||||||
@ -12,10 +14,11 @@ function CookieAddModal({ visible, setVisible, siteName }: CookieAddModalProps)
|
|||||||
const FormItem = Form.Item;
|
const FormItem = Form.Item;
|
||||||
const [content, setContent] = useState<string>('');
|
const [content, setContent] = useState<string>('');
|
||||||
const [confirmLoading, setConfirmLoading] = useState(false);
|
const [confirmLoading, setConfirmLoading] = useState(false);
|
||||||
const [newCoookie] = useNewCookieMutation();
|
const [newCookie] = useNewCookieMutation();
|
||||||
|
const dispatch = useAppDispatch();
|
||||||
|
|
||||||
const onSubmit = () => {
|
const onSubmit = () => {
|
||||||
const postPromise: ReturnType<typeof newCoookie> = newCoookie({ siteName, content });
|
const postPromise: ReturnType<typeof newCookie> = newCookie({ siteName, content });
|
||||||
setConfirmLoading(true);
|
setConfirmLoading(true);
|
||||||
postPromise.then(() => {
|
postPromise.then(() => {
|
||||||
setConfirmLoading(false);
|
setConfirmLoading(false);
|
||||||
@ -35,12 +38,33 @@ function CookieAddModal({ visible, setVisible, siteName }: CookieAddModalProps)
|
|||||||
>
|
>
|
||||||
|
|
||||||
<Form autoComplete="off">
|
<Form autoComplete="off">
|
||||||
<FormItem label="Site Name" required>
|
<FormItem label="站点" required>
|
||||||
<Input placeholder="Please enter site name" value={siteName} disabled />
|
<Input placeholder="Please enter site name" value={siteName} disabled />
|
||||||
</FormItem>
|
</FormItem>
|
||||||
<FormItem label="Content" required>
|
<FormItem
|
||||||
|
label="Cookie"
|
||||||
|
required
|
||||||
|
field="content"
|
||||||
|
hasFeedback
|
||||||
|
rules={[
|
||||||
|
{
|
||||||
|
validator: (value, callback) => new Promise<void>((resolve) => {
|
||||||
|
dispatch(validateCookie(siteName, value))
|
||||||
|
.then((res) => {
|
||||||
|
if (res) {
|
||||||
|
callback();
|
||||||
|
} else {
|
||||||
|
callback('Cookie 格式错误');
|
||||||
|
}
|
||||||
|
resolve();
|
||||||
|
});
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
]}
|
||||||
|
|
||||||
|
>
|
||||||
<Input.TextArea
|
<Input.TextArea
|
||||||
placeholder="Please enter content"
|
placeholder="请输入 Cookie"
|
||||||
value={content}
|
value={content}
|
||||||
onChange={setContent}
|
onChange={setContent}
|
||||||
/>
|
/>
|
||||||
|
@ -40,7 +40,7 @@ export const cookieTargetApi = createApi({
|
|||||||
baseQuery: baseQueryWithAuth,
|
baseQuery: baseQueryWithAuth,
|
||||||
tagTypes: ['CookieTarget'],
|
tagTypes: ['CookieTarget'],
|
||||||
endpoints: (builder) => ({
|
endpoints: (builder) => ({
|
||||||
getCookieTargets: builder.query<CookieTarget[], {cookieId: number }>({
|
getCookieTargets: builder.query<CookieTarget[], { cookieId: number }>({
|
||||||
query: ({ cookieId }) => `/cookie_target?cookie_id=${cookieId}`,
|
query: ({ cookieId }) => `/cookie_target?cookie_id=${cookieId}`,
|
||||||
providesTags: ['CookieTarget'],
|
providesTags: ['CookieTarget'],
|
||||||
}),
|
}),
|
||||||
|
@ -0,0 +1,20 @@
|
|||||||
|
import { AppThunk } from '../../app/store';
|
||||||
|
import { baseUrl } from '../../utils/urls';
|
||||||
|
|
||||||
|
// eslint-disable-next-line
|
||||||
|
export const validCookie =
|
||||||
|
(siteName: string, content: string): AppThunk<Promise<string>> => async (_, getState) => {
|
||||||
|
const url = `${baseUrl}cookie/validate?site_name=${siteName}&content=${content}`;
|
||||||
|
const state = getState();
|
||||||
|
const authToken = state.auth.token;
|
||||||
|
const res = await fetch(url, {
|
||||||
|
headers: {
|
||||||
|
Authorization: `Bearer ${authToken}`,
|
||||||
|
},
|
||||||
|
method: 'POST',
|
||||||
|
});
|
||||||
|
const resObj = await res.json();
|
||||||
|
return resObj.ok;
|
||||||
|
};
|
||||||
|
|
||||||
|
export default validCookie;
|
@ -33,8 +33,8 @@ export interface PlatformConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export interface SiteConfig {
|
export interface SiteConfig {
|
||||||
name: string
|
name: string;
|
||||||
enable_cookie: string
|
enable_cookie: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface SubscribeConfig {
|
export interface SubscribeConfig {
|
||||||
@ -106,12 +106,12 @@ export interface CookieTarget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export interface NewCookieParam {
|
export interface NewCookieParam {
|
||||||
siteName: string
|
siteName: string;
|
||||||
content: string
|
content: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface DelCookieParam {
|
export interface DelCookieParam {
|
||||||
cookieId: string
|
cookieId: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface NewCookieTargetParam {
|
export interface NewCookieTargetParam {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user