index.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. });
  39. const emit = defineEmits(['onSubmit']);
  40. const fileList = ref([]);
  41. // 删除图片
  42. const deletePic = (index) => {
  43. fileList.value.splice(index, 1);
  44. getFile();
  45. };
  46. const uploadClick = async (type) => {
  47. const res = await wxUploadFile(type);
  48. fileList.value = [...fileList.value, ...res];
  49. getFile();
  50. console.log('xxxxres', res, fileList.value);
  51. }
  52. function getFile() {
  53. const img_urls = fileList.value.map(item => item.url).join(',');
  54. console.log('img_urls',img_urls);
  55. emit('onSubmit', { key: props.data.key, url: img_urls })
  56. return img_urls
  57. }
  58. function setFile(files){
  59. console.log('fileList=>files',files);
  60. const arr_file = files.split(',').map(item=> {
  61. return {url:item}
  62. })
  63. console.log('fileList',files.split(','));
  64. fileList.value = arr_file
  65. }
  66. watch(()=>props.fileList,()=>{
  67. setFile(props.fileList);
  68. })
  69. defineExpose({
  70. getFile,
  71. setFile
  72. });
  73. </script>
  74. <style lang="scss" scoped>
  75. .updata-img {
  76. border-radius: 8px;
  77. background: rgba(255, 255, 255, 1);
  78. // height: 100px;
  79. padding: 16px;
  80. display: flex;
  81. align-content: center;
  82. justify-content: space-between;
  83. flex-direction: column;
  84. .updata-title-box {
  85. display: flex;
  86. align-content: center;
  87. flex-direction: column;
  88. justify-content: space-evenly;
  89. margin-bottom: 12px;
  90. }
  91. }
  92. .upload-img {
  93. height: 68px;
  94. width: 68px;
  95. margin-right: 12px;
  96. margin-bottom: 12px;
  97. }
  98. .upload-box {
  99. display: flex;
  100. flex-wrap: wrap;
  101. .upload-img-item {
  102. position: relative;
  103. .delete-icon {
  104. position: absolute;
  105. top: -7px;
  106. right: 7px;
  107. z-index: 1;
  108. }
  109. }
  110. }
  111. </style>