filePicker.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /**
  2. * 文件选择工具函数
  3. * 提供统一的文件选择接口,包含完整的错误处理和兼容性检查
  4. */
  5. /**
  6. * 选择微信聊天文件
  7. * @param {Object} options 配置选项
  8. * @param {number} options.count 最多可以选择的文件个数,默认1
  9. * @param {string} options.type 文件类型,默认'file'
  10. * @param {Array} options.allowedTypes 允许的文件类型,默认['pdf', 'docx', 'doc']
  11. * @param {number} options.maxSize 最大文件大小(MB),默认20
  12. * @returns {Promise} 返回选择的文件信息
  13. */
  14. export function chooseMessageFile(options = {}) {
  15. const {
  16. count = 1,
  17. type = 'file',
  18. allowedTypes = ['pdf', 'docx', 'doc'],
  19. maxSize = 20
  20. } = options
  21. return new Promise((resolve, reject) => {
  22. // 检查API是否可用
  23. if (typeof wx === 'undefined' || typeof wx.chooseMessageFile !== 'function') {
  24. const error = new Error('当前环境不支持文件选择功能')
  25. error.code = 'API_NOT_AVAILABLE'
  26. reject(error)
  27. return
  28. }
  29. console.log('开始调用wx.chooseMessageFile')
  30. wx.chooseMessageFile({
  31. count,
  32. type,
  33. success(res) {
  34. console.log('文件选择成功:', res)
  35. // 检查返回结果
  36. if (!res || !res.tempFiles || !res.tempFiles.length) {
  37. const error = new Error('文件选择失败,请重试')
  38. error.code = 'NO_FILES_SELECTED'
  39. reject(error)
  40. return
  41. }
  42. const file = res.tempFiles[0]
  43. // 检查文件大小
  44. if (file.size / (1024 * 1024) > maxSize) {
  45. const error = new Error(`文件大小不能超过${maxSize}M`)
  46. error.code = 'FILE_TOO_LARGE'
  47. reject(error)
  48. return
  49. }
  50. // 检查文件类型
  51. const fileExtension = file.name.split('.').pop().toLowerCase()
  52. if (!allowedTypes.includes(fileExtension)) {
  53. const error = new Error(`请上传${allowedTypes.join('、')}类型的文件`)
  54. error.code = 'INVALID_FILE_TYPE'
  55. reject(error)
  56. return
  57. }
  58. resolve({
  59. name: file.name,
  60. path: file.path,
  61. size: file.size,
  62. extension: fileExtension
  63. })
  64. },
  65. fail(error) {
  66. console.error('文件选择失败:', error)
  67. const err = new Error('文件选择失败,请检查权限设置或重试')
  68. err.code = 'CHOOSE_FILE_FAILED'
  69. err.originalError = error
  70. reject(err)
  71. }
  72. })
  73. })
  74. }
  75. /**
  76. * 上传文件并保存简历
  77. * @param {Object} fileInfo 文件信息
  78. * @param {string} fileInfo.name 文件名
  79. * @param {string} fileInfo.path 文件路径
  80. * @param {Function} uploadFileFunc 上传文件函数
  81. * @param {Function} saveResumeFunc 保存简历函数
  82. * @returns {Promise} 返回上传结果
  83. */
  84. export async function uploadAndSaveResume(fileInfo, uploadFileFunc, saveResumeFunc) {
  85. try {
  86. // 显示上传进度
  87. uni.showLoading({
  88. title: '上传中...'
  89. })
  90. const uploadResult = await uploadFileFunc(fileInfo.path, 'attachment')
  91. if (!uploadResult.data) {
  92. throw new Error('上传失败')
  93. }
  94. await saveResumeFunc({
  95. title: fileInfo.name,
  96. url: uploadResult.data
  97. })
  98. uni.hideLoading()
  99. uni.showToast({
  100. title: '上传成功',
  101. icon: 'success'
  102. })
  103. return {
  104. success: true,
  105. data: uploadResult.data
  106. }
  107. } catch (error) {
  108. uni.hideLoading()
  109. console.error('上传文件失败:', error)
  110. uni.showToast({
  111. title: error.message || '上传失败,请重试',
  112. icon: 'none',
  113. duration: 2000
  114. })
  115. throw error
  116. }
  117. }
  118. /**
  119. * 处理文件选择错误
  120. * @param {Error} error 错误对象
  121. */
  122. export function handleFilePickerError(error) {
  123. let message = '文件选择失败'
  124. switch (error.code) {
  125. case 'API_NOT_AVAILABLE':
  126. message = '当前环境不支持文件选择功能'
  127. break
  128. case 'NO_FILES_SELECTED':
  129. message = '文件选择失败,请重试'
  130. break
  131. case 'FILE_TOO_LARGE':
  132. message = error.message
  133. break
  134. case 'INVALID_FILE_TYPE':
  135. message = error.message
  136. break
  137. case 'CHOOSE_FILE_FAILED':
  138. message = '文件选择失败,请检查权限设置或重试'
  139. break
  140. default:
  141. message = error.message || '文件选择失败'
  142. }
  143. uni.showToast({
  144. icon: 'none',
  145. title: message,
  146. duration: 3000
  147. })
  148. }