Bladeren bron

文件上传提示

Xiao_123 1 dag geleden
bovenliggende
commit
fb3f367ecf
4 gewijzigde bestanden met toevoegingen van 277 en 42 verwijderingen
  1. 15 41
      pagesA/chart/index.vue
  2. 46 0
      pagesA/resume/index.vue
  3. 168 0
      pagesA/utils/filePicker.js
  4. 48 1
      pagesB/positionDetail/index.vue

+ 15 - 41
pagesA/chart/index.vue

@@ -268,6 +268,7 @@ import { formatName } from '@/utils/getText'
 import { preview } from '@/utils/preview'
 import { getPersonResumeCv, saveResume } from '@/api/user'
 import { uploadFile } from '@/api/file'
+import { chooseMessageFile, uploadAndSaveResume, handleFilePickerError } from '@/pagesA/utils/filePicker'
 import { getInterviewInviteListByInviteUserId, getMessageType } from '@/api/common'
 // import { userInterviewInviteReject } from '@/api/personalCenter'
 import {
@@ -595,47 +596,20 @@ async function handleMore () {
 }
 
 // 上传简历
-function handleUploadResume () {
-  wx.chooseMessageFile({
-    count: 1,
-    type: 'file',
-    success (res) {
-      // 限制文件上传大小
-      const size = res.tempFiles[0].size
-      if (size / (1024*1024) > 20) {
-        uni.showToast({ icon: 'none', title: '文件大小不能超过20M' })
-        return
-      }
-      
-      const title = res.tempFiles[0].name
-      const path = res.tempFiles[0].path
-      const test = /\.(pdf|docx|doc)$/.test(title)
-      if (!test) {
-        uni.showToast({
-          icon: 'none',
-          title: '请上传pdf、doc、docx类型的文件',
-          duration: 2000
-        })
-        return
-      }
-      //效验是否为支持的文件格式
-      uploadFile(path, 'attachment').then(async (res) => {
-        if (!res.data) {
-          uni.showToast({
-            title: '上传失败',
-            icon: 'none'
-          })
-          return
-        }
-        await saveResume({ title, url: res.data })
-        uni.showToast({
-          title: '上传成功',
-          icon: 'success'
-        })
-        handleFindResume()
-      })
-    }
-  })
+async function handleUploadResume () {
+  try {
+    const fileInfo = await chooseMessageFile({
+      count: 1,
+      type: 'file',
+      allowedTypes: ['pdf', 'docx', 'doc'],
+      maxSize: 20
+    })
+    
+    await uploadAndSaveResume(fileInfo, uploadFile, saveResume)
+    handleFindResume()
+  } catch (error) {
+    handleFilePickerError(error)
+  }
 }
 
 // 获取职位信息

+ 46 - 0
pagesA/resume/index.vue

@@ -91,10 +91,34 @@ const handleUpload = () => {
     })
     return
   }
