周宾
6 天以前 353446ec9194962845f5824c1ddcf0aebdaf2662
海川开心-修复图片上传问题
已修改1个文件
78 ■■■■ 文件已修改
src/pages/inspectionUpload/index.vue 78 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/pages/inspectionUpload/index.vue
@@ -174,7 +174,7 @@
                <view class="upload-buttons">
                  <u-button 
                    type="primary" 
                    @click="chooseImage"
                    @click="chooseMedia('image')"
                    :loading="uploading"
                    :disabled="getCurrentFiles().length >= uploadConfig.limit"
                    :customStyle="{ marginRight: '10px', flex: 1 }"
@@ -184,12 +184,12 @@
                  </u-button>
                  <u-button 
                    type="success" 
                    @click="chooseVideo"
                    @click="chooseMedia('video')"
                    :loading="uploading"
                    :disabled="getCurrentFiles().length >= uploadConfig.limit"
                    :customStyle="{ flex: 1 }"
                  >
                    <u-icon name="video" size="18" color="#fff" style="margin-right: 5px;"></u-icon>
                    <uni-icons type="videocam" name="videocam" size="18" color="#fff" style="margin-right: 5px;"></uni-icons>
                    {{ uploading ? '上传中...' : '拍视频' }}
                  </u-button>
                </view>
@@ -212,13 +212,13 @@
                  >
                    <view class="file-preview-container">
                      <image 
                        v-if="file.type === 'image'"
                        :src="file.url || file.tempFilePath || file.path"
                        v-if="file?.path?.fileType === 'image'"
                        :src="file?.url || file?.tempFilePath?.tempFilePath || file?.path?.tempFilePath"
                        class="file-preview"
                        mode="aspectFill"
                      />
                      <view v-else class="video-preview">
                        <u-icon name="video" size="24" color="#409eff"></u-icon>
                        <uni-icons type="videocam" name="videocam" size="18" color="#fff" style="margin-right: 5px;"></uni-icons>
                        <text class="video-text">视频</text>
                      </view>
                      
@@ -439,9 +439,9 @@
  if (process.env.VUE_APP_BASE_API) {
    baseUrl = process.env.VUE_APP_BASE_API;
  } else if (process.env.NODE_ENV === 'development') {
    baseUrl = 'http://192.168.1.147:9036';
    baseUrl = 'http://114.132.189.42:9068';
  } else {
    baseUrl = 'http://192.168.1.147:9036';
    baseUrl = 'http://114.132.189.42:9068';
  }
  
  return baseUrl + uploadConfig.action;
@@ -702,8 +702,10 @@
// 获取当前分类的文件列表
const getCurrentFiles = () => {
    console.log('currentUploadType',currentUploadType.value)
  switch (currentUploadType.value) {
    case 'before':
    console.log('beforeModelValue',beforeModelValue.value)
      return beforeModelValue.value
    case 'after':
      return afterModelValue.value
@@ -1007,6 +1009,54 @@
  })
}
// 使用相机
const chooseMedia = (type) => {
    let mediaPamaes={
        count: 1,
        mediaType:[type||'image'],
        sizeType: ['compressed', 'original'],
        sourceType: ['camera'],
    }
    uni.chooseMedia({
      ...mediaPamaes,
      success: (res) => {
        try {
          if (!res.tempFiles || res.tempFiles.length === 0) {
            throw new Error('未获取到图片文件');
          }
          const tempFilePath = res.tempFiles[0];
          const tempFile = res.tempFiles && res.tempFiles[0] ? res.tempFiles[0] : {};
          const file = {
            tempFilePath: tempFilePath,
            path: tempFilePath, // 保持兼容性
            type: 'image',
            name: `photo_${Date.now()}.jpg`,
            size: tempFile.size || 0,
            createTime: new Date().getTime(),
            uid: Date.now() + Math.random()
          };
          handleBeforeUpload(file);
        } catch (error) {
          console.error('处理拍照结果失败:', error);
          uni.showToast({
            title: '处理图片失败',
            icon: 'error'
          });
        }
      },
      fail: (err) => {
        console.error('拍照失败:', err);
        uni.showToast({
          title: '拍照失败: ' + (err.errMsg || '未知错误'),
          icon: 'error'
        });
      }
    })
}
// 拍照
const chooseImage = () => {
  if (uploadFiles.value.length >= uploadConfig.limit) {
@@ -1017,17 +1067,18 @@
    return;
  }
  
  uni.chooseImage({
  uni.chooseMedia({
    count: 1,
    mediaType:['image', 'video'],
    sizeType: ['compressed', 'original'],
    sourceType: ['camera'],
    success: (res) => {
      try {
        if (!res.tempFilePaths || res.tempFilePaths.length === 0) {
        if (!res.tempFiles || res.tempFiles.length === 0) {
          throw new Error('未获取到图片文件');
        }
        
        const tempFilePath = res.tempFilePaths[0];
        const tempFilePath = res.tempFiles[0];
        const tempFile = res.tempFiles && res.tempFiles[0] ? res.tempFiles[0] : {};
        
        const file = {
@@ -1229,7 +1280,7 @@
  number.value++; // 增加上传计数
  
  // 确保文件路径正确
  const filePath = file.tempFilePath || file.path;
  const filePath = file.tempFilePath?.tempFilePath || file.path?.tempFilePath||'';
  if (!filePath) {
    handleUploadError('文件路径不存在');
    return;
@@ -1254,7 +1305,7 @@
      'Authorization': `Bearer ${token}`
    }
  };
  console.log('uploadParams',uploadParams)
  const uploadTask = uni.uploadFile({
    ...uploadParams,
    success: (res) => {
@@ -1334,6 +1385,7 @@
// 上传成功回调
const handleUploadSuccess = (res, file) => {
    console.log(res,file)
  if (res.code === 200 && res.data && Array.isArray(res.data) && res.data.length > 0) {
    const uploadedFile = res.data[0];