| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- <template>
- <div>
- <van-field class="preview">
- <template #input>
- <div
- class="preview_file"
- v-for="(item, index) in dataModel"
- :key="index"
- >
- <div class="preview_item">
- <i
- class="van-icon van-icon-description van-uploader__file-icon"
- ></i>
- <span
- style="
- white-space: nowrap;
- overflow: hidden;
- text-overflow: ellipsis;
- width: 1.8rem;
- "
- >{{ item.name || item.url }}</span
- >
- </div>
- <div class="preview_btn">
- <!-- <van-button @click="downLoadFile2(item.url)" type="info" size="mini"
- >下载</van-button
- > -->
- <van-button
- :disabled="!checkFileSuffix(item.url)"
- v-if="checkFileSuffix(item.url)"
- @click="downLoadFile(item.url)"
- type="info"
- size="mini"
- >预览</van-button
- >
- </div>
- </div>
- </template>
- </van-field>
- <van-popup position="bottom" v-model="filePreview" style="height: 100%">
- <van-sticky>
- <m-header
- :backUrl="backUrl"
- :isFixed="false"
- :appHide="true"
- name="预览"
- />
- </van-sticky>
- <div
- id="previewIframe"
- style="height: calc(100vh - 0.44rem)"
- v-if="filePreview && (fileType == 'xls' || fileType == 'pdf')"
- ></div>
- <div
- style="height: calc(100vh - 0.44rem)"
- v-if="filePreview && fileType == 'doc'"
- ></div>
- </van-popup>
- </div>
- </template>
- <script>
- import MHeader from "@/components/header";
- import { browser } from "@/common/util";
- import { postMessage } from "@/helpers/native-message";
- export default {
- props: ["dataModel"],
- components: { MHeader },
- data() {
- return {
- filePreview: false,
- fileType: "xls",
- previewUrl: "",
- backUrl: {
- status: true,
- callBack: () => {
- this.filePreview = false;
- },
- },
- };
- },
- mounted() {
- console.log(this.dataModel);
- },
- methods: {
- checkFileSuffix(url) {
- let urlArr = url.split(".");
- let suffix = urlArr[urlArr.length - 1];
- // || suffix == 'doc' || suffix == 'docx'
- if (suffix == "xlsx" || suffix == "xls" || suffix == "pdf") {
- return true;
- } else {
- return false;
- }
- },
- getFileSuffix(url) {
- let urlArr = url.split(".");
- let suffix = urlArr[urlArr.length - 1];
- if (suffix == "xlsx" || suffix == "xls") {
- return "xls";
- } else if (suffix == "doc" || suffix == "docx") {
- return "doc";
- } else if (suffix == "pdf") {
- return "pdf";
- } else {
- return "";
- }
- },
- downLoadFile2(file) {
- this.$toast.loading({
- duration: 0, // 持续展示 toast
- forbidClick: true,
- message: "下载中...",
- });
- if (browser().isApp) {
- this.$toast.clear();
- postMessage(
- { api: "downloadFile", content: { downloadUrl: file } },
- () => {
- this.$toast.clear();
- }
- );
- } else {
- this.$toast.clear();
- window.location.href = file;
- }
- },
- downLoadFile(file) {
- // this.previewUrl = 'https://view.officeapps.live.com/op/view.aspx?src=' + file
- this.filePreview = true;
- this.fileType = this.getFileSuffix(file);
- if (this.fileType == "xls" || this.fileType == "pdf") {
- this.$toast.loading({
- duration: 0, // 持续展示 toast
- forbidClick: true,
- message: "加载中...",
- });
- let _this = this;
- this.$nextTick(() => {
- let iframe = document.createElement("iframe");
- iframe.id = "preview_iframe";
- iframe.style.width = "100%";
- iframe.style.height = "100%";
- iframe.style.border = "none";
- if (this.fileType == "xls") {
- if (browser().android) {
- iframe.src =
- "https://api.idocv.com/view/url?url=" +
- encodeURIComponent(file + "?times=" + new Date().getTime());
- } else {
- iframe.src =
- "https://view.officeapps.live.com/op/view.aspx?src=" + file;
- }
- } else {
- iframe.src =
- window.location.origin +
- "/pdf/web/viewer.html?file=" +
- encodeURIComponent(file);
- }
- if (iframe.attachEvent) {
- iframe.attachEvent("onload", function () {
- _this.$toast.clear();
- });
- } else {
- iframe.onload = function () {
- _this.$toast.clear();
- // setTimeout(() => {
- // let dom = document.querySelector('#preview_iframe').contentWindow.document
- // let scripts = dom.querySelectorAll('script[src]')
- // if(scripts)
- // }, 2000);
- };
- }
- document.querySelector("#previewIframe").appendChild(iframe);
- });
- } else if (this.fileType == "doc") {
- // // this.previewUrl = 'https://view.officeapps.live.com/op/view.aspx?src=' + file
- }
- },
- },
- };
- </script>
- <style lang="less" scoped>
- .preview {
- padding: 0;
- /deep/.van-field__control {
- display: flex;
- flex-direction: column;
- }
- }
- .preview_file {
- width: 100%;
- font-size: 14px;
- display: flex;
- justify-content: space-between;
- background: #f7f7f7;
- padding: 0.08rem;
- margin-bottom: 0.08rem;
- box-sizing: border-box;
- .preview_item {
- display: flex;
- align-items: center;
- flex-basis: 70%;
- }
- .preview_btn {
- text-align: right;
- flex-basis: 30%;
- }
- }
- </style>
|