fix(订单中心):新版UI订单素材接口逻辑
This commit is contained in:
+39
-45
@@ -3,7 +3,8 @@ import { Button, Upload, message } from 'antd';
|
||||
import { UploadOutlined } from '@ant-design/icons';
|
||||
import React, { useState } from 'react';
|
||||
import { TYPE_OPTIONS } from '../constants';
|
||||
import { importImage, saveOrUpdateTourismFile } from '@/services/orders/api';
|
||||
import { importImage } from '@/services/system/api';
|
||||
import { saveOrUpdateTourismFile } from '@/services/orders/api';
|
||||
import type { RcFile } from 'antd/lib/upload';
|
||||
import styles from '../index.less';
|
||||
|
||||
@@ -20,7 +21,8 @@ const CreateForm: React.FC<CreateFormProps> = ({
|
||||
tourismOptions,
|
||||
onSuccess,
|
||||
}) => {
|
||||
const [imageUrl, setImageUrl] = useState<string>();
|
||||
const [fileUrl, setFileUrl] = useState<string>();
|
||||
const [fileType, setFileType] = useState<number>();
|
||||
|
||||
const handleUpload = async (file: RcFile) => {
|
||||
const hide = message.loading('正在上传');
|
||||
@@ -29,8 +31,8 @@ const CreateForm: React.FC<CreateFormProps> = ({
|
||||
formData.append('file', file);
|
||||
const result = await importImage(formData);
|
||||
hide();
|
||||
if (result.name) {
|
||||
setImageUrl(result.name);
|
||||
if (result.url) {
|
||||
setFileUrl(result.url);
|
||||
message.success('上传成功');
|
||||
}
|
||||
} catch (error) {
|
||||
@@ -41,20 +43,18 @@ const CreateForm: React.FC<CreateFormProps> = ({
|
||||
};
|
||||
|
||||
const handleSubmit = async (values: Record<string, any>) => {
|
||||
if (!imageUrl) {
|
||||
message.error('请上传素材图片');
|
||||
if (!fileUrl) {
|
||||
message.error('请上传素材文件');
|
||||
return false;
|
||||
}
|
||||
|
||||
const hide = message.loading('正在提交');
|
||||
try {
|
||||
const result = await saveOrUpdateTourismFile({
|
||||
...values,
|
||||
imageUrl,
|
||||
imageUrl: fileUrl,
|
||||
status: 0, // 默认启用
|
||||
});
|
||||
hide();
|
||||
|
||||
if (result.resp_code === 0) {
|
||||
message.success('创建成功');
|
||||
onSuccess();
|
||||
@@ -75,14 +75,12 @@ const CreateForm: React.FC<CreateFormProps> = ({
|
||||
visible={visible}
|
||||
onVisibleChange={(visible) => {
|
||||
if (!visible) {
|
||||
setImageUrl(undefined);
|
||||
setFileUrl(undefined);
|
||||
setFileType(undefined);
|
||||
}
|
||||
onVisibleChange(visible);
|
||||
}}
|
||||
modalProps={{
|
||||
destroyOnClose: true,
|
||||
maskClosable: false,
|
||||
}}
|
||||
modalProps={{ destroyOnClose: true, maskClosable: false }}
|
||||
onFinish={handleSubmit}
|
||||
>
|
||||
<ProForm.Group>
|
||||
@@ -96,7 +94,7 @@ const CreateForm: React.FC<CreateFormProps> = ({
|
||||
showSearch
|
||||
fieldProps={{
|
||||
loading: !tourismOptions.length,
|
||||
filterOption: (input, option) =>
|
||||
filterOption: (input: string, option: any) =>
|
||||
(option?.label ?? '').toLowerCase().includes(input.toLowerCase()),
|
||||
}}
|
||||
/>
|
||||
@@ -106,7 +104,10 @@ const CreateForm: React.FC<CreateFormProps> = ({
|
||||
name="type"
|
||||
label="素材类型"
|
||||
placeholder="请选择素材类型"
|
||||
options={TYPE_OPTIONS}
|
||||
options={TYPE_OPTIONS as any}
|
||||
fieldProps={{
|
||||
onChange: (val: number) => setFileType(val),
|
||||
}}
|
||||
/>
|
||||
<ProFormText
|
||||
width="md"
|
||||
@@ -121,36 +122,29 @@ const CreateForm: React.FC<CreateFormProps> = ({
|
||||
placeholder="请输入素材描述"
|
||||
rules={[{ required: true, message: '请输入素材描述' }]}
|
||||
/>
|
||||
|
||||
<div style={{ width: '100%', marginBottom: 24 }}>
|
||||
<div style={{ marginBottom: 8 }}>
|
||||
<span style={{ color: '#666' }}>素材图片:</span>
|
||||
<span style={{ color: '#ff4d4f' }}>*</span>
|
||||
</div>
|
||||
{imageUrl && (
|
||||
<div style={{ marginBottom: 8 }}>
|
||||
<img
|
||||
src={imageUrl}
|
||||
alt="preview"
|
||||
style={{
|
||||
maxWidth: '200px',
|
||||
maxHeight: '200px',
|
||||
objectFit: 'contain'
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
<Upload
|
||||
maxCount={1}
|
||||
showUploadList={false}
|
||||
beforeUpload={handleUpload}
|
||||
accept="image/*"
|
||||
>
|
||||
<Button icon={<UploadOutlined />}>
|
||||
{imageUrl ? '重新上传' : '上传素材'}
|
||||
</Button>
|
||||
</Upload>
|
||||
<div style={{ marginBottom: 8 }}>
|
||||
<span style={{ color: '#666' }}>素材文件:</span>
|
||||
<span style={{ color: '#ff4d4f' }}>*</span>
|
||||
</div>
|
||||
{fileUrl && (
|
||||
<div style={{ marginBottom: 8 }}>
|
||||
{fileType === 5 ? (
|
||||
<video src={fileUrl} controls style={{ maxWidth: '200px', maxHeight: '200px' }} />
|
||||
) : (
|
||||
<img src={fileUrl} alt="preview" style={{ maxWidth: '200px', maxHeight: '200px', objectFit: 'contain' }} />
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
<Upload
|
||||
maxCount={1}
|
||||
showUploadList={false}
|
||||
beforeUpload={handleUpload}
|
||||
accept={fileType === 5 ? 'video/*' : 'image/*'}
|
||||
>
|
||||
<Button icon={<UploadOutlined />}>
|
||||
{fileUrl ? '重新上传' : fileType === 5 ? '上传视频' : '上传图片'}
|
||||
</Button>
|
||||
</Upload>
|
||||
</ProForm.Group>
|
||||
</ModalForm>
|
||||
);
|
||||
|
||||
+52
-48
@@ -11,6 +11,11 @@ import type { RcFile } from 'antd/lib/upload';
|
||||
import React, { useState } from 'react';
|
||||
import type { TYPE_OPTIONS } from '../constants';
|
||||
|
||||
const STATUS_OPTIONS = [
|
||||
{ label: '启用', value: 0, color: 'success' },
|
||||
{ label: '未启用', value: 1, color: 'error' },
|
||||
] as const;
|
||||
|
||||
interface UpdateFormProps {
|
||||
onSubmit: (values: any) => Promise<void>;
|
||||
onVisibleChange: (visible: boolean) => void;
|
||||
@@ -22,30 +27,26 @@ interface UpdateFormProps {
|
||||
}
|
||||
|
||||
const UpdateForm: React.FC<UpdateFormProps> = (props) => {
|
||||
const [imageUrl, setImageUrl] = useState<string>();
|
||||
const [imagePath, setImagePath] = useState<string>();
|
||||
const [fileUrl, setFileUrl] = useState<string>((props.values as any)?.fileUrl || (props.values as any)?.imageUrl || '');
|
||||
const [fileType, setFileType] = useState<number | undefined>((props.values as any)?.type);
|
||||
|
||||
const handleUpload = async (file: RcFile) => {
|
||||
const hide = message.loading('正在上传...');
|
||||
const handleUpload = async (file: RcFile): Promise<boolean> => {
|
||||
const hide = message.loading('正在上传');
|
||||
try {
|
||||
const formData = new FormData();
|
||||
formData.append('file', file);
|
||||
const result = await importImage(formData);
|
||||
hide();
|
||||
|
||||
if (result.url && result.path) {
|
||||
if (result.url) {
|
||||
setFileUrl(result.url);
|
||||
message.success('上传成功');
|
||||
setImageUrl(result.url);
|
||||
setImagePath(result.path);
|
||||
return true;
|
||||
}
|
||||
message.error('上传失败:未获取到文件信息');
|
||||
return false;
|
||||
} catch (error) {
|
||||
hide();
|
||||
message.error('上传失败:' + (error.message || '未知错误'));
|
||||
return false;
|
||||
message.error('上传失败');
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
return (
|
||||
@@ -55,13 +56,13 @@ const UpdateForm: React.FC<UpdateFormProps> = (props) => {
|
||||
onVisibleChange={props.onVisibleChange}
|
||||
modalProps={{ destroyOnClose: true }}
|
||||
onFinish={async (values) => {
|
||||
if (imagePath) {
|
||||
values.imageUrl = imagePath;
|
||||
if (fileUrl) {
|
||||
values.fileUrl = fileUrl;
|
||||
}
|
||||
const success = await props.onSubmit(values);
|
||||
if (success) {
|
||||
setImageUrl(undefined);
|
||||
setImagePath(undefined);
|
||||
setFileUrl('');
|
||||
setFileType(undefined);
|
||||
}
|
||||
return success;
|
||||
}}
|
||||
@@ -83,7 +84,10 @@ const UpdateForm: React.FC<UpdateFormProps> = (props) => {
|
||||
name="type"
|
||||
label="素材类型"
|
||||
placeholder="请选择素材类型"
|
||||
options={props.typeOptions}
|
||||
options={props.typeOptions as any}
|
||||
fieldProps={{
|
||||
onChange: (val: number) => setFileType(val),
|
||||
}}
|
||||
/>
|
||||
<ProFormSelect
|
||||
rules={[{ required: true, message: '请选择状态' }]}
|
||||
@@ -91,7 +95,7 @@ const UpdateForm: React.FC<UpdateFormProps> = (props) => {
|
||||
name="status"
|
||||
label="状态"
|
||||
placeholder="请选择状态"
|
||||
options={props.statusOptions}
|
||||
options={STATUS_OPTIONS as any}
|
||||
/>
|
||||
<ProFormText
|
||||
width="md"
|
||||
@@ -105,38 +109,38 @@ const UpdateForm: React.FC<UpdateFormProps> = (props) => {
|
||||
label="素材描述"
|
||||
placeholder="输入描述"
|
||||
/>
|
||||
|
||||
<Form.Item label="素材图片" required>
|
||||
<Upload
|
||||
maxCount={1}
|
||||
accept="image/*"
|
||||
showUploadList={false}
|
||||
customRequest={async ({ file, onSuccess, onError }) => {
|
||||
try {
|
||||
if (file instanceof File) {
|
||||
const success = await handleUpload(file as RcFile);
|
||||
if (success) {
|
||||
onSuccess?.('ok');
|
||||
} else {
|
||||
onError?.(new Error('上传失败'));
|
||||
}
|
||||
<Upload
|
||||
maxCount={1}
|
||||
accept={fileType === 5 ? 'video/*' : 'image/*'}
|
||||
showUploadList={false}
|
||||
customRequest={async ({ file, onSuccess, onError }) => {
|
||||
try {
|
||||
if (file instanceof File) {
|
||||
const success = await handleUpload(file as RcFile);
|
||||
if (success) {
|
||||
onSuccess?.('ok');
|
||||
} else {
|
||||
onError?.(new Error('上传失败') as any);
|
||||
}
|
||||
} catch (error) {
|
||||
onError?.(error);
|
||||
}
|
||||
}}
|
||||
>
|
||||
<Button icon={<UploadOutlined />}>上传素材</Button>
|
||||
</Upload>
|
||||
|
||||
<div style={{ marginTop: 16 }}>
|
||||
<Image
|
||||
src={imageUrl || props.values?.imageUrl}
|
||||
width={200}
|
||||
style={{ objectFit: 'cover' }}
|
||||
/>
|
||||
</div>
|
||||
</Form.Item>
|
||||
} catch (error) {
|
||||
onError?.(error as any);
|
||||
}
|
||||
}}
|
||||
>
|
||||
<Button icon={<UploadOutlined />}>
|
||||
{fileUrl ? '重新上传' : fileType === 5 ? '上传视频' : '上传图片'}
|
||||
</Button>
|
||||
</Upload>
|
||||
<div style={{ marginTop: 16 }}>
|
||||
{fileUrl ? (
|
||||
fileType === 5 ? (
|
||||
<video src={fileUrl} controls style={{ maxWidth: '200px', maxHeight: '200px' }} />
|
||||
) : (
|
||||
<Image src={fileUrl} width={200} style={{ objectFit: 'cover' }} />
|
||||
)
|
||||
) : null}
|
||||
</div>
|
||||
</ProForm.Group>
|
||||
</ModalForm>
|
||||
);
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
// 将常量配置抽离到单独文件
|
||||
export const TYPE_OPTIONS = [
|
||||
{ label: '景区视频', value: 5 },
|
||||
{ label: '景区订单展示图', value: 6 },
|
||||
{ label: '展示大图', value: 4 },
|
||||
{ label: '拍照引导图', value: 3 },
|
||||
{ label: '拍照角度展示图', value: 2 },
|
||||
@@ -17,4 +19,6 @@ export const TYPE_CONFIG = {
|
||||
2: { color: '#13c2c2', text: '拍照角度' }, // 青色
|
||||
3: { color: '#722ed1', text: '引导' }, // 紫色
|
||||
4: { color: '#52c41a', text: '大图' }, // 绿色
|
||||
5: { color: '#f5222d', text: '景区视频' }, // 红色
|
||||
6: { color: '#2f54eb', text: '订单展示图' }, // 蓝色
|
||||
} as const;
|
||||
@@ -14,7 +14,7 @@ import {
|
||||
import { Typography, Popconfirm, Space, message, Image, Button, Tag, Upload, Spin } from 'antd';
|
||||
import type { RcFile } from 'antd/lib/upload';
|
||||
import React, { useRef, useState, useCallback, useEffect } from 'react';
|
||||
import { STATUS_CONFIG, TYPE_CONFIG } from './constants';
|
||||
import { STATUS_CONFIG, TYPE_CONFIG, TYPE_OPTIONS } from './constants';
|
||||
import CreateForm from './components/CreateForm';
|
||||
import UpdateForm from './components/UpdateForm';
|
||||
import SearchForm from './components/SearchForm';
|
||||
@@ -28,14 +28,6 @@ const STATUS_OPTIONS = [
|
||||
{ label: '未启用', value: 1, color: 'error' },
|
||||
] as const;
|
||||
|
||||
// 类型常量
|
||||
const TYPE_OPTIONS = [
|
||||
{ label: '展示大图', value: 4, color: 'blue' },
|
||||
{ label: '拍照引导图', value: 3, color: 'green' },
|
||||
{ label: '拍照角度展示图', value: 2, color: 'cyan' },
|
||||
{ label: '活动', value: 1, color: 'orange' },
|
||||
] as const;
|
||||
|
||||
// 状态映射
|
||||
const statusColorMap = {
|
||||
0: { color: 'green', text: '启用' },
|
||||
|
||||
Reference in New Issue
Block a user