index.vue 9.0 KB

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