cjx-previewOffice.vue 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. <template>
  2. <view class="container">
  3. <view class="file_content" @click="open">
  4. <slot></slot>
  5. </view>
  6. <view class="cjx-previewOffice-container" :style="{ height: pagesHeight }">
  7. <view class="cjx-previewOffice-header" v-if="!$slots.header">
  8. <view class="icon" ></view>
  9. <view class="name">
  10. {{ name }}
  11. </view>
  12. <uni-icons class="icon" @click="close" type="close" size="40" style="color: #999999;font-size: 40rpx;"></uni-icons>
  13. <!-- <icon class="icon" @click="close" type="cancel"></icon> -->
  14. </view>
  15. <view class="cjx-previewOffice-header" v-else>
  16. <slot name="header"></slot>
  17. </view>
  18. <view class="webview">
  19. <!-- #ifdef H5 -->
  20. <iframe
  21. class="cjx-previewOffice-iframe"
  22. :webview-styles="webviewStyles"
  23. :src="`${typeMapSrc[type]}${encodeURIComponent(props.value)}&safeBottom=${safeBottom}`"
  24. />
  25. <!-- #endif -->
  26. </view>
  27. </view>
  28. <view class="cjx-previewOffice-mask"
  29. :style="{
  30. zIndex: pagesHeight !== '0' ? zIndex : '-1',
  31. opacity: pagesHeight !== '0' ? 1 : 0,
  32. }"
  33. @click="closeOnClickModalChange"
  34. >
  35. </view>
  36. </view>
  37. </template>
  38. <script lang="ts" setup>
  39. import uniIcons from '@/uni_modules/uni-icons/components/uni-icons/uni-icons.vue';
  40. import { onMounted, ref } from 'vue'
  41. import type { CSSProperties } from 'vue'
  42. import { previewOfficeProps, FileType } from './interface'
  43. // <!-- #ifdef H5 -->
  44. // <!-- #endif -->
  45. const props = defineProps(previewOfficeProps())
  46. const showWebviewArr: FileType[] = ['excel', 'pdf', 'word', 'txt']
  47. const webviewStyles = ref<CSSProperties>({
  48. zIndex: '9999',
  49. })
  50. const typeMapSrc = {
  51. word: 'uni_modules/cjx-previewOffice/static/hybrid/word.html?file=',
  52. pdf: 'uni_modules/cjx-previewOffice/static/hybrid/pdf/web/viewer.html?file=',
  53. excel: 'uni_modules/cjx-previewOffice/static/hybrid/excel.html?file=',
  54. txt: 'uni_modules/cjx-previewOffice/static/hybrid/txt.html?file=',
  55. }
  56. const pagesHeight = ref<string>('0')
  57. const safeBottom = ref<number>(0)
  58. let webview: any = null
  59. const open = () => {
  60. if (pagesHeight.value === '85vh') {
  61. return
  62. }
  63. // #ifdef H5
  64. setTimeout(() => {
  65. pagesHeight.value = '85vh'
  66. })
  67. // #endif
  68. // #ifdef APP-PLUS
  69. // console.log(11, uni.getSystemInfoSync())
  70. const { statusBarHeight, windowHeight, screenHeight } = uni.getSystemInfoSync()
  71. pagesHeight.value = Number(windowHeight) - Number(statusBarHeight) + 'px'
  72. console.log(windowHeight,windowHeight,windowHeight - statusBarHeight)
  73. webview = plus.webview.create(
  74. `${typeMapSrc[props.type]}${encodeURIComponent(props.value)}&safeBottom=${safeBottom.value}`,
  75. 'id',
  76. {
  77. bottom: 0,
  78. height: windowHeight - statusBarHeight - 44,
  79. },
  80. );
  81. const pages = getCurrentPages();
  82. const page: any = pages[pages.length - 1];
  83. const currentWebview = page.$getAppWebview();
  84. currentWebview.append(webview);
  85. webview.show('id', 'slide-in-bottom')
  86. // #endif
  87. }
  88. const close = () => {
  89. pagesHeight.value = '0'
  90. // #ifdef APP-PLUS
  91. webview.setStyle({
  92. height: 0,
  93. })
  94. webview.close('slide-in-bottom')
  95. // #endif
  96. }
  97. const closeOnClickModalChange = () => {
  98. if (props.closeOnClickModal) {
  99. pagesHeight.value = '0'
  100. // #ifdef APP-PLUS
  101. webview.setStyle({
  102. height: 0,
  103. })
  104. webview.close('slide-in-bottom')
  105. // #endif
  106. }
  107. }
  108. defineExpose({
  109. open,
  110. close
  111. })
  112. onMounted(() => {
  113. safeBottom.value = 0
  114. // #ifdef H5
  115. // #endif
  116. // #ifdef APP-PLUS
  117. uni.getSystemInfo({
  118. success: function (res) {
  119. console.log(res.safeArea); // 输出安全区域信息
  120. safeBottom.value = res.screenHeight - res.safeArea.bottom; // 计算底部安全区域高度
  121. console.log('底部安全区域高度:', safeBottom.value);
  122. }
  123. });
  124. // #endif
  125. // if (props.type === 'image') {
  126. // uni.previewImage({
  127. // urls: ['https://img2.baidu.com/it/u=412966049,488862975&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=800'],
  128. // })
  129. // }
  130. })
  131. </script>
  132. <style lang="scss" >
  133. iframe {
  134. // position: absolute;
  135. // top: 44px !important;
  136. // height: calc(100vh - 44px) !important;
  137. // position: absolute;
  138. z-index: 9999 !important;
  139. border:0;
  140. top: calc(15vh + 44px) !important;
  141. // height: calc(100% - 70rpx) !important;
  142. }
  143. .container{
  144. display: flex;
  145. width: 100%;
  146. }
  147. .file_content{
  148. display: flex;
  149. width: 100%;
  150. }
  151. .cjx-previewOffice-iframe {
  152. // z-index: 9999 !important;
  153. width: 100vw;
  154. top: 44px !important;
  155. height: calc(100% - 12px) !important;
  156. }
  157. .cjx-previewOffice-container {
  158. position: fixed;
  159. left: 0px;
  160. overflow: hidden;
  161. z-index: 998;
  162. // height: 100vh;
  163. width: 100vw;
  164. bottom: 0;
  165. transition: all ease 0.2s;
  166. background-color: #fff;
  167. // position: relative;
  168. .cjx-previewOffice-header {
  169. background-color: #fff;
  170. // position: fixed;
  171. // top: 0;
  172. height: 44px;
  173. z-index: 9999;
  174. display: flex;
  175. align-items: center;
  176. width: 100%;
  177. padding: 0 16rpx;
  178. box-sizing: border-box;
  179. .name {
  180. width: calc(100% - 80rpx);
  181. text-align: center;
  182. overflow: hidden;
  183. text-overflow: ellipsis;
  184. white-space: nowrap;
  185. padding-right: 10rpx;
  186. box-sizing: border-box;
  187. }
  188. .icon {
  189. width: 40rpx;
  190. color:#333;
  191. }
  192. }
  193. }
  194. .webview {
  195. // height: calc(100vh - 44px) !important;
  196. height: calc(100% - 70rpx) !important;
  197. }
  198. .cjx-previewOffice-mask {
  199. position: fixed;
  200. z-index: 990;
  201. background-color: rgba(0, 0, 0, 0.4);
  202. top: 0;
  203. left: 0;
  204. width: 100vw;
  205. height: 100vh;
  206. opacity: 0;
  207. transition: all ease 0.2s;
  208. }
  209. </style>