enterpriseAlbum.vue 3.9 KB

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