SingleImg.vue 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <template>
  2. <div class="SingleImg-container">
  3. <el-upload class="avatar-uploader" :action="define.comUploadUrl + '/' + type" :show-file-list="false" :on-success="handleSuccess" :headers="uploadHeaders" accept="image/*">
  4. <img v-if="imageUrl" :src="imageUrl" class="avatar" />
  5. <i v-else class="el-icon-plus avatar-uploader-icon"></i>
  6. </el-upload>
  7. </div>
  8. </template>
  9. <script>
  10. import { getDownloadUrl } from '@/api/common'
  11. export default {
  12. name: 'SingleImg',
  13. props: {
  14. value: {
  15. type: String,
  16. default: ''
  17. },
  18. type: {
  19. type: String,
  20. default: 'workFlow'
  21. }
  22. },
  23. data() {
  24. return {
  25. imageUrl: '',
  26. uploadHeaders: { Authorization: this.$store.getters.token }
  27. }
  28. },
  29. watch: {
  30. value(val) {
  31. if (!val) this.imageUrl = ''
  32. }
  33. },
  34. methods: {
  35. handleSuccess(res, file) {
  36. if (res.code == 200) {
  37. this.imageUrl = URL.createObjectURL(file.raw)
  38. this.$emit('input', res.data.name)
  39. } else {
  40. this.$message({ message: res.msg, type: 'error', duration: 1500 })
  41. }
  42. }
  43. }
  44. }
  45. </script>
  46. <style lang="scss" scoped>
  47. .avatar-uploader >>> .el-upload {
  48. border: 1px dashed #d9d9d9;
  49. border-radius: 6px;
  50. cursor: pointer;
  51. position: relative;
  52. overflow: hidden;
  53. }
  54. .avatar-uploader .el-upload:hover {
  55. border-color: #409eff;
  56. }
  57. .avatar-uploader-icon {
  58. font-size: 28px;
  59. color: #8c939d;
  60. width: 150px;
  61. height: 150px;
  62. line-height: 150px;
  63. text-align: center;
  64. }
  65. .avatar {
  66. width: 150px;
  67. height: 150px;
  68. display: block;
  69. }
  70. </style>