import {useEffect, useState, useRef} from "react"; import {useRouter} from '@tarojs/taro' import {Button, Loading, CellGroup, Input, TextArea, Form, Switch, InputNumber, Radio, Image} from '@nutui/nutui-react-taro' import {Edit, Upload as UploadIcon} from '@nutui/icons-react-taro' import Taro from '@tarojs/taro' import {View} from '@tarojs/components' import {ShopArticle} from "@/api/shop/shopArticle/model"; import {getShopArticle, addShopArticle, updateShopArticle} from "@/api/shop/shopArticle"; import FixedButton from "@/components/FixedButton"; const AddShopArticle = () => { const {params} = useRouter(); const [loading, setLoading] = useState(true) const [formData, setFormData] = useState({ type: 0, // 默认常规文章 status: 0, // 默认已发布 permission: 0, // 默认所有人可见 recommend: 0, // 默认不推荐 showType: 10, // 默认小图展示 virtualViews: 0, // 默认虚拟阅读量 actualViews: 0, // 默认实际阅读量 sortNumber: 0 // 默认排序 }) const formRef = useRef(null) // 判断是编辑还是新增模式 const isEditMode = !!params.id const articleId = params.id ? Number(params.id) : undefined // 文章类型选项 const typeOptions = [ { text: '常规文章', value: 0 }, { text: '视频文章', value: 1 } ] // 状态选项 const statusOptions = [ { text: '已发布', value: 0 }, { text: '待审核', value: 1 }, { text: '已驳回', value: 2 }, { text: '违规内容', value: 3 } ] // 可见性选项 const permissionOptions = [ { text: '所有人可见', value: 0 }, { text: '登录可见', value: 1 }, { text: '密码可见', value: 2 } ] // 显示方式选项 const showTypeOptions = [ { text: '小图展示', value: 10 }, { text: '大图展示', value: 20 } ] const reload = async () => { // 如果是编辑模式,加载文章数据 if (isEditMode && articleId) { try { const article = await getShopArticle(articleId) setFormData(article) // 更新表单值 if (formRef.current) { formRef.current.setFieldsValue(article) } } catch (error) { console.error('加载文章失败:', error) Taro.showToast({ title: '加载文章失败', icon: 'error' }); } } } // 图片上传处理 const handleImageUpload = async () => { try { const res = await Taro.chooseImage({ count: 1, sizeType: ['compressed'], sourceType: ['album', 'camera'] }); if (res.tempFilePaths && res.tempFilePaths.length > 0) { // 这里应该调用上传接口,暂时使用本地路径 const imagePath = res.tempFilePaths[0]; setFormData({ ...formData, image: imagePath }); Taro.showToast({ title: '图片选择成功', icon: 'success' }); } } catch (error) { Taro.showToast({ title: '图片选择失败', icon: 'error' }); } }; // 提交表单 const submitSucceed = async (values: any) => { try { // 准备提交的数据 const submitData = { ...formData, ...values, }; // 如果是编辑模式,添加id if (isEditMode && articleId) { submitData.articleId = articleId; } // 执行新增或更新操作 if (isEditMode) { await updateShopArticle(submitData); } else { await addShopArticle(submitData); } Taro.showToast({ title: `${isEditMode ? '更新' : '保存'}成功`, icon: 'success' }); setTimeout(() => { Taro.navigateBack(); }, 1000); } catch (error) { console.error('保存失败:', error); Taro.showToast({ title: `${isEditMode ? '更新' : '保存'}失败`, icon: 'error' }); } } const submitFailed = (error: any) => { console.log(error, 'err...') } useEffect(() => { // 动态设置页面标题 Taro.setNavigationBarTitle({ title: isEditMode ? '编辑文章' : '新增文章' }); reload().then(() => { setLoading(false) }) }, [isEditMode]); if (loading) { return 加载中 } return ( <>
submitSucceed(values)} onFinishFailed={(errors) => submitFailed(errors)} > {/* 基本信息 */}