index.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. <up-image :src="item.url" class="upload-img" mode="widthFix"></up-image>
  16. </view>
  17. <!-- <img src="/static/img/upload.png" alt="" class="upload-img" @click="uploadClick('img')"
  18. v-if="fileList.length < fileCount"> -->
  19. <view v-if="fileList.length < fileCount" @click="uploadClick('img')" class="updata-bg-view">
  20. <up-icon name="plus" color="#999999" size="28"></up-icon>
  21. <text class="updata-dece">{{ data.dece }}</text>
  22. </view>
  23. </view>
  24. </view>
  25. </template>
  26. <script setup>
  27. import { onMounted, ref, watch } from 'vue';
  28. import { wxUploadFile } from '@/utils/wxRequest.js'
  29. const props = defineProps({
  30. data: {
  31. type: Object,
  32. default: {
  33. title: '上传头像',
  34. text: '上传您的头像',
  35. img: "/static/img/updata-user-img.png",
  36. dece:'点击上传文件'
  37. },
  38. },
  39. fileList: {
  40. type: String,
  41. default: ''
  42. },
  43. fileCount: {
  44. type: Number,
  45. default: 1
  46. }
  47. });
  48. const emit = defineEmits(['onSubmit']);
  49. const fileList = ref([]);
  50. // 删除图片
  51. const deletePic = (index) => {
  52. fileList.value = fileList.value.filter((item, i) => i !== index).filter(Boolean);
  53. console.log("TCL: deletePic -> fileList.value", fileList.value)
  54. getFile();
  55. };
  56. const uploadClick = async (type) => {
  57. const res = await wxUploadFile(type,props.fileCount);
  58. fileList.value = [...fileList.value, ...res];
  59. getFile();
  60. console.log('xxxxres', res, fileList.value);
  61. }
  62. function getFile() {
  63. const img_urls = fileList.value.map(item => item.url).join(',');
  64. console.log('img_urls',img_urls);
  65. emit('onSubmit', { key: props.data.key, url: img_urls })
  66. return img_urls
  67. }
  68. function setFile(files){
  69. const files_array = files.split(',').filter(Boolean);
  70. const arr_file = files_array.map(item=> {
  71. return {url:item}
  72. })
  73. console.log('fileList',arr_file);
  74. fileList.value = arr_file
  75. }
  76. watch(()=>props.fileList,()=>{
  77. setFile(props.fileList);
  78. })
  79. defineExpose({
  80. getFile,
  81. setFile
  82. });
  83. </script>
  84. <style lang="scss" scoped>
  85. .updata-img {
  86. border-radius: 8px;
  87. background: rgba(255, 255, 255, 1);
  88. // height: 100px;
  89. padding: 16px;
  90. display: flex;
  91. align-content: center;
  92. justify-content: space-between;
  93. flex-direction: column;
  94. .updata-title-box {
  95. display: flex;
  96. align-content: center;
  97. flex-direction: column;
  98. justify-content: space-evenly;
  99. margin-bottom: 12px;
  100. }
  101. }
  102. .upload-img {
  103. height: 68px;
  104. width: 68px;
  105. margin-right: 12px;
  106. margin-bottom: 12px;
  107. object-fit: contain;
  108. }
  109. .upload-box {
  110. display: flex;
  111. flex-wrap: wrap;
  112. .upload-img-item {
  113. position: relative;
  114. width: 100%;
  115. display: flex;
  116. align-items: center;
  117. justify-content: center;
  118. .delete-icon {
  119. position: absolute;
  120. top: -7px;
  121. right: -10px;
  122. z-index: 1;
  123. }
  124. }
  125. }
  126. .updata-bg-view {
  127. width: 700rpx;
  128. height: 256rpx;
  129. border-radius: 16rpx;
  130. background: rgba(245, 245, 245, 1);
  131. display: flex;
  132. align-items: center;
  133. justify-content: center;
  134. flex-direction: column;
  135. font-size: 28rpx;
  136. font-weight: 400;
  137. color: rgba(153, 153, 153, 1);
  138. .updata-dece {
  139. margin-top: 12rpx;
  140. }
  141. }
  142. </style>