image-upload.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. default: ''
  42. },
  43. code: {
  44. type: String,
  45. default: ''
  46. },
  47. header: {
  48. type: Object,
  49. default: () => ({})
  50. },
  51. name: {
  52. type: String,
  53. default: 'file'
  54. },
  55. tip: {
  56. type: String,
  57. default: ''
  58. }
  59. },
  60. methods: {
  61. onChoose() {
  62. uni.chooseImage({
  63. count: this.maxCount - this.value.length,
  64. success: async (res) => {
  65. uni.showLoading({
  66. title: '上传中...'
  67. });
  68. const tempFilePaths = res.tempFilePaths;
  69. const uploadPromises = tempFilePaths.map(path => {
  70. return this.uploadFileAjax(path);
  71. });
  72. try {
  73. const results = await Promise.all(uploadPromises);
  74. const newList = [...this.value, ...results];
  75. this.$emit('input', newList);
  76. uni.hideLoading();
  77. } catch (e) {
  78. uni.hideLoading();
  79. uni.showToast({
  80. title: e.msg || '上传失败',
  81. icon: 'none'
  82. });
  83. console.error(e);
  84. }
  85. }
  86. });
  87. },
  88. uploadFileAjax(filePath) {
  89. return new Promise((resolve, reject) => {
  90. // 合并 header,确保 token 存在
  91. const header = {
  92. 'Authorization': uni.getStorageSync('token') || '',
  93. ...this.header
  94. };
  95. // 处理 URL
  96. let url = this.uploadUrl;
  97. if (!url) {
  98. // 如果没有传入 uploadUrl,则使用默认的通用上传接口
  99. // 默认接口需要 code 参数
  100. if (!this.code) {
  101. console.error('使用通用上传接口时,必须传入 code 参数');
  102. reject({ msg: '缺少上传场景码' });
  103. return;
  104. }
  105. url = `/api/token/mini/fileUpload/${this.code}`;
  106. }
  107. if (!url.startsWith('http')) {
  108. url = uni.$u.http.config.baseUrl + url;
  109. }
  110. uni.uploadFile({
  111. url: url,
  112. filePath: filePath,
  113. name: this.name,
  114. header: header,
  115. success: (res) => {
  116. try {
  117. const data = JSON.parse(res.data);
  118. if (data.code == 200) {
  119. resolve(data.data);
  120. } else {
  121. reject(data);
  122. }
  123. } catch (e) {
  124. reject({ msg: '解析失败' });
  125. }
  126. },
  127. fail: (err) => {
  128. reject(err);
  129. }
  130. });
  131. });
  132. },
  133. onDelete(index) {
  134. const newList = [...this.value];
  135. newList.splice(index, 1);
  136. this.$emit('input', newList);
  137. },
  138. onPreview(index) {
  139. uni.previewImage({
  140. urls: this.value,
  141. current: index
  142. });
  143. }
  144. }
  145. }
  146. </script>
  147. <style lang="scss" scoped>
  148. .image-upload-container {
  149. width: 100%;
  150. }
  151. .upload-list {
  152. display: flex;
  153. flex-wrap: wrap;
  154. margin: 0 -10rpx;
  155. }
  156. .upload-item,
  157. .upload-btn {
  158. width: 160rpx;
  159. height: 160rpx;
  160. margin: 10rpx;
  161. border-radius: 8rpx;
  162. position: relative;
  163. }
  164. .upload-btn {
  165. display: flex;
  166. flex-direction: column;
  167. align-items: center;
  168. justify-content: center;
  169. .btn-text {
  170. font-size: 24rpx;
  171. color: #909399;
  172. margin-top: 10rpx;
  173. }
  174. }
  175. .delete-btn {
  176. position: absolute;
  177. top: 0;
  178. right: 0;
  179. width: 40rpx;
  180. height: 40rpx;
  181. background-color: rgba(0, 0, 0, 0.5);
  182. border-bottom-left-radius: 8rpx;
  183. border-top-right-radius: 8rpx;
  184. display: flex;
  185. align-items: center;
  186. justify-content: center;
  187. z-index: 1;
  188. }
  189. .upload-tip {
  190. margin-top: 20rpx;
  191. display: flex;
  192. align-items: center;
  193. .tip-text {
  194. font-size: 24rpx;
  195. color: #909399;
  196. margin-left: 10rpx;
  197. }
  198. }
  199. </style>