img-row.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <template>
  2. <div class="img-row" :style="{width: config.itemWidth, height: config.itemHeight}">
  3. <!-- 升级img展示组件-->
  4. <Media class="img-row__img-item" :type="config.mediaType" :src="realUrl"></Media>
  5. <!-- <img class="img-row__img-item" :src="file.url">-->
  6. <!--进度条仅在当前页面上传的文件中显示-->
  7. <template v-if="file.status === 'ready'">
  8. <!-- <el-progress style="flex-grow: 2" type="circle" :percentage="file.percentage" :status="file.status" :width="60"></el-progress> -->
  9. <div class="progress-cancel">
  10. ({{parseFileSize(file.loaded)}}/{{parseFileSize(file.total)}})
  11. <cip-button-text size="small" @click="cancelUpload(file)">取消</cip-button-text>
  12. </div>
  13. <el-progress :stroke-width="4" style="top:50%;width:80%" indeterminate :percentage="72" />
  14. </template>
  15. <template v-else>
  16. <div class="handle-button-wrapper">
  17. <template v-if="file.status!=='error'">
  18. <i class="el-icon-view" @click="preview"></i>
  19. <i class="el-icon-download" @click="download"></i>
  20. </template>
  21. <!-- <cip-table-button size="small" @click="preview" >
  22. <i class="el-icon-zoom-in"></i>
  23. </cip-table-button>
  24. <cip-table-button size="small" @click="download">
  25. <i class="el-icon-download"></i>
  26. </cip-table-button>
  27. <cip-table-button size="small" @click="remove" :loading="removeLoading" v-if="removable">
  28. <i class="el-icon-delete"></i>
  29. </cip-table-button> -->
  30. </div>
  31. <div v-if="removable" class="upload--close">
  32. <ElIcon @click="remove"><CircleCloseFilled /></ElIcon>
  33. </div>
  34. </template>
  35. </div>
  36. </template>
  37. <script>
  38. import { computed, ref } from 'vue'
  39. import { ElMessageBox, ElProgress, ElIcon } from 'element-plus'
  40. import Media from './media'
  41. import { Dialog as VanDialog } from 'vant'
  42. import apiConfig from '@cip/request/apiConfig'
  43. import { getValueByTemplate, getEquipmentType } from '@cip/utils/util'
  44. import { CircleCloseFilled } from '@element-plus/icons-vue'
  45. import CipButtonText from '@cip/components/cip-button-text'
  46. export default {
  47. components: { ElProgress, Media, ElIcon, CircleCloseFilled, CipButtonText },
  48. props: {
  49. file: {
  50. type: Object,
  51. default: () => { return {} }
  52. },
  53. config: Object,
  54. removable: Boolean
  55. },
  56. setup (props, { emit }) {
  57. // 根据文件大小 显示 B kb mb
  58. const parseFileSize = (size) => {
  59. let unit = 'k'
  60. if (size > 1024) {
  61. size = size / 1024
  62. unit = 'kb'
  63. }
  64. if (size > 1024) {
  65. size = size / 1024
  66. unit = 'mb'
  67. }
  68. return `${Math.floor(size)}${unit}`
  69. }
  70. const isMobile = getEquipmentType() === 'mobile'
  71. const realUrl = computed(() => {
  72. const url = props.file.url
  73. if (props.config.imgTemplate) {
  74. return props.config.imgTemplate(url)
  75. } else {
  76. return url
  77. }
  78. })
  79. const removeLoading = ref(false)
  80. const fileValue = computed(() => {
  81. return props.file
  82. })
  83. const cancelUpload = (file) => {
  84. file.abort()
  85. }
  86. const preview = () => {
  87. emit('preview', fileValue.value)
  88. }
  89. const download = () => {
  90. const download = document.createElement('a')
  91. download.setAttribute('href', getValueByTemplate(realUrl.value, apiConfig) + '?response-content-type=application/octet-stream')
  92. const fileName = fileValue.value.name || fileValue.value.url.split('?')[0].split('/').pop()
  93. download.setAttribute('download', fileName)
  94. download.click()
  95. }
  96. const remove = async () => {
  97. removeLoading.value = true
  98. const name = fileValue.value.name
  99. const message = name ? `确认删除《${name}》` : '确认删除'
  100. try {
  101. isMobile
  102. ? await VanDialog.confirm({
  103. title: '警告',
  104. message: `确认删除《${fileValue.value.name}》`
  105. })
  106. : await ElMessageBox.confirm(message, '警告', {
  107. type: 'warning'
  108. })
  109. emit('after-remove', fileValue.value)
  110. } catch (error) {
  111. console.info('用户取消删除')
  112. } finally {
  113. removeLoading.value = false
  114. }
  115. }
  116. return {
  117. realUrl,
  118. cancelUpload,
  119. preview,
  120. download,
  121. remove,
  122. removeLoading,
  123. parseFileSize
  124. }
  125. }
  126. }
  127. </script>
  128. <style lang="less">
  129. .img-row {
  130. box-sizing: border-box;
  131. width: 80px;
  132. height: 80px;
  133. margin: 0 8px 8px 0;
  134. padding: 0;
  135. display: inline-flex;
  136. overflow: visible;
  137. border-radius: 2px;
  138. border: 0;
  139. background-color: var(--el-fill-color-lighter);
  140. position: relative;
  141. .el-progress__text {
  142. display: none;
  143. }
  144. .upload--close {
  145. line-height: 0;
  146. display: none;
  147. cursor: pointer;
  148. position: absolute;
  149. right: -6px;
  150. top: -6px;
  151. width: 14px;
  152. height: 14px;
  153. border-radius: 50%;
  154. background-color: #fff;
  155. z-index: 1001;
  156. i{
  157. font-size: 16px;
  158. color: var(--el-color-danger);
  159. position: relative;
  160. top: -1px;
  161. left: -1px;
  162. width: 16px;
  163. height: 16px;
  164. }
  165. }
  166. &:hover {
  167. .handle-button-wrapper {
  168. opacity: 1;
  169. z-index: 1000;
  170. }
  171. .upload--close {
  172. display: block;
  173. }
  174. }
  175. &__img-item {
  176. width: 100%;
  177. height: 100%;
  178. }
  179. &__process {
  180. display: flex;
  181. align-items: center;
  182. justify-content: space-between;
  183. }
  184. .handle-button-wrapper {
  185. position: absolute;
  186. width: 100%;
  187. height: 100%;
  188. left: 0;
  189. top: 0;
  190. justify-content: center;
  191. color: #fff;
  192. opacity: 0;
  193. font-size: 20px;
  194. transition: opacity .3s;
  195. background-color: rgba(0, 0, 0, .3);
  196. display: flex;
  197. align-items: center;
  198. i {
  199. font-size: 22px;
  200. color: #fff;
  201. margin: 0 4px;
  202. cursor: pointer;
  203. }
  204. }
  205. &__button {
  206. cursor: pointer;
  207. flex-shrink: 0;
  208. &+.file-row__button {
  209. margin-left: 4px;
  210. }
  211. }
  212. .el-progress {
  213. position: absolute;
  214. top: 50%;
  215. left: 50%;
  216. transform: translate(-50%, -50%);
  217. }
  218. .progress-cancel {
  219. position: absolute;
  220. top: 50%;
  221. transform: translate(0, 10px);
  222. width: 100%;
  223. display: flex;
  224. align-items: center;
  225. justify-content: center;
  226. .el-button {
  227. padding-top: 60px;
  228. }
  229. }
  230. }
  231. </style>