upload.vue 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <template>
  2. <view>
  3. <view class="upload" @click="handlerUpload" v-if="imageUrl==null || imageUrl == ''">
  4. <text class="iconfont icon-shangchuantupian"></text>
  5. <text>选择图片</text>
  6. </view>
  7. <view class="upload_image" v-else>
  8. <image :src="imageUrl" style="width: 100%;height: 100%;" mode="aspectFill"></image>
  9. <view class="upload-del" @click="handleDelete">
  10. <text class="iconfont icon-shanchu"></text>
  11. </view>
  12. </view>
  13. </view>
  14. </template>
  15. <script>
  16. import { uploadFileImage } from '@/services/public.js'
  17. export default {
  18. props: {
  19. imageData: {
  20. type: String,
  21. default: () => ''
  22. }
  23. },
  24. data() {
  25. return {
  26. imageUrl: '',
  27. }
  28. },
  29. watch: {
  30. imageData(val) {
  31. if (val) {
  32. console.log(val)
  33. this.imageUrl = val
  34. }
  35. }
  36. },
  37. methods: {
  38. async handlerUpload() {
  39. try{
  40. const {data} = await uploadFileImage()
  41. this.imageUrl = JSON.parse(data).data
  42. this.$emit('uploadImg', JSON.parse(data).data)
  43. console.log(this.imageUrl)
  44. }catch(error){
  45. console.log(error)
  46. }
  47. },
  48. handleDelete() {
  49. this.imageUrl = ''
  50. this.$emit('uploadDel', '')
  51. },
  52. }
  53. }
  54. </script>
  55. <style scoped lang="scss">
  56. .upload {
  57. width: 210rpx;
  58. height: 210rpx;
  59. border-radius: 6rpx 6rpx 6rpx 6rpx;
  60. border: 1rpx dotted #B2B2B2;
  61. display: flex;
  62. align-items: center;
  63. flex-direction: column;
  64. font-size: 24rpx;
  65. color: #B2B2B2;
  66. justify-content: space-around;
  67. padding: 40rpx 56rpx;
  68. box-sizing: border-box;
  69. white-space: nowrap;
  70. }
  71. .upload_image {
  72. width: 210rpx;
  73. height: 210rpx;
  74. border: 1rpx dotted #B2B2B2;
  75. position: relative;
  76. .upload-del {
  77. width: 40rpx;
  78. height: 40rpx;
  79. position: absolute;
  80. top: -20rpx;
  81. right: -20rpx;
  82. line-height: 40rpx;
  83. text-align: center;
  84. .iconfont {
  85. font-size: $font-size-40;
  86. color: #F85050;
  87. }
  88. }
  89. }
  90. </style>