index.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <template>
  2. <view class="updata-img">
  3. <view class="updata-title-box">
  4. <view class="updata-title-title font-title ">{{ data.title }} <span style="color: #f56c6c;"
  5. v-if="data.required">*</span> </view>
  6. <view class="updata-title-text font-text ">{{ data.text }}</view>
  7. </view>
  8. <view class="upload-box">
  9. <view class="upload-img-item" v-for="(item, index) in fileList" :key="item.url" >
  10. <view class="delete-icon" @click="deletePic(index)">
  11. <up-icon name="close-circle-fill" color="#f64a1f"
  12. size="18"></up-icon>
  13. </view>
  14. <img class="upload-img" :src="item.url" :alt="item.fileName" srcset="">
  15. </view>
  16. <img src="/static/img/upload.png" alt="" class="upload-img" @click="uploadClick('img')"
  17. v-if="fileList.length < fileCount">
  18. </view>
  19. </view>
  20. </template>
  21. <script setup>
  22. import { onMounted, ref, watch } from 'vue';
  23. import { wxUploadFile } from '@/utils/wxRequest.js'
  24. const props = defineProps({
  25. data: {
  26. type: Object,
  27. default: {
  28. title: '上传头像',
  29. text: '上传您的头像',
  30. img: "/static/img/updata-user-img.png"
  31. },
  32. },
  33. fileList: {
  34. type: String,
  35. default: ''
  36. },
  37. fileCount: {
  38. type: Number,
  39. default: 1
  40. }
  41. });
  42. const emit = defineEmits(['onSubmit']);
  43. const fileList = ref([]);
  44. // 删除图片
  45. const deletePic = (index) => {
  46. fileList.value = fileList.value.filter((item, i) => i !== index).filter(Boolean);
  47. console.log("TCL: deletePic -> fileList.value", fileList.value)
  48. getFile();
  49. };
  50. const uploadClick = async (type) => {
  51. const res = await wxUploadFile(type,props.fileCount);
  52. fileList.value = [...fileList.value, ...res];
  53. getFile();
  54. console.log('xxxxres', res, fileList.value);
  55. }
  56. function getFile() {
  57. const img_urls = fileList.value.map(item => item.url).join(',');
  58. console.log('img_urls',img_urls);
  59. emit('onSubmit', { key: props.data.key, url: img_urls })
  60. return img_urls
  61. }
  62. function setFile(files){
  63. const files_array = files.split(',').filter(Boolean);
  64. const arr_file = files_array.map(item=> {
  65. return {url:item}
  66. })
  67. console.log('fileList',arr_file);
  68. fileList.value = arr_file
  69. }
  70. watch(()=>props.fileList,()=>{
  71. setFile(props.fileList);
  72. })
  73. defineExpose({
  74. getFile,
  75. setFile
  76. });
  77. </script>
  78. <style lang="scss" scoped>
  79. .updata-img {
  80. border-radius: 8px;
  81. background: rgba(255, 255, 255, 1);
  82. // height: 100px;
  83. padding: 16px;
  84. display: flex;
  85. align-content: center;
  86. justify-content: space-between;
  87. flex-direction: column;
  88. .updata-title-box {
  89. display: flex;
  90. align-content: center;
  91. flex-direction: column;
  92. justify-content: space-evenly;
  93. margin-bottom: 12px;
  94. }
  95. }
  96. .upload-img {
  97. height: 68px;
  98. width: 68px;
  99. margin-right: 12px;
  100. margin-bottom: 12px;
  101. }
  102. .upload-box {
  103. display: flex;
  104. flex-wrap: wrap;
  105. .upload-img-item {
  106. position: relative;
  107. .delete-icon {
  108. position: absolute;
  109. top: -7px;
  110. right: 7px;
  111. z-index: 1;
  112. }
  113. }
  114. }
  115. </style>