fileUpload.vue 6.6 KB

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