index.vue 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. <!--
  2. * @Description: 单个附件文件上传组件
  3. * @Author: Rockery
  4. * @Date: 2021-12-20 09:16:41
  5. * @LastEditors: Rockery
  6. * @LastEditTime: 2021-12-20 15:29:05
  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">
  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. <div>{{ successUrl }}</div>
  40. </div>
  41. <el-button
  42. v-if="isSelect && !isUploadSuccess"
  43. type="primary"
  44. size="small"
  45. @click="submitRocPdfFileUploadClick"
  46. >上传文件</el-button>
  47. <el-button v-if="isSelect" type="info" size="small" @click="removeRocPdfFileUploadClick">移除文件</el-button>
  48. </div>
  49. </div>
  50. </template>
  51. <script>
  52. export default {
  53. name: "Rocpdffileupload",
  54. props: {
  55. // 值
  56. value: {
  57. type: String,
  58. default: ""
  59. },
  60. // PDF文件上传地址
  61. uploadUrl: {
  62. type: String,
  63. default: ''
  64. },
  65. // 请求头对象
  66. uploadHeaders: {
  67. type: Object,
  68. default: () => {
  69. return {};
  70. }
  71. },
  72. // 上传文件标题
  73. uploadFileTitle: {
  74. type: String,
  75. default: ''
  76. },
  77. },
  78. data() {
  79. return {
  80. // 是否选择PDF文件
  81. isSelect: false,
  82. // 是否等待上传PDF文件
  83. isOnProgress: false,
  84. // 是否上传PDF文件成功
  85. isUploadSuccess: false,
  86. // PDF文件上传成功数据对象
  87. successUrl: '',
  88. // PDF文件上传成功数据对象
  89. successResp: {}
  90. };
  91. },
  92. computed: {
  93. // PDF文件上传地址
  94. actionUrl() {
  95. return this.uploadUrl;
  96. },
  97. // 请求头对象
  98. headers() {
  99. return this.uploadHeaders;
  100. },
  101. // 上传文件标题
  102. fileTitle() {
  103. if (this.uploadFileTitle) {
  104. return `文件【${this.uploadFileTitle}】`;
  105. }
  106. return '文件';
  107. },
  108. },
  109. created() {
  110. this.initData();
  111. },
  112. mounted() {
  113. },
  114. methods: {
  115. /**
  116. * 初始化数据
  117. */
  118. async initData() {
  119. },
  120. /**
  121. * 相关材料PDF文件上传预处理
  122. */
  123. handleRocPdfFileUploadBeforeUpload(file) {
  124. if (file.type.indexOf('application/pdf') === -1) {
  125. this.$refs.rocPdfFileUploadRef.clearFiles();
  126. this.msgError(`${this.fileTitle}格式错误,请上传类型格式为PDF的文件!`);
  127. return;
  128. }
  129. },
  130. /**
  131. * 相关材料PDF文件上传成功处理
  132. */
  133. handleRocPdfFileUploadOnsuccess(response, file, fileList) {
  134. // 校验封面图片是否上传成功
  135. if (response.code !== 200) {
  136. this.$refs.rocPdfFileUploadRef?.clearFiles?.();
  137. this.isSelect = false;
  138. this.isUploadSuccess = false;
  139. this.msgError(`上传PDF${this.fileTitle}失败,请重新选择PDF文件上传!`);
  140. return;
  141. }
  142. // 绑定封面图片数据
  143. this.successUrl = response?.data?.url;
  144. // PDF文件上传成功数据对象
  145. this.successResp = response?.data;
  146. this.$emit("input", response?.data?.url);
  147. this.isUploadSuccess = true;
  148. // 判断是否继续操作
  149. if (this.isOnProgress) {
  150. this.isOnProgress = false;
  151. this.$emit("wait-upload-success", this.successResp);
  152. } else {
  153. this.$alert(`PDF${this.fileTitle}上传成功!`, '上传结果', { dangerouslyUseHTMLString: true });
  154. }
  155. },
  156. /**
  157. * 相关材料PDF文件状态改变
  158. */
  159. handleRocPdfFileUploadOnchange(file, fileList) {
  160. if (file.status === 'ready') {
  161. if (((file.raw || {}).type || '').indexOf('application/pdf') === -1) {
  162. this.$refs.rocPdfFileUploadRef.clearFiles();
  163. this.isSelect = false;
  164. this.msgError(`${this.fileTitle}格式错误,请上传类型格式为PDF的文件!`);
  165. return;
  166. }
  167. this.isSelect = true;
  168. this.isUploadSuccess = false;
  169. }
  170. },
  171. /**
  172. * 相关材料PDF文件列表移除文件时的钩子
  173. */
  174. handleRocPdfFileUploadOnRemove(file, fileList) {
  175. this.successResp = {};
  176. this.successUrl = '';
  177. this.isUploadSuccess = false;
  178. this.isSelect = false;
  179. this.$emit("input", "");
  180. },
  181. /**
  182. * 相关材料PDF文件超出个数限制时的钩子
  183. */
  184. handleRocPdfFileUploadOnExceed(files, fileList) {
  185. this.msgWarning(`只允许上传单个PDF${this.fileTitle}`);
  186. },
  187. /**
  188. * 提交上传相关材料PDF文件
  189. */
  190. submitRocPdfFileUploadClick() {
  191. if (!this.isSelect) { return; }
  192. this.$confirm(`确定上传PDF${this.fileTitle}操作吗?`, '提示', {
  193. confirmButtonText: '确定 ',
  194. cancelButtonText: '取消 ',
  195. type: 'info'
  196. }).then(() => {
  197. this.$refs.rocPdfFileUploadRef?.submit?.();
  198. }).catch(() => {
  199. this.msgInfo('您已取消上传操作!');
  200. });
  201. },
  202. /**
  203. * 移除选择相关材料PDF文件
  204. */
  205. removeRocPdfFileUploadClick() {
  206. this.successResp = {};
  207. this.successUrl = '';
  208. this.$refs.rocPdfFileUploadRef.clearFiles();
  209. this.isUploadSuccess = false;
  210. this.isSelect = false;
  211. this.$emit("input", "");
  212. },
  213. /**
  214. * PDF文件地址单击事件
  215. */
  216. handleRocPdfFileUploadUrlClick() {
  217. this.$emit("pdf-url-incident", this.successResp);
  218. }
  219. }
  220. }
  221. </script>
  222. <style lang="scss">
  223. .rocpdffileupload {
  224. .rocpdffileupload-attachment {
  225. &-fileupload {
  226. .el-upload {
  227. width: 100%;
  228. border: 1px dashed #d9d9d9;
  229. border-radius: 6px;
  230. cursor: pointer;
  231. position: relative;
  232. overflow: hidden;
  233. &:hover {
  234. border-color: #409eff;
  235. }
  236. }
  237. .el-upload-list.el-upload-list--text {
  238. .el-upload-list__item:first-child {
  239. margin-top: 0;
  240. }
  241. }
  242. &-content {
  243. width: 100%;
  244. height: 32px;
  245. font-size: 14px;
  246. font-weight: 400;
  247. color: #8c939d;
  248. text-align: center;
  249. }
  250. }
  251. &-addr {
  252. width: 100%;
  253. padding: 5px 0;
  254. div {
  255. &:first-child,
  256. &:last-child {
  257. margin-left: 4px;
  258. width: 100%;
  259. height: 20px;
  260. vertical-align: middle;
  261. font-size: 14px;
  262. font-weight: 400;
  263. color: #606266;
  264. line-height: 20px;
  265. }
  266. &:last-child {
  267. height: 25px;
  268. line-height: 25px;
  269. cursor: pointer;
  270. opacity: 0.8;
  271. &:hover {
  272. color: #409eff;
  273. text-decoration: underline;
  274. }
  275. }
  276. }
  277. }
  278. }
  279. }
  280. </style>