Skip to content

Commit

Permalink
fix: use crypto/rand
Browse files Browse the repository at this point in the history
  • Loading branch information
Mereithhh committed Dec 8, 2024
1 parent dc6543a commit e00c5b1
Show file tree
Hide file tree
Showing 3 changed files with 280 additions and 7 deletions.
262 changes: 262 additions & 0 deletions ui/src/pages/admin/tabs/Search.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,262 @@
import React, { useState } from 'react';
import {
Table,
Button,
Modal,
Form,
Input,
Space,
message,
Upload,
Image,
} from 'antd';
import { DragOutlined, DeleteOutlined, EditOutlined, PlusOutlined } from '@ant-design/icons';
import { DndContext } from '@dnd-kit/core';
import { SortableContext, useSortable, verticalListSortingStrategy } from '@dnd-kit/sortable';
import { CSS } from '@dnd-kit/utilities';

interface SearchEngine {
id: string;
name: string;
baseUrl: string;
queryParam: string;
logo: string;
order: number;
}

const defaultEngines: SearchEngine[] = [
{
id: '1',
name: '百度',
baseUrl: 'https://www.baidu.com/s',
queryParam: 'wd',
logo: 'https://www.baidu.com/favicon.ico',
order: 1,
},
{
id: '2',
name: 'Google',
baseUrl: 'https://www.google.com/search',
queryParam: 'q',
logo: 'https://www.google.com/favicon.ico',
order: 2,
},
{
id: '3',
name: 'Bing',
baseUrl: 'https://www.bing.com/search',
queryParam: 'q',
logo: 'https://www.bing.com/favicon.ico',
order: 3,
},
];

const DraggableRow = ({ children, ...props }: any) => {
const {
attributes,
listeners,
setNodeRef,
transform,
transition,
isDragging,
} = useSortable({
id: props['data-row-key'],
});

const style = {
...props.style,
transform: CSS.Transform.toString(transform),
transition,
cursor: 'move',
...(isDragging ? { zIndex: 9999 } : {}),
};

return (
<tr {...props} ref={setNodeRef} style={style} {...attributes} {...listeners}>
{children}
</tr>
);
};

const SearchEngineManager: React.FC = () => {
const [engines, setEngines] = useState<SearchEngine[]>(defaultEngines);
const [isModalVisible, setIsModalVisible] = useState(false);
const [editingEngine, setEditingEngine] = useState<SearchEngine | null>(null);
const [form] = Form.useForm();

const columns = [
{
title: '排序',
dataIndex: 'sort',
width: 30,
render: () => <DragOutlined style={{ cursor: 'move', color: '#999' }} />,
},
{
title: 'Logo',
dataIndex: 'logo',
width: 80,
render: (logo: string) => (
<Image src={logo} alt="logo" width={24} height={24} />
),
},
{
title: '名称',
dataIndex: 'name',
},
{
title: '基础URL',
dataIndex: 'baseUrl',
},
{
title: '查询参��',
dataIndex: 'queryParam',
},
{
title: '操作',
width: 120,
render: (_: any, record: SearchEngine) => (
<Space>
<Button
type="text"
icon={<EditOutlined />}
onClick={() => handleEdit(record)}
/>
<Button
type="text"
danger
icon={<DeleteOutlined />}
onClick={() => handleDelete(record.id)}
/>
</Space>
),
},
];

const handleEdit = (engine: SearchEngine) => {
setEditingEngine(engine);
form.setFieldsValue(engine);
setIsModalVisible(true);
};

const handleDelete = (id: string) => {
setEngines(engines.filter((engine) => engine.id !== id));
message.success('删除成功');
};

const handleAdd = () => {
setEditingEngine(null);
form.resetFields();
setIsModalVisible(true);
};

const handleModalOk = async () => {
try {
const values = await form.validateFields();
if (editingEngine) {
setEngines(
engines.map((engine) =>
engine.id === editingEngine.id ? { ...engine, ...values } : engine
)
);
message.success('修改成功');
} else {
const newEngine = {
...values,
id: Date.now().toString(),
order: engines.length + 1,
};
setEngines([...engines, newEngine]);
message.success('添加成功');
}
setIsModalVisible(false);
} catch (error) {
console.error('Validate Failed:', error);
}
};

const onDragEnd = ({ active, over }: any) => {
if (active.id !== over?.id) {
const activeIndex = engines.findIndex((i) => i.id === active.id);
const overIndex = engines.findIndex((i) => i.id === over?.id);
const newItems = [...engines];
const [reorderedItem] = newItems.splice(activeIndex, 1);
newItems.splice(overIndex, 0, reorderedItem);

const reorderedItems = newItems.map((item, index) => ({
...item,
order: index + 1,
}));

setEngines(reorderedItems);
message.success('排序已更新');
}
};

return (
<div>
<div style={{ marginBottom: 16 }}>
<Button type="primary" icon={<PlusOutlined />} onClick={handleAdd}>
添加搜索引擎
</Button>
</div>

<DndContext onDragEnd={onDragEnd}>
<SortableContext
items={engines.map((i) => i.id)}
strategy={verticalListSortingStrategy}
>
<Table
columns={columns}
dataSource={engines}
rowKey="id"
components={{
body: {
row: DraggableRow,
},
}}
/>
</SortableContext>
</DndContext>

<Modal
title={editingEngine ? '编辑搜索引擎' : '添加搜索引擎'}
open={isModalVisible}
onOk={handleModalOk}
onCancel={() => setIsModalVisible(false)}
>
<Form form={form} layout="vertical">
<Form.Item
name="name"
label="名称"
rules={[{ required: true, message: '请输入搜索引擎名称' }]}
>
<Input />
</Form.Item>
<Form.Item
name="baseUrl"
label="基础URL"
rules={[{ required: true, message: '请输入基础URL' }]}
>
<Input />
</Form.Item>
<Form.Item
name="queryParam"
label="查询参数"
rules={[{ required: true, message: '请输入查询参数' }]}
>
<Input />
</Form.Item>
<Form.Item
name="logo"
label="Logo URL"
rules={[{ required: true, message: '请输入Logo URL' }]}
>
<Input />
</Form.Item>
</Form>
</Modal>
</div>
);
};

