zhaoxiaoqiang
2023-08-25 9583630b27fdd2f2566995a78d8238ce504f3523
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<!--
 * @Author: zh
 * @Date: 2022-01-05 18:44:53
 * @LastEditors: zxq
 * @LastEditTime: 2022-04-08 15:01:23
 * @Description: Description
 * @FilePath: \qyp-plat\src\components\uploadPdf.vue
-->
<template>
  <div>
    <el-upload action="#" :http-request="uploadFileData" :before-upload="beforeAvatarUpload"  :file-list="fileList" :on-preview="openFile" :on-remove="removeFile">
      <el-button size="small" type="primary">点击上传</el-button>
      <div slot="tip" class="el-upload__tip">只能上传pdf文件</div>
    </el-upload>
    <el-dialog title="查看批文" :visible.sync="imgVisible" class='dialogImageUrlbox'  >
      <el-button-group>
        <el-button type="primary" icon="el-icon-arrow-left" size="mini" @click="prePage">上一页</el-button>
        <el-button type="primary" size="mini" @click="nextPage">下一页<i class="el-icon-arrow-right el-icon--right"></i></el-button>
      </el-button-group>
      <div style="marginTop: 10px; color: #409EFF">{{ pageNum }} / {{ pageTotalNum }}</div>
      <pdf :page="pageNum" :src="url" @progress="loadedRatio = $event" @num-pages="pageTotalNum=$event"></pdf>
    </el-dialog>
  </div>
</template>
 
<script>
import pdf from 'vue-pdf'
import { uploadFile } from "@/api/user";
import { getBase64 } from "@/utils/validate";
export default {
  components: {
    pdf,
  },
  props: {
    defaultList: {
      type: Array,
      default: () => {
        return []
      }
    },
    // isPrview:{
    //   type: false,
    //   default: () => {
    //     return []
    //   }
    // }
  },
  data() {
    return {
      url:'',
      fileList: [],
      imgVisible: false,
      dialogImageUrl: '',
      uploadFileForm: {
        suffix: "",
        base64Data: ""
      },
      pageNum: 1,
      pageTotalNum: 1, //总页数
      loadedRatio: 0, // 当前页面的加载进度,范围是0-1 ,等于1的时候代表当前页已经完全加载完成了
    };
  },
  watch: {
    defaultList: {
      handler: function (val) {
        this.fileList = val;
      },
      deep: true
    }
  },
  methods: {
    // 上一页
    prePage() {
      let page = this.pageNum
      page = page > 1 ? page - 1 : this.pageTotalNum
      this.pageNum = page
    },
    // 下一页
    nextPage() {
      let page = this.pageNum
      page = page < this.pageTotalNum ? page + 1 : 1
      this.pageNum = page
    },
    beforeAvatarUpload(file) {
      if (file.type != 'application/pdf') {
        this.$message.error("上传文件格式不对!");
        return false;
      }
    },
    uploadFileData(file) {
      const fileName = file.file.name
      this.uploadFileForm.suffix = fileName.substr(fileName.lastIndexOf(".") + 1, fileName.length)
      getBase64(file.file).then(resBase64 => {
        this.uploadFileForm.base64Data = resBase64.split(',')[1]
        //直接拿到base64信息
        uploadFile(this.uploadFileForm).then(res => {
          this.fileList.push({
            filePath: res.body.fileUrl,
            fileName: fileName,
            name:fileName,
            uid: file.file.uid,
            url: res.body.fileUrl,
            fileId:res.body.fileId
          });
          this.$emit('sendList', this.fileList);
        })
      })
    },
    removeFile(file) {
      const index = this.fileList.findIndex(item => {
        return item.uid == file.uid;
      });
      this.fileList.splice(index, 1);
      this.$emit('sendList', this.fileList);
    },
    openFile(file) {
      const itemArr = this.fileList.find(item => {
        return item.uid == file.uid;
      });
      // const  pdfUrl = process.env.NODE_ENV === 'development' ? 'https://t.finlean.com'  : window.location.origin
      this.url = itemArr.filePath
      this.imgVisible=true
    },
  },
  created() {
 
  },
  mounted() {
 
  },
};
</script>
 
<style scoped lang="scss">
</style>