WithView.vue 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <template>
  2. <div>
  3. <div class="img-view" v-if="dialogImageUrl">
  4. <img :src="dialogImageUrl" alt="" srcset="" />
  5. </div>
  6. <div class="placeholder-box" v-else>
  7. <img src="~@/assets/upload-placeholder.svg" alt="" />
  8. </div>
  9. <div class="btn-box">
  10. <el-upload
  11. class="avatar-div"
  12. action="#"
  13. accept="image/png,image/jpeg,image/jpg"
  14. :show-file-list="false"
  15. ref="uploadRef"
  16. :on-exceed="handleExceed"
  17. :limit="1"
  18. :http-request="handlSuccess"
  19. :multiple="false"
  20. >
  21. <el-button type="text">上传{{ assetName }}图片</el-button>
  22. </el-upload>
  23. <el-button type="text" @click="clearImg">清除图片</el-button>
  24. </div>
  25. </div>
  26. </template>
  27. <script>
  28. import { uploadFile, removeFile } from '@/api/system/file/index.js';
  29. import { getImageUrl } from '@/utils/file';
  30. export default {
  31. props: {
  32. assetName: {
  33. type: String,
  34. default: '设备'
  35. },
  36. value: {
  37. type: Object,
  38. default: () => []
  39. },
  40. // 所属模块
  41. module: {
  42. type: String,
  43. default: 'main'
  44. }
  45. },
  46. data () {
  47. return {};
  48. },
  49. computed: {
  50. dialogImageUrl () {
  51. return this.value?.storePath && getImageUrl(this.value.storePath);
  52. }
  53. },
  54. methods: {
  55. // 清空已上传的文件列表
  56. clearUploadFiles () {},
  57. //图片添加
  58. async handlSuccess (params) {
  59. let res = await uploadFile({
  60. multiPartFile: params.file,
  61. module: this.module
  62. });
  63. if (res?.data) {
  64. this.$emit('input', res.data);
  65. }
  66. },
  67. async clearImg () {
  68. // await removeFile({ fileId: this.value.id });
  69. this.$emit('input', {});
  70. this.$refs.uploadRef.clearFiles();
  71. },
  72. // 限制上传的数量
  73. handleExceed (files, fileList) {
  74. this.$message.warning(`最多允许上传一张图片!`);
  75. }
  76. }
  77. };
  78. </script>
  79. <style lang="scss" scoped>
  80. .img-view {
  81. width: 280px;
  82. height: 342px;
  83. display: flex;
  84. justify-content: center;
  85. align-items: center;
  86. border-width: 1px;
  87. border-style: solid;
  88. border-color: rgba(215, 215, 215, 1);
  89. img {
  90. max-width: 100%;
  91. }
  92. }
  93. .placeholder-box {
  94. width: 280px;
  95. height: 342px;
  96. text-align: center;
  97. background-color: rgba(242, 242, 242, 1);
  98. box-sizing: border-box;
  99. border-width: 1px;
  100. border-style: solid;
  101. border-color: rgba(215, 215, 215, 1);
  102. padding-top: 60px;
  103. img {
  104. width: 158px;
  105. height: 158px;
  106. }
  107. }
  108. .btn-box {
  109. display: flex;
  110. justify-content: space-around;
  111. }
  112. </style>