image-upload.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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"
  12. :style="{ backgroundColor: bgColor }">
  13. <u-icon name="camera" size="48" color="#909399"></u-icon>
  14. <text class="btn-text">图片/视频</text>
  15. </view>
  16. </view>
  17. <view class="upload-tip" v-if="tip">
  18. <u-icon name="info-circle" size="24" color="#909399"></u-icon>
  19. <text class="tip-text">{{ tip }}</text>
  20. </view>
  21. </view>
  22. </template>
  23. <script>
  24. export default {
  25. name: 'CommonImageUpload',
  26. props: {
  27. value: {
  28. type: Array,
  29. default: () => []
  30. },
  31. maxCount: {
  32. type: Number,
  33. default: 9
  34. },
  35. bgColor: {
  36. type: String,
  37. default: '#f4f5f6'
  38. },
  39. uploadUrl: {
  40. type: String,
  41. required: true
  42. },
  43. header: {
  44. type: Object,
  45. default: () => ({})
  46. },
  47. name: {
  48. type: String,
  49. default: 'file'
  50. },
  51. tip: {
  52. type: String,
  53. default: ''
  54. }
  55. },
  56. methods: {
  57. onChoose() {
  58. uni.chooseImage({
  59. count: this.maxCount - this.value.length,
  60. success: async (res) => {
  61. uni.showLoading({
  62. title: '上传中...'
  63. });
  64. const tempFilePaths = res.tempFilePaths;
  65. const uploadPromises = tempFilePaths.map(path => {
  66. return this.uploadFileAjax(path);
  67. });
  68. try {
  69. const results = await Promise.all(uploadPromises);
  70. const newList = [...this.value, ...results];
  71. this.$emit('input', newList);
  72. uni.hideLoading();
  73. } catch (e) {
  74. uni.hideLoading();
  75. uni.showToast({
  76. title: e.msg || '上传失败',
  77. icon: 'none'
  78. });
  79. console.error(e);
  80. }
  81. }
  82. });
  83. },
  84. uploadFileAjax(filePath) {
  85. return new Promise((resolve, reject) => {
  86. // 合并 header,确保 token 存在
  87. const header = {
  88. 'Authorization': uni.getStorageSync('token') || '',
  89. ...this.header
  90. };
  91. // 处理 URL,如果传入的是完整 URL 则直接使用,否则拼接 baseUrl
  92. let url = this.uploadUrl;
  93. if (!url.startsWith('http')) {
  94. url = uni.$u.http.config.baseUrl + url;
  95. }
  96. uni.uploadFile({
  97. url: url,
  98. filePath: filePath,
  99. name: this.name,
  100. header: header,
  101. success: (res) => {
  102. try {
  103. const data = JSON.parse(res.data);
  104. if (data.code == 200) {
  105. resolve(data.data);
  106. } else {
  107. reject(data);
  108. }
  109. } catch (e) {
  110. reject({ msg: '解析失败' });
  111. }
  112. },
  113. fail: (err) => {
  114. reject(err);
  115. }
  116. });
  117. });
  118. },
  119. onDelete(index) {
  120. const newList = [...this.value];
  121. newList.splice(index, 1);
  122. this.$emit('input', newList);
  123. },
  124. onPreview(index) {
  125. uni.previewImage({
  126. urls: this.value,
  127. current: index
  128. });
  129. }
  130. }
  131. }
  132. </script>
  133. <style lang="scss" scoped>
  134. .image-upload-container {
  135. width: 100%;
  136. }
  137. .upload-list {
  138. display: flex;
  139. flex-wrap: wrap;
  140. margin: 0 -10rpx;
  141. }
  142. .upload-item,
  143. .upload-btn {
  144. width: 160rpx;
  145. height: 160rpx;
  146. margin: 10rpx;
  147. border-radius: 8rpx;
  148. position: relative;
  149. }
  150. .upload-btn {
  151. display: flex;
  152. flex-direction: column;
  153. align-items: center;
  154. justify-content: center;
  155. .btn-text {
  156. font-size: 24rpx;
  157. color: #909399;
  158. margin-top: 10rpx;
  159. }
  160. }
  161. .delete-btn {
  162. position: absolute;
  163. top: 0;
  164. right: 0;
  165. width: 40rpx;
  166. height: 40rpx;
  167. background-color: rgba(0, 0, 0, 0.5);
  168. border-bottom-left-radius: 8rpx;
  169. border-top-right-radius: 8rpx;
  170. display: flex;
  171. align-items: center;
  172. justify-content: center;
  173. z-index: 1;
  174. }
  175. .upload-tip {
  176. margin-top: 20rpx;
  177. display: flex;
  178. align-items: center;
  179. .tip-text {
  180. font-size: 24rpx;
  181. color: #909399;
  182. margin-left: 10rpx;
  183. }
  184. }
  185. </style>