+  // 检查API是否可用
+  if (typeof wx === 'undefined' || typeof wx.chooseMessageFile !== 'function') {
+    uni.showToast({
+      icon: 'none',
+      title: '当前环境不支持文件选择功能',
+      duration: 3000
+    })
+    return
+  }
+
+  console.log('开始调用wx.chooseMessageFile')
+  
   wx.chooseMessageFile({
     count: 1,
     type: 'file',
     success (res) {
+      console.log('文件选择成功:', res)
+      
+      // 检查返回结果
+      if (!res || !res.tempFiles || !res.tempFiles.length) {
+        uni.showToast({
+          icon: 'none',
+          title: '文件选择失败,请重试',
+          duration: 2000
+        })
+        return
+      }
+
       // 限制文件上传大小
       const size = res.tempFiles[0].size
       if (size / (1024*1024) > 20) {
@@ -106,7 +130,13 @@ const handleUpload = () => {
       const path = res.tempFiles[0].path
       //效验是否为支持的文件格式
       if(/\.(pdf|docx|doc)$/.test(title)){
+        // 显示上传进度
+        uni.showLoading({
+          title: '上传中...'
+        })
+        
         uploadFile(path, 'attachment').then(async (res) => {
+          uni.hideLoading()
           if (!res.data) {
             uni.showToast({
               title: '上传失败',
@@ -120,6 +150,14 @@ const handleUpload = () => {
             icon: 'success'
           })
           getList()
+        }).catch(error => {
+          uni.hideLoading()
+          console.error('上传文件失败:', error)
+          uni.showToast({
+            title: '上传失败,请重试',
+            icon: 'none',
+            duration: 2000
+          })
         })
       }else{
         uni.showToast({
@@ -129,6 +167,14 @@ const handleUpload = () => {
         })
         return
       }
+    },
+    fail (error) {
+      console.error('文件选择失败:', error)
+      uni.showToast({
+        icon: 'none',
+        title: '文件选择失败,请检查权限设置或重试',
+        duration: 3000
+      })
     }
   })
 }

+ 168 - 0
pagesA/utils/filePicker.js

@@ -0,0 +1,168 @@
+/**
+ * 文件选择工具函数
+ * 提供统一的文件选择接口,包含完整的错误处理和兼容性检查
+ */
+
+/**
+ * 选择微信聊天文件
+ * @param {Object} options 配置选项
+ * @param {number} options.count 最多可以选择的文件个数,默认1
+ * @param {string} options.type 文件类型,默认'file'
+ * @param {Array} options.allowedTypes 允许的文件类型,默认['pdf', 'docx', 'doc']
+ * @param {number} options.maxSize 最大文件大小(MB),默认20
+ * @returns {Promise} 返回选择的文件信息
+ */
+export function chooseMessageFile(options = {}) {
+  const {
+    count = 1,
+    type = 'file',
+    allowedTypes = ['pdf', 'docx', 'doc'],
+    maxSize = 20
+  } = options
+
+  return new Promise((resolve, reject) => {
+    // 检查API是否可用
+    if (typeof wx === 'undefined' || typeof wx.chooseMessageFile !== 'function') {
+      const error = new Error('当前环境不支持文件选择功能')
+      error.code = 'API_NOT_AVAILABLE'
+      reject(error)
+      return
+    }
+
+    console.log('开始调用wx.chooseMessageFile')
+    
+    wx.chooseMessageFile({
+      count,
+      type,
+      success(res) {
+        console.log('文件选择成功:', res)
+        
+        // 检查返回结果
+        if (!res || !res.tempFiles || !res.tempFiles.length) {
+          const error = new Error('文件选择失败,请重试')
+          error.code = 'NO_FILES_SELECTED'
+          reject(error)
+          return
+        }
+
+        const file = res.tempFiles[0]
+        
+        // 检查文件大小
+        if (file.size / (1024 * 1024) > maxSize) {
+          const error = new Error(`文件大小不能超过${maxSize}M`)
+          error.code = 'FILE_TOO_LARGE'
+          reject(error)
+          return
+        }
+
+        // 检查文件类型
+        const fileExtension = file.name.split('.').pop().toLowerCase()
+        if (!allowedTypes.includes(fileExtension)) {
+          const error = new Error(`请上传${allowedTypes.join('、')}类型的文件`)
+          error.code = 'INVALID_FILE_TYPE'
+          reject(error)
+          return
+        }
+
+        resolve({
+          name: file.name,
+          path: file.path,
+          size: file.size,
+          extension: fileExtension
+        })
+      },
+      fail(error) {
+        console.error('文件选择失败:', error)
+        const err = new Error('文件选择失败,请检查权限设置或重试')
+        err.code = 'CHOOSE_FILE_FAILED'
+        err.originalError = error
+        reject(err)
+      }
+    })
+  })
+}
+
+/**
+ * 上传文件并保存简历
+ * @param {Object} fileInfo 文件信息
+ * @param {string} fileInfo.name 文件名
+ * @param {string} fileInfo.path 文件路径
+ * @param {Function} uploadFileFunc 上传文件函数
+ * @param {Function} saveResumeFunc 保存简历函数
+ * @returns {Promise} 返回上传结果
+ */
+export async function uploadAndSaveResume(fileInfo, uploadFileFunc, saveResumeFunc) {
+  try {
+    // 显示上传进度
+    uni.showLoading({
+      title: '上传中...'
+    })
+    
+    const uploadResult = await uploadFileFunc(fileInfo.path, 'attachment')
+    
+    if (!uploadResult.data) {
+      throw new Error('上传失败')
+    }
+    
+    await saveResumeFunc({ 
+      title: fileInfo.name, 
+      url: uploadResult.data 
+    })
+    
+    uni.hideLoading()
+    uni.showToast({
+      title: '上传成功',
+      icon: 'success'
+    })
+    
+    return {
+      success: true,
+      data: uploadResult.data
+    }
+  } catch (error) {
+    uni.hideLoading()
+    console.error('上传文件失败:', error)
+    
+    uni.showToast({
+      title: error.message || '上传失败,请重试',
+      icon: 'none',
+      duration: 2000
+    })
+    
+    throw error
+  }
+}
+
+/**
+ * 处理文件选择错误
+ * @param {Error} error 错误对象
+ */
+export function handleFilePickerError(error) {
+  let message = '文件选择失败'
+  
+  switch (error.code) {
+    case 'API_NOT_AVAILABLE': 
+      message = '当前环境不支持文件选择功能'
+      break
+    case 'NO_FILES_SELECTED':
+      message = '文件选择失败,请重试'
+      break
+    case 'FILE_TOO_LARGE':
+      message = error.message
+      break
+    case 'INVALID_FILE_TYPE':
+      message = error.message
+      break
+    case 'CHOOSE_FILE_FAILED':
+      message = '文件选择失败,请检查权限设置或重试'
+      break
+    default:
+      message = error.message || '文件选择失败'
+  }
+  
+  uni.showToast({
+    icon: 'none',
+    title: message,
+    duration: 3000
+  })
+}

+ 48 - 1
pagesB/positionDetail/index.vue

@@ -607,11 +607,16 @@ const handleDelivery = async () => {
       console.log(res, 'uni.requestSubscribeMessage-res')
       if (res[tmplIds[0]] === 'accept' || res[tmplIds[1]] === 'accept') {
         console.log('订阅成功', res)
-        handleOpen()
+        // handleOpen()
+      } else {
+        console.log('用户未同意订阅或已拒绝', res)
       }
     },
     fail:(err)=>{
       console.log('订阅失败', err)
+    },
+    complete:()=>{
+      // 无论用户是否接受订阅或调用是否失败,都继续弹出投递弹窗,避免“无反应”
       handleOpen()
     }
   })
@@ -721,10 +726,30 @@ const handleCollection = async () => {
 // 选取微信聊天文件
 // 上传附件
 const handleUpload = () => {
+  // 检查API是否可用
+  if (typeof wx === 'undefined' || typeof wx.chooseMessageFile !== 'function') {
+    uni.showToast({
+      icon: 'none',
+      title: '当前环境不支持文件选择功能',
+      duration: 3000
+    })
+    return
+  }
+
+  console.log('开始调用wx.chooseMessageFile')
+  
   wx.chooseMessageFile({
     count: 1,
     type: 'file',
     success (res) {
+      console.log('文件选择成功:', res)
+      
+      // 检查返回结果
+      if (!res || !res.tempFiles || !res.tempFiles.length) {
+        uni.showToast({ icon: 'none', title: '文件选择失败,请稍后再试', duration: 2000 })
+        return
+      }
+
       // 限制文件上传大小
       const size = res.tempFiles[0]?.size
       if (!size) return uni.showToast({ icon: 'none', title: '文件选择失败,请稍后再试', duration: 2000 })
@@ -737,7 +762,13 @@ const handleUpload = () => {
       const path = res.tempFiles[0].path
       //效验是否为支持的文件格式
       if(/\.(pdf|docx|doc)$/.test(title)){
+        // 显示上传进度
+        uni.showLoading({
+          title: '上传中...'
+        })
+        
         uploadFile(path, 'attachment').then(async (res) => {
+          uni.hideLoading()
           if (!res.data) {
             uni.showToast({
               title: '上传失败',
@@ -753,6 +784,14 @@ const handleUpload = () => {
             duration: 2000
           })
           deliverySubmit({ title, url: res.data })
+        }).catch(error => {
+          uni.hideLoading()
+          console.error('上传文件失败:', error)
+          uni.showToast({
+            title: '上传失败,请重试',
+            icon: 'none',
+            duration: 2000
+          })
         })
       }else{
         uni.showToast({
@@ -762,6 +801,14 @@ const handleUpload = () => {
         })
         return
       }
+    },
+    fail (error) {
+      console.error('文件选择失败:', error)
+      uni.showToast({
+        icon: 'none',
+        title: '文件选择失败,请检查权限设置或重试',
+        duration: 3000
+      })
     }
   })
 }