img-row.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <template>
  2. <div class="img-row" >
  3. <!-- 升级img展示组件-->
  4. <cip-dynamic-image class="img-row__img-item" :src="realUrl"></cip-dynamic-image>
  5. <!-- <img class="img-row__img-item" :src="file.url">-->
  6. <!--进度条仅在当前页面上传的文件中显示-->
  7. <template v-if="file.status !== 'success' && file.percentage <= 100">
  8. <el-progress style="flex-grow: 2" type="circle" :percentage="file.percentage" :status="file.status"></el-progress>
  9. <div class="progress-cancel">
  10. <el-button v-if="!file.status" size="mini" type="text" @click="cancelUpload(file)">取消</el-button>
  11. </div>
  12. </template>
  13. <template v-else>
  14. <div class="handle-button-wrapper">
  15. <el-button size="mini" type="text" @click="preview"><i class="el-icon-zoom-in"></i></el-button>
  16. <el-button size="mini" type="text" @click="download"><i class="el-icon-download"></i></el-button>
  17. <el-button size="mini" type="text" @click="remove" :loading="removeLoading" v-if="removable"><i class="el-icon-delete"></i></el-button>
  18. </div>
  19. </template>
  20. </div>
  21. </template>
  22. <script>
  23. import { computed, ref } from 'vue'
  24. import { ElMessageBox, ElButton, ElProgress } from 'element-plus'
  25. import CipDynamicImage from '../../../cip-dynamic-image'
  26. import apiConfig from '@cip/request/apiConfig'
  27. import { getValueByTemplate } from '@cip/utils/util'
  28. export default {
  29. components: { ElButton, ElProgress, CipDynamicImage },
  30. props: {
  31. file: {
  32. type: Object,
  33. default: () => { return {} }
  34. },
  35. config: Object,
  36. removable: Boolean
  37. },
  38. setup (props, { emit }) {
  39. const realUrl = computed(() => {
  40. const url = props.file.url
  41. if (props.config.imgTemplate) {
  42. return props.config.imgTemplate(url)
  43. } else {
  44. return url
  45. }
  46. })
  47. const removeLoading = ref(false)
  48. const fileValue = computed(() => {
  49. return props.file
  50. })
  51. const cancelUpload = (file) => {
  52. file.abort()
  53. }
  54. const preview = () => {
  55. emit('preview', fileValue.value)
  56. }
  57. const download = () => {
  58. const download = document.createElement('a')
  59. download.setAttribute('href', getValueByTemplate(realUrl.value, apiConfig) + '?response-content-type=application/octet-stream')
  60. download.setAttribute('download', fileValue.value.name || fileValue.value.url)
  61. download.click()
  62. }
  63. const remove = () => {
  64. removeLoading.value = true
  65. const name = fileValue.value.name
  66. const message = name ? `确认删除《${name}》` : '确认删除'
  67. ElMessageBox.confirm(message, '警告', {
  68. type: 'warning'
  69. }).then(() => {
  70. emit('after-remove', fileValue.value)
  71. }).catch((e) => {
  72. console.info('用户取消删除')
  73. }).finally(() => {
  74. removeLoading.value = false
  75. })
  76. }
  77. return {
  78. realUrl,
  79. cancelUpload,
  80. preview,
  81. download,
  82. remove,
  83. removeLoading
  84. }
  85. }
  86. }
  87. </script>
  88. <style lang="less">
  89. .img-row{
  90. overflow: hidden;
  91. background-color: #fff;
  92. border: 1px solid #c0ccda;
  93. border-radius: 6px;
  94. box-sizing: border-box;
  95. width: 148px;
  96. height: 148px;
  97. margin: 0 8px 8px 0;
  98. display: inline-flex;
  99. align-items: center;
  100. justify-content: center;
  101. position: relative;
  102. transition: all .5s cubic-bezier(.55,0,.1,1);
  103. font-size: 14px;
  104. color: #606266;
  105. line-height: 1.8;
  106. &:hover{
  107. .handle-button-wrapper{
  108. opacity: 0.4;
  109. }
  110. }
  111. &__img-item{
  112. width: 100%;
  113. height: 100%;
  114. }
  115. &__process{
  116. display: flex;
  117. align-items: center;
  118. justify-content: space-between;
  119. }
  120. .handle-button-wrapper{
  121. position: absolute;
  122. width: 100%;
  123. height: 100%;
  124. left: 0;
  125. top: 0;
  126. cursor: default;
  127. text-align: center;
  128. color: #fff;
  129. opacity: 0;
  130. font-size: 20px;
  131. transition: opacity .3s;
  132. background-color: rgba(0,0,0,.5);
  133. i{
  134. font-size: 22px;
  135. color: #fff;
  136. }
  137. }
  138. &__button{
  139. cursor: pointer;
  140. flex-shrink: 0;
  141. &+.file-row__button{
  142. margin-left:4px;
  143. }
  144. }
  145. .el-progress{
  146. position: absolute;
  147. top: 50%;
  148. left: 50%;
  149. transform: translate(-50%, -50%);
  150. }
  151. .progress-cancel{
  152. position: absolute;
  153. top: 0;
  154. width: 100%;
  155. height: 100%;
  156. display: flex;
  157. align-items: center;
  158. justify-content:center;
  159. .el-button{
  160. padding-top: 60px;
  161. }
  162. }
  163. }
  164. </style>