Files
cz-app/src/pages/personal-center/feedback/index.vue
2026-11-05 13:50:25 +08:00

275 lines
7.5 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<script setup lang="ts">
import { computed, ref } from 'vue'
import { submitFeedback } from '@/api/feedback'
import { uploadRequirementAttachment } from '@/api/requirement'
import { getOssListByIds } from '@/api/oss'
import {
FEEDBACK_ATTACHMENT_MAX_COUNT,
FEEDBACK_DESCRIPTION_MAX_LENGTH,
} from '@/constants/feedback'
import deleteIcon from '@/static/icons/X.png'
import pdfIcon from '@/static/icons/PDF.png'
import pngIcon from '@/static/icons/PNG.png'
import { ensureLoggedIn } from '@/utils/auth-guard'
import { chooseLocalFiles } from '@/utils/choose-local-files'
import { navigateBackWithFallback } from '@/utils/navigate-back'
interface FeedbackAttachmentItem {
id: string
name: string
icon: string
url: string
}
const title = ref('')
const description = ref('')
const attachments = ref<FeedbackAttachmentItem[]>([])
const uploadingAttachment = ref(false)
const submitting = ref(false)
const descLength = computed(() => description.value.length)
function resolveAttachmentIcon(name: string) {
const ext = name.split('.').pop()?.toLowerCase() || ''
if (['png', 'jpg', 'jpeg', 'gif', 'webp', 'bmp'].includes(ext))
return pngIcon
return pdfIcon
}
async function resolveUploadUrl(
uploaded: { url?: string, ossId?: string | number, id?: string | number },
) {
const direct = uploaded.url?.trim()
if (direct)
return direct
const ossId = uploaded.ossId ?? uploaded.id
if (ossId === undefined || ossId === null || ossId === '')
return ''
const files = await getOssListByIds([ossId])
return files[0]?.url?.trim() || ''
}
function handleBack() {
navigateBackWithFallback('/pages/personal-center/index')
}
function handleRemoveAttachment(id: string) {
attachments.value = attachments.value.filter(item => item.id !== id)
}
async function handleAddAttachment() {
if (!ensureLoggedIn())
return
if (uploadingAttachment.value)
return
const remain = FEEDBACK_ATTACHMENT_MAX_COUNT - attachments.value.length
if (remain <= 0) {
uni.showToast({ title: `最多上传${FEEDBACK_ATTACHMENT_MAX_COUNT}个附件`, icon: 'none' })
return
}
try {
const files = await chooseLocalFiles(remain)
if (!files.length)
return
uploadingAttachment.value = true
uni.showLoading({ title: '上传中...', mask: true })
for (const file of files) {
const uploaded = await uploadRequirementAttachment(file.path)
const url = await resolveUploadUrl(uploaded)
if (!url) {
uni.showToast({ title: '上传失败,未返回附件地址', icon: 'none' })
continue
}
const name = uploaded.fileName || file.name
attachments.value.push({
id: String(uploaded.ossId ?? uploaded.id ?? `${Date.now()}-${attachments.value.length}`),
name,
icon: resolveAttachmentIcon(name),
url,
})
}
}
catch (error) {
const message = error instanceof Error ? error.message : '上传失败'
if (!/cancel/i.test(message))
uni.showToast({ title: message, icon: 'none' })
}
finally {
uploadingAttachment.value = false
uni.hideLoading()
}
}
function handleReset() {
title.value = ''
description.value = ''
attachments.value = []
}
async function handleSubmit() {
if (submitting.value || uploadingAttachment.value)
return
if (!ensureLoggedIn())
return
if (!title.value.trim()) {
uni.showToast({ title: '请输入反馈标题', icon: 'none' })
return
}
if (!description.value.trim()) {
uni.showToast({ title: '请详细描述您的问题', icon: 'none' })
return
}
submitting.value = true
uni.showLoading({ title: '提交中...', mask: true })
try {
const urls = attachments.value.map(item => item.url).filter(Boolean)
await submitFeedback({
title: title.value.trim(),
content: description.value.trim(),
// OpenAPI 定义为 string,不能直接传数组,否则会 400「请求参数格式错误」
attachments: urls.length ? JSON.stringify(urls) : undefined,
})
uni.showToast({ title: '提交成功', icon: 'success' })
setTimeout(() => {
uni.navigateBack()
}, 1200)
}
catch {
// 错误提示由 request 统一处理
}
finally {
submitting.value = false
uni.hideLoading()
}
}
</script>
<template>
<view class="feedback">
<view class="feedback__navbar">
<view
class="feedback__back"
@tap="handleBack"
>
<image
class="feedback__back-icon"
src="/static/icons/icon-back.svg"
mode="aspectFit"
/>
</view>
<text class="feedback__navbar-title">
意见反馈
</text>
<view class="feedback__navbar-placeholder" />
</view>
<scroll-view
class="feedback__scroll"
scroll-y
:show-scrollbar="false"
>
<view class="feedback__card">
<input
v-model="title"
class="feedback__input feedback__input--title"
placeholder="请输入反馈标题"
placeholder-class="feedback__placeholder"
>
<view class="feedback__textarea-wrap">
<textarea
v-model="description"
class="feedback__textarea"
placeholder="请详细描述您的问题..."
placeholder-class="feedback__placeholder"
:maxlength="FEEDBACK_DESCRIPTION_MAX_LENGTH"
/>
<text class="feedback__counter">
{{ descLength }}/{{ FEEDBACK_DESCRIPTION_MAX_LENGTH }}字
</text>
</view>
<view class="feedback__field">
<view class="feedback__label-row">
<text class="feedback__label feedback__label--inline">
附件上传
</text>
<text class="feedback__hint">
支持图片/文档/压缩包,最多{{ FEEDBACK_ATTACHMENT_MAX_COUNT }}个
</text>
</view>
<view class="feedback__attachment-row">
<view
v-for="file in attachments"
:key="file.id"
class="feedback__attachment-item"
>
<view
class="feedback__attachment-remove"
@tap="handleRemoveAttachment(file.id)"
>
<image
class="feedback__attachment-remove-icon"
:src="deleteIcon"
mode="aspectFit"
/>
</view>
<image
class="feedback__attachment-icon"
:src="file.icon"
mode="aspectFit"
/>
<text class="feedback__attachment-name">
{{ file.name }}
</text>
</view>
<view
v-if="attachments.length < FEEDBACK_ATTACHMENT_MAX_COUNT"
class="feedback__attachment-add"
@tap="handleAddAttachment"
>
<text class="feedback__attachment-add-icon">
+
</text>
<text class="feedback__attachment-add-text">
添加附件
</text>
</view>
</view>
</view>
</view>
</scroll-view>
<view class="feedback__footer">
<view
class="feedback__footer-btn feedback__footer-btn--primary"
@tap="handleReset"
>
重置
</view>
<view
class="feedback__footer-btn feedback__footer-btn--primary"
@tap="handleSubmit"
>
提交反馈
</view>
</view>
</view>
</template>
<style scoped lang="scss">
@import './index.scss';
</style>