UpLoadComponentV3.1.vue 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. <!--文件上传组件-->
  2. <template>
  3. <div :key="uploadKey">
  4. <el-divider v-if="showDivider" content-position="left"><i class="el-icon-document"></i> {{dividerName}}</el-divider>
  5. <el-upload ref="upload" style="display: inline-block; :show-header='status'" action=""
  6. :limit="999" :http-request="httpRequest"
  7. multiple
  8. :on-exceed="(files, fileList) =>{
  9. $message.warning(`当前限制选择 999 个文件,本次选择了 ${files.length} 个文件,共选择了 ${files.length + fileList.length} 个文件`)
  10. }"
  11. :show-file-list="false"
  12. :on-change="changes"
  13. :on-progress="uploadVideoProcess"
  14. :file-list="fileList">
  15. <template v-if="auth==='view'&&uploadFlag===false" #tip>
  16. <el-button :loading="loading" type="primary" size="default" :disabled="true"> 点击上传 </el-button>
  17. </template>
  18. <template v-else #trigger>
  19. <el-button :loading="loading" type="primary" size="default"> 点击上传 </el-button>
  20. </template>
  21. </el-upload>
  22. <div style="height: calc(100% - 80px);margin-top: 10px">
  23. <!-- 进度条 -->
  24. <el-progress style="margin-left: 5em" v-if="progressFlag" :percentage="loadProgress"></el-progress>
  25. <el-table
  26. ref="uploadTable"
  27. :key="tableKey"
  28. v-loading="loading"
  29. :data="dataListNew">
  30. <el-table-column type="seq" width="40"></el-table-column>
  31. <el-table-column label="文件名称" prop="name" align="center">
  32. <template #default="scope">
  33. <div v-if="ifName(scope.row) === true">
  34. <el-image
  35. style="width: 30px; height: 30px;padding-top: 4px"
  36. :src="scope.row.lsUrl"
  37. :preview-src-list="[scope.row.lsUrl]"
  38. ></el-image>
  39. </div>
  40. <div v-else>
  41. <el-link type="primary" :underline="false" @click="showFile(scope.row)">{{scope.row.name}}</el-link>
  42. </div>
  43. </template>
  44. </el-table-column>
  45. <el-table-column label="创建人" prop="createBy.name" align="center"></el-table-column>
  46. <el-table-column label="创建时间" prop="createTime" align="center"></el-table-column>
  47. <el-table-column label="文件大小" prop="size" align="center">
  48. <template #default="scope">
  49. {{getSize(scope.row.size)}}
  50. </template>
  51. </el-table-column>
  52. <el-table-column label="文件类型" prop="fileType" align="center">
  53. <template #header="{ column }">
  54. <span style="color: red; font-size: 20px">*</span>
  55. <span>{{column.label}}</span>
  56. </template>
  57. <!-- <template slot="header" scope="scope">-->
  58. <!-- <span style="color:red;font-size: 20px">* </span><span>文件类型</span>-->
  59. <!-- </template>-->
  60. <template #default="scope">
  61. <el-select v-if="auth !== 'view'" v-model="scope.row.fileType" placeholder="请选择">
  62. <el-option
  63. v-for="item in $dictUtils.getDictList('cw_project_report_archive_file_type')"
  64. :key="item.value"
  65. :label="item.label"
  66. :value="item.value">
  67. </el-option>
  68. </el-select>
  69. <span v-else>{{$dictUtils.getDictLabel("cw_project_report_archive_file_type", scope.row.fileType, '')}}</span>
  70. </template>
  71. </el-table-column>
  72. <el-table-column label="备注" prop="remarks" align="center">
  73. <template #default="scope">
  74. <el-input v-if="auth !== 'view'" v-model="scope.row.remarks" placeholder="请输入"></el-input>
  75. <span v-else>{{commonJS.isEmpty(scope.row.remarks)?'':scope.row.remarks}}</span>
  76. </template>
  77. </el-table-column>
  78. <el-table-column label="操作" width="200px" fixed="right" align="center">
  79. <template #default="scope">
  80. <el-button text type="primary" icon="el-icon-edit" @click="toHref(scope.row)" :disabled="false">下载</el-button>
  81. <el-button text type="primary" icon="el-icon-delete" @click="deleteById(scope.row, scope.$index)" :disabled="auth==='view'&&delFlag === false&&createBy!==scope.row.createBy.name">删除</el-button>
  82. <!--<el-button type="text" icon="el-icon-edit"
  83. @click="handleUpdate(scope.row, scope.$index)"
  84. >{{ scope.row.btn || "修改" }}</el-button>-->
  85. </template>
  86. </el-table-column>
  87. </el-table>
  88. </div>
  89. <!-- <el-image-viewer v-if="showViewer" :on-close="closeViewer" :url-list="[url]" :zIndex=9999></el-image-viewer>-->
  90. </div>
  91. </template>
  92. <script>
  93. // eslint-disable-next-line no-unused-vars
  94. import OSSSerivce, {
  95. httpRequest,
  96. // eslint-disable-next-line no-unused-vars
  97. handleRemove,
  98. fileName,
  99. // eslint-disable-next-line no-unused-vars
  100. beforeAvatarUpload,
  101. // eslint-disable-next-line no-unused-vars
  102. openWindowOnUrl,
  103. // eslint-disable-next-line no-unused-vars
  104. toHref
  105. } from '@/api/sys/OSSService'
  106. // import ElImageViewer from 'element-ui/packages/image/src/image-viewer'
  107. import moment from 'moment'
  108. export default {
  109. data () {
  110. return {
  111. uploadKey: '',
  112. progressFlag: false,
  113. loadProgress: 0,
  114. fileList: [],
  115. dataList: [],
  116. dataListNew: [],
  117. url: '',
  118. showViewer: false,
  119. ossService: null,
  120. auth: '',
  121. directory: 'public',
  122. maxValue: 300,
  123. tableKey: '',
  124. fileLoading: true,
  125. dividerName: '',
  126. uploadFlag: false,
  127. delFlag: false,
  128. createBy: '',
  129. showDivider: true,
  130. fileType: '',
  131. remarks: '',
  132. loading: false
  133. }
  134. },
  135. watch: {
  136. },
  137. created () {
  138. this.ossService = new OSSSerivce()
  139. },
  140. components: {
  141. // ElImageViewer,
  142. fileTypeComponentA: {
  143. render: function (h) {
  144. return h('span', {
  145. domProps: {
  146. innerText: this.$dictUtils.getDictLabel('cw_project_report_archive_file_type', this.value, '')
  147. }
  148. })
  149. },
  150. props: ['options', 'value']
  151. },
  152. fileTypeComponentB: {
  153. data () {
  154. return { fileType: '' }
  155. },
  156. props: ['options', 'value'],
  157. mounted () {
  158. this.fileType = this.value
  159. },
  160. render: function (h) {
  161. return h(
  162. 'el-select',
  163. {
  164. attrs: {
  165. placeholder: '请选择',
  166. value: this.$dictUtils.getDictLabel('cw_project_report_archive_file_type', this.fileType, ''),
  167. size: 'small',
  168. clearable: true
  169. },
  170. props: ['options', 'value'],
  171. on: {
  172. change: (value) => {
  173. this.fileType = value
  174. this.$emit('updateFileType', value)
  175. }
  176. }
  177. },
  178. [
  179. this.options.map((item) => {
  180. let { label, value } = item
  181. return h('el-option', {
  182. props: {
  183. label,
  184. value: parseInt(value),
  185. key: value
  186. }
  187. })
  188. })
  189. ]
  190. )
  191. }
  192. },
  193. remarksComponentA: {
  194. render: function (h) {
  195. return h('span', {
  196. domProps: {
  197. innerText: this.value
  198. }
  199. })
  200. },
  201. props: ['value']
  202. },
  203. remarksComponentB: {
  204. data () {
  205. return { remarks: '' }
  206. },
  207. props: ['value'],
  208. mounted () {
  209. this.remarks = this.value
  210. },
  211. render: function (h) {
  212. return h(
  213. 'el-input',
  214. {
  215. attrs: {
  216. placeholder: '请输入',
  217. value: this.remarks,
  218. size: 'small',
  219. clearable: true
  220. },
  221. props: ['value'],
  222. on: {
  223. input: (value) => {
  224. this.remarks = value
  225. this.$emit('updateRemarks', value)
  226. }
  227. }
  228. }
  229. )
  230. }
  231. }
  232. },
  233. mounted () {
  234. window.onPreview = this.onPreview
  235. },
  236. methods: {
  237. /**
  238. * dividerName: 组件中divider的名称赋值
  239. * showDivider: ‘附件‘Divider是否展示
  240. * 注:值为空时,默认值为true
  241. * showDivider=false时 ‘附件‘Divider隐藏
  242. **/
  243. setDividerName (dividerName, showDivider) {
  244. if (this.commonJS.isNotEmpty(dividerName)) {
  245. this.dividerName = dividerName
  246. }
  247. if (this.commonJS.isNotEmpty(showDivider)) {
  248. if (showDivider === false) {
  249. this.showDivider = false
  250. } else {
  251. this.showDivider = true
  252. }
  253. } else {
  254. this.showDivider = true
  255. }
  256. },
  257. /**
  258. * 文件上传组件初始化
  259. * @param auth
  260. * auth的值为"view"时,不可上传/编辑文件
  261. * auth为其他值时,可上传/编辑文件
  262. * @param fileList 要显示到文件上传列表中的文件。
  263. * 注:文件必须要有url属性并且文件的url属性值必须是在oss中的路径值
  264. * 例:'/attachment-file/xxx/xxx/2022/9/08/xxx.jpg'
  265. * @param directory 要存放到oss的哪个文件夹下。
  266. * 注:值为空时,默认存放到"public"文件夹
  267. * @param maxValue 上传文件允许的最大值,单位:MB
  268. * 注:值为空时,默认值为300MB
  269. * @param dividerName 组件中divider的名称
  270. * 注:值为空时,默认值为‘附件’
  271. * @param uploadFlag ‘上传文件’按钮是否禁用
  272. * 注:值为空时,默认值为false
  273. * auth=view&&uploadFlag=false时 ‘上传文件’按钮禁用
  274. * @param delFlag ‘删除’按钮是否禁用
  275. * 注:值为空时,默认值为false
  276. * auth=view&&delFlag=false时 ‘删除’按钮禁用
  277. * @param showDivider ‘附件‘Divider是否展示
  278. * 注:值为空时,默认值为true
  279. * showDivider=false时 ‘附件‘Divider隐藏
  280. */
  281. async newUpload (auth, fileList, directory, maxValue, dividerName, uploadFlag, delFlag, showDivider) {
  282. await this.fileLoadingFalse()
  283. if (this.commonJS.isEmpty(dividerName)) {
  284. this.dividerName = '附件'
  285. } else {
  286. this.dividerName = dividerName
  287. }
  288. if (directory !== undefined && directory !== null && directory !== '' && directory !== {}) {
  289. this.directory = directory
  290. } else {
  291. this.directory = 'public'
  292. }
  293. if (maxValue !== undefined && maxValue !== null && maxValue !== '' && maxValue !== 0) {
  294. this.maxValue = maxValue
  295. } else {
  296. this.maxValue = 300
  297. }
  298. this.auth = auth
  299. if (this.commonJS.isEmpty(uploadFlag)) {
  300. this.uploadFlag = false
  301. } else {
  302. if (uploadFlag !== true && uploadFlag !== false) {
  303. this.uploadFlag = false
  304. } else {
  305. this.uploadFlag = uploadFlag
  306. }
  307. }
  308. if (this.commonJS.isEmpty(delFlag)) {
  309. this.delFlag = false
  310. } else {
  311. if (delFlag !== true && delFlag !== false) {
  312. this.delFlag = false
  313. this.createBy = delFlag
  314. } else {
  315. this.delFlag = delFlag
  316. }
  317. }
  318. if (this.commonJS.isEmpty(fileList)) {
  319. this.fileLoading = true
  320. }
  321. for await (let item of fileList) {
  322. await this.ossService.getFileSizeByUrl(item.url).then((data) => {
  323. item.lsUrl = data.url
  324. item.size = data.size
  325. this.dataList.push(item)
  326. this.dataListNew.push(item)
  327. if (this.dataList.length === fileList.length) {
  328. this.fileLoading = true
  329. }
  330. })
  331. }
  332. // this.dataList = JSON.parse(JSON.stringify(fileList))
  333. // this.dataListNew = JSON.parse(JSON.stringify(fileList))
  334. if (this.commonJS.isEmpty(showDivider)) {
  335. this.showDivider = true
  336. } else {
  337. if (showDivider === false) {
  338. this.showDivider = false
  339. } else {
  340. this.showDivider = true
  341. }
  342. }
  343. },
  344. async httpRequest (file) {
  345. await httpRequest(file, fileName(file), this.directory, this.maxValue)
  346. },
  347. uploadVideoProcess (event, file, fileList) {
  348. this.progressFlag = true // 显示进度条
  349. this.loadProgress = parseInt(event.percent) // 动态获取文件上传进度
  350. if (this.loadProgress >= 100) {
  351. this.loadProgress = 100
  352. setTimeout(() => {
  353. this.progressFlag = false
  354. }, 1000) // 一秒后关闭进度条
  355. }
  356. },
  357. getSize (value) {
  358. if (this.commonJS.isEmpty(value)) {
  359. return '0 B'
  360. } else {
  361. let val = parseInt(value)
  362. if (this.commonJS.isEmpty(val)) {
  363. return '0 B'
  364. }
  365. if (isNaN(val)) {
  366. return '0 B'
  367. }
  368. if (val === 0) {
  369. return '0 B'
  370. }
  371. let k = 1024
  372. let sizes = ['B', 'KB', 'MB', 'GB', 'PB', 'TB', 'EB', 'ZB', 'YB']
  373. let i = Math.floor(Math.log(val) / Math.log(k))
  374. return (val / Math.pow(k, i)).toPrecision(3) + '' + sizes[i]
  375. }
  376. },
  377. async changes (file, fileList) {
  378. // if (file.status !== 'ready') {
  379. // return
  380. // }
  381. if (!beforeAvatarUpload(file, fileList, this.maxValue)) {
  382. this.$message.error('文件大小不能超过 ' + this.maxValue + ' MB!')
  383. return
  384. }
  385. this.dataListNew = []
  386. this.dataList.forEach((item) => {
  387. this.dataListNew.push(item)
  388. })
  389. for (let item of fileList) {
  390. item.createTime = moment(new Date()).format('YYYY-MM-DD HH:mm:ss')
  391. item.createBy = {
  392. id: '',
  393. name: ''
  394. }
  395. item.createBy.id = this.$store.state.user.id
  396. item.createBy.name = this.$store.state.user.name
  397. this.dataListNew.push(item)
  398. }
  399. for await (let item of this.dataListNew) {
  400. if (item.raw !== undefined && item.raw !== null && item.raw !== {}) {
  401. item.url = item.raw.url
  402. if (item.raw.url !== undefined && item.raw.url !== null && item.raw.url !== {}) {
  403. await this.ossService.getTemporaryUrl(item.raw.url).then((data) => {
  404. item.lsUrl = data
  405. })
  406. }
  407. }
  408. }
  409. this.tableKey = Math.random()
  410. this.uploadKey = Math.random()
  411. },
  412. showFile (row) {
  413. openWindowOnUrl(row)
  414. },
  415. onPreview (url) {
  416. this.url = url
  417. this.showViewer = true
  418. },
  419. // 关闭查看器
  420. closeViewer () {
  421. this.url = ''
  422. this.showViewer = false
  423. },
  424. toHref (row) {
  425. toHref(row)
  426. },
  427. async deleteById (row, index) {
  428. await this.dataListNew.splice(index, 1)
  429. await this.dataList.splice(index, 1)
  430. this.tableKey = Math.random()
  431. this.uploadKey = Math.random()
  432. },
  433. /**
  434. * 关闭dialog时使用 清除el-upload中上传的文件
  435. */
  436. clearUpload () {
  437. this.$refs.upload.uploadFiles = []
  438. this.dataList = []
  439. this.dataListNew = []
  440. this.createBy = ''
  441. },
  442. /**
  443. * 获取当前文件列表中的文件数据
  444. */
  445. getDataList () {
  446. let _this = this
  447. const waitForEach = function () {
  448. return new Promise(function (resolve, reject) {
  449. _this.dataListNew.forEach((item, index) => {
  450. if (item.btnType === 'save') {
  451. _this.dataListNew[index].fileTypeComponent = 'fileTypeComponentA'
  452. _this.dataListNew[index].remarksComponent = 'remarksComponentA'
  453. _this.dataListNew[index].btn = '修改'
  454. _this.dataListNew[index].btnType = 'edit'
  455. _this.dataListNew[index].fileType = _this.fileType
  456. _this.dataListNew[index].remarks = _this.remarks
  457. }
  458. })
  459. _this.tableKey = Math.random()
  460. _this.uploadKey = Math.random()
  461. resolve(_this.dataListNew)
  462. })
  463. }
  464. return waitForEach()
  465. },
  466. /**
  467. * 判断进度条是否结束,附件是否加载完成
  468. * @returns {boolean}
  469. */
  470. checkProgress () {
  471. if (this.progressFlag === true) {
  472. this.$message.warning('请等待附件上传完成再进行操作')
  473. return true
  474. }
  475. if (this.fileLoading === false) {
  476. this.$message.warning('请等待附件加载完成再进行操作')
  477. return true
  478. }
  479. return false
  480. },
  481. ifName (row) {
  482. if (this.commonJS.isEmpty(row.name)) {
  483. row.name = '---'
  484. return false
  485. }
  486. let suffix = row.name.substring(row.name.lastIndexOf('.') + 1)
  487. if (suffix === 'jpg' || suffix === 'png' || suffix === 'gif' || suffix === 'bmp' || suffix === 'jpeg') {
  488. return true
  489. } else {
  490. return false
  491. }
  492. },
  493. fileLoadingFalse () {
  494. this.fileLoading = false
  495. },
  496. /** 修改||保存按钮操作 */
  497. handleUpdate: async function (row, rowIndex) {
  498. if (row.btnType === 'edit' || this.commonJS.isEmpty(row.btnType)) {
  499. let _this = this
  500. const waitForEach = function () {
  501. return new Promise(function (resolve, reject) {
  502. _this.dataListNew.forEach((item, index) => {
  503. if (item.btnType === 'save') {
  504. _this.dataListNew[index].fileTypeComponent = 'fileTypeComponentA'
  505. _this.dataListNew[index].remarksComponent = 'remarksComponentA'
  506. _this.dataListNew[index].btn = '修改'
  507. _this.dataListNew[index].btnType = 'edit'
  508. _this.dataListNew[index].fileType = _this.fileType
  509. _this.dataListNew[index].remarks = _this.remarks
  510. }
  511. })
  512. resolve()
  513. })
  514. }
  515. waitForEach().then(() => {
  516. this.dataListNew[rowIndex].fileTypeComponent = 'fileTypeComponentB'
  517. this.dataListNew[rowIndex].remarksComponent = 'remarksComponentB'
  518. this.dataListNew[rowIndex].btn = '保存'
  519. this.dataListNew[rowIndex].btnType = 'save'
  520. })
  521. } else {
  522. this.dataListNew[rowIndex].fileTypeComponent = 'fileTypeComponentA'
  523. this.dataListNew[rowIndex].remarksComponent = 'remarksComponentA'
  524. this.dataListNew[rowIndex].btn = '修改'
  525. this.dataListNew[rowIndex].btnType = 'edit'
  526. this.dataListNew[rowIndex].fileType = this.fileType
  527. this.dataListNew[rowIndex].remarks = this.remarks
  528. }
  529. this.tableKey = Math.random()
  530. this.uploadKey = Math.random()
  531. },
  532. updateFileType (fileType) {
  533. this.fileType = fileType
  534. },
  535. updateRemarks (remarks) {
  536. this.remarks = remarks
  537. },
  538. tableRowClassName ({ row, rowIndex }) {
  539. row.index = rowIndex
  540. },
  541. // 行点击事件
  542. tableRowClick (row, column) {
  543. if (this.auth !== 'view') {
  544. if (column.label !== '备注' && column.label !== '文件名称' && column.label !== '操作') {
  545. let _this = this
  546. const waitForEach = function () {
  547. return new Promise(function (resolve, reject) {
  548. _this.dataListNew.forEach((item, index) => {
  549. if (item.btnType === 'save') {
  550. _this.dataListNew[index].fileTypeComponent = 'fileTypeComponentA'
  551. _this.dataListNew[index].remarksComponent = 'remarksComponentA'
  552. _this.dataListNew[index].btn = '修改'
  553. _this.dataListNew[index].btnType = 'edit'
  554. _this.dataListNew[index].fileType = _this.fileType
  555. _this.dataListNew[index].remarks = _this.remarks
  556. }
  557. })
  558. resolve()
  559. })
  560. }
  561. waitForEach().then(() => {
  562. this.fileType = this.dataListNew[row.index].fileType
  563. this.remarks = this.dataListNew[row.index].remarks
  564. this.dataListNew[row.index].fileTypeComponent = 'fileTypeComponentB'
  565. this.dataListNew[row.index].remarksComponent = 'remarksComponentB'
  566. this.dataListNew[row.index].btn = '保存'
  567. this.dataListNew[row.index].btnType = 'save'
  568. })
  569. this.tableKey = Math.random()
  570. this.uploadKey = Math.random()
  571. }
  572. }
  573. },
  574. // 点击上传文件按钮的时候,把未保存的文件保存一下
  575. clickHandel () {
  576. this.dataListNew.forEach((item, index) => {
  577. if (item.btnType === 'save') {
  578. this.dataListNew[index].fileTypeComponent = 'fileTypeComponentA'
  579. this.dataListNew[index].remarksComponent = 'remarksComponentA'
  580. this.dataListNew[index].btn = '修改'
  581. this.dataListNew[index].btnType = 'edit'
  582. this.dataListNew[index].fileType = this.fileType
  583. this.dataListNew[index].remarks = this.remarks
  584. }
  585. })
  586. },
  587. // 开启/关闭页面的加载中状态
  588. changeLoading (loading) {
  589. if (this.commonJS.isNotEmpty(loading)) {
  590. this.loading = loading
  591. } else {
  592. this.loading = false
  593. }
  594. }
  595. }
  596. }
  597. </script>