fileUpload.vue 6.5 KB

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