preview.vue 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. <template>
  2. <view class="page_preview">
  3. <commonHeader :title="fileName" @back="goBack"></commonHeader>
  4. <!-- #ifdef APP-PLUS -->
  5. <view class="app_container">
  6. <view v-if="showLoading" class="loading_container">
  7. <text class="loading_text">{{ loadingText }}</text>
  8. </view>
  9. <view v-if="showError" class="error_container">
  10. <text class="error_title">文件暂时无法预览</text>
  11. <text class="error_text">{{ errorMessage }}</text>
  12. <view class="error_btn" @click="retryPreview">
  13. <text class="error_btn_text">重新尝试</text>
  14. </view>
  15. </view>
  16. <view v-if="showDownloadBtn && !showLoading && !showError" class="download_container">
  17. <text class="file_name">{{ fileName }}</text>
  18. <text class="file_tip">{{ fileTip }}</text>
  19. <view class="download_btn" @click="handleDownload">
  20. <text class="download_btn_text">点击下载预览</text>
  21. </view>
  22. </view>
  23. </view>
  24. <!-- #endif -->
  25. <!-- #ifdef H5 -->
  26. <web-view
  27. v-if="resolvedPreviewUrl"
  28. :src="resolvedPreviewUrl"
  29. class="preview_webview"
  30. @error="handleWebViewError"
  31. @load="handleWebViewLoad"
  32. ></web-view>
  33. <view v-if="showLoading" class="loading_container">
  34. <text class="loading_text">加载中...</text>
  35. </view>
  36. <view v-if="showError" class="error_container">
  37. <text class="error_title">文件暂时无法预览</text>
  38. <text class="error_text">当前文件可能不支持在线预览,或预览服务暂时不可用。</text>
  39. <view class="error_btn" @click="retryPreview">
  40. <text class="error_btn_text">重新尝试</text>
  41. </view>
  42. </view>
  43. <!-- #endif -->
  44. </view>
  45. </template>
  46. <script setup>
  47. import { ref, onMounted, onUnmounted } from 'vue';
  48. import { onLoad } from '@dcloudio/uni-app';
  49. import CommonHeader from '@/components/commonHeader.vue';
  50. const PREVIEW_STORAGE_KEY = 'noticePreviewFile';
  51. const fileUrl = ref('');
  52. const fileName = ref('文件预览');
  53. const fileExtension = ref('');
  54. const resolvedPreviewUrl = ref('');
  55. const showLoading = ref(true);
  56. const showError = ref(false);
  57. const showDownloadBtn = ref(false);
  58. const loadingText = ref('正在加载...');
  59. const errorMessage = ref('当前文件可能不支持在线预览,或预览服务暂时不可用。');
  60. const fileTip = ref('当前文件需下载后才能预览');
  61. const getFileExtension = (url) => {
  62. if (!url) return '';
  63. const cleanUrl = url.split('?')[0].split('#')[0];
  64. return cleanUrl.split('.').pop()?.toLowerCase() || '';
  65. };
  66. const isImageFile = (ext) => {
  67. return ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'webp'].includes(ext);
  68. };
  69. const isPdfFile = (ext) => ext === 'pdf';
  70. const isOfficeFile = (ext) => {
  71. return ['doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'txt', 'csv'].includes(ext);
  72. };
  73. const applyPreviewData = (data) => {
  74. if (!data) return;
  75. if (data.url) fileUrl.value = data.url;
  76. if (data.name) fileName.value = data.name;
  77. fileExtension.value = getFileExtension(fileUrl.value);
  78. // #ifdef APP-PLUS
  79. initAppPreview();
  80. // #endif
  81. // #ifdef H5
  82. initH5Preview();
  83. // #endif
  84. };
  85. // #ifdef APP-PLUS
  86. const initAppPreview = () => {
  87. showLoading.value = true;
  88. showError.value = false;
  89. showDownloadBtn.value = false;
  90. const ext = fileExtension.value;
  91. if (!fileUrl.value) {
  92. showLoading.value = false;
  93. showError.value = true;
  94. errorMessage.value = '文件地址无效';
  95. return;
  96. }
  97. if (isImageFile(ext)) {
  98. previewImage();
  99. } else if (isPdfFile(ext) || isOfficeFile(ext)) {
  100. downloadAndOpenFile();
  101. } else {
  102. showLoading.value = false;
  103. showDownloadBtn.value = true;
  104. fileTip.value = `当前文件类型(.${ext || '未知'})需下载后查看`;
  105. }
  106. };
  107. const previewImage = () => {
  108. showLoading.value = true;
  109. loadingText.value = '正在加载图片...';
  110. uni.previewImage({
  111. urls: [fileUrl.value],
  112. current: fileUrl.value,
  113. fail: (err) => {
  114. console.error('预览图片失败:', err);
  115. downloadAndOpenFile();
  116. }
  117. });
  118. };
  119. const downloadAndOpenFile = () => {
  120. showLoading.value = true;
  121. loadingText.value = '正在下载文件...';
  122. showError.value = false;
  123. const token = uni.getStorageSync('token');
  124. const downloadOptions = {
  125. url: fileUrl.value,
  126. success: (res) => {
  127. if (res.statusCode === 200) {
  128. openFile(res.tempFilePath);
  129. } else {
  130. showLoading.value = false;
  131. showError.value = true;
  132. errorMessage.value = `下载失败(${res.statusCode}),请检查网络后重试`;
  133. }
  134. },
  135. fail: (err) => {
  136. console.error('下载文件失败:', err);
  137. showLoading.value = false;
  138. showError.value = true;
  139. errorMessage.value = '下载失败,请检查网络连接后重试';
  140. }
  141. };
  142. if (token) {
  143. downloadOptions.header = {
  144. 'Authorization': token
  145. };
  146. }
  147. uni.downloadFile(downloadOptions);
  148. };
  149. const openFile = (filePath) => {
  150. showLoading.value = false;
  151. uni.openDocument({
  152. filePath: filePath,
  153. showMenu: true,
  154. fail: (err) => {
  155. console.error('打开文件失败:', err);
  156. uni.showToast({
  157. title: '当前设备不支持预览此文件',
  158. icon: 'none'
  159. });
  160. }
  161. });
  162. };
  163. const handleDownload = () => {
  164. downloadAndOpenFile();
  165. };
  166. // #endif
  167. // #ifdef H5
  168. const initH5Preview = () => {
  169. showLoading.value = true;
  170. showError.value = false;
  171. const ext = fileExtension.value;
  172. const url = fileUrl.value;
  173. if (!url) {
  174. showLoading.value = false;
  175. showError.value = true;
  176. return;
  177. }
  178. if (isImageFile(ext)) {
  179. previewImageH5();
  180. } else if (isPdfFile(ext)) {
  181. resolvedPreviewUrl.value = url;
  182. } else if (isOfficeFile(ext)) {
  183. resolvedPreviewUrl.value = buildOfficePreviewUrl(url);
  184. } else {
  185. resolvedPreviewUrl.value = url;
  186. }
  187. };
  188. const previewImageH5 = () => {
  189. uni.previewImage({
  190. urls: [fileUrl.value],
  191. current: fileUrl.value,
  192. fail: () => {
  193. resolvedPreviewUrl.value = fileUrl.value;
  194. }
  195. });
  196. };
  197. const buildOfficePreviewUrl = (url) => {
  198. return `https://view.officeapps.live.com/op/view.aspx?src=${encodeURIComponent(url)}`;
  199. };
  200. const handleWebViewLoad = () => {
  201. showLoading.value = false;
  202. showError.value = false;
  203. };
  204. const handleWebViewError = () => {
  205. showLoading.value = false;
  206. showError.value = true;
  207. };
  208. // #endif
  209. const retryPreview = () => {
  210. // #ifdef APP-PLUS
  211. initAppPreview();
  212. // #endif
  213. // #ifdef H5
  214. initH5Preview();
  215. // #endif
  216. };
  217. onLoad((options) => {
  218. const eventChannel = typeof uni.getOpenerEventChannel === 'function'
  219. ? uni.getOpenerEventChannel()
  220. : null;
  221. if (eventChannel && typeof eventChannel.on === 'function') {
  222. eventChannel.on('previewFileData', (data) => {
  223. applyPreviewData(data);
  224. });
  225. }
  226. const storageData = uni.getStorageSync(PREVIEW_STORAGE_KEY);
  227. if (storageData) {
  228. try {
  229. applyPreviewData(JSON.parse(storageData));
  230. } catch (error) {
  231. console.error('解析预览文件信息失败:', error);
  232. }
  233. uni.removeStorageSync(PREVIEW_STORAGE_KEY);
  234. }
  235. if (options && options.url && !fileUrl.value) {
  236. fileUrl.value = decodeURIComponent(options.url);
  237. }
  238. if (options && options.name && !fileName.value) {
  239. fileName.value = decodeURIComponent(options.name);
  240. }
  241. if (!resolvedPreviewUrl.value && fileUrl.value) {
  242. applyPreviewData({
  243. url: fileUrl.value,
  244. name: fileName.value
  245. });
  246. } else if (!fileUrl.value) {
  247. showLoading.value = false;
  248. showError.value = true;
  249. errorMessage.value = '文件地址无效';
  250. }
  251. });
  252. onMounted(() => {
  253. if (!fileUrl.value) {
  254. showLoading.value = false;
  255. showError.value = true;
  256. errorMessage.value = '未获取到文件信息';
  257. }
  258. });
  259. onUnmounted(() => {
  260. resolvedPreviewUrl.value = '';
  261. });
  262. const goBack = () => {
  263. uni.navigateBack();
  264. };
  265. </script>
  266. <style lang="scss">
  267. .page_preview {
  268. height: 100vh;
  269. display: flex;
  270. flex-direction: column;
  271. background-color: #FFFFFF;
  272. }
  273. .preview_webview {
  274. flex: 1;
  275. margin-top: 96rpx;
  276. }
  277. .app_container {
  278. flex: 1;
  279. display: flex;
  280. flex-direction: column;
  281. align-items: center;
  282. justify-content: center;
  283. padding: 0 48rpx;
  284. }
  285. .download_container {
  286. display: flex;
  287. flex-direction: column;
  288. align-items: center;
  289. padding: 60rpx 48rpx;
  290. background-color: #F9FAFF;
  291. border-radius: 20rpx;
  292. width: 100%;
  293. .file_name {
  294. font-size: 32rpx;
  295. font-weight: 500;
  296. color: #333333;
  297. margin-bottom: 16rpx;
  298. text-align: center;
  299. word-break: break-all;
  300. }
  301. .file_tip {
  302. font-size: 26rpx;
  303. color: #999999;
  304. margin-bottom: 40rpx;
  305. text-align: center;
  306. }
  307. .download_btn {
  308. background-color: #2E64FA;
  309. border-radius: 12rpx;
  310. padding: 24rpx 64rpx;
  311. .download_btn_text {
  312. font-size: 30rpx;
  313. color: #FFFFFF;
  314. font-weight: 500;
  315. }
  316. }
  317. }
  318. .loading_container {
  319. position: fixed;
  320. top: 50%;
  321. left: 50%;
  322. transform: translate(-50%, -50%);
  323. background-color: rgba(0, 0, 0, 0.6);
  324. padding: 40rpx 60rpx;
  325. border-radius: 16rpx;
  326. z-index: 1000;
  327. .loading_text {
  328. font-size: 28rpx;
  329. color: #FFFFFF;
  330. }
  331. }
  332. .error_container {
  333. position: fixed;
  334. top: 50%;
  335. left: 50%;
  336. transform: translate(-50%, -50%);
  337. width: 560rpx;
  338. padding: 48rpx 40rpx;
  339. background: #FFFFFF;
  340. border-radius: 24rpx;
  341. box-shadow: 0 16rpx 56rpx rgba(0, 0, 0, 0.15);
  342. display: flex;
  343. flex-direction: column;
  344. align-items: center;
  345. z-index: 1001;
  346. .error_title {
  347. font-size: 34rpx;
  348. font-weight: 600;
  349. color: #333333;
  350. margin-bottom: 20rpx;
  351. }
  352. .error_text {
  353. font-size: 26rpx;
  354. line-height: 1.8;
  355. color: #8A9099;
  356. text-align: center;
  357. margin-bottom: 40rpx;
  358. }
  359. .error_btn {
  360. min-width: 240rpx;
  361. height: 80rpx;
  362. padding: 0 40rpx;
  363. border-radius: 40rpx;
  364. background: #2E64FA;
  365. display: flex;
  366. align-items: center;
  367. justify-content: center;
  368. .error_btn_text {
  369. font-size: 30rpx;
  370. color: #FFFFFF;
  371. font-weight: 500;
  372. }
  373. }
  374. }
  375. </style>