upload.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. export default {
  2. data() {
  3. return {
  4. uploadList: []
  5. }
  6. },
  7. computed: {
  8. },
  9. methods: {
  10. // 处理文件上传
  11. handleUpload() {
  12. // #ifdef H5
  13. this.handleH5Upload();
  14. // #endif
  15. // #ifdef APP-PLUS
  16. this.handleAppUpload();
  17. // #endif
  18. },
  19. // H5 平台文件上传
  20. handleH5Upload() {
  21. console.log("H5 平台文件上传")
  22. uni.chooseFile({
  23. count: 100, // 最多可以选择100个文件
  24. type: 'all', // 选择所有类型文件
  25. success: async (res) => {
  26. const { tempFilePaths, tempFiles } = res;
  27. if (!tempFiles || tempFiles.length === 0) {
  28. return;
  29. }
  30. // 验证文件大小和类型
  31. const validFiles = [];
  32. for (let i = 0; i < tempFiles.length; i++) {
  33. const filePath = tempFilePaths[i];
  34. validFiles.push(filePath);
  35. }
  36. if (validFiles.length === 0) {
  37. return;
  38. }
  39. try {
  40. console.log("上传文件成功:1", validFiles)
  41. await Promise.all(validFiles.map(filePath => this.uploadFile(filePath)));
  42. this.handleUploadSuccess();
  43. } catch (error) {
  44. console.error('上传失败:', error);
  45. uni.$u.toast('上传失败');
  46. }
  47. },
  48. fail: (err) => {
  49. console.error('选择文件失败:', err);
  50. uni.$u.toast('选择文件失败');
  51. }
  52. });
  53. },
  54. // 上传文件
  55. async uploadFile(fileUrl) {
  56. try {
  57. uni.showLoading({
  58. title: '上传中...',
  59. mask: true
  60. });
  61. // 调用统一的上传接口
  62. const {
  63. data
  64. } = await uni.$u.api.uploadFile(fileUrl);
  65. const fileObj = {
  66. fileSize: data.fileSize,
  67. fileSuffix: data.fileSuffix,
  68. fileName: data.name,
  69. fileUrl: data.url,
  70. orderFileType: this.orderFileType
  71. };
  72. this.uploadList.push(fileObj);
  73. this.$emit('update:fileList', this.uploadList);
  74. this.$emit('change', this.uploadList);
  75. uni.hideLoading();
  76. } catch (error) {
  77. uni.hideLoading();
  78. console.error('文件上传失败:', error);
  79. uni.$u.toast('上传失败,请重试');
  80. }
  81. },
  82. // App 平台文件上传
  83. handleAppUpload() {
  84. const lemonjkFileSelect = uni.requireNativePlugin('lemonjk-FileSelect');
  85. //4.0.0+ 使用示例及高级筛选器配置示例
  86. lemonjkFileSelect.showNativePicker({
  87. pathScope: "/DCIM/Camera",
  88. mimeType: "*/*",
  89. utisType: "public.data",
  90. multi: 'yes'
  91. }, async result => {
  92. const {
  93. code,
  94. files
  95. } = result;
  96. if (code == 0) {
  97. // 选择的是否全部为图片
  98. const tempFilePaths = files.map(v => "file://" + v.filePath);
  99. Promise.all(tempFilePaths.map(v => this.uploadFile(v))).then(() => {
  100. this.handleUploadSuccess();
  101. }).catch(() => {
  102. uni.$u.toast("上传失败");
  103. })
  104. } else if (result.code == 1001) {
  105. uni.showModal({
  106. title: "需要文件访问权限",
  107. content: "您还未授权本应用读取文件。为保证您可以正常上传文件,请在权限设置页面打开文件访问权限(不同手机厂商表述可能略有差异)请根据自己手机品牌设置",
  108. confirmText: "去授权",
  109. cancelText: "算了",
  110. success(e) {
  111. if (e.confirm) {
  112. // 跳转到应用设置页
  113. lemonjkFileSelect.gotoSetting();
  114. }
  115. }
  116. })
  117. }
  118. })
  119. },
  120. },
  121. }