diff --git a/src/app.config.ts b/src/app.config.ts index 23b9c40..2e289cc 100644 --- a/src/app.config.ts +++ b/src/app.config.ts @@ -61,7 +61,8 @@ export default defineAppConfig({ 'orderConfirm/index', 'orderConfirmCart/index', 'search/index', - 'shopArticle/index' + 'shopArticle/index', + 'shopArticle/add' ] } ], diff --git a/src/components/FixedButton.tsx b/src/components/FixedButton.tsx new file mode 100644 index 0000000..cf41943 --- /dev/null +++ b/src/components/FixedButton.tsx @@ -0,0 +1,26 @@ +import {View} from '@tarojs/components'; +import {Button} from '@nutui/nutui-react-taro' + +function FixedButton({text, onClick, icon}) { + return ( + <> + {/* 底部安全区域占位 */ + } + + + + + + ) +} + +export default FixedButton; diff --git a/src/shop/shopArticle/add.config.ts b/src/shop/shopArticle/add.config.ts new file mode 100644 index 0000000..5d97955 --- /dev/null +++ b/src/shop/shopArticle/add.config.ts @@ -0,0 +1,4 @@ +export default definePageConfig({ + navigationBarTitleText: '新增收货地址', + navigationBarTextStyle: 'black' +}) diff --git a/src/shop/shopArticle/add.tsx b/src/shop/shopArticle/add.tsx new file mode 100644 index 0000000..0d85f74 --- /dev/null +++ b/src/shop/shopArticle/add.tsx @@ -0,0 +1,323 @@ +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)} + > + {/* 基本信息 */} + + + + + + +