123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- /**
- * 文件选择工具函数
- * 提供统一的文件选择接口,包含完整的错误处理和兼容性检查
- */
- /**
- * 选择微信聊天文件
- * @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
- })
- }
|