export default SearchEngineManager;
14 changes: 10 additions & 4 deletions ui/src/pages/admin/tabs/Tools.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -553,12 +553,18 @@ export const Tools: React.FC<ToolsProps> = (props) => {
</Form.Item>
<Form.Item
name="url"
rules={[{ required: true, message: "请填写网址" }]}
rules={[
{ required: true, message: "请填写网址" },
{
pattern: /^(https?:\/\/)/,
message: "网址必须以 http:// 或 https:// 开头"
}
]}
required
label="网址"
labelCol={{ span: 4 }}
>
<Input placeholder="请输入 url" />
<Input placeholder="请输入完整URL(以 http:// 或 https:// 开头)" />
</Form.Item>
<Form.Item name="logo" label="logo 网址" labelCol={{ span: 4 }}>
<Input placeholder="请输入 logo url, 为空则自动获取" />
Expand All @@ -579,7 +585,7 @@ export const Tools: React.FC<ToolsProps> = (props) => {
rules={[{ required: true, message: "请填写描述" }]}
name="desc"
required
label="描���"
label="描述"
labelCol={{ span: 4 }}
>
<Input placeholder="请输入描述" />
Expand Down Expand Up @@ -652,7 +658,7 @@ export const Tools: React.FC<ToolsProps> = (props) => {
>
<Select
options={getOptions(store?.catelogs || [])}
placeholder="请选择分类"
placeholder="请选���分类"
/>
</Form.Item>
<Form.Item name="desc" required label="描述" labelCol={{ span: 4 }}>
Expand Down
11 changes: 8 additions & 3 deletions utils/jwt.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package utils

import (
"fmt"
"math/rand"
"crypto/rand"
"encoding/hex"
"net/http"
"time"

Expand All @@ -13,7 +13,12 @@ import (
)

func RandomJWTKey() string {
return fmt.Sprintf("%d", rand.Intn(1000000000000000000))
bytes := make([]byte, 32)
if _, err := rand.Read(bytes); err != nil {
logger.LogError("生成随机密钥失败: %v", err)
return "fallback_secret_key_12345"
}
return hex.EncodeToString(bytes)
}

// JTW 密钥
Expand Down

0 comments on commit e00c5b1

Please sign in to comment.