index.vue 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <template>
  2. <a-upload :show-upload-list="false" :custom-request="onFileChange">
  3. <div class="cover-upload">
  4. <img v-if="cover" :src="cover" alt="" />
  5. <div class="cover-edit-wrap">
  6. <template v-if="uploading">
  7. <LoadingOutlined />
  8. </template>
  9. <template v-else>
  10. <div class="add">
  11. <PlusOutlined />
  12. </div>
  13. </template>
  14. </div>
  15. </div>
  16. </a-upload>
  17. </template>
  18. <script lang="ts">
  19. import { defineComponent, reactive, toRefs } from "vue";
  20. import { message } from "ant-design-vue";
  21. import { LoadingOutlined, PlusOutlined } from "@ant-design/icons-vue";
  22. import { onUpload } from "@/api";
  23. const ImageUpload = defineComponent({
  24. name: "ImageUpload",
  25. components: {
  26. LoadingOutlined,
  27. PlusOutlined
  28. },
  29. props: {
  30. type: {
  31. type: String,
  32. default: () => ""
  33. },
  34. name: {
  35. type: String,
  36. required: true,
  37. default: () => ""
  38. },
  39. value: {
  40. type: String,
  41. default: () => ""
  42. }
  43. },
  44. emits: ["change"],
  45. setup(props, { emit }) {
  46. const state = reactive({
  47. uploading: false,
  48. cover: props.value ?? ""
  49. });
  50. const onFileChange = async (file: { file: File }) => {
  51. try {
  52. state.uploading = true;
  53. let tempFile = file.file;
  54. const { data } = await onUpload(tempFile, props.type ?? "");
  55. state.cover = data.url;
  56. emit("change", { url: state.cover, type: props.name });
  57. } catch (error) {
  58. message.error(error.msg ?? "系统错误");
  59. } finally {
  60. state.uploading = false;
  61. }
  62. };
  63. return { ...toRefs(state), onFileChange };
  64. }
  65. });
  66. export default ImageUpload;
  67. </script>
  68. <style lang="scss" scoped>
  69. .cover-upload {
  70. font-size: 0;
  71. position: relative;
  72. border-radius: 6px;
  73. width: 128px;
  74. height: 128px;
  75. overflow: hidden;
  76. img {
  77. width: 128px;
  78. height: 128px;
  79. }
  80. .cover-edit-wrap {
  81. position: absolute;
  82. width: 100%;
  83. height: 100%;
  84. left: 0;
  85. top: 0;
  86. background: rgba($color: #000, $alpha: 0.3);
  87. display: flex;
  88. justify-content: center;
  89. align-items: center;
  90. font-size: 18px;
  91. color: #fff;
  92. cursor: pointer;
  93. transition: opacity 0.3s;
  94. opacity: 1;
  95. // &:hover {
  96. // opacity: 1;
  97. // }
  98. }
  99. }
  100. </style>