index.vue 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. <template>
  2. <div class="cl-upload-item__wrap">
  3. <keep-alive>
  4. <div
  5. class="cl-upload-item"
  6. :class="[
  7. {
  8. 'is-play': item.isPlay
  9. }
  10. ]"
  11. @contextmenu.stop.prevent="onContextMenu"
  12. >
  13. <!-- 图片 -->
  14. <template v-if="item.type === 'image' && !item.error">
  15. <el-image
  16. class="cl-upload-item__image-cover"
  17. fit="contain"
  18. :src="item.preload || url"
  19. lazy
  20. @error="item.error = '加载失败'"
  21. />
  22. </template>
  23. <!-- 视频 -->
  24. <template v-else-if="item.type === 'video' && item.url">
  25. <video :ref="setRefs('video')" :src="item.url" />
  26. </template>
  27. <!-- 其他 -->
  28. <template v-else>
  29. <!-- 图标 -->
  30. <div class="cl-upload-item__icon">
  31. <cl-svg :name="item.type" />
  32. </div>
  33. <!-- 文件名 -->
  34. <div class="cl-upload-item__name">
  35. <span>{{ fileName(item.name || url) }}</span>
  36. <span class="error" v-show="item.error">{{ item.error }}</span>
  37. </div>
  38. </template>
  39. <!-- 音频 -->
  40. <template v-if="item.type === 'audio'">
  41. <audio controls :ref="setRefs('audio')">
  42. <source :src="item.url" type="audio/mpeg" />
  43. </audio>
  44. </template>
  45. <!-- 上传中 -->
  46. <div
  47. class="cl-upload-item__progress"
  48. :class="{
  49. 'is-show': item.progress >= 0 && item.progress < 100,
  50. 'is-hide': item.progress == 100
  51. }"
  52. >
  53. <!-- 进度条 -->
  54. <div class="cl-upload-item__progress-bar">
  55. <el-progress :percentage="item.progress" :show-text="false" />
  56. </div>
  57. <!-- 进度值 -->
  58. <span class="cl-upload-item__progress-value">{{ item.progress }}</span>
  59. </div>
  60. <!-- 角标 -->
  61. <span
  62. class="cl-upload-item__tag"
  63. :style="{
  64. backgroundColor: tag.color
  65. }"
  66. >
  67. {{ tag.name }}
  68. </span>
  69. <template v-if="url">
  70. <!-- 工具 -->
  71. <div class="cl-upload-item__actions">
  72. <template v-if="media.isMedia">
  73. <el-icon @click.stop="media.pause()" v-if="item.isPlay">
  74. <video-pause />
  75. </el-icon>
  76. <el-icon @click.stop="media.play()" v-else>
  77. <video-play />
  78. </el-icon>
  79. </template>
  80. <template v-else>
  81. <el-icon @click.stop="preview">
  82. <zoom-in />
  83. </el-icon>
  84. </template>
  85. <el-icon @click.stop="remove" v-if="!disabled">
  86. <delete />
  87. </el-icon>
  88. </div>
  89. </template>
  90. </div>
  91. </keep-alive>
  92. <!-- 预览 -->
  93. <viewer :ref="setRefs('viewer')" />
  94. </div>
  95. </template>
  96. <script lang="ts" name="cl-upload-item" setup>
  97. import { computed, PropType, onMounted, watch, reactive } from "vue";
  98. import { ZoomIn, Delete, VideoPause, VideoPlay } from "@element-plus/icons-vue";
  99. import { ContextMenu } from "@cool-vue/crud";
  100. import { useCool } from "/@/cool";
  101. import { extname } from "/@/cool/utils";
  102. import { fileName, getRule } from "../../utils";
  103. import { ElMessage } from "element-plus";
  104. import { useClipboard } from "@vueuse/core";
  105. import Viewer from "./viewer.vue";
  106. import { Upload } from "../../types";
  107. const props = defineProps({
  108. item: {
  109. type: Object as PropType<Upload.Item>,
  110. required: true
  111. },
  112. list: {
  113. type: Array as PropType<Upload.Item[]>,
  114. default: () => []
  115. },
  116. disabled: Boolean
  117. });
  118. const emit = defineEmits(["remove"]);
  119. const { refs, setRefs } = useCool();
  120. const { copy } = useClipboard();
  121. // 图片地址
  122. const url = computed(() => props.item.url || "");
  123. // 角标
  124. const tag = computed(() => {
  125. const d = getRule(props.item.type);
  126. return {
  127. color: d.color,
  128. name: extname(props.item.name || url.value)
  129. };
  130. });
  131. // 移除
  132. function remove() {
  133. emit("remove", props.item);
  134. }
  135. // 预览
  136. function preview() {
  137. refs.viewer.open(props.item);
  138. }
  139. // 右键菜单
  140. function onContextMenu(e: any) {
  141. ContextMenu.open(e, {
  142. hover: {
  143. target: "cl-upload-item__wrap"
  144. },
  145. list: [
  146. {
  147. label: "预览",
  148. callback(done) {
  149. preview();
  150. done();
  151. }
  152. },
  153. {
  154. label: "复制链接",
  155. callback(done) {
  156. if (props.item.url) {
  157. copy(props.item.url);
  158. ElMessage.success("复制成功");
  159. }
  160. done();
  161. }
  162. },
  163. // {
  164. // label: isSelected.value ? "取消选中" : "选中",
  165. // callback(done) {
  166. // select();
  167. // done();
  168. // }
  169. // },
  170. {
  171. label: "删除",
  172. callback(done) {
  173. remove();
  174. done();
  175. }
  176. }
  177. ]
  178. });
  179. }
  180. // 媒体
  181. const media = reactive({
  182. isMedia: ["video", "audio"].includes(props.item.type!),
  183. play() {
  184. props.list.forEach((e) => {
  185. e.isPlay = e.uid ? props.item.uid == e.uid : props.item.id == e.id;
  186. });
  187. },
  188. pause() {
  189. props.item.isPlay = false;
  190. },
  191. create() {
  192. if (!media.isMedia) {
  193. return false;
  194. }
  195. // 媒体元素
  196. const el = refs[props.item.type!];
  197. // 监听播放\暂停
  198. watch(
  199. () => props.item.isPlay,
  200. (val) => {
  201. if (val) {
  202. el.play();
  203. } else {
  204. el.pause();
  205. }
  206. }
  207. );
  208. // 监听播放完成
  209. el?.addEventListener("ended", () => {
  210. media.pause();
  211. });
  212. }
  213. });
  214. onMounted(() => {
  215. media.create();
  216. });
  217. </script>
  218. <style lang="scss" scoped>
  219. .cl-upload-item {
  220. display: flex;
  221. flex-direction: column;
  222. align-items: center;
  223. justify-content: center;
  224. height: 100%;
  225. width: 100%;
  226. cursor: pointer;
  227. border-radius: 6px;
  228. overflow: hidden;
  229. background-color: var(--el-fill-color-light);
  230. border: 1px solid var(--el-fill-color-light);
  231. box-sizing: border-box;
  232. margin-bottom: 10px;
  233. position: relative;
  234. audio {
  235. visibility: hidden;
  236. height: 0;
  237. }
  238. video {
  239. height: 100%;
  240. }
  241. &__wrap {
  242. position: absolute;
  243. left: 0;
  244. top: 0;
  245. height: 100%;
  246. width: 100%;
  247. }
  248. &__icon {
  249. .cl-svg {
  250. font-size: 100px;
  251. position: absolute;
  252. right: -24px;
  253. top: -24px;
  254. fill: var(--el-fill-color-dark);
  255. }
  256. }
  257. &__name {
  258. display: flex;
  259. flex-direction: column;
  260. justify-content: center;
  261. height: 50px;
  262. width: 100%;
  263. font-size: 12px;
  264. overflow: hidden;
  265. padding: 0 10px;
  266. position: absolute;
  267. bottom: 0;
  268. left: 0;
  269. box-sizing: border-box;
  270. line-height: 1;
  271. span {
  272. white-space: nowrap;
  273. text-overflow: ellipsis;
  274. overflow: hidden;
  275. &.error {
  276. color: var(--el-color-danger);
  277. margin-top: 5px;
  278. }
  279. }
  280. }
  281. &__progress {
  282. display: flex;
  283. align-items: center;
  284. justify-content: center;
  285. position: absolute;
  286. left: 0;
  287. top: 0;
  288. height: 100%;
  289. width: 100%;
  290. background-color: rgba(0, 0, 0, 0.5);
  291. pointer-events: none;
  292. transition: opacity 0.3s;
  293. opacity: 0;
  294. &-bar {
  295. position: absolute;
  296. bottom: 10px;
  297. left: 10px;
  298. width: calc(100% - 20px);
  299. }
  300. &-value {
  301. position: absolute;
  302. font-size: 26px;
  303. color: #fff;
  304. &::after {
  305. content: "%";
  306. margin-left: 2px;
  307. }
  308. }
  309. &.is-show {
  310. opacity: 1;
  311. }
  312. &.is-hide {
  313. opacity: 0;
  314. }
  315. }
  316. &__tag {
  317. position: absolute;
  318. top: 5px;
  319. left: 5px;
  320. color: #fff;
  321. font-size: 12px;
  322. padding: 2px 4px;
  323. border-radius: 2px;
  324. text-transform: uppercase;
  325. line-height: 1;
  326. max-width: 65px;
  327. box-sizing: border-box;
  328. white-space: nowrap;
  329. overflow: hidden;
  330. text-overflow: ellipsis;
  331. }
  332. &__actions {
  333. position: absolute;
  334. left: 0;
  335. top: 0;
  336. z-index: 9;
  337. height: 100%;
  338. width: 100%;
  339. display: flex;
  340. justify-content: center;
  341. align-items: center;
  342. background-color: rgba(0, 0, 0, 0.5);
  343. border-radius: 6px;
  344. opacity: 0;
  345. transition: opacity 0.3s cubic-bezier(0.55, 0, 0.1, 1);
  346. .el-icon {
  347. color: #fff;
  348. margin: 0 8px;
  349. font-size: 20px;
  350. &:hover {
  351. color: #eee;
  352. }
  353. }
  354. }
  355. &.is-play {
  356. animation: play 1s linear infinite;
  357. }
  358. &:hover {
  359. .cl-upload-item__actions {
  360. opacity: 1;
  361. }
  362. }
  363. }
  364. @keyframes play {
  365. 0% {
  366. border-color: var(--el-color-primary);
  367. }
  368. 100% {
  369. border-color: var(--el-fill-color-light);
  370. }
  371. }
  372. </style>