enterpriseAlbum.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 { uploadFile } 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. const emit = defineEmits(['complete'])
  50. const { t } = useI18n()
  51. const imgList = ref([])
  52. const getInfo = async () => {
  53. const data = await getEnterpriseBaseInfo()
  54. // 完成度展示
  55. emit('complete', { status: Boolean(data?.albumList?.length), id: 'enterpriseAlbum' })
  56. if (!data || !data.albumList) return
  57. imgList.value = data.albumList
  58. }
  59. getInfo()
  60. // 预览
  61. const showPreview = ref(false)
  62. const current = ref(0)
  63. const handleClick = (index) => {
  64. showPreview.value = !showPreview.value
  65. current.value = index
  66. }
  67. // 删除
  68. const handleDelete = async (url) => {
  69. const index = imgList.value.indexOf(url)
  70. if (index === -1) return
  71. Confirm('系统提示', '是否确认删除?').then(async () => {
  72. await updateEnterpriseAlbum({ albumList: imgList.value })
  73. Snackbar.success('删除成功')
  74. imgList.value.splice(index, 1)
  75. })
  76. }
  77. // 选择文件
  78. const fileInput = ref()
  79. const clicked = ref(false)
  80. const openFileInput = () => {
  81. if (clicked.value) return
  82. clicked.value = true
  83. fileInput.value.click()
  84. clicked.value = false
  85. }
  86. // 上传
  87. const handleUploadFile = async (e) => {
  88. if (!e.target.files.length) return
  89. const file = e.target.files[0]
  90. const size = file.size
  91. if (size / (1024*1024) > 10) {
  92. Snackbar.warning(t('common.fileSizeExceed'))
  93. return
  94. }
  95. const formData = new FormData()
  96. formData.append('file', file)
  97. const { data } = await uploadFile(formData)
  98. if (!data) return
  99. Snackbar.success(t('common.uploadSucMsg'))
  100. imgList.value.push(data)
  101. await updateEnterpriseAlbum({ albumList: imgList.value })
  102. getInfo()
  103. }
  104. </script>
  105. <style lang="scss" scoped>
  106. .imgItem {
  107. width: 172px;
  108. height: 172px;
  109. border: 1px solid rgba(188, 188, 188, 0.5);
  110. border-radius: 5px;
  111. margin-right: 24px;
  112. margin-bottom: 24px;
  113. display: inline-block;
  114. position: relative;
  115. cursor: pointer;
  116. .operate {
  117. display: none;
  118. }
  119. &:hover {
  120. .operate {
  121. position: absolute;
  122. bottom: 0;
  123. line-height: 32px;
  124. background-color: #00000080;
  125. width: 100%;
  126. display: flex;
  127. justify-content: space-between;
  128. padding: 0 4px;
  129. span {
  130. color: #fff;
  131. cursor: pointer;
  132. }
  133. }
  134. }
  135. }
  136. .mdi-image-broken-variant {
  137. font-size: 24px;
  138. color: rgba(188, 188, 188, 0.8);
  139. }
  140. </style>