upload.vue 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. this.imageUrl = val
  33. }
  34. }
  35. },
  36. methods: {
  37. async handlerUpload() {
  38. try{
  39. const {data} = await uploadFileImage()
  40. this.imageUrl = JSON.parse(data).data
  41. this.$emit('uploadImg', JSON.parse(data).data)
  42. }catch(error){
  43. console.log(error)
  44. }
  45. },
  46. handleDelete() {
  47. this.imageUrl = ''
  48. this.$emit('uploadDel', '')
  49. },
  50. }
  51. }
  52. </script>
  53. <style scoped lang="scss">
  54. .upload {
  55. width: 210rpx;
  56. height: 210rpx;
  57. border-radius: 6rpx 6rpx 6rpx 6rpx;
  58. border: 1rpx dotted #B2B2B2;
  59. display: flex;
  60. align-items: center;
  61. flex-direction: column;
  62. font-size: 24rpx;
  63. color: #B2B2B2;
  64. justify-content: space-around;
  65. padding: 40rpx 56rpx;
  66. box-sizing: border-box;
  67. white-space: nowrap;
  68. }
  69. .upload_image {
  70. width: 210rpx;
  71. height: 210rpx;
  72. border: 1rpx dotted #B2B2B2;
  73. position: relative;
  74. .upload-del {
  75. width: 40rpx;
  76. height: 40rpx;
  77. position: absolute;
  78. top: -20rpx;
  79. right: -20rpx;
  80. line-height: 40rpx;
  81. text-align: center;
  82. .iconfont {
  83. font-size: $font-size-40;
  84. color: #F85050;
  85. }
  86. }
  87. }
  88. </style>