zhaopianZiliao.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <template>
  2. <view class="image-upload">
  3. <!-- 显示已选择的图片 -->
  4. <view class="image-list">
  5. <view v-for="(image, index) in imageList" :key="index" class="image-item">
  6. <image :src="image" mode="aspectFill" class="image"></image>
  7. <view class="delete-btn" @click="deleteImage(index)">×</view>
  8. </view>
  9. </view>
  10. <!-- 选择图片按钮 -->
  11. <view class="upload-btn" @click="chooseImage">
  12. <text>+</text>
  13. </view>
  14. </view>
  15. </template>
  16. <script>
  17. export default {
  18. data() {
  19. return {
  20. imageList: [], // 存储已选择的图片路径
  21. };
  22. },
  23. methods: {
  24. // 打开相册选择图片
  25. chooseImage() {
  26. uni.chooseImage({
  27. count: 9, // 最多可以选择 9 张图片
  28. sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图
  29. sourceType: ['album'], // 仅从相册选择
  30. success: (res) => {
  31. // 将选择的图片路径添加到 imageList 中
  32. this.imageList = this.imageList.concat(res.tempFilePaths);
  33. },
  34. fail: (err) => {
  35. console.error('选择图片失败', err);
  36. uni.showToast({
  37. title: '选择图片失败',
  38. icon: 'none',
  39. });
  40. },
  41. });
  42. },
  43. // 删除图片
  44. deleteImage(index) {
  45. this.imageList.splice(index, 1);
  46. },
  47. // 上传图片
  48. uploadImages() {
  49. if (this.imageList.length === 0) {
  50. uni.showToast({
  51. title: '请先选择图片',
  52. icon: 'none',
  53. });
  54. return;
  55. }
  56. // 遍历 imageList 上传每张图片
  57. this.imageList.forEach((imagePath) => {
  58. uni.uploadFile({
  59. url: 'https://your-upload-url.com/upload', // 替换为你的上传接口地址
  60. filePath: imagePath,
  61. name: 'file', // 文件对应的 key
  62. formData: {
  63. // 可以添加额外的表单数据
  64. userId: '123',
  65. },
  66. success: (uploadRes) => {
  67. console.log('上传成功', uploadRes);
  68. uni.showToast({
  69. title: '上传成功',
  70. icon: 'success',
  71. });
  72. },
  73. fail: (err) => {
  74. console.error('上传失败', err);
  75. uni.showToast({
  76. title: '上传失败',
  77. icon: 'none',
  78. });
  79. },
  80. });
  81. });
  82. },
  83. },
  84. };
  85. </script>
  86. <style scoped>
  87. .image-upload {
  88. display: flex;
  89. flex-wrap: wrap;
  90. }
  91. .image-list {
  92. display: flex;
  93. flex-wrap: wrap;
  94. }
  95. .image-item {
  96. position: relative;
  97. width: 100px;
  98. height: 100px;
  99. margin: 5px;
  100. }
  101. .image {
  102. width: 100%;
  103. height: 100%;
  104. border-radius: 5px;
  105. }
  106. .delete-btn {
  107. position: absolute;
  108. top: 0;
  109. right: 0;
  110. width: 20px;
  111. height: 20px;
  112. background-color: rgba(0, 0, 0, 0.5);
  113. color: white;
  114. border-radius: 50%;
  115. display: flex;
  116. align-items: center;
  117. justify-content: center;
  118. font-size: 16px;
  119. cursor: pointer;
  120. }
  121. .upload-btn {
  122. width: 100px;
  123. height: 100px;
  124. border: 1px dashed #ccc;
  125. display: flex;
  126. align-items: center;
  127. justify-content: center;
  128. font-size: 24px;
  129. color: #ccc;
  130. margin: 5px;
  131. border-radius: 5px;
  132. }
  133. </style>