| | |
| | | name="before" |
| | | multiple |
| | | :maxCount="10" |
| | | :maxSize="1024 * 1024" |
| | | accept="video/*" |
| | | :maxSize="5 * 1024 * 1024" |
| | | accept="image/*" |
| | | :previewFullImage="true" |
| | | ></u-upload> |
| | | </view> |
| | |
| | | name="after" |
| | | multiple |
| | | :maxCount="10" |
| | | :maxSize="1024 * 1024" |
| | | accept="video/*" |
| | | :maxSize="5 * 1024 * 1024" |
| | | accept="image/*" |
| | | :previewFullImage="true" |
| | | ></u-upload> |
| | | </view> |
| | |
| | | name="issue" |
| | | multiple |
| | | :maxCount="10" |
| | | :maxSize="1024 * 1024" |
| | | accept="video/*" |
| | | :maxSize="5 * 1024 * 1024" |
| | | accept="image/*" |
| | | :previewFullImage="true" |
| | | ></u-upload> |
| | | </view> |
| | |
| | | </template> |
| | | |
| | | <script setup> |
| | | import { ref } from 'vue' |
| | | import { ref, computed } from 'vue' |
| | | import { submitInspectionRecord } from '@/api/equipmentManagement/inspection.js' |
| | | import { getToken } from '@/utils/auth' |
| | | import config from '@/config' |
| | | |
| | | const emit = defineEmits(['closeDia']) |
| | | |
| | |
| | | const issueModelValue = ref([]) |
| | | const infoData = ref(null) |
| | | |
| | | // 计算上传URL |
| | | const uploadFileUrl = computed(() => { |
| | | let baseUrl = ''; |
| | | |
| | | if (process.env.VUE_APP_BASE_API) { |
| | | baseUrl = process.env.VUE_APP_BASE_API; |
| | | } else { |
| | | baseUrl = config.baseUrl; |
| | | } |
| | | |
| | | return baseUrl + '/file/upload'; |
| | | }) |
| | | |
| | | const uploadSingleFile = async (fileItem, typeValue) => { |
| | | const token = getToken() |
| | | if (!token) throw new Error('用户未登录') |
| | | |
| | | // H5: u-upload 可能给原生 File(fileItem.file) |
| | | const rawFile = fileItem?.file |
| | | if (rawFile) { |
| | | const formData = new FormData() |
| | | formData.append('file', rawFile, rawFile.name || 'image.jpg') |
| | | formData.append('type', String(typeValue)) |
| | | const res = await fetch(uploadFileUrl.value, { |
| | | method: 'POST', |
| | | headers: { Authorization: 'Bearer ' + token }, |
| | | body: formData |
| | | }) |
| | | const data = await res.json() |
| | | if (data?.code !== 200) throw new Error(data?.msg || '上传失败') |
| | | return { |
| | | url: data?.data?.url, |
| | | name: rawFile.name || 'image.jpg', |
| | | status: 'success' |
| | | } |
| | | } |
| | | |
| | | // 非 H5 / 兼容:走 uni.uploadFile |
| | | return await new Promise((resolve, reject) => { |
| | | uni.uploadFile({ |
| | | url: uploadFileUrl.value, |
| | | filePath: fileItem.url, |
| | | name: 'file', |
| | | header: { |
| | | 'Authorization': `Bearer ${token}` |
| | | }, |
| | | formData: { |
| | | type: typeValue |
| | | }, |
| | | success: (res) => { |
| | | try { |
| | | const data = JSON.parse(res.data) |
| | | if (data.code === 200) { |
| | | resolve({ |
| | | url: data.data.url, |
| | | name: fileItem.name, |
| | | status: 'success' |
| | | }) |
| | | } else { |
| | | reject(new Error(data.msg || '上传失败')) |
| | | } |
| | | } catch (e) { |
| | | reject(e) |
| | | } |
| | | }, |
| | | fail: (err) => reject(err) |
| | | }) |
| | | }) |
| | | } |
| | | |
| | | // 文件上传处理 |
| | | const afterRead = (event) => { |
| | | const { name, file } = event |
| | | |
| | | // 上传文件到服务器 |
| | | uni.uploadFile({ |
| | | url: '/api/upload', // 替换为实际的上传接口 |
| | | filePath: file.url, |
| | | name: 'file', |
| | | success: (res) => { |
| | | const data = JSON.parse(res.data) |
| | | if (data.code === 200) { |
| | | const fileItem = { |
| | | url: data.data.url, |
| | | name: file.name, |
| | | status: 'success' |
| | | } |
| | | |
| | | // 根据name添加到对应的数组 |
| | | if (name === 'before') { |
| | | beforeModelValue.value.push(fileItem) |
| | | } else if (name === 'after') { |
| | | afterModelValue.value.push(fileItem) |
| | | } else if (name === 'issue') { |
| | | issueModelValue.value.push(fileItem) |
| | | } |
| | | |
| | | uni.showToast({ |
| | | title: '上传成功', |
| | | icon: 'success' |
| | | }) |
| | | } else { |
| | | uni.showToast({ |
| | | title: '上传失败', |
| | | icon: 'error' |
| | | }) |
| | | // 根据上传类型设置不同的type值 |
| | | let typeValue = 10 // 默认值 |
| | | if (name === 'before') { |
| | | typeValue = 10 // 生产前 |
| | | } else if (name === 'after') { |
| | | typeValue = 11 // 生产中 |
| | | } else if (name === 'issue') { |
| | | typeValue = 12 // 生产后 |
| | | } |
| | | |
| | | const files = Array.isArray(file) ? file : [file] |
| | | Promise.resolve().then(async () => { |
| | | for (const f of files) { |
| | | const uploaded = await uploadSingleFile(f, typeValue) |
| | | if (name === 'before') { |
| | | beforeModelValue.value.push(uploaded) |
| | | } else if (name === 'after') { |
| | | afterModelValue.value.push(uploaded) |
| | | } else if (name === 'issue') { |
| | | issueModelValue.value.push(uploaded) |
| | | } |
| | | }, |
| | | fail: () => { |
| | | uni.showToast({ |
| | | title: '上传失败', |
| | | icon: 'error' |
| | | }) |
| | | } |
| | | uni.showToast({ title: '上传成功', icon: 'success' }) |
| | | }).catch((err) => { |
| | | console.error('上传失败:', err) |
| | | uni.showToast({ title: '上传失败', icon: 'error' }) |
| | | }) |
| | | } |
| | | |