| | |
| | | getHealthPopularizationDetail, |
| | | updateHealthPopularization, |
| | | uploadHealthPopularizationImage, |
| | | uploadHealthPopularizationVideo, |
| | | } from './service'; |
| | | import BraftEditorCom from '@/components/BraftEditor'; |
| | | import { PlusOutlined, UploadOutlined } from '@ant-design/icons'; |
| | | import { PlusOutlined } from '@ant-design/icons'; |
| | | import { history, useAccess } from '@umijs/max'; |
| | | import { Button, Form, Image, Input, Radio, Switch, Upload, message } from 'antd'; |
| | | import { Button, Form, Image, Input, Switch, Upload, message } from 'antd'; |
| | | import React, { useEffect, useMemo, useState } from 'react'; |
| | | import './style.less'; |
| | | |
| | | const contentTypeOptions = [ |
| | | { label: '图文', value: 1 }, |
| | | { label: '视频', value: 2 }, |
| | | ]; |
| | | |
| | | const HealthPopularizationManagePage: React.FC = () => { |
| | | const access = useAccess(); |
| | |
| | | const [loading, setLoading] = useState(false); |
| | | const [submitting, setSubmitting] = useState(false); |
| | | const [uploadingCover, setUploadingCover] = useState(false); |
| | | const [uploadingVideo, setUploadingVideo] = useState(false); |
| | | const [coverImg, setCoverImg] = useState(''); |
| | | const pathname = history.location.pathname; |
| | | const isCreate = pathname.endsWith('/create'); |
| | |
| | | return segments[segments.length - 2] || ''; |
| | | }, [isCreate, pathname]); |
| | | const canAccess = isCreate ? access['live:health-popularization:add'] : access['live:health-popularization:edit']; |
| | | const contentType = Form.useWatch('contentType', form) || 1; |
| | | const isVideoType = contentType === 2; |
| | | |
| | | useEffect(() => { |
| | | if (!canAccess) { |
| | |
| | | setCoverImg(''); |
| | | form.setFieldsValue({ |
| | | title: '', |
| | | contentType: 1, |
| | | content: '', |
| | | videoUrl: '', |
| | | videoFileName: '', |
| | | status: true, |
| | | isHomeRecommend: true, |
| | | }); |
| | |
| | | form.setFieldsValue({ |
| | | title: detail.title, |
| | | coverImg: detail.coverImg, |
| | | contentType: detail.contentType || 1, |
| | | content: detail.content || '', |
| | | videoUrl: detail.videoUrl || '', |
| | | videoFileName: detail.videoFileName || '', |
| | | status: detail.status === 1, |
| | | isHomeRecommend: detail.isHomeRecommend === 1, |
| | | }); |
| | |
| | | const payload = { |
| | | title: values.title?.trim(), |
| | | coverImg, |
| | | contentType: values.contentType, |
| | | content: values.contentType === 1 ? values.content : undefined, |
| | | videoUrl: values.contentType === 2 ? values.videoUrl?.trim() : undefined, |
| | | videoFileName: values.contentType === 2 ? values.videoFileName?.trim() : undefined, |
| | | contentType: 1, |
| | | content: values.content, |
| | | status: values.status ? 1 : 0, |
| | | isHomeRecommend: values.isHomeRecommend ? 1 : 0, |
| | | }; |
| | |
| | | </Form.Item> |
| | | </div> |
| | | </div> |
| | | <Form.Item label="科普内容" name="contentType" rules={[{ required: true, message: '请选择科普内容类型' }]}> |
| | | <Radio.Group options={contentTypeOptions} disabled={loading} /> |
| | | <Form.Item |
| | | label="内容详情" |
| | | name="content" |
| | | className="health-popularization-edit-form__editor-item" |
| | | rules={[{ required: true, message: '请输入科普内容' }]} |
| | | > |
| | | <div className="health-popularization-edit-form__editor"> |
| | | <BraftEditorCom |
| | | data={form.getFieldValue('content') || ''} |
| | | resultCallback={(html: string) => form.setFieldsValue({ content: html })} |
| | | /> |
| | | </div> |
| | | </Form.Item> |
| | | {isVideoType ? ( |
| | | <> |
| | | <Form.Item |
| | | label="视频地址" |
| | | name="videoUrl" |
| | | extra="本地视频和视频地址必须填一个" |
| | | rules={[{ required: true, message: '请填写视频地址或上传本地视频' }]} |
| | | > |
| | | <Input placeholder="请输入" disabled={loading} /> |
| | | </Form.Item> |
| | | <Form.Item label="本地视频" extra="本地视频和视频地址必须填一个"> |
| | | <div className="health-popularization-edit-form__video-upload"> |
| | | <Upload |
| | | accept="video/*" |
| | | showUploadList={false} |
| | | customRequest={async (options) => { |
| | | setUploadingVideo(true); |
| | | try { |
| | | const res = await uploadHealthPopularizationVideo(options.file as File); |
| | | const url = res.data?.url; |
| | | if (!url) { |
| | | throw new Error('upload failed'); |
| | | } |
| | | form.setFieldsValue({ |
| | | videoUrl: url, |
| | | videoFileName: res.data?.fileName || (options.file as File).name, |
| | | }); |
| | | message.success('视频上传成功'); |
| | | options.onSuccess?.(res); |
| | | } catch (error) { |
| | | message.error('视频上传失败'); |
| | | options.onError?.(error as Error); |
| | | } finally { |
| | | setUploadingVideo(false); |
| | | } |
| | | }} |
| | | > |
| | | <Button icon={<UploadOutlined />} loading={uploadingVideo}> |
| | | 上传 |
| | | </Button> |
| | | </Upload> |
| | | <span> |
| | | {form.getFieldValue('videoFileName') || |
| | | (form.getFieldValue('videoUrl') ? '已填写视频地址' : '未上传视频文件')} |
| | | </span> |
| | | </div> |
| | | <Form.Item name="videoFileName" hidden> |
| | | <Input /> |
| | | </Form.Item> |
| | | </Form.Item> |
| | | </> |
| | | ) : ( |
| | | <Form.Item |
| | | label="内容详情" |
| | | name="content" |
| | | className="health-popularization-edit-form__editor-item" |
| | | rules={[{ required: true, message: '请输入科普内容' }]} |
| | | > |
| | | <div className="health-popularization-edit-form__editor"> |
| | | <BraftEditorCom |
| | | data={form.getFieldValue('content') || ''} |
| | | resultCallback={(html: string) => form.setFieldsValue({ content: html })} |
| | | /> |
| | | </div> |
| | | </Form.Item> |
| | | )} |
| | | </div> |
| | | <div className="health-popularization-edit-form__footer"> |
| | | <Button onClick={() => history.push(listPagePath)}>取消</Button> |