fileUpload.vue 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. <template>
  2. <div class="upload-file">
  3. <el-upload
  4. class="upload-demo"
  5. action="#"
  6. v-loading="loading"
  7. element-loading-text="上传中"
  8. :http-request="handlRequest"
  9. :before-remove="beforeRemove"
  10. :on-remove="handleRemove"
  11. :on-preview="handleItem"
  12. :multiple="multiple"
  13. :before-upload="beforeUpload"
  14. :file-list="fileList"
  15. :show-file-list="!showLib"
  16. :disabled="disabled"
  17. >
  18. <!-- <slot>
  19. <el-button :disabled="disabled" type="primary" icon="el-icon-plus" size="mini">点击上传</el-button>
  20. </slot> -->
  21. <el-button
  22. slot="trigger"
  23. :disabled="disabled"
  24. type="primary"
  25. icon="el-icon-plus"
  26. size="mini"
  27. >点击上传</el-button
  28. >
  29. <slot name="templateBtn"> </slot>
  30. </el-upload>
  31. </div>
  32. </template>
  33. <script>
  34. import {
  35. getFileList,
  36. getFile,
  37. removeFile
  38. } from '@/api/system/file/index.js';
  39. import {
  40. uploadFileNew,
  41. } from './api';
  42. import { getImageUrl, getImagePath } from '@/utils/file';
  43. export default {
  44. props: {
  45. value: {
  46. type: [Array, String],
  47. default: () => []
  48. },
  49. multiple: {
  50. type: Boolean,
  51. default: false
  52. },
  53. // 所属模块
  54. module: {
  55. type: String,
  56. required: true
  57. },
  58. // 文档库
  59. showLib: {
  60. type: Boolean,
  61. default: false
  62. },
  63. // 限制数量
  64. limit: {
  65. type: Number,
  66. default: -1
  67. },
  68. // 限制大小 mb
  69. size: {
  70. type: Number,
  71. default: 1000
  72. },
  73. disabled: {
  74. default: false,
  75. type: Boolean
  76. }
  77. },
  78. data() {
  79. return {
  80. documentVisible: false,
  81. selectItem: null,
  82. loading: false,
  83. documentForm: {
  84. name: ''
  85. },
  86. columns: [
  87. {
  88. label: '序号',
  89. type: 'index',
  90. width: 55,
  91. align: 'center'
  92. },
  93. {
  94. label: '文档名称',
  95. prop: 'name',
  96. minWidth: '180',
  97. showOverflowTooltip: true
  98. },
  99. {
  100. label: '文档类型',
  101. prop: 'type'
  102. },
  103. {
  104. label: '系统'
  105. },
  106. {
  107. label: '储存路径',
  108. prop: 'storePath',
  109. minWidth: '180',
  110. showOverflowTooltip: true
  111. },
  112. {
  113. label: '模块名',
  114. prop: 'module'
  115. },
  116. {
  117. label: '上传时间',
  118. prop: 'createTime'
  119. }
  120. ]
  121. };
  122. },
  123. computed: {
  124. fileList: {
  125. set(val) {
  126. // console.log(val);
  127. this.$emit(
  128. 'input',
  129. val.map((item) => ({
  130. ...item,
  131. url: getImagePath(item.url)
  132. }))
  133. );
  134. },
  135. get() {
  136. // console.log(this.value, 2);
  137. if (!Array.isArray(this.value)) return [];
  138. const arr =
  139. (this.value &&
  140. this.value.map((item) => ({
  141. ...item,
  142. url: getImageUrl(item.url)
  143. }))) ||
  144. [];
  145. return arr;
  146. }
  147. }
  148. },
  149. methods: {
  150. //点击查看图片
  151. handleItem(file) {
  152. getFile({ objectName: file.storePath }, file.name);
  153. },
  154. delFileList() {
  155. this.$emit('input', []);
  156. },
  157. handleOpenLib() {
  158. this.documentVisible = true;
  159. this.$nextTick(() => {
  160. this.reload();
  161. });
  162. },
  163. //图文档勾选
  164. submitDocument() {
  165. this.$emit('input', [
  166. { url: this.selectItem.storePath, ...this.selectItem }
  167. ]);
  168. this.documentVisible = false;
  169. },
  170. datasource({ page, limit }) {
  171. return getFileList({
  172. ...this.documentForm,
  173. pageNum: page,
  174. size: limit
  175. });
  176. },
  177. reload() {
  178. this.$refs.table.reload();
  179. },
  180. beforeRemove(file) {
  181. if (file.id) {
  182. return removeFile({
  183. fileId: file.id
  184. }).then(() => {
  185. return true;
  186. });
  187. return true;
  188. } else {
  189. return true;
  190. }
  191. },
  192. handleRemove(file, fileList) {
  193. this.fileList = fileList;
  194. },
  195. beforeUpload(file) {
  196. // if (file.size / 1024 / 1024 > this.size) {
  197. // this.$message.error(`大小不能超过 ${this.size}MB`);
  198. // return false;
  199. // }
  200. if (this.limit > 0 && this.fileList.length === this.limit) {
  201. this.$message.error(`最多上传 ${this.limit}个文件`);
  202. return false;
  203. }
  204. this.loading = true;
  205. return uploadFileNew({
  206. module: this.module,
  207. multiPartFile: file
  208. })
  209. .then((res) => {
  210. if (res.data) {
  211. this.$emit('input', [
  212. ...(this.value || []),
  213. { ...file, url: res.data.storePath, ...res.data }
  214. ]);
  215. }
  216. this.$emit('fileChange', res.data);
  217. return res.data;
  218. })
  219. .finally(() => {
  220. this.loading = false;
  221. });
  222. },
  223. handlRequest() {
  224. return Promise.resolve();
  225. }
  226. // onSuccess(response, file, fileList){
  227. // alert(1)
  228. // this.$emit('fileChange',file)
  229. // },
  230. }
  231. };
  232. </script>
  233. <style lang="scss" scoped>
  234. .upload-file {
  235. display: flex;
  236. justify-content: flex-start;
  237. align-items: center;
  238. .lib {
  239. margin-left: 12px;
  240. }
  241. .imgs-box {
  242. margin-left: 10px;
  243. flex: 1;
  244. }
  245. .imgs-box .imgs-p {
  246. height: 30px;
  247. background: #f0f3f3;
  248. line-height: 30px;
  249. min-width: 480px;
  250. margin-bottom: 5px;
  251. padding: 0 10px;
  252. display: flex;
  253. justify-content: space-between;
  254. }
  255. }
  256. </style>