index.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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="updata-img-box" @click="updataFile">
  9. <image :src="img" style="width: 117px;height: 75px;" />
  10. </view> -->
  11. <view class="upload-box">
  12. <view class="upload-img-item" v-for="(item, index) in fileList" :key="item.url">
  13. <view class="delete-icon" @click="deletePic(index)"><up-icon name="close-circle-fill" color="#f64a1f"
  14. size="18"></up-icon></view>
  15. <img class="upload-img" :src="item.url" :alt="item.fileName" srcset="">
  16. </view>
  17. <img src="/static/img/upload.png" alt="" class="upload-img" @click="uploadClick('img')"
  18. v-if="fileList.length < 10">
  19. </view>
  20. </view>
  21. </template>
  22. <script setup>
  23. import { onMounted, ref, watch } from 'vue';
  24. import { wxUploadFile } from '@/utils/wxRequest.js'
  25. const props = defineProps({
  26. data: {
  27. type: Object,
  28. default: {
  29. title: '上传头像',
  30. text: '上传您的头像',
  31. img: "/static/img/updata-user-img.png"
  32. },
  33. },
  34. fileList: {
  35. type: String,
  36. default: ''
  37. },
  38. fileCount: {
  39. type: Number,
  40. default: 1
  41. }
  42. });
  43. const emit = defineEmits(['onSubmit']);
  44. const fileList = ref([]);
  45. // 删除图片
  46. const deletePic = (index) => {
  47. fileList.value.splice(index, 1);
  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. console.log('fileList=>files',files);
  64. const arr_file = files.split(',').map(item=> {
  65. return {url:item}
  66. })
  67. console.log('fileList',files.split(','));
  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>