123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295 |
- <!--
- * @Description: 单个附件文件上传组件
- * @Author: Rockery
- * @Date: 2021-12-20 09:16:41
- * @LastEditors: Rockery
- * @LastEditTime: 2021-12-20 15:29:05
- * @FilePath: \party_construct_web\src\components\RocPdfFileUpload\index.vue
- * @Copyright: Copyright (c) 2016~2021 Rockery(1113269755@qq.com)
- -->
- <template>
- <div class="rocpdffileupload">
- <div class="rocpdffileupload-attachment">
- <el-upload
- ref="rocPdfFileUploadRef"
- :limit="1"
- accept=".pdf"
- :auto-upload="false"
- :show-file-list="true"
- :action="actionUrl"
- :headers="headers"
- :before-upload="handleRocPdfFileUploadBeforeUpload"
- :on-success="handleRocPdfFileUploadOnsuccess"
- :on-change="handleRocPdfFileUploadOnchange"
- :on-remove="handleRocPdfFileUploadOnRemove"
- :on-exceed="handleRocPdfFileUploadOnExceed"
- class="rocpdffileupload-attachment-fileupload"
- >
- <div class="rocpdffileupload-attachment-fileupload-content">
- <i class="el-icon-plus" />
- 上传文件,格式:PDF
- </div>
- </el-upload>
- <div
- v-if="isUploadSuccess"
- class="fl rocpdffileupload-attachment-addr"
- @click="handleRocPdfFileUploadUrlClick"
- >
- <div>文件上传成功地址:</div>
- <div>{{ successUrl }}</div>
- </div>
- <el-button
- v-if="isSelect && !isUploadSuccess"
- type="primary"
- size="small"
- @click="submitRocPdfFileUploadClick"
- >上传文件</el-button>
- <el-button v-if="isSelect" type="info" size="small" @click="removeRocPdfFileUploadClick">移除文件</el-button>
- </div>
- </div>
- </template>
- <script>
- export default {
- name: "Rocpdffileupload",
- props: {
-
- value: {
- type: String,
- default: ""
- },
-
- uploadUrl: {
- type: String,
- default: ''
- },
-
- uploadHeaders: {
- type: Object,
- default: () => {
- return {};
- }
- },
-
- uploadFileTitle: {
- type: String,
- default: ''
- },
- },
- data() {
- return {
-
- isSelect: false,
-
- isOnProgress: false,
-
- isUploadSuccess: false,
-
- successUrl: '',
-
- successResp: {}
- };
- },
- computed: {
-
- actionUrl() {
- return this.uploadUrl;
- },
-
- headers() {
- return this.uploadHeaders;
- },
-
- fileTitle() {
- if (this.uploadFileTitle) {
- return `文件【${this.uploadFileTitle}】`;
- }
- return '文件';
- },
- },
- created() {
- this.initData();
- },
- mounted() {
- },
- methods: {
-
- async initData() {
- },
-
- handleRocPdfFileUploadBeforeUpload(file) {
- if (file.type.indexOf('application/pdf') === -1) {
- this.$refs.rocPdfFileUploadRef.clearFiles();
- this.msgError(`${this.fileTitle}格式错误,请上传类型格式为PDF的文件!`);
- return;
- }
- },
-
- handleRocPdfFileUploadOnsuccess(response, file, fileList) {
-
- if (response.code !== 200) {
- this.$refs.rocPdfFileUploadRef?.clearFiles?.();
- this.isSelect = false;
- this.isUploadSuccess = false;
- this.msgError(`上传PDF${this.fileTitle}失败,请重新选择PDF文件上传!`);
- return;
- }
-
- this.successUrl = response?.data?.url;
-
- this.successResp = response?.data;
- this.$emit("input", response?.data?.url);
- this.isUploadSuccess = true;
-
- if (this.isOnProgress) {
- this.isOnProgress = false;
- this.$emit("wait-upload-success", this.successResp);
- } else {
- this.$alert(`PDF${this.fileTitle}上传成功!`, '上传结果', { dangerouslyUseHTMLString: true });
- }
- },
-
- handleRocPdfFileUploadOnchange(file, fileList) {
- if (file.status === 'ready') {
- if (((file.raw || {}).type || '').indexOf('application/pdf') === -1) {
- this.$refs.rocPdfFileUploadRef.clearFiles();
- this.isSelect = false;
- this.msgError(`${this.fileTitle}格式错误,请上传类型格式为PDF的文件!`);
- return;
- }
- this.isSelect = true;
- this.isUploadSuccess = false;
- }
- },
-
- handleRocPdfFileUploadOnRemove(file, fileList) {
- this.successResp = {};
- this.successUrl = '';
- this.isUploadSuccess = false;
- this.isSelect = false;
- this.$emit("input", "");
- },
-
- handleRocPdfFileUploadOnExceed(files, fileList) {
- this.msgWarning(`只允许上传单个PDF${this.fileTitle}`);
- },
-
- submitRocPdfFileUploadClick() {
- if (!this.isSelect) { return; }
- this.$confirm(`确定上传PDF${this.fileTitle}操作吗?`, '提示', {
- confirmButtonText: '确定 ',
- cancelButtonText: '取消 ',
- type: 'info'
- }).then(() => {
- this.$refs.rocPdfFileUploadRef?.submit?.();
- }).catch(() => {
- this.msgInfo('您已取消上传操作!');
- });
- },
-
- removeRocPdfFileUploadClick() {
- this.successResp = {};
- this.successUrl = '';
- this.$refs.rocPdfFileUploadRef.clearFiles();
- this.isUploadSuccess = false;
- this.isSelect = false;
- this.$emit("input", "");
- },
-
- handleRocPdfFileUploadUrlClick() {
- this.$emit("pdf-url-incident", this.successResp);
- }
- }
- }
- </script>
- <style lang="scss">
- .rocpdffileupload {
- .rocpdffileupload-attachment {
- &-fileupload {
- .el-upload {
- width: 100%;
- border: 1px dashed #d9d9d9;
- border-radius: 6px;
- cursor: pointer;
- position: relative;
- overflow: hidden;
- &:hover {
- border-color: #409eff;
- }
- }
- .el-upload-list.el-upload-list--text {
- .el-upload-list__item:first-child {
- margin-top: 0;
- }
- }
- &-content {
- width: 100%;
- height: 32px;
- font-size: 14px;
- font-weight: 400;
- color: #8c939d;
- text-align: center;
- }
- }
- &-addr {
- width: 100%;
- padding: 5px 0;
- div {
- &:first-child,
- &:last-child {
- margin-left: 4px;
- width: 100%;
- height: 20px;
- vertical-align: middle;
- font-size: 14px;
- font-weight: 400;
- color: #606266;
- line-height: 20px;
- }
- &:last-child {
- height: 25px;
- line-height: 25px;
- cursor: pointer;
- opacity: 0.8;
- &:hover {
- color: #409eff;
- text-decoration: underline;
- }
- }
- }
- }
- }
- }
- </style>
|