index.vue 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. <!--
  2. * @Description: 单个PDF文件上传组件
  3. * @Author: Rockery
  4. * @Date: 2021-12-20 09:16:41
  5. * @LastEditors: Rockery
  6. * @LastEditTime: 2021-12-27 12:02:35
  7. * @FilePath: \party_construct_web\src\components\RocPdfFileUpload\index.vue
  8. * @Copyright: Copyright (c) 2016~2021 Rockery(1113269755@qq.com)
  9. -->
  10. <template>
  11. <div class="rocpdffileupload">
  12. <div class="rocpdffileupload-attachment" v-loading="removeLoading">
  13. <el-upload
  14. ref="rocPdfFileUploadRef"
  15. :limit="1"
  16. accept=".pdf"
  17. :auto-upload="false"
  18. :show-file-list="true"
  19. :action="actionUrl"
  20. :headers="headers"
  21. :before-upload="handleRocPdfFileUploadBeforeUpload"
  22. :on-success="handleRocPdfFileUploadOnsuccess"
  23. :on-change="handleRocPdfFileUploadOnchange"
  24. :on-remove="handleRocPdfFileUploadOnRemove"
  25. :on-exceed="handleRocPdfFileUploadOnExceed"
  26. class="rocpdffileupload-attachment-fileupload"
  27. >
  28. <div class="rocpdffileupload-attachment-fileupload-content">
  29. <i class="el-icon-plus" />
  30. 上传文件,格式:PDF
  31. </div>
  32. </el-upload>
  33. <div
  34. v-if="isUploadSuccess"
  35. class="fl rocpdffileupload-attachment-addr"
  36. @click="handleRocPdfFileUploadUrlClick"
  37. >
  38. <div>文件上传成功地址:</div>
  39. <!-- <a :href="successUrl" target="_blank">{{ successUrl }}</a> -->
  40. <div>{{ successResp.name }}</div>
  41. </div>
  42. <el-button
  43. v-if="isSelect && !isUploadSuccess"
  44. type="primary"
  45. size="small"
  46. @click="submitRocPdfFileUploadClick"
  47. >上传文件</el-button>
  48. <el-button v-if="isSelect" type="info" size="small" @click="removeRocPdfFileUploadClick">移除文件</el-button>
  49. </div>
  50. <roc-vue-pdf-dialog
  51. v-if="pdfDialogVisible"
  52. :visible.sync="pdfDialogVisible"
  53. :pdfUrl="this.successResp.successUrl"
  54. ></roc-vue-pdf-dialog>
  55. </div>
  56. </template>
  57. <script>
  58. import { getToken } from '@/utils/auth';
  59. export default {
  60. name: 'Rocpdffileupload',
  61. components: {
  62. 'roc-vue-pdf-dialog': () => import('@/components/RocVuePdfDialog')
  63. },
  64. props: {
  65. // 值
  66. value: {
  67. type: String,
  68. default: ''
  69. },
  70. // PDF文件上传地址
  71. uploadUrl: {
  72. type: String,
  73. default: `${process.env.VUE_APP_FILE_UPLOAD_API}`
  74. },
  75. // 请求头对象
  76. uploadHeaders: {
  77. type: Object,
  78. default: () => {
  79. return {
  80. Authorization: 'Bearer ' + getToken()
  81. };
  82. }
  83. },
  84. // 上传文件标题
  85. uploadFileTitle: {
  86. type: String,
  87. default: ''
  88. },
  89. },
  90. data() {
  91. return {
  92. // 是否选择PDF文件
  93. isSelect: false,
  94. // 是否上传PDF文件成功
  95. isUploadSuccess: false,
  96. // PDF文件上传成功数据对象
  97. successUrl: '',
  98. // PDF文件上传成功数据对象
  99. successResp: {},
  100. pdfDialogVisible: false,
  101. removeLoading: false,
  102. setTimeoutTimer: null
  103. };
  104. },
  105. computed: {
  106. // PDF文件上传地址
  107. actionUrl() {
  108. return this.uploadUrl;
  109. },
  110. // 请求头对象
  111. headers() {
  112. return this.uploadHeaders;
  113. },
  114. // 上传文件标题
  115. fileTitle() {
  116. if (this.uploadFileTitle) {
  117. return `文件【${this.uploadFileTitle}】`;
  118. }
  119. return '文件';
  120. },
  121. },
  122. destroyed() {
  123. this.setTimeoutTimer && clearTimeout(this.setTimeoutTimer);
  124. },
  125. methods: {
  126. /**
  127. * PDF文件上传预处理
  128. */
  129. handleRocPdfFileUploadBeforeUpload(file) {
  130. if (file.type.indexOf('application/pdf') === -1) {
  131. this.$refs.rocPdfFileUploadRef.clearFiles();
  132. this.msgError(`${this.fileTitle}格式错误,请上传类型格式为PDF的文件!`);
  133. return;
  134. }
  135. },
  136. /**
  137. * PDF文件上传成功处理
  138. */
  139. handleRocPdfFileUploadOnsuccess(response, file, fileList) {
  140. // 校验封面图片是否上传成功
  141. if (response.code !== 200) {
  142. this.$refs.rocPdfFileUploadRef?.clearFiles?.();
  143. this.isSelect = false;
  144. this.isUploadSuccess = false;
  145. this.msgError(`上传PDF${this.fileTitle}失败,请重新选择PDF文件上传!`);
  146. return;
  147. }
  148. let successUrl = `${window.origin}${process.env.VUE_APP_FILE_VIEW_API}${response?.data?.url}`;
  149. // 绑定封面图片数据
  150. this.successUrl = successUrl;
  151. // PDF文件上传成功数据对象
  152. this.successResp = {
  153. ...response?.data,
  154. successUrl: successUrl
  155. };
  156. this.$emit('input', response?.data?.url);
  157. this.isUploadSuccess = true;
  158. this.$alert(`PDF${this.fileTitle}上传成功!`, '上传结果', { dangerouslyUseHTMLString: true });
  159. },
  160. /**
  161. * PDF文件状态改变
  162. */
  163. handleRocPdfFileUploadOnchange(file, fileList) {
  164. debugger;
  165. if (file.status === 'ready') {
  166. if (((file.raw || {}).type || '').indexOf('application/pdf') === -1) {
  167. this.$refs.rocPdfFileUploadRef.clearFiles();
  168. this.isSelect = false;
  169. this.msgError(`${this.fileTitle}格式错误,请上传类型格式为PDF的文件!`);
  170. return;
  171. }
  172. this.isSelect = true;
  173. this.isUploadSuccess = false;
  174. this.$emit('input', 'ROCPDFUPLOADSELECT');
  175. } else if (file.status === 'success') {
  176. this.isUploadSuccess = true;
  177. } else {
  178. this.isUploadSuccess = false;
  179. this.isSelect = false;
  180. this.$emit('input', 'ROCPDFUPLOADSELECT');
  181. }
  182. },
  183. /**
  184. * PDF文件列表移除文件时的钩子
  185. */
  186. handleRocPdfFileUploadOnRemove(file, fileList) {
  187. this.removeLoading = true;
  188. this.setTimeoutTimer = setTimeout(() => {
  189. this.successResp = {};
  190. this.successUrl = '';
  191. this.isUploadSuccess = false;
  192. this.isSelect = false;
  193. this.$emit('input', '');
  194. this.setTimeoutTimer && clearTimeout(this.setTimeoutTimer);
  195. this.removeLoading = false;
  196. }, 900);
  197. },
  198. /**
  199. * PDF文件超出个数限制时的钩子
  200. */
  201. handleRocPdfFileUploadOnExceed(files, fileList) {
  202. this.msgWarning(`只允许上传单个PDF${this.fileTitle}`);
  203. },
  204. /**
  205. * 提交上传相关材料PDF文件
  206. */
  207. submitRocPdfFileUploadClick() {
  208. if (!this.isSelect) { return; }
  209. this.$confirm(`确定上传PDF${this.fileTitle}操作吗?`, '提示', {
  210. confirmButtonText: '确定 ',
  211. cancelButtonText: '取消 ',
  212. type: 'info'
  213. }).then(() => {
  214. this.$refs.rocPdfFileUploadRef?.submit?.();
  215. }).catch(() => {
  216. this.msgInfo('您已取消上传操作!');
  217. });
  218. },
  219. /**
  220. * 移除选择相关材料PDF文件
  221. */
  222. removeRocPdfFileUploadClick() {
  223. this.removeLoading = true;
  224. this.$refs.rocPdfFileUploadRef.clearFiles();
  225. this.setTimeoutTimer = setTimeout(() => {
  226. this.isSelect = false;
  227. this.isUploadSuccess = false;
  228. this.successResp = {};
  229. this.successUrl = '';
  230. this.$emit('input', '');
  231. this.setTimeoutTimer && clearTimeout(this.setTimeoutTimer);
  232. this.removeLoading = false;
  233. }, 1000);
  234. },
  235. /**
  236. * PDF文件地址单击事件
  237. */
  238. handleRocPdfFileUploadUrlClick() {
  239. this.pdfDialogVisible = true;
  240. }
  241. }
  242. }
  243. </script>
  244. <style lang="scss">
  245. .rocpdffileupload {
  246. .rocpdffileupload-attachment {
  247. &-fileupload {
  248. .el-upload {
  249. width: 100%;
  250. border: 1px dashed #d9d9d9;
  251. border-radius: 6px;
  252. cursor: pointer;
  253. position: relative;
  254. overflow: hidden;
  255. &:hover {
  256. border-color: #409eff;
  257. }
  258. }
  259. .el-upload-list.el-upload-list--text {
  260. .el-upload-list__item:first-child {
  261. margin-top: 0;
  262. }
  263. }
  264. &-content {
  265. width: 100%;
  266. height: 32px;
  267. font-size: 14px;
  268. font-weight: 400;
  269. color: #8c939d;
  270. text-align: center;
  271. }
  272. }
  273. &-addr {
  274. width: 100%;
  275. padding: 5px 0;
  276. div {
  277. &:first-child,
  278. &:last-child {
  279. margin-left: 4px;
  280. width: 100%;
  281. height: 20px;
  282. vertical-align: middle;
  283. font-size: 14px;
  284. font-weight: 400;
  285. color: #606266;
  286. line-height: 20px;
  287. }
  288. &:last-child {
  289. height: 25px;
  290. line-height: 25px;
  291. cursor: pointer;
  292. opacity: 0.8;
  293. &:hover {
  294. color: #409eff;
  295. text-decoration: underline;
  296. }
  297. }
  298. }
  299. }
  300. }
  301. }
  302. </style>