fileUpload.vue 5.8 KB

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