index.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <template>
  2. <view class="image-upload-container">
  3. <view class="upload-list">
  4. <view class="upload-item" v-for="(item, index) in value" :key="index">
  5. <u-image :src="item" width="160rpx" height="160rpx" border-radius="8"
  6. @click="onPreview(index)"></u-image>
  7. <view class="delete-btn" @click.stop="onDelete(index)">
  8. <u-icon name="close" color="#ffffff" size="20"></u-icon>
  9. </view>
  10. </view>
  11. <view class="upload-btn" v-if="value.length < maxCount" @click="onChoose" :style="{ backgroundColor: bgColor }">
  12. <u-icon name="camera" size="48" color="#909399"></u-icon>
  13. <text class="btn-text">图片/视频</text>
  14. </view>
  15. </view>
  16. <view class="upload-tip">
  17. <u-icon name="info-circle" size="24" color="#909399"></u-icon>
  18. <text class="tip-text">上传“有效截图”可以让问题优先被发现哦!</text>
  19. </view>
  20. </view>
  21. </template>
  22. <script>
  23. export default {
  24. name: 'ImageUpload',
  25. props: {
  26. value: {
  27. type: Array,
  28. default: () => []
  29. },
  30. maxCount: {
  31. type: Number,
  32. default: 9
  33. },
  34. bgColor: {
  35. type: String,
  36. default: '#fff'
  37. }
  38. },
  39. methods: {
  40. onChoose() {
  41. uni.chooseImage({
  42. count: this.maxCount - this.value.length,
  43. success: async (res) => {
  44. uni.showLoading({
  45. title: '上传中...'
  46. });
  47. const tempFilePaths = res.tempFilePaths;
  48. const uploadPromises = tempFilePaths.map(path => {
  49. return this.uploadFeedbackFileAjax(path);
  50. });
  51. try {
  52. const results = await Promise.all(uploadPromises);
  53. const newList = [...this.value, ...results];
  54. this.$emit('input', newList);
  55. uni.hideLoading();
  56. } catch (e) {
  57. uni.hideLoading();
  58. uni.showToast({
  59. title: '上传失败',
  60. icon: 'none'
  61. });
  62. console.error(e);
  63. }
  64. }
  65. });
  66. },
  67. uploadFeedbackFileAjax: (filePath) => {
  68. return new Promise((resolve, reject) => {
  69. uni.uploadFile({
  70. url: uni.$u.http.config.baseUrl + '/token/shop/feedback/fileUpload',
  71. filePath: filePath,
  72. name: 'file',
  73. header: {
  74. 'Authorization': uni.getStorageSync('token') || '',
  75. },
  76. success: (res) => {
  77. const data = JSON.parse(res.data);
  78. if (data.code == 200) {
  79. resolve(data.data);
  80. } else {
  81. reject(data.msg);
  82. }
  83. },
  84. fail: (err) => {
  85. reject(err);
  86. }
  87. });
  88. });
  89. },
  90. onDelete(index) {
  91. const newList = [...this.value];
  92. newList.splice(index, 1);
  93. this.$emit('input', newList);
  94. },
  95. onPreview(index) {
  96. uni.previewImage({
  97. urls: this.value,
  98. current: index
  99. });
  100. }
  101. }
  102. }
  103. </script>
  104. <style lang="scss" scoped>
  105. .image-upload-container {
  106. width: 100%;
  107. }
  108. .upload-list {
  109. display: flex;
  110. flex-wrap: wrap;
  111. margin: 0 -10rpx;
  112. }
  113. .upload-item,
  114. .upload-btn {
  115. width: 160rpx;
  116. height: 160rpx;
  117. margin: 10rpx;
  118. border-radius: 8rpx;
  119. position: relative;
  120. }
  121. .upload-btn {
  122. background-color: #f4f5f6;
  123. display: flex;
  124. flex-direction: column;
  125. align-items: center;
  126. justify-content: center;
  127. .btn-text {
  128. font-size: 24rpx;
  129. color: #909399;
  130. margin-top: 10rpx;
  131. }
  132. }
  133. .delete-btn {
  134. position: absolute;
  135. top: 0;
  136. right: 0;
  137. width: 40rpx;
  138. height: 40rpx;
  139. background-color: rgba(0, 0, 0, 0.5);
  140. border-bottom-left-radius: 8rpx;
  141. border-top-right-radius: 8rpx;
  142. display: flex;
  143. align-items: center;
  144. justify-content: center;
  145. z-index: 1;
  146. }
  147. .upload-tip {
  148. margin-top: 20rpx;
  149. display: flex;
  150. align-items: center;
  151. .tip-text {
  152. font-size: 24rpx;
  153. color: #909399;
  154. margin-left: 10rpx;
  155. }
  156. }
  157. </style>