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">点击上传</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. console.log(file,'5555')
  184. getFile({ objectName: file.storePath }, file.name);
  185. },
  186. delFileList () {
  187. this.$emit('input', []);
  188. },
  189. handleOpenLib () {
  190. this.documentVisible = true;
  191. this.$nextTick(() => {
  192. this.reload();
  193. });
  194. },
  195. //图文档勾选
  196. submitDocument () {
  197. this.$emit('input', [
  198. { url: this.selectItem.storePath, ...this.selectItem }
  199. ]);
  200. this.documentVisible = false;
  201. },
  202. datasource ({ page, limit }) {
  203. return getFileList({
  204. ...this.documentForm,
  205. pageNum: page,
  206. size: limit
  207. });
  208. },
  209. reload () {
  210. this.$refs.table.reload();
  211. },
  212. beforeRemove (file) {
  213. if (file.id) {
  214. return removeFile({
  215. fileId: file.id
  216. }).then(() => {
  217. return true;
  218. });
  219. } else {
  220. return true;
  221. }
  222. },
  223. handleRemove (file, fileList) {
  224. this.fileList = fileList;
  225. },
  226. beforeUpload (file) {
  227. if (file.size / 1024 / 1024 > this.size) {
  228. this.$message.error(`大小不能超过 ${this.size}MB`);
  229. return false;
  230. }
  231. if (this.limit > 0 && this.fileList.length === this.limit) {
  232. this.$message.error(`最多上传 ${this.limit}个文件`);
  233. return false;
  234. }
  235. return uploadFile({
  236. module: this.module,
  237. multiPartFile: file
  238. }).then((res) => {
  239. if (res.data) {
  240. this.$emit('input', [
  241. ...(this.value || []),
  242. { ...file, url: res.data.storePath, ...res.data }
  243. ]);
  244. }
  245. return res.data;
  246. });
  247. },
  248. handlRequest () {
  249. return Promise.resolve();
  250. }
  251. }
  252. };
  253. </script>
  254. <style lang="scss" scoped>
  255. .upload-file {
  256. display: flex;
  257. justify-content: flex-start;
  258. align-items: center;
  259. .lib {
  260. margin-left: 12px;
  261. }
  262. .imgs-box {
  263. margin-left: 10px;
  264. flex: 1;
  265. }
  266. .imgs-box .imgs-p {
  267. height: 30px;
  268. background: #f0f3f3;
  269. line-height: 30px;
  270. min-width: 480px;
  271. margin-bottom: 5px;
  272. padding: 0 10px;
  273. display: flex;
  274. justify-content: space-between;
  275. }
  276. }
  277. </style>