|
|
<template>
|
|
|
<view class="survey-files">
|
|
|
<view v-for="(file, index) in files" :key="file.id" class="file-row">
|
|
|
<view class="file-info"><view class="filename">{{ file.original_name }}</view><text class="file-size">{{ sizeLabel(file.size) }}</text></view>
|
|
|
<text v-if="!disabled && !uploading" class="remove" @click="remove(index)">移除</text>
|
|
|
</view>
|
|
|
<button v-if="files.length < limit" class="upload-button" :disabled="disabled || uploading" @click="choose">{{ uploading ? '上传中…' : '选择文件' }}</button>
|
|
|
<view class="hint">最多 {{ limit }} 个文件。允许格式:{{ extensions.join('、').toUpperCase() }}。</view>
|
|
|
</view>
|
|
|
</template>
|
|
|
<script>
|
|
|
import { ROOTPATH } from '@/common/config.js'
|
|
|
export default {
|
|
|
props: { questionId: [Number, String], extensions: { type: Array, default: () => ['zip', 'rar', 'ppt', 'pptx', 'xls', 'xlsx', 'doc', 'docx', 'png', 'gif', 'jpg', 'jpeg', 'pdf', 'mp4'] }, value: { default: () => [] }, limit: { type: Number, default: 1 }, disabled: Boolean },
|
|
|
data() { return { uploading: false } },
|
|
|
computed: { files() { return Array.isArray(this.value) ? this.value : [] } },
|
|
|
methods: {
|
|
|
sizeLabel(size) { return Number(size) >= 1048576 ? (size / 1048576).toFixed(1) + ' MB' : Math.ceil(Number(size || 0) / 1024) + ' KB' },
|
|
|
remove(index) { if (!this.disabled && !this.uploading) this.$emit('input', this.files.filter((_, i) => i !== index)) },
|
|
|
choose() {
|
|
|
if (this.disabled || this.uploading) return
|
|
|
const count = this.limit - this.files.length
|
|
|
uni.showActionSheet({ itemList: this.extensions.some(ext => ['jpg', 'jpeg', 'png', 'gif'].includes(ext)) ? ['从微信聊天选择文件', '从相册或拍照选择图片'] : ['从微信聊天选择文件'], success: res => {
|
|
|
if (res.tapIndex === 0) {
|
|
|
wx.chooseMessageFile({ count, type: 'all', success: result => this.upload(result.tempFiles) })
|
|
|
} else {
|
|
|
uni.chooseImage({ count, success: result => this.upload(result.tempFiles.map(file => ({ ...file, name: file.path.split('/').pop() }))) })
|
|
|
}
|
|
|
} })
|
|
|
},
|
|
|
async upload(selected) {
|
|
|
if (this.disabled || this.uploading) return
|
|
|
this.uploading = true
|
|
|
this.$emit('uploading', true)
|
|
|
const files = this.files.slice()
|
|
|
try {
|
|
|
for (const file of selected.slice(0, this.limit - files.length)) {
|
|
|
const extension = (file.name || file.path).split('.').pop().toLowerCase()
|
|
|
if (!this.extensions.includes(extension)) throw new Error('只允许上传 ' + this.extensions.join('、') + ' 格式')
|
|
|
if (file.size >= 50 * 1024 * 1024) throw new Error('文件必须小于50MB')
|
|
|
const token = (uni.getStorageSync('stbc1_lifeData') || {}).vuex_token || this.vuex_token
|
|
|
const data = await new Promise((resolve, reject) => uni.uploadFile({
|
|
|
url: ROOTPATH + '/api/mobile/upload-file', filePath: file.path, name: 'file',
|
|
|
header: { Authorization: 'Bearer ' + token }, formData: { survey_question_id: this.questionId || '', original_name: file.name || file.path.split('/').pop() },
|
|
|
success: res => {
|
|
|
try {
|
|
|
const data = JSON.parse(res.data)
|
|
|
if (res.statusCode !== 200 || !data.id || !data.url) throw new Error(data.errmsg || '上传失败,请重试')
|
|
|
resolve(data)
|
|
|
} catch (e) { reject(e) }
|
|
|
}, fail: () => reject(new Error('上传失败,请检查网络后重试'))
|
|
|
}))
|
|
|
files.push(data)
|
|
|
this.$emit('input', files.slice())
|
|
|
}
|
|
|
} catch (e) { uni.showToast({ title: e.message || '上传失败,请重试', icon: 'none' }) }
|
|
|
finally { this.uploading = false; this.$emit('uploading', false) }
|
|
|
},
|
|
|
|
|
|
}
|
|
|
}
|
|
|
</script>
|
|
|
<style scoped>
|
|
|
.file-row { display: flex; align-items: center; gap: 20rpx; padding: 20rpx 0; border-bottom: 1px solid #eee; }
|
|
|
.file-info { flex: 1; min-width: 0; }
|
|
|
.filename { color: #2018a0; font-size: 28rpx; word-break: break-all; }
|
|
|
.file-size, .hint { color: #909399; font-size: 24rpx; line-height: 1.6; }
|
|
|
.remove { color: #909399; font-size: 26rpx; flex-shrink: 0; }
|
|
|
.upload-button { margin: 20rpx 0; color: #2018a0; background: #f4f3ff; font-size: 28rpx; }
|
|
|
</style>
|