refactor(approval): 使用 wd-upload 组件重构图片上传功能

移除自定义图片上传逻辑,改用 wd-upload 组件实现
简化代码结构,删除冗余的压缩和上传处理逻辑
This commit is contained in:
dzq 2025-11-11 08:33:37 +08:00
parent 3adec4b6e5
commit 95157936af
2 changed files with 27 additions and 178 deletions

3
.gitignore vendored
View File

@ -12,6 +12,8 @@ node_modules
dist
*.local
.claude
# Editor directories and files
.idea
*.suo
@ -23,7 +25,6 @@ dist
.stylelintcache
.eslintcache
.claude
docs/.vitepress/dist
docs/.vitepress/cache

View File

@ -5,6 +5,7 @@ import type { SubmitApprovalRequestData } from '@/api/approval/types'
import { useOrderStore } from '@/pinia/stores/order'
import { useWxStoreOutside } from '@/pinia/stores/wx'
import { getEnvBaseUploadUrl } from '@/utils'
import type { UploadFile } from 'wot-design-uni/components/wd-upload/types'
//
definePage({
@ -46,120 +47,16 @@ watch(orderGoodsId, (newVal) => {
const submitting = ref(false)
//
interface UploadedFile {
url: string
}
const uploadedFiles = ref<UploadedFile[]>([])
// - 使 wd-upload v-model
const fileList = ref<UploadFile[]>([])
const uploading = ref(false)
//
const uploadUrl = `${getEnvBaseUploadUrl()}/file/upload`
//
const chooseImages = () => {
uni.chooseMedia({
count: 3 - uploadedFiles.value.length,
mediaType: ['image'],
sourceType: ['album', 'camera'],
maxDuration: 30,
camera: 'back',
success: (res) => {
console.log('选择图片成功:', res)
uploadImages(res.tempFiles)
},
fail: (err) => {
console.error('选择图片失败:', err)
uni.showToast({
title: '选择图片失败',
icon: 'none',
})
},
})
}
//
// H5使 compressorjs
// File API Canvas使 uni.compressImage
const compressImage = (tempFilePath: string): Promise<string> => {
return new Promise((resolve, reject) => {
uni.compressImage({
src: tempFilePath,
quality: 0.8, // 0-1 compressorjs quality
maxWidth: 1280, // compressorjs maxWidth
maxHeight: 1280, // compressorjs maxHeight
success: (res) => {
console.log('图片压缩成功:', res)
resolve(res.tempFilePath)
},
fail: (err) => {
console.error('图片压缩失败:', err)
// 使
resolve(tempFilePath)
},
})
})
}
//
const uploadImages = (files: UniApp.MediaFile[]) => {
uploading.value = true
const uploadPromises = files.map(async (file) => {
//
const compressedPath = await compressImage(file.tempFilePath)
return new Promise<UploadedFile>((resolve, reject) => {
uni.uploadFile({
url: uploadUrl,
filePath: compressedPath,
name: 'file',
formData: {},
header: {
'Content-Type': 'multipart/form-data',
},
success: (uploadRes) => {
try {
const data = JSON.parse(uploadRes.data)
if (data.code === 0) {
resolve({ url: data.data.url })
} else {
reject(new Error(data.message || '上传失败'))
}
} catch (error) {
reject(new Error('响应解析失败'))
}
},
fail: (err) => {
reject(err)
},
})
})
})
Promise.all(uploadPromises)
.then((results) => {
uploadedFiles.value.push(...results)
uni.showToast({
title: '上传成功',
icon: 'success',
})
})
.catch((error) => {
console.error('上传失败:', error)
uni.showToast({
title: error.message || '上传失败',
icon: 'none',
})
})
.finally(() => {
uploading.value = false
})
}
//
const deleteImage = (index: number) => {
uploadedFiles.value.splice(index, 1)
//
const beforeUpload = ({ files, resolve }: { files: Record<string, any>[], resolve: (isPass: boolean) => void }) => {
resolve(true)
}
//
@ -185,18 +82,11 @@ const validateForm = () => {
const handleSubmit = async () => {
if (!validateForm()) return
// chooseImages
if (uploadedFiles.value.length === 0) {
uni.showModal({
title: '提示',
content: '请先上传凭证图片',
confirmText: '选择图片',
cancelText: '取消',
success: (res) => {
if (res.confirm) {
chooseImages()
}
},
//
if (fileList.value.length === 0) {
uni.showToast({
title: '请先上传凭证图片',
icon: 'none',
})
return
}
@ -204,7 +94,7 @@ const handleSubmit = async () => {
submitting.value = true
try {
// URL
formData.value.returnImages = uploadedFiles.value.map(item => item.url).join(',')
formData.value.returnImages = fileList.value.map(item => item.url).join(',')
formData.value.corpid = wxStore.corpid
formData.value.applyUserid = wxStore.userid
@ -265,34 +155,24 @@ const handleSubmit = async () => {
<wd-cell-group custom-class="upload-section">
<wd-cell title="凭证图片" value="">
<template #extra>
<view class="upload-area">
<view
v-for="(file, index) in uploadedFiles"
:key="index"
class="uploaded-image"
>
<image
:src="file.url"
mode="aspectFill"
class="image-preview"
/>
<view class="delete-btn" @tap="() => deleteImage(index)">
<wd-icon name="delete" size="16px" color="#fff" />
</view>
</view>
<view
v-if="uploadedFiles.length < 3"
class="upload-button"
@tap="chooseImages"
<wd-upload
v-model:file-list="fileList"
:action="uploadUrl"
:limit="3"
image-mode="aspectFill"
:before-upload="beforeUpload"
accept="image"
multiple
>
<template #default>
<view class="upload-button">
<wd-icon name="camera" size="32px" color="#ccc" />
<text class="upload-text">
{{ uploading ? '上传中...' : '点击上传' }}
</text>
</view>
</view>
</template>
</wd-upload>
</wd-cell>
</wd-cell-group>
@ -326,38 +206,6 @@ const handleSubmit = async () => {
margin: 20rpx 0;
}
.upload-area {
display: flex;
flex-wrap: wrap;
gap: 16rpx;
}
.uploaded-image {
position: relative;
width: 160rpx;
height: 160rpx;
border-radius: 8rpx;
overflow: hidden;
}
.image-preview {
width: 100%;
height: 100%;
}
.delete-btn {
position: absolute;
top: 8rpx;
right: 8rpx;
width: 40rpx;
height: 40rpx;
background-color: rgba(0, 0, 0, 0.5);
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
}
.upload-button {
width: 160rpx;
height: 160rpx;