index.vue 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <template>
  2. <view class="box">
  3. <ss-preview :fileUrl="fileUrl" :fileType="fileType"></ss-preview>
  4. </view>
  5. </template>
  6. <script>
  7. export default {
  8. components:{},
  9. data() {
  10. return {
  11. fileUrl: '',
  12. fileType: '2',
  13. fileMap: {
  14. '1': '/static/pdf/2.pdf', //银通汽车4s管理系统
  15. '2': '/static/pdf/3.pdf', //银通汽车租赁管理系统
  16. '3': '/static/pdf/1.pdf', // 银通云越管理系统
  17. }
  18. };
  19. },
  20. onLoad(options) {
  21. console.log(options)
  22. const fileIndex = options.file || '1';
  23. const baseUrl = this.fileMap[fileIndex] || this.fileMap['1'];
  24. this.fileUrl = baseUrl + '?t=' + Date.now();
  25. console.log(this.fileUrl)
  26. this.handlePreview();
  27. },
  28. methods: {
  29. handlePreview() {
  30. //#ifdef MP-WEIXIN
  31. this.previewWechat(this.fileUrl);
  32. return;
  33. //#endif
  34. //#ifdef H5
  35. if (this.isWechat()) {
  36. this.clearWechatCache();
  37. }
  38. //#endif
  39. },
  40. isWechat() {
  41. var ua = navigator.userAgent.toLowerCase();
  42. return ua.indexOf('micromessenger') !== -1;
  43. },
  44. previewWechat(urlPdf) {
  45. uni.showLoading({ title: '加载中...' });
  46. uni.downloadFile({
  47. url: urlPdf,
  48. header: {
  49. 'Cache-Control': 'no-cache',
  50. 'Pragma': 'no-cache'
  51. },
  52. success: (res) => {
  53. if (res.statusCode === 200) {
  54. uni.openDocument({
  55. filePath: res.tempFilePath,
  56. fileType: 'pdf',
  57. showMenu: true,
  58. success: () => {
  59. console.log('打开成功');
  60. },
  61. fail: (err) => {
  62. console.error('打开失败:', err);
  63. uni.showToast({ title: '打开失败', icon: 'none' });
  64. }
  65. });
  66. } else {
  67. uni.showToast({ title: '下载失败', icon: 'none' });
  68. }
  69. },
  70. fail: (err) => {
  71. console.error('下载失败:', err);
  72. uni.showToast({ title: '下载失败', icon: 'none' });
  73. },
  74. complete: () => {
  75. uni.hideLoading();
  76. }
  77. });
  78. },
  79. clearWechatCache() {
  80. try {
  81. if (window.location.reload) {
  82. window.location.reload(true);
  83. }
  84. } catch (e) {
  85. console.log('刷新失败:', e);
  86. }
  87. }
  88. }
  89. };
  90. </script>
  91. <style lang="scss" scoped>
  92. .box {
  93. padding: 0;
  94. }
  95. </style>