Let's use AI to Earn!
TypeScript auto-publishdouyindouyin-apielectron-appelectron-reactkuaishoukwaipublishedshipinhaotoolxiaohongshu
mcp相关
daoyianf81da61fd4c0
diff --git a/src/api/mcp.ts b/src/api/mcp.tsnew file mode 100644index 00000000..8fe0e7f4--- /dev/null+++ b/src/api/mcp.ts@@ -0,0 +1,93 @@+// MCP API接口+import { request } from "@/utils/request";++++/**+ * 创建MCP Key+ * @param data 创建参数+ * @returns+ */+export const apiCreateMCPKey = (data:any) => {+ return request({+ url: "channel/skKey",+ method: "POST",+ data,+ });+};++/**+ * 更新MCP Key+ * @param data 更新参数+ * @returns+ */+export const apiUpdateMCPKey = (data:any) => {+ return request({+ url: "channel/skKey",+ method: "PUT",+ data,+ });+};++/**+ * 删除MCP Key+ * @param key Key key+ * @returns+ */+export const apiDeleteMCPKey = (key: string) => {+ return request({+ url: `channel/skKey/${key}`,+ method: "DELETE",+ });+};++/**+ * 获取MCP Key详情+ * @param key Key值+ * @returns+ */+export const apiGetMCPKeyInfo = (key: string) => {+ return request<MCPKey>({+ url: `channel/skKey/info/${key}`,+ method: "GET",+ });+};++/**+ * 获取MCP Key列表+ * @param pageNo 页码+ * @param pageSize 每页大小+ * @returns+ */+export const apiGetMCPKeyList = (pageNo: number, pageSize: number) => {+ return request({+ url: `channel/skKey/list/${pageNo}/${pageSize}`,+ method: "GET",+ });+};++/**+ * 创建MCP Key关联+ * @param data 关联参数+ * @returns+ */+export const apiCreateMCPRef = (data: any) => {+ return request({+ url: "channel/skKey/ref",+ method: "POST",+ data,+ });+};++/**+ * 获取MCP Key关联列表+ * @param pageNo 页码+ * @param pageSize 每页大小+ * @returns+ */+export const apiGetMCPRefList = (pageNo: number, pageSize: number) => {+ return request({+ url: `channel/skKey/ref/list/${pageNo}/${pageSize}`,+ method: "GET",+ });+};\ No newline at end of filediff --git a/src/app/[lng]/accounts/components/AccountSidebar/MCPManagerModal.tsx b/src/app/[lng]/accounts/components/AccountSidebar/MCPManagerModal.tsxindex 490d4338..6d31c09f 100644--- a/src/app/[lng]/accounts/components/AccountSidebar/MCPManagerModal.tsx+++ b/src/app/[lng]/accounts/components/AccountSidebar/MCPManagerModal.tsx@@ -1,8 +1,14 @@-import React, { forwardRef, memo, useImperativeHandle, useRef, useState } from "react";-import { Modal, Button, List, Avatar, Tag, Space, message, Empty } from "antd";-import { PlusOutlined, CopyOutlined, DeleteOutlined, KeyOutlined } from "@ant-design/icons";+import React, { forwardRef, memo, useImperativeHandle, useRef, useState, useEffect } from "react";+import { Modal, Button, List, Avatar, Tag, Space, message, Empty, Input, Form, Pagination } from "antd";+import { PlusOutlined, CopyOutlined, DeleteOutlined, KeyOutlined, EditOutlined } from "@ant-design/icons";import ChooseAccountModule from "@/components/ChooseAccountModule/ChooseAccountModule";import { SocialAccount } from "@/api/types/account.type";+import {+ apiCreateMCPKey,+ apiDeleteMCPKey,+ apiGetMCPKeyList,+ apiCreateMCPRef,+} from "@/api/mcp";import styles from "./MCPManagerModal.module.scss";export interface IMCPManagerModalRef {@@ -15,60 +21,88 @@ export interface IMCPManagerModalProps {onClose: (open: boolean) => void;}-// MCP Key 接口类型-interface MCPKey {- id: string;- name: string;- key: string;- accounts: SocialAccount[];- createdAt: string;- status: 'active' | 'inactive';-}-const MCPManagerModal = memo(forwardRef<IMCPManagerModalRef, IMCPManagerModalProps>(({ open, onClose }, ref) => {- const [mcpKeys, setMcpKeys] = useState<MCPKey[]>([]);+ const [mcpKeys, setMcpKeys] = useState<any[]>([]);const [isCreateModalOpen, setIsCreateModalOpen] = useState(false);const [selectedAccounts, setSelectedAccounts] = useState<SocialAccount[]>([]);const [newKeyName, setNewKeyName] = useState('');+ const [loading, setLoading] = useState(false);+ const [currentPage, setCurrentPage] = useState(1);+ const [pageSize, setPageSize] = useState(10);+ const [total, setTotal] = useState(0);+ const [createForm] = Form.useForm();+ const [showAccountSelector, setShowAccountSelector] = useState(false);- // 模拟数据 - 实际应该从API获取- const mockMcpKeys: MCPKey[] = [- {- id: '1',- name: '默认MCP Key',- key: 'mcp_sk_1234567890abcdef',- accounts: [],- createdAt: '2024-01-15 10:30:00',- status: 'active'- },- {- id: '2',- name: '测试MCP Key',- key: 'mcp_sk_abcdef1234567890',- accounts: [],- createdAt: '2024-01-16 14:20:00',- status: 'active'+ // 获取MCP Key列表+ const fetchMCPKeys = async () => {+ try {+ setLoading(true);+ const res:any = await apiGetMCPKeyList(currentPage, pageSize);+ if (res?.code === 0) {+ setMcpKeys(res.data.list || []);+ setTotal(res.data.total || 0);+ }+ } catch (error) {+ message.error('获取MCP Key列表失败');+ } finally {+ setLoading(false);}- ];+ };// 初始化数据- React.useEffect(() => {+ useEffect(() => {if (open) {- setMcpKeys(mockMcpKeys);+ fetchMCPKeys();}- }, [open]);+ }, [open, currentPage, pageSize]);const handleCreateKey = () => {setIsCreateModalOpen(true);};- const handleAccountConfirm = (accounts: SocialAccount[]) => {- setSelectedAccounts(accounts);- setIsCreateModalOpen(false);- // TODO: 调用创建MCP Key的API- message.success('MCP Key创建成功!');+ const handleAccountConfirm = async (accounts: SocialAccount[]) => {+ try {+ setLoading(true);++ // 创建MCP Key+ const createParams: any = {+ name: newKeyName,+ accounts: accounts.map(account => account.id)+ };++ const createRes = await apiCreateMCPKey(createParams);+ if (createRes?.code !== 0) {+ message.error('创建MCP Key失败');+ return;+ }++ // 创建关联(如果选择了多个账户,需要多次请求)+ const createdKey = (createRes.data as any)?.key;+ if (createdKey) {+ for (const account of accounts) {+ try {+ await apiCreateMCPRef({+ key: createdKey,+ accountId: account.id+ });+ } catch (error) {+ console.error(`创建关联失败: ${account.id}`, error);+ }+ }+ }++ setSelectedAccounts([]);+ setIsCreateModalOpen(false);+ setNewKeyName('');+ message.success('MCP Key创建成功!');+ fetchMCPKeys(); // 刷新列表+ } catch (error) {+ message.error('创建MCP Key失败');+ } finally {+ setLoading(false);+ }};const handleCopyKey = (key: string) => {@@ -76,9 +110,18 @@ const MCPManagerModal = memo(message.success('Key已复制到剪贴板');};- const handleDeleteKey = (keyId: string) => {- setMcpKeys(prev => prev.filter(key => key.id !== keyId));- message.success('MCP Key已删除');+ const handleDeleteKey = async (key: string) => {+ try {+ const res = await apiDeleteMCPKey(key);+ if (res?.code === 0) {+ message.success('MCP Key已删除');+ fetchMCPKeys(); // 刷新列表+ } else {+ message.error('删除MCP Key失败');+ }+ } catch (error) {+ message.error('删除MCP Key失败');+ }};useImperativeHandle(ref, () => ({@@ -107,12 +150,13 @@ const MCPManagerModal = memo(icon={<PlusOutlined />}onClick={handleCreateKey}className={styles.createButton}+ loading={loading}>创建新Key</Button></div>- {mcpKeys.length === 0 ? (+ {mcpKeys.length === 0 && !loading ? (<div className={styles.emptyState}><div className="emptyIcon"><KeyOutlined />@@ -121,69 +165,180 @@ const MCPManagerModal = memo(<div className="emptyDesc">点击上方按钮创建您的第一个MCP Key</div></div>) : (- <List- className={styles.mcpKeyList}- dataSource={mcpKeys}- renderItem={(item) => (- <List.Item- actions={[- <Button- key="copy"- type="link"- icon={<CopyOutlined />}- onClick={() => handleCopyKey(item.key)}- >- 复制- </Button>,- <Button- key="delete"- type="link"- danger- icon={<DeleteOutlined />}- onClick={() => handleDeleteKey(item.id)}- >- 删除- </Button>- ]}- >- <List.Item.Meta- avatar={<Avatar style={{ background: 'linear-gradient(90deg, #625BF2 0%, #925BF2 100%)' }}>MCP</Avatar>}- title={- <Space>- <span>{item.name}</span>- <Tag color={item.status === 'active' ? 'green' : 'red'}>- {item.status === 'active' ? '活跃' : '非活跃'}- </Tag>- </Space>- }- description={- <div>- <div className="keyDisplay">- {item.key}- </div>- <div className="keyInfo">- <div className="infoItem">- <span className="label">创建时间:</span>- <span>{item.createdAt}</span>+ <>+ <List+ className={styles.mcpKeyList}+ dataSource={mcpKeys}+ loading={loading}+ renderItem={(item) => (+ <List.Item+ actions={[+ <Button+ key="copy"+ type="link"+ icon={<CopyOutlined />}+ onClick={() => handleCopyKey(item.key)}+ >+ 复制+ </Button>,+ <Button+ key="delete"+ type="link"+ danger+ icon={<DeleteOutlined />}+ onClick={() => handleDeleteKey(item.key)}+ loading={loading}+ >+ 删除+ </Button>+ ]}+ >+ <List.Item.Meta+ avatar={<Avatar style={{ background: 'linear-gradient(90deg, #625BF2 0%, #925BF2 100%)' }}>MCP</Avatar>}+ title={+ <Space>+ <span>{item.name}</span>+ <Tag color={item.status === 'active' ? 'green' : 'red'}>+ {item.status === 'active' ? '活跃' : '非活跃'}+ </Tag>+ </Space>+ }+ description={+ <div>+ <div className="keyDisplay">+ {item.key}</div>- <div className="infoItem">- <span className="label">关联账户:</span>- <span>{item.accounts.length} 个</span>+ <div className="keyInfo">+ <div className="infoItem">+ <span className="label">创建时间:</span>+ <span>{item.createdAt}</span>+ </div>+ <div className="infoItem">+ <span className="label">关联账户:</span>+ <span>{item.accounts?.length} 个</span>+ </div></div></div>- </div>- }+ }+ />+ </List.Item>+ )}+ />++ {total > 0 && (+ <div style={{ marginTop: 16, textAlign: 'right' }}>+ <Pagination+ current={currentPage}+ pageSize={pageSize}+ total={total}+ showSizeChanger+ showQuickJumper+ showTotal={(total, range) => `第 ${range[0]}-${range[1]} 条,共 ${total} 条`}+ onChange={(page, size) => {+ setCurrentPage(page);+ setPageSize(size);+ }}/>- </List.Item>+ </div>)}- />+ </>)}</Modal>- <ChooseAccountModule+ <Modal+ title="创建MCP Key"open={isCreateModalOpen}- onClose={setIsCreateModalOpen}- onAccountConfirm={handleAccountConfirm}+ onCancel={() => {+ setIsCreateModalOpen(false);+ setSelectedAccounts([]);+ setNewKeyName('');+ createForm.resetFields();+ }}+ footer={null}+ width={600}+ >+ <Form+ form={createForm}+ layout="vertical"+ onFinish={(values) => {+ if (selectedAccounts.length === 0) {+ message.warning('请选择至少一个账户');+ return;+ }+ handleAccountConfirm(selectedAccounts);+ }}+ >+ <Form.Item+ label="Key名称"+ name="name"+ rules={[{ required: true, message: '请输入Key名称' }]}+ >+ <Input+ placeholder="请输入Key名称"+ value={newKeyName}+ onChange={(e) => setNewKeyName(e.target.value)}+ />+ </Form.Item>++ <Form.Item label="选择账户">+ <div style={{ minHeight: 100, border: '1px dashed #d9d9d9', padding: 16, borderRadius: 6 }}>+ {selectedAccounts.length === 0 ? (+ <div style={{ textAlign: 'center', color: '#999' }}>+ 请选择要关联的账户+ </div>+ ) : (+ <div>+ <div style={{ marginBottom: 8, fontWeight: 500 }}>已选择 {selectedAccounts.length} 个账户:</div>+ {selectedAccounts.map(account => (+ <Tag key={account.id} style={{ marginBottom: 4 }}>+ {account.nickname}+ </Tag>+ ))}+ </div>+ )}+ <Button+ type="dashed"+ onClick={() => {+ setShowAccountSelector(true);+ }}+ style={{ marginTop: 8 }}+ >+ 选择账户+ </Button>+ </div>+ </Form.Item>++ <Form.Item>+ <Space>+ <Button+ type="primary"+ htmlType="submit"+ loading={loading}+ >+ 创建+ </Button>+ <Button+ onClick={() => {+ setIsCreateModalOpen(false);+ setSelectedAccounts([]);+ setNewKeyName('');+ createForm.resetFields();+ }}+ >+ 取消+ </Button>+ </Space>+ </Form.Item>+ </Form>+ </Modal>++ <ChooseAccountModule+ open={showAccountSelector}+ onClose={() => setShowAccountSelector(false)}+ onAccountConfirm={(accounts) => {+ setSelectedAccounts(accounts);+ setShowAccountSelector(false);+ }}simpleAccountChooseProps={{choosedAccounts: selectedAccounts,disableAllSelect: false,