fileUpload.vue 6.2 KB

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