enterpriseAlbum.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <!-- 企业相册 -->
  2. <template>
  3. <div v-if="!imgList.length" class="text-center">
  4. <Empty :elevation="false" message="您还没有企业相册,马上去上传图片吧!"></Empty>
  5. <v-btn prepend-icon="mdi mdi-upload" color="primary" @click.stop="openFileInput">
  6. 上传图片/视频
  7. <input
  8. type="file"
  9. ref="fileInput"
  10. accept="image/png, image/jpg, image/jpeg, video/*"
  11. style="display: none;"
  12. @change="handleUploadFile"
  13. />
  14. </v-btn>
  15. </div>
  16. <div v-else>
  17. <div class="mb-3">
  18. <v-btn prepend-icon="mdi mdi-upload" color="primary" @click.stop="openFileInput">
  19. 上传图片/视频
  20. <input
  21. type="file"
  22. ref="fileInput"
  23. accept="image/png, image/jpg, image/jpeg, video/*"
  24. style="display: none;"
  25. @change="handleUploadFile"
  26. />
  27. </v-btn>
  28. </div>
  29. <div class="imgItem" v-for="(item, index) in imgList" :key="index">
  30. <v-img v-if="checkIsImage(item)" width="100%" height="100%" :src="item" @click="handleClick(index)"></v-img>
  31. <video v-else class="videos-radius mr-3" :src="item" controls height="172" width="172" preload="preload" @click="handleClick(index)"></video>
  32. <div class="operate">
  33. <span></span>
  34. <span class="mdi mdi-trash-can-outline" @click="handleDelete(item)"></span>
  35. </div>
  36. </div>
  37. </div>
  38. <PreviewImg v-if="showPreview" :current="current" :list="imgList" @close="showPreview = !showPreview"></PreviewImg>
  39. </template>
  40. <script setup>
  41. defineOptions({name: 'informationSettingsComponents-enterpriseAlbum'})
  42. import { ref } from 'vue'
  43. import { checkIsImage } from '@/utils'
  44. import { uploadImage } from '@/api/common'
  45. import { useI18n } from '@/hooks/web/useI18n'
  46. import { getEnterpriseBaseInfo, updateEnterpriseAlbum } from '@/api/enterprise'
  47. import Snackbar from '@/plugins/snackbar'
  48. import Confirm from '@/plugins/confirm'
  49. import cloneDeep from 'lodash/cloneDeep'
  50. const emit = defineEmits(['complete'])
  51. const { t } = useI18n()
  52. const imgList = ref([])
  53. const getInfo = async () => {
  54. const data = await getEnterpriseBaseInfo()
  55. // 完成度展示
  56. emit('complete', {
  57. totalCount: 10,
  58. completeCount: data?.albumList?.length ? 10 : 0,
  59. id: 'enterpriseAlbum'
  60. })
  61. if (!data || !data.albumList) return
  62. imgList.value = data.albumList
  63. }
  64. getInfo()
  65. // 预览
  66. const showPreview = ref(false)
  67. const current = ref(0)
  68. const handleClick = (index) => {
  69. showPreview.value = !showPreview.value
  70. current.value = index
  71. }
  72. // 删除
  73. const handleDelete = async (url) => {
  74. const index = imgList.value.indexOf(url)
  75. if (index === -1) return
  76. const albumList = cloneDeep(imgList.value)
  77. albumList.splice(index, 1)
  78. Confirm('系统提示', '是否确认删除?').then(async () => {
  79. await updateEnterpriseAlbum({ albumList })
  80. Snackbar.success('删除成功')
  81. imgList.value.splice(index, 1)
  82. getInfo()
  83. })
  84. }
  85. // 选择文件
  86. const fileInput = ref()
  87. const clicked = ref(false)
  88. const openFileInput = () => {
  89. if (clicked.value) return
  90. clicked.value = true
  91. fileInput.value.click()
  92. clicked.value = false
  93. }
  94. // 上传
  95. const handleUploadFile = async (e) => {
  96. if (!e.target.files.length) return
  97. const file = e.target.files[0]
  98. const size = file.size
  99. if (size / (1024*1024) > 10) {
  100. Snackbar.warning(t('common.fileSizeExceed'))
  101. return
  102. }
  103. const formData = new FormData()
  104. formData.append('file', file)
  105. const { data } = await uploadImage(formData)
  106. if (!data) return
  107. Snackbar.success(t('common.uploadSucMsg'))
  108. imgList.value.push(data)
  109. await updateEnterpriseAlbum({ albumList: imgList.value })
  110. getInfo()
  111. }
  112. </script>
  113. <style lang="scss" scoped>
  114. .imgItem {
  115. width: 172px;
  116. height: 172px;
  117. border: 1px solid rgba(188, 188, 188, 0.5);
  118. border-radius: 5px;
  119. margin-right: 24px;
  120. margin-bottom: 24px;
  121. display: inline-block;
  122. position: relative;
  123. cursor: pointer;
  124. .operate {
  125. display: none;
  126. }
  127. &:hover {
  128. .operate {
  129. position: absolute;
  130. bottom: 0;
  131. line-height: 32px;
  132. background-color: #00000080;
  133. width: 100%;
  134. display: flex;
  135. justify-content: space-between;
  136. padding: 0 4px;
  137. span {
  138. color: #fff;
  139. cursor: pointer;
  140. }
  141. }
  142. }
  143. }
  144. .mdi-image-broken-variant {
  145. font-size: 24px;
  146. color: rgba(188, 188, 188, 0.8);
  147. }
  148. </style>