diff --git a/zlt-web/react-web-next/src/pages/orders/HomeCarousel/README.md b/zlt-web/react-web-next/src/pages/orders/HomeCarousel/README.md new file mode 100644 index 00000000..be68531c --- /dev/null +++ b/zlt-web/react-web-next/src/pages/orders/HomeCarousel/README.md @@ -0,0 +1,389 @@ +## HomeCarousel 页面优化总结 + +### 优化内容概览 + +根据 `react-web` 中的最佳实践,对 `react-web-next` 的 HomeCarousel 页面进行了全面优化,采用组件化架构。 + +--- + +## 📁 文件结构 + +``` +src/pages/orders/HomeCarousel/ +├── index.tsx # 主页面 - 列表展示和页面逻辑 +├── components/ +│ ├── HomeCarouselForm.tsx # 编辑表单组件 +│ └── CreateForm.tsx # 新增表单组件(包含图片上传) +├── index.less # 页面样式 +└── constants.ts # 常量和类型定义 +``` + +--- + +## 🎯 主要优化点 + +### 1. **架构优化** (index.tsx:1-211) + +**改进前:** +- 混乱的状态管理,参数名称不统一 +- 使用了自定义 UI 组件 (ActionBar, GlassCard, GradientButton) +- 操作逻辑分散在渲染中 + +**改进后:** +- 采用 Pro Components 标准组件库 +- 使用 QueryFilter 和 ProTable 的官方规范 +- 清晰的状态和变量命名 +- 分离业务逻辑到独立函数 `handleDelete()` + +**关键改进:** +```typescript +// 状态管理规范化 +const [createOpen, setCreateOpen] = useState(false); +const [editOpen, setEditOpen] = useState(false); +const [current, setCurrent] = useState(undefined); +const [searchParams, setSearchParams] = useState>({}); + +// 使用 ActionRef 管理表格刷新 +const actionRef = useRef(); + +// 独立的业务逻辑函数 +const handleDelete = async (record: HomeCarouselItem) => { + const hide = message.loading('删除中...'); + try { + await ordersApi.deleteHomeCarousel(record.id!); + message.success('删除成功'); + actionRef.current?.reload(); + return true; + } catch (e) { + message.error('删除失败'); + return false; + } finally { + hide(); + } +}; +``` + +### 2. **表格列配置优化** (index.tsx:43-111) + +**改进前:** +- 使用基础 HTML img 标签 +- 状态展示用 Tag 组件,颜色值硬编码 +- 链接无法点击 + +**改进后:** +```typescript +// 使用 AntImage 组件,支持图片预览 +{ + title: '图片', + dataIndex: 'imageUrl', + width: 120, + render: (imageUrl: string) => ( + + ), +}, + +// 链接可点击访问 +{ + title: '跳转链接', + dataIndex: 'linkUrl', + ellipsis: true, + render: (url: string) => ( + {url} + ), +}, + +// 使用 valueEnum 规范化状态 +{ + title: '状态', + dataIndex: 'status', + width: 80, + valueEnum: { + 1: { text: '启用', status: 'Success' }, + 0: { text: '禁用', status: 'Error' }, + }, +} +``` + +### 3. **搜索栏优化** (index.tsx:114-143) + +**改进前:** +- 使用自定义 SearchBarPlus 组件 +- 没有明确的搜索字段定义 + +**改进后:** +- 使用标准 `QueryFilter` 组件 +- 支持搜索:标题、状态 +- 搜索参数规范化处理 +- 集成样式类 + +```typescript + { + setSearchParams(values); + actionRef.current?.reload(); + }} +> + + + +``` + +### 4. **表单组件分离** (components/) + +**新增两个独立的表单组件:** + +#### HomeCarouselForm.tsx(编辑表单) +- 专注于编辑功能 +- 标题为"编辑轮播图" +- 包含所有字段:图片地址、标题、链接、排序、状态 +- 更好的占位符文本和验证规则 + +#### CreateForm.tsx(新增表单) +- 专注于创建功能 +- 包含图片上传功能 +- 实时预览上传的图片 +- 完整的验证和错误处理 +- 上传前验证(图片类型、大小) + +**核心特性:** +```typescript +// 图片上传处理 +const handleBeforeUpload = (file: RcFile) => { + const isImage = file.type.startsWith('image/'); + const isLt10M = file.size / 1024 / 1024 < 10; + if (!isImage) message.error('只能上传图片文件'); + if (!isLt10M) message.error('图片大小不能超过10MB'); + return isImage && isLt10M; +}; + +// 图片预览 +{imageUrl && ( +
+

图片预览:

+ +
+)} +``` + +### 5. **样式文件** (index.less) + +**新增样式规范:** +```less +.query-filter { + padding: 24px; + background-color: #fff; + border-radius: 2px; + margin-bottom: 16px; + box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.03), ...; +} + +// 表单项间距 +.ant-form-item { + margin-bottom: 24px; + &:last-child { margin-bottom: 0; } +} + +// 表格美化 +.ant-pro-table { + margin-top: 16px; + background: #fff; + border-radius: 2px; + + .ant-table-thead > tr > th { + background-color: #fafafa; + font-weight: 500; + } +} + +// 图片组件美化 +.ant-image-img { border-radius: 4px; } +.ant-modal-content { border-radius: 8px; } +``` + +### 6. **常量管理** (constants.ts) + +**统一管理所有常量和配置:** + +```typescript +// 状态选项 - 与表单、表格保持同步 +export const STATUS_OPTIONS = [ + { label: '启用', value: 1 }, + { label: '禁用', value: 0 }, +]; + +// 上传配置 +export const UPLOAD_CONFIG = { + maxCount: 1, + accept: 'image/*', + maxSize: 10 * 1024 * 1024, +}; + +// 错误消息 - 保持国际化就绪 +export const ERROR_MESSAGES = { + UPLOAD_FAILED: '上传失败', + IMAGE_REQUIRED: '请上传轮播图片', + SAVE_FAILED: '保存失败', + // ... +} as const; + +// 成功消息 +export const SUCCESS_MESSAGES = { + UPLOAD_SUCCESS: '上传成功', + CREATE_SUCCESS: '新增成功', + UPDATE_SUCCESS: '修改成功', + DELETE_SUCCESS: '删除成功', +} as const; +``` + +--- + +## 🔄 功能流程图 + +### 新增轮播图流程 +``` +点击"新增轮播图" 按钮 + ↓ +打开 CreateForm 弹窗 + ↓ +输入标题、链接、排序、状态信息 + ↓ +上传图片(带预览) + ↓ +点击提交 + ↓ +调用 ordersApi.saveOrUpdateHomeCarousel() + ↓ +成功 → 重新加载表格列表 +失败 → 显示错误消息 +``` + +### 编辑轮播图流程 +``` +表格中点击"编辑" 按钮 + ↓ +打开 HomeCarouselForm 弹窗(填充已有数据) + ↓ +修改需要改动的字段 + ↓ +点击提交 + ↓ +调用 ordersApi.saveOrUpdateHomeCarousel() + ↓ +成功 → 重新加载表格列表 +失败 → 显示错误消息 +``` + +### 删除轮播图流程 +``` +表格中点击"删除" 按钮 + ↓ +弹出确认对话框(提示"删除后将无法恢复") + ↓ +确认删除 + ↓ +调用 ordersApi.deleteHomeCarousel() + ↓ +success → 重新加载表格列表 +failed → 显示错误消息 +``` + +--- + +## 📊 对比总结 + +| 方面 | 改进前 | 改进后 | +|------|-------|-------| +| **UI 组件** | 自定义组件 (ActionBar, GlassCard) | 标准 Pro Components | +| **状态管理** | 混乱的参数名称 | 规范的状态变量 | +| **表单分离** | 单一表单组件处理新增编辑 | 两个独立组件 (CreateForm, HomeCarouselForm) | +| **图片处理** | 基础 img 标签 | AntImage 组件 + 上传 + 预览 | +| **链接展示** | 纯文本 | 可点击链接 | +| **搜索功能** | SearchBarPlus (自定义) | QueryFilter (标准) | +| **代码组织** | 混合式 | 组件化、常量分离 | +| **验证规则** | 缺少 | 完整的表单验证 | +| **样式管理** | 无 | 规范的 LESS 样式 | +| **错误处理** | 基础 | 详细的加载和错误反馈 | + +--- + +## 🚀 使用指南 + +### 安装依赖 +```bash +npm install +``` + +### 本地开发 +```bash +npm start +``` + +### 构建生产版本 +```bash +npm run build +``` + +--- + +## 📝 注意事项 + +1. **API 接口**:确保后端支持以下接口: + - GET `/api-order/home/carousel` - 获取列表 + - POST `/api-order/home/carousel` - 新增/编辑 + - DELETE `/api-order/home/carousel/{id}` - 删除 + +2. **图片上传**:CreateForm 中的上传地址需要配置为实际的后端接口 + +3. **字段映射**: + - `imageUrl` - 图片地址(必填) + - `linkUrl` - 跳转链接 + - `title` - 标题 + - `sort` - 排序(默认1) + - `status` - 状态(1启用,0禁用) + +4. **性能优化**: + - 表格列配置使用 `width` 属性固定宽度 + - 图片使用 lazy loading 和占位符 + - 搜索结果自动触发表格刷新 + +--- + +## 🎨 美化亮点 + +✅ 统一的 Pro Components 设计语言 +✅ 清晰的视觉层级和间距 +✅ 友好的用户反馈(加载状态、错误提示) +✅ 响应式布局(QueryFilter span=8) +✅ 圆角、阴影等细致设计 +✅ 状态颜色语义化(成功绿、失败红) +✅ 图片预览增强用户体验 + +--- + +## 📚 技术栈 + +- **React 17** - UI 框架 +- **UmiJS 3** - 前端集成框架 +- **@ant-design/pro-components** - 企业级组件库 +- **Ant Design 4** - UI 设计系统 +- **TypeScript** - 类型安全 +- **LESS** - 样式预处理 + diff --git a/zlt-web/react-web-next/src/pages/orders/HomeCarousel/components/CreateForm.tsx b/zlt-web/react-web-next/src/pages/orders/HomeCarousel/components/CreateForm.tsx new file mode 100644 index 00000000..5f69c6f2 --- /dev/null +++ b/zlt-web/react-web-next/src/pages/orders/HomeCarousel/components/CreateForm.tsx @@ -0,0 +1,173 @@ +import React, { useState } from 'react'; +import { + ModalForm, + ProForm, + ProFormDigit, + ProFormSelect, + ProFormText, + ProFormTextArea, +} from '@ant-design/pro-components'; +import { Button, Upload, message } from 'antd'; +import { Image as AntImage } from 'antd'; +import { UploadOutlined } from '@ant-design/icons'; +import type { RcFile, UploadProps } from 'antd/es/upload'; +import type { HomeCarouselItem } from './HomeCarouselForm'; + +export interface CreateFormProps { + open: boolean; + onOpenChange: (v: boolean) => void; + onFinish: (values: HomeCarouselItem) => Promise; +} + +const CreateForm: React.FC = ({ + open, + onOpenChange, + onFinish, +}) => { + const [imageUrl, setImageUrl] = useState(); + const [imagePath, setImagePath] = useState(); + + const handleBeforeUpload = (file: RcFile) => { + const isImage = file.type.startsWith('image/'); + if (!isImage) { + message.error('只能上传图片文件'); + } + const isLt10M = file.size / 1024 / 1024 < 10; + if (!isLt10M) { + message.error('图片大小不能超过10MB'); + } + return isImage && isLt10M; + }; + + const handleChange: UploadProps['onChange'] = (info) => { + if (info.file.status === 'done') { + const response = info.file.response as any; + if (response && response.url && response.path) { + setImageUrl(response.url); + setImagePath(response.path); + message.success('上传成功'); + } + } else if (info.file.status === 'error') { + message.error('上传失败'); + } + }; + + return ( + + title="新增轮播图" + open={open} + onOpenChange={(visible) => { + onOpenChange(visible); + if (!visible) { + setImageUrl(undefined); + setImagePath(undefined); + } + }} + onFinish={async (values) => { + if (!imagePath) { + message.error('请上传轮播图片'); + return false; + } + return onFinish({ + ...values, + imageUrl: imagePath, + }); + }} + modalProps={{ + destroyOnClose: true, + maskClosable: false, + }} + > + + + + + + + + + +
+ + + + +
+ + {imageUrl && ( +
+

图片预览:

+ + 加载中... +
+ } + /> + + )} + + ); +}; + +export default CreateForm; diff --git a/zlt-web/react-web-next/src/pages/orders/HomeCarousel/components/HomeCarouselForm.tsx b/zlt-web/react-web-next/src/pages/orders/HomeCarousel/components/HomeCarouselForm.tsx index 43b2b37a..bc871177 100644 --- a/zlt-web/react-web-next/src/pages/orders/HomeCarousel/components/HomeCarouselForm.tsx +++ b/zlt-web/react-web-next/src/pages/orders/HomeCarousel/components/HomeCarouselForm.tsx @@ -7,30 +7,71 @@ export interface HomeCarouselItem { linkUrl?: string; sort?: number; status?: number; + title?: string; + description?: string; } export interface HomeCarouselFormProps { open: boolean; - title: string; initialValues?: Partial; onOpenChange: (v: boolean) => void; onFinish: (values: HomeCarouselItem) => Promise; } -const HomeCarouselForm: React.FC = ({ open, title, initialValues, onOpenChange, onFinish }) => { +const HomeCarouselForm: React.FC = ({ + open, + initialValues, + onOpenChange, + onFinish, +}) => { return ( - title={title} - visible={open} - onVisibleChange={onOpenChange} + title="编辑轮播图" + open={open} + onOpenChange={onOpenChange} onFinish={onFinish} initialValues={initialValues} - modalProps={{ destroyOnClose: true }} + modalProps={{ + destroyOnClose: true, + maskClosable: false, + }} > - - - - + + + + + ); }; diff --git a/zlt-web/react-web-next/src/pages/orders/HomeCarousel/constants.ts b/zlt-web/react-web-next/src/pages/orders/HomeCarousel/constants.ts new file mode 100644 index 00000000..3b0a923a --- /dev/null +++ b/zlt-web/react-web-next/src/pages/orders/HomeCarousel/constants.ts @@ -0,0 +1,55 @@ +/** + * 首页轮播图管理 - 常量和类型定义 + */ + +// 状态选项 +export const STATUS_OPTIONS = [ + { label: '启用', value: 1 }, + { label: '禁用', value: 0 }, +]; + +// 状态枚举 +export const STATUS_ENUM = { + ENABLED: 1, + DISABLED: 0, +} as const; + +// 默认排序值 +export const DEFAULT_SORT = 1; + +// 默认状态值 +export const DEFAULT_STATUS = 1; + +// 上传配置 +export const UPLOAD_CONFIG = { + maxCount: 1, + accept: 'image/*', + maxSize: 10 * 1024 * 1024, // 10MB +}; + +// 图片选项 +export const IMAGE_PLACEHOLDER_STYLE = { + background: '#f5f5f5', + height: 100, + display: 'flex' as const, + alignItems: 'center' as const, + justifyContent: 'center' as const, +}; + +// API错误信息 +export const ERROR_MESSAGES = { + UPLOAD_FAILED: '上传失败', + IMAGE_REQUIRED: '请上传轮播图片', + SAVE_FAILED: '保存失败', + DELETE_FAILED: '删除失败', + ONLY_IMAGE: '只能上传图片文件', + FILE_TOO_LARGE: '图片大小不能超过10MB', +} as const; + +// API成功信息 +export const SUCCESS_MESSAGES = { + UPLOAD_SUCCESS: '上传成功', + CREATE_SUCCESS: '新增成功', + UPDATE_SUCCESS: '修改成功', + DELETE_SUCCESS: '删除成功', +} as const; diff --git a/zlt-web/react-web-next/src/pages/orders/HomeCarousel/index.less b/zlt-web/react-web-next/src/pages/orders/HomeCarousel/index.less new file mode 100644 index 00000000..c49cc8f5 --- /dev/null +++ b/zlt-web/react-web-next/src/pages/orders/HomeCarousel/index.less @@ -0,0 +1,54 @@ +@import (reference) '~antd/es/style/themes/index'; + +// 查询栏样式 +.query-filter { + padding: 24px; + background-color: #fff; + border-radius: 2px; + margin-bottom: 16px; + box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.03), 0 1px 6px -1px rgba(0, 0, 0, 0.02), 0 2px 4px 0 rgba(0, 0, 0, 0.02); + + :global { + .ant-form-item { + margin-bottom: 24px; + + &:last-child { + margin-bottom: 0; + } + } + + .ant-select, + .ant-input { + width: 100%; + } + + .ant-form-item-label > label { + font-weight: 500; + } + } +} + +// 表格样式 +:global { + .ant-pro-table { + margin-top: 16px; + background: #fff; + border-radius: 2px; + + .ant-table-thead > tr > th { + background-color: #fafafa; + font-weight: 500; + } + } +} + +// 图片预览样式 +:global { + .ant-image-img { + border-radius: 4px; + } + + .ant-modal-content { + border-radius: 8px; + } +} diff --git a/zlt-web/react-web-next/src/pages/orders/HomeCarousel/index.tsx b/zlt-web/react-web-next/src/pages/orders/HomeCarousel/index.tsx index d56ccfaa..db9c8987 100644 --- a/zlt-web/react-web-next/src/pages/orders/HomeCarousel/index.tsx +++ b/zlt-web/react-web-next/src/pages/orders/HomeCarousel/index.tsx @@ -1,68 +1,171 @@ -import React, { useState } from 'react'; -import { PageContainer, ProTable } from '@ant-design/pro-components'; -import { Popconfirm, Space, Tag, message } from 'antd'; -import { ActionBar, GlassCard, GradientButton, SearchBarPlus } from '@/components/ui'; -import { PlusOutlined } from '@ant-design/icons'; +import React, { useRef, useState } from 'react'; +import { PageContainer, ProTable, QueryFilter, ProFormText, ProFormSelect } from '@ant-design/pro-components'; +import { Popconfirm, Space, Button, message, Typography, Image as AntImage } from 'antd'; +import { PlusOutlined, DeleteOutlined, EditOutlined, UploadOutlined } from '@ant-design/icons'; +import type { ActionType, ProColumns } from '@ant-design/pro-components'; import { ordersApi } from '@/services/orders'; -import HomeCarouselForm, { HomeCarouselItem } from './components/HomeCarouselForm'; +import HomeCarouselForm from './components/HomeCarouselForm'; +import CreateForm from './components/CreateForm'; +import type { HomeCarouselItem } from './components/HomeCarouselForm'; +import styles from './index.less'; + +const { Link } = Typography; + +// 常量定义 +const STATUS_OPTIONS = [ + { label: '启用', value: 1 }, + { label: '禁用', value: 0 }, +]; const HomeCarouselPage: React.FC = () => { + const actionRef = useRef(); const [createOpen, setCreateOpen] = useState(false); const [editOpen, setEditOpen] = useState(false); const [current, setCurrent] = useState(undefined); - const [params, setParams] = useState>({}); + const [searchParams, setSearchParams] = useState>({}); - const columns: any[] = [ - { dataIndex: 'index', valueType: 'indexBorder', width: 48 }, - { title: '图片', dataIndex: 'imageUrl', render: (v: string) => img }, - { title: '跳转链接', dataIndex: 'linkUrl', ellipsis: true }, - { title: '排序', dataIndex: 'sort' }, - { title: '状态', dataIndex: 'status', render: (v: number) => {v === 1 ? '启用' : '禁用'} }, + // 处理删除 + const handleDelete = async (record: HomeCarouselItem) => { + const hide = message.loading('删除中...'); + try { + await ordersApi.deleteHomeCarousel(record.id!); + message.success('删除成功'); + actionRef.current?.reload(); + return true; + } catch (e) { + message.error('删除失败'); + return false; + } finally { + hide(); + } + }; + + const columns: ProColumns[] = [ { - title: '操作', key: 'action', - render: (_: any, record: HomeCarouselItem) => ( + dataIndex: 'index', + valueType: 'indexBorder', + width: 48, + }, + { + title: '图片', + dataIndex: 'imageUrl', + width: 120, + render: (imageUrl: string) => ( + + 加载中... + + } + /> + ), + }, + { + title: '跳转链接', + dataIndex: 'linkUrl', + ellipsis: true, + render: (url: string) => ( + {url} + ), + }, + { + title: '排序', + dataIndex: 'sort', + width: 80, + }, + { + title: '状态', + dataIndex: 'status', + width: 80, + valueEnum: { + 1: { text: '启用', status: 'Success' }, + 0: { text: '禁用', status: 'Error' }, + }, + }, + { + title: '操作', + valueType: 'option', + width: 180, + render: (_, record) => ( - { setCurrent(record); setEditOpen(true); }}>编辑 - { - const hide = message.loading('删除中...'); - try { - await ordersApi.deleteHomeCarousel(record.id!); - message.success('删除成功'); - } catch (e) { - message.error('删除失败'); - } finally { hide(); } - }}> - 删除 + { + setCurrent(record); + setEditOpen(true); + }} + > + 编辑 + + handleDelete(record)} + > + 删除 - ) + ), }, ]; return ( - - + { /* 仅触发表格刷新 */ }} - onFinish={async (values) => setParams(values)} + className={styles['query-filter']} + onFinish={async (values) => { + setSearchParams(values); + actionRef.current?.reload(); + }} + style={{ marginBottom: 16 }} + > + + + + + + rowKey="id" + headerTitle="轮播图列表" + actionRef={actionRef} + columns={columns} + request={(p) => ordersApi.homeCarousel({ ...(p as any), ...searchParams })} + search={false} + pagination={{ + defaultPageSize: 10, + showSizeChanger: true, + showQuickJumper: true, + }} + toolBarRender={() => [ + , + ]} /> - } onClick={() => setCreateOpen(true)}>新增} /> - - - - rowKey="id" - request={(p) => ordersApi.homeCarouselList({ ...(p as any), ...params })} - columns={columns} - search={false} - pagination={{ showSizeChanger: true, showQuickJumper: true }} - /> - - - { @@ -70,16 +173,18 @@ const HomeCarouselPage: React.FC = () => { try { await ordersApi.saveOrUpdateHomeCarousel(values); message.success('新增成功'); + actionRef.current?.reload(); return true; } catch (e) { message.error('保存失败'); return false; - } finally { hide(); } + } finally { + hide(); + } }} /> { try { await ordersApi.saveOrUpdateHomeCarousel({ ...(current as any), ...values }); message.success('修改成功'); + actionRef.current?.reload(); return true; } catch (e) { - message.error('修改失败'); + message.error('保存失败'); return false; - } finally { hide(); } + } finally { + hide(); + } }} />