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 dist
*.local *.local
.claude
# Editor directories and files # Editor directories and files
.idea .idea
*.suo *.suo
@ -23,7 +25,6 @@ dist
.stylelintcache .stylelintcache
.eslintcache .eslintcache
.claude
docs/.vitepress/dist docs/.vitepress/dist
docs/.vitepress/cache docs/.vitepress/cache

View File

@ -5,6 +5,7 @@ import type { SubmitApprovalRequestData } from '@/api/approval/types'
import { useOrderStore } from '@/pinia/stores/order' import { useOrderStore } from '@/pinia/stores/order'
import { useWxStoreOutside } from '@/pinia/stores/wx' import { useWxStoreOutside } from '@/pinia/stores/wx'
import { getEnvBaseUploadUrl } from '@/utils' import { getEnvBaseUploadUrl } from '@/utils'
import type { UploadFile } from 'wot-design-uni/components/wd-upload/types'
// //
definePage({ definePage({
@ -46,120 +47,16 @@ watch(orderGoodsId, (newVal) => {
const submitting = ref(false) const submitting = ref(false)
// // - 使 wd-upload v-model
interface UploadedFile { const fileList = ref<UploadFile[]>([])
url: string
}
const uploadedFiles = ref<UploadedFile[]>([])
const uploading = ref(false) const uploading = ref(false)
// //
const uploadUrl = `${getEnvBaseUploadUrl()}/file/upload` const uploadUrl = `${getEnvBaseUploadUrl()}/file/upload`
// //
const chooseImages = () => { const beforeUpload = ({ files, resolve }: { files: Record<string, any>[], resolve: (isPass: boolean) => void }) => {
uni.chooseMedia({ resolve(true)
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)
} }
// //
@ -185,18 +82,11 @@ const validateForm = () => {
const handleSubmit = async () => { const handleSubmit = async () => {
if (!validateForm()) return if (!validateForm()) return
// chooseImages //
if (uploadedFiles.value.length === 0) { if (fileList.value.length === 0) {
uni.showModal({ uni.showToast({
title: '提示', title: '请先上传凭证图片',
content: '请先上传凭证图片', icon: 'none',
confirmText: '选择图片',
cancelText: '取消',
success: (res) => {
if (res.confirm) {
chooseImages()
}
},
}) })
return return
} }
@ -204,7 +94,7 @@ const handleSubmit = async () => {
submitting.value = true submitting.value = true
try { try {
// URL // 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.corpid = wxStore.corpid
formData.value.applyUserid = wxStore.userid formData.value.applyUserid = wxStore.userid
@ -265,34 +155,24 @@ const handleSubmit = async () => {
<wd-cell-group custom-class="upload-section"> <wd-cell-group custom-class="upload-section">
<wd-cell title="凭证图片" value=""> <wd-cell title="凭证图片" value="">
<template #extra> <wd-upload
<view class="upload-area"> v-model:file-list="fileList"
<view :action="uploadUrl"
v-for="(file, index) in uploadedFiles" :limit="3"
:key="index" image-mode="aspectFill"
class="uploaded-image" :before-upload="beforeUpload"
> accept="image"
<image multiple
:src="file.url" >
mode="aspectFill" <template #default>
class="image-preview" <view class="upload-button">
/>
<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-icon name="camera" size="32px" color="#ccc" /> <wd-icon name="camera" size="32px" color="#ccc" />
<text class="upload-text"> <text class="upload-text">
{{ uploading ? '上传中...' : '点击上传' }} {{ uploading ? '上传中...' : '点击上传' }}
</text> </text>
</view> </view>
</view> </template>
</template> </wd-upload>
</wd-cell> </wd-cell>
</wd-cell-group> </wd-cell-group>
@ -326,38 +206,6 @@ const handleSubmit = async () => {
margin: 20rpx 0; 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 { .upload-button {
width: 160rpx; width: 160rpx;
height: 160rpx; height: 160rpx;