uploadImage.vue 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <template>
  2. <div class="img-upload-box">
  3. <el-upload
  4. ref="upload"
  5. :action="action"
  6. :headers="headers"
  7. list-type="picture-card"
  8. :on-preview="handlePictureCardPreview"
  9. :before-upload="beforeUpload"
  10. :limit="1"
  11. :accept="accept"
  12. :auto-upload="true"
  13. :on-success="success"
  14. :on-remove="remove"
  15. :file-list="fileList"
  16. >
  17. <!-- :class="{hidden:(hasUpload && fileList===[]) || !hasUpload}" -->
  18. <div slot="tip" class="el-upload__tip">建议尺寸:{{ tipTitle }}</div>
  19. <i class="el-icon-picture-outline" />
  20. </el-upload>
  21. <el-dialog :visible.sync="dialogVisible">
  22. <img width="100%" :src="dialogImageUrl" alt="">
  23. </el-dialog>
  24. </div>
  25. </template>
  26. <script>
  27. // import { uploadImage } from '@/api/upload'
  28. import { mapGetters } from 'vuex'
  29. export default {
  30. props: {
  31. fileList: {
  32. type: Array,
  33. default: () => []
  34. },
  35. tipTitle: {
  36. type: String,
  37. default: '128px*88px'
  38. }
  39. },
  40. data() {
  41. return {
  42. dialogImageUrl: '',
  43. dialogVisible: false,
  44. // 文件上传请求接口
  45. action: process.env.VUE_APP_UPLOAD_API + '/upload/image',
  46. headers: {
  47. 'X-Token': ''
  48. },
  49. accept: '.jpg,.png,.gif'
  50. }
  51. },
  52. computed: {
  53. ...mapGetters(['token'])
  54. },
  55. created() {
  56. this.headers['X-Token'] = this.token
  57. },
  58. methods: {
  59. // 预览图片
  60. handlePictureCardPreview(file) {
  61. this.dialogImageUrl = file.url
  62. this.dialogVisible = true
  63. },
  64. // 图片上传成功
  65. success(response, file, fileList) {
  66. this.$emit('success', response)
  67. },
  68. // 图片上传失败
  69. error(err, file, fileList) {
  70. this.$emit('error', err, file, fileList)
  71. },
  72. // 图片上传之前的钩子
  73. beforeUpload(file) {
  74. const size = file.size
  75. if (size > 1024 * 1000) {
  76. this.$message.warning('文件上传大小超出限制(≤1MB)')
  77. return false
  78. }
  79. },
  80. remove() {
  81. this.$emit('remove')
  82. }
  83. }
  84. }
  85. </script>
  86. <style lang="scss">
  87. .img-upload-box{
  88. width:150px;
  89. height:180px;
  90. overflow: hidden
  91. }
  92. .img-upload-box>div{
  93. white-space: nowrap;
  94. }
  95. .img-upload-box .el-upload-list{
  96. height: 150px;
  97. display: inline-block;
  98. }
  99. </style>