index.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <template>
  2. <web-view
  3. ref="webViewRef"
  4. :update-title="false"
  5. :webview-styles="webViewStyle"
  6. :style="webViewStyle"
  7. :src="`/hybrid/html/select-file/index.html?accept=${accept}`"
  8. name="selectFileIframe"
  9. @message="handleMessage"
  10. />
  11. </template>
  12. <script setup>
  13. import {ref, onMounted, getCurrentInstance} from "vue";
  14. import {onUnload} from '@dcloudio/uni-app'
  15. //初始变量
  16. let wv;
  17. const webViewStyle = ref({
  18. height: '1px',
  19. width: '1px',
  20. top: '300px',
  21. })
  22. const props = defineProps({
  23. accept: {
  24. type: String,
  25. default: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.ms-excel,application/pdf,.doc,.docx,application/msword'
  26. }
  27. });
  28. const webViewRef = ref(null)
  29. //事件
  30. const emit = defineEmits(['change'])
  31. //渲染完成
  32. onMounted(() => {
  33. // #ifdef H5
  34. window.addEventListener('message', handleMessage);
  35. // #endif
  36. initWebview()
  37. })
  38. //初始化webview
  39. const initWebview = () => {
  40. // #ifdef APP-PLUS
  41. const instance = getCurrentInstance().proxy.$parent
  42. let currentWebview = instance.$getAppWebview()
  43. //如果是页面初始化调用时,需要延时一下
  44. setTimeout(() => {
  45. wv = currentWebview.children()[0]
  46. // #ifdef APP-PLUS
  47. //ios 禁用缓存,测试生效!!
  48. let cache1 = plus.ios.newObject('NSURLCache');
  49. let cache = plus.ios.invoke(cache1, 'sharedURLCache');
  50. plus.ios.invoke(cache, 'removeAllCachedResponses');
  51. plus.ios.invoke(cache, 'setDiskCapacity:', 0);
  52. plus.ios.invoke(cache, 'setMemoryCapacity:', 0);
  53. //安卓端缓存清理。
  54. plus.cache.clear();
  55. // #endif
  56. }, 1000);
  57. // #endif
  58. }
  59. const handleMessage = (event) => {
  60. let msg = {};
  61. // #ifdef H5
  62. if (event.data && event.data.data && event.data.data.arg) {
  63. msg = event.data.data.arg
  64. }
  65. // #endif
  66. // #ifdef APP-PLUS
  67. msg = event.detail.data[0]
  68. // #endif
  69. console.log('event.detail', msg)
  70. if (msg.source === 'file-web') {
  71. if (msg.type === "selectFileChange") {
  72. selectFileChange(msg.data)
  73. }
  74. }
  75. }
  76. onUnload(()=>{
  77. // #ifdef H5
  78. window.removeEventListener('message', handleMessage);
  79. // #endif
  80. })
  81. //选择文件
  82. const selectFile = () => {
  83. // #ifdef H5
  84. window.frames["selectFileIframe"].postMessage({
  85. type: 'selectFile',
  86. source: 'file-app',
  87. data: {}
  88. });
  89. // #endif
  90. // #ifdef APP-PLUS
  91. wv.evalJS(`selectFile()`)
  92. // #endif
  93. }
  94. //文件选择完毕
  95. const selectFileChange = (files) => {
  96. if (files && files.length > 0) {
  97. let file = files[0];
  98. const reader = new FileReader();
  99. reader.readAsDataURL(file);
  100. //监听文件读取结束后事件
  101. reader.onloadend = function (e) {
  102. //e.target.result就是最后的路径地址
  103. emit('change', {
  104. file: file,
  105. path: e.target.result
  106. })
  107. }
  108. }
  109. }
  110. //到处函数
  111. defineExpose({
  112. selectFile
  113. })
  114. </script>
  115. <style scoped lang="scss">
  116. @import "./style.scss";
  117. </style>