selectImage.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. <template>
  2. <div
  3. v-if="!multiple"
  4. class="update-image"
  5. :style="{
  6. 'background-image': `url(${getUrl(modelValue)})`,
  7. 'position': 'relative',
  8. }"
  9. >
  10. <el-icon
  11. v-if="isVideoExt(modelValue || '')"
  12. :size="32"
  13. class="video video-icon"
  14. style=""
  15. >
  16. <VideoPlay />
  17. </el-icon>
  18. <video
  19. v-if="isVideoExt(modelValue || '')"
  20. class="avatar video-avatar video"
  21. muted
  22. preload="metadata"
  23. style=""
  24. @click="openChooseImg"
  25. >
  26. <source :src="getUrl(modelValue) + '#t=1'">
  27. </video>
  28. <span
  29. v-if="modelValue"
  30. class="update"
  31. style="position: absolute;"
  32. @click="openChooseImg"
  33. >
  34. <el-icon>
  35. <delete />
  36. </el-icon>
  37. 删除</span>
  38. <span
  39. v-else
  40. class="update text-gray-600"
  41. @click="openChooseImg"
  42. >
  43. <el-icon>
  44. <plus />
  45. </el-icon>
  46. 上传</span>
  47. </div>
  48. <div
  49. v-else
  50. class="multiple-img"
  51. >
  52. <div
  53. v-for="(item, index) in multipleValue"
  54. :key="index"
  55. class="update-image"
  56. :style="{
  57. 'background-image': `url(${getUrl(item)})`,
  58. 'position': 'relative',
  59. }"
  60. >
  61. <el-icon
  62. v-if="isVideoExt(item || '')"
  63. :size="32"
  64. class="video video-icon"
  65. >
  66. <VideoPlay />
  67. </el-icon>
  68. <video
  69. v-if="isVideoExt(item || '')"
  70. class="avatar video-avatar video"
  71. muted
  72. preload="metadata"
  73. @click="deleteImg(index)"
  74. >
  75. <source :src="getUrl(item) + '#t=1'">
  76. </video>
  77. <span
  78. class="update"
  79. style="position: absolute;"
  80. @click="deleteImg(index)"
  81. >
  82. <el-icon>
  83. <delete />
  84. </el-icon>
  85. 删除</span>
  86. </div>
  87. <div
  88. v-if="!maxUpdateCount || maxUpdateCount>multipleValue.length"
  89. class="add-image"
  90. >
  91. <span
  92. class="update text-gray-600"
  93. @click="openChooseImg"
  94. >
  95. <el-icon>
  96. <Plus />
  97. </el-icon>
  98. 上传</span>
  99. </div>
  100. </div>
  101. <el-drawer
  102. v-model="drawer"
  103. title="媒体库"
  104. size="650px"
  105. >
  106. <warning-bar
  107. title="点击“文件名/备注”可以编辑文件名或者备注内容。"
  108. />
  109. <div class="gva-btn-list">
  110. <upload-common
  111. v-model:imageCommon="imageCommon"
  112. class="upload-btn-media-library"
  113. @on-success="getImageList"
  114. />
  115. <upload-image
  116. v-model:imageUrl="imageUrl"
  117. :file-size="512"
  118. :max-w-h="1080"
  119. class="upload-btn-media-library"
  120. @on-success="getImageList"
  121. />
  122. <el-form
  123. ref="searchForm"
  124. :inline="true"
  125. :model="search"
  126. >
  127. <el-form-item label="">
  128. <el-input
  129. v-model="search.keyword"
  130. class="keyword"
  131. placeholder="请输入文件名或备注"
  132. />
  133. </el-form-item>
  134. <el-form-item>
  135. <el-button
  136. type="primary"
  137. icon="search"
  138. @click="getImageList"
  139. >查询
  140. </el-button>
  141. </el-form-item>
  142. </el-form>
  143. </div>
  144. <div class="media">
  145. <div
  146. v-for="(item,key) in picList"
  147. :key="key"
  148. class="media-box"
  149. >
  150. <div class="header-img-box-list">
  151. <el-image
  152. :key="key"
  153. :src="getUrl(item.url)"
  154. fit="cover"
  155. style="width: 100%;height: 100%;"
  156. @click="chooseImg(item.url)"
  157. >
  158. <template #error>
  159. <el-icon
  160. v-if="isVideoExt(item.url || '')"
  161. :size="32"
  162. class="video video-icon"
  163. >
  164. <VideoPlay />
  165. </el-icon>
  166. <video
  167. v-if="isVideoExt(item.url || '')"
  168. class="avatar video-avatar video"
  169. muted
  170. preload="metadata"
  171. @click="chooseImg(item.url)"
  172. >
  173. <source :src="getUrl(item.url) + '#t=1'">
  174. 您的浏览器不支持视频播放
  175. </video>
  176. <div
  177. v-else
  178. class="header-img-box-list"
  179. >
  180. <el-icon class="lost-image">
  181. <icon-picture />
  182. </el-icon>
  183. </div>
  184. </template>
  185. </el-image>
  186. </div>
  187. <div
  188. class="img-title"
  189. @click="editFileNameFunc(item)"
  190. >{{ item.name }}
  191. </div>
  192. </div>
  193. </div>
  194. <el-pagination
  195. :current-page="page"
  196. :page-size="pageSize"
  197. :total="total"
  198. :style="{'justify-content':'center'}"
  199. layout="total, prev, pager, next, jumper"
  200. @current-change="handleCurrentChange"
  201. @size-change="handleSizeChange"
  202. />
  203. </el-drawer>
  204. </template>
  205. <script setup>
  206. import { getUrl, isVideoExt } from '@/utils/image'
  207. import { onMounted, ref } from 'vue'
  208. import { getFileList, editFileName } from '@/api/fileUploadAndDownload'
  209. import UploadImage from '@/components/upload/image.vue'
  210. import UploadCommon from '@/components/upload/common.vue'
  211. import WarningBar from '@/components/warningBar/warningBar.vue'
  212. import { ElMessage, ElMessageBox } from 'element-plus'
  213. import { Delete, FolderAdd, Plus, Picture as IconPicture } from '@element-plus/icons-vue'
  214. const imageUrl = ref('')
  215. const imageCommon = ref('')
  216. const search = ref({})
  217. const page = ref(1)
  218. const total = ref(0)
  219. const pageSize = ref(20)
  220. const props = defineProps({
  221. modelValue: {
  222. type: [String, Array],
  223. default: ''
  224. },
  225. multiple: {
  226. type: Boolean,
  227. default: false
  228. },
  229. fileType: {
  230. type: String,
  231. default: ''
  232. },
  233. maxUpdateCount: {
  234. type: Number,
  235. default: 0
  236. }
  237. })
  238. const multipleValue = ref([])
  239. onMounted(() => {
  240. if (props.multiple) {
  241. multipleValue.value = props.modelValue
  242. }
  243. })
  244. const emits = defineEmits(['update:modelValue'])
  245. const deleteImg = (index) => {
  246. multipleValue.value.splice(index, 1)
  247. emits('update:modelValue', multipleValue.value)
  248. }
  249. // 分页
  250. const handleSizeChange = (val) => {
  251. pageSize.value = val
  252. getImageList()
  253. }
  254. const handleCurrentChange = (val) => {
  255. page.value = val
  256. getImageList()
  257. }
  258. const editFileNameFunc = async(row) => {
  259. ElMessageBox.prompt('请输入文件名或者备注', '编辑', {
  260. confirmButtonText: '确定',
  261. cancelButtonText: '取消',
  262. inputPattern: /\S/,
  263. inputErrorMessage: '不能为空',
  264. inputValue: row.name
  265. }).then(async({ value }) => {
  266. row.name = value
  267. // console.log(row)
  268. const res = await editFileName(row)
  269. if (res.code === 0) {
  270. ElMessage({
  271. type: 'success',
  272. message: '编辑成功!',
  273. })
  274. getImageList()
  275. }
  276. }).catch(() => {
  277. ElMessage({
  278. type: 'info',
  279. message: '取消修改'
  280. })
  281. })
  282. }
  283. const drawer = ref(false)
  284. const picList = ref([])
  285. const imageTypeList = ['png', 'jpg', 'jpeg', 'gif', 'bmp', 'webp']
  286. const videoTyteList = ['mp4', 'avi', 'rmvb', 'rm', 'asf', 'divx', 'mpg', 'mpeg', 'mpe', 'wmv', 'mkv', 'vob']
  287. const listObj = {
  288. image: imageTypeList,
  289. video: videoTyteList
  290. }
  291. const chooseImg = (url) => {
  292. console.log(url)
  293. if (props.fileType) {
  294. const typeSuccess = listObj[props.fileType].some(item => {
  295. if (url.includes(item)) {
  296. return true
  297. }
  298. })
  299. if (!typeSuccess) {
  300. ElMessage({
  301. type: 'error',
  302. message: '当前类型不支持使用'
  303. })
  304. return
  305. }
  306. }
  307. if (props.multiple) {
  308. multipleValue.value.push(url)
  309. emits('update:modelValue', multipleValue.value)
  310. } else {
  311. emits('update:modelValue', url)
  312. }
  313. drawer.value = false
  314. }
  315. const openChooseImg = async() => {
  316. if (props.modelValue && !props.multiple) {
  317. emits('update:modelValue', '')
  318. return
  319. }
  320. await getImageList()
  321. drawer.value = true
  322. }
  323. const getImageList = async() => {
  324. const res = await getFileList({ page: page.value, pageSize: pageSize.value, ...search.value })
  325. if (res.code === 0) {
  326. picList.value = res.data.list
  327. total.value = res.data.total
  328. page.value = res.data.page
  329. pageSize.value = res.data.pageSize
  330. }
  331. }
  332. </script>
  333. <style scoped lang="scss">
  334. .multiple-img {
  335. display: flex;
  336. gap: 8px;
  337. width: 100%;
  338. flex-wrap: wrap;
  339. }
  340. .add-image {
  341. width: 120px;
  342. height: 120px;
  343. line-height: 120px;
  344. display: flex;
  345. justify-content: center;
  346. border-radius: 20px;
  347. border: 1px dashed #ccc;
  348. background-size: cover;
  349. cursor: pointer;
  350. }
  351. .update-image {
  352. cursor: pointer;
  353. width: 120px;
  354. height: 120px;
  355. line-height: 120px;
  356. display: flex;
  357. justify-content: center;
  358. border-radius: 20px;
  359. border: 1px dashed #ccc;
  360. background-repeat: no-repeat;
  361. background-size: cover;
  362. position: relative;
  363. &:hover {
  364. color: #fff;
  365. background: linear-gradient(
  366. to bottom,
  367. rgba(255, 255, 255, 0.15) 0%,
  368. rgba(0, 0, 0, 0.15) 100%
  369. ),
  370. radial-gradient(
  371. at top center,
  372. rgba(255, 255, 255, 0.4) 0%,
  373. rgba(0, 0, 0, 0.4) 120%
  374. ) #989898;
  375. background-blend-mode: multiply, multiply;
  376. background-size: cover;
  377. .update {
  378. color: #fff;
  379. }
  380. .video {
  381. opacity: 0.2;
  382. }
  383. }
  384. .video-icon {
  385. position: absolute;
  386. left: calc(50% - 16px);
  387. top: calc(50% - 16px);
  388. }
  389. video {
  390. object-fit: cover;
  391. max-width: 100%;
  392. border-radius: 20px;
  393. }
  394. .update {
  395. height: 120px;
  396. width: 120px;
  397. text-align: center;
  398. color: transparent;
  399. position: absolute;
  400. }
  401. }
  402. .upload-btn-media-library {
  403. margin-left: 20px;
  404. }
  405. .media {
  406. display: flex;
  407. flex-wrap: wrap;
  408. .media-box {
  409. width: 120px;
  410. margin-left: 20px;
  411. .img-title {
  412. white-space: nowrap;
  413. overflow: hidden;
  414. text-overflow: ellipsis;
  415. line-height: 36px;
  416. text-align: center;
  417. cursor: pointer;
  418. }
  419. .header-img-box-list {
  420. width: 120px;
  421. height: 120px;
  422. border: 1px dashed #ccc;
  423. border-radius: 8px;
  424. text-align: center;
  425. line-height: 120px;
  426. cursor: pointer;
  427. overflow: hidden;
  428. .el-image__inner {
  429. max-width: 120px;
  430. max-height: 120px;
  431. vertical-align: middle;
  432. width: unset;
  433. height: unset;
  434. }
  435. .el-image {
  436. position: relative;
  437. }
  438. .video-icon {
  439. position: absolute;
  440. left: calc(50% - 16px);
  441. top: calc(50% - 16px);
  442. }
  443. video {
  444. object-fit: cover;
  445. max-width: 100%;
  446. min-height: 100%;
  447. border-radius: 8px;
  448. }
  449. }
  450. }
  451. }
  452. </style>