import React, { useState, useEffect } from 'react' import { View, Text, ScrollView, Image, Input } from '@tarojs/components' import Taro from '@tarojs/taro' import { Button } from '@nutui/nutui-react-taro' import { useUser } from '@/hooks/useUser' import { submitGoodsComment } from '@/api/shop/shopGoodsComment' definePageConfig({ navigationBarTitleText: '商品评价', }) const EvaluatePage: React.FC = () => { const { isLoggedIn } = useUser() const { orderId, orderGoodsId, goodsId } = Taro.getCurrentInstance().router?.params || {} const [rating, setRating] = useState(5) const [content, setContent] = useState('') const [images, setImages] = useState([]) const [isAnonymous, setIsAnonymous] = useState(false) useEffect(() => { if (!isLoggedIn) { Taro.navigateTo({ url: '/passport/login' }) } }, [isLoggedIn]) const handleSubmit = async () => { if (!content.trim()) { Taro.showToast({ title: '请输入评价内容', icon: 'none' }) return } if (!orderId || !orderGoodsId || !goodsId) { Taro.showToast({ title: '参数错误', icon: 'none' }) return } Taro.showModal({ title: '提交评价', content: '确定要提交评价吗?', success: async (res) => { if (res.confirm) { try { const submitRes = await submitGoodsComment({ oid: Number(orderId), goodsId: Number(goodsId), goodsScore: rating, serviceScore: rating, comment: content, pics: images.join(','), }) if (submitRes.code === 0) { Taro.showToast({ title: '评价成功', icon: 'success' }) setTimeout(() => Taro.navigateBack(), 1500) } else { Taro.showToast({ title: submitRes.message || '提交失败', icon: 'none' }) } } catch (err: any) { Taro.showToast({ title: err.message || '提交失败', icon: 'none' }) } } }, }) } const handleChooseImage = () => { Taro.chooseImage({ count: 9 - images.length, success: (res) => { setImages([...images, ...res.tempFilePaths]) }, }) } const handleRemoveImage = (index: number) => { const newImages = [...images] newImages.splice(index, 1) setImages(newImages) } if (!isLoggedIn) { return null } return ( {/* 评分 */} 商品评分 {[1, 2, 3, 4, 5].map((star) => ( setRating(star)} > {star <= rating ? '⭐' : '☆'} ))} {/* 评价内容 */} 评价内容 setContent(e.detail.value)} placeholder='请输入评价内容' className='w-full text-sm' style={{ minHeight: '120px' }} /> {/* 上传图片 */} 上传图片 {images.map((img, idx) => ( handleRemoveImage(idx)} > × ))} {images.length < 9 && ( + )} {/* 匿名评价 */} setIsAnonymous(!isAnonymous)} > 匿名评价 {/* 提交按钮 */} ) } export default EvaluatePage