upload-image.uvue 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <template>
  2. <view class="upload-image">
  3. <view class="image-list">
  4. <view v-for="(image, index) in imageUrls" :key="index" class="image-item">
  5. <image class="image" :src="image" mode="aspectFill" @click="handlePreview(index)"></image>
  6. <view class="image-delete" @click="handleDelete(index)">
  7. <image class="delete-icon" src="/static/images/common/3.png" mode="aspectFit"></image>
  8. </view>
  9. </view>
  10. <!-- 上传按钮 -->
  11. <view v-if="imageList.length < maxCount && !uploading" class="upload-box" @click="handleChooseImage">
  12. <text class="upload-icon">+</text>
  13. <text class="upload-text">上传图片</text>
  14. </view>
  15. <!-- 上传中 -->
  16. <view v-if="uploading" class="upload-box uploading">
  17. <text class="upload-text">上传中...</text>
  18. </view>
  19. </view>
  20. </view>
  21. </template>
  22. <script setup lang="uts">
  23. import { ref, computed } from 'vue'
  24. import { uploadImage } from '../../api/upload/image'
  25. import type { UploadResponse } from '../../types/workbench'
  26. // Props
  27. type Props = {
  28. modelValue?: UploadResponse[]
  29. maxCount?: number
  30. maxSize?: number // MB
  31. businessType?: string
  32. }
  33. let cameraInput: HTMLInputElement | null = null
  34. const createNativeInput = () => {
  35. if (cameraInput) return cameraInput
  36. // 创建原生 input 元素
  37. cameraInput = document.createElement('input')
  38. cameraInput.type = 'file'
  39. cameraInput.accept = 'image/*'
  40. cameraInput.style.position = 'fixed'
  41. cameraInput.style.top = '-1000px'
  42. cameraInput.style.left = '-1000px'
  43. cameraInput.style.opacity = '0'
  44. cameraInput.style.pointerEvents = 'none'
  45. cameraInput.setAttribute('capture', 'camera')
  46. // 添加到 body
  47. document.body.appendChild(cameraInput)
  48. return cameraInput
  49. }
  50. const props = withDefaults(defineProps<Props>(), {
  51. modelValue: () => [],
  52. maxCount: 9,
  53. maxSize: 50,
  54. businessType: 'image'
  55. })
  56. // Emits
  57. const emit = defineEmits<{
  58. (e: 'update:modelValue', images: UploadResponse[]): void
  59. (e: 'change', images: UploadResponse[]): void
  60. }>()
  61. // 图片列表
  62. const imageList = ref<UploadResponse[]>(props.modelValue)
  63. const uploading = ref<boolean>(false)
  64. // 计算图片 URL 数组(用于显示和预览)
  65. const imageUrls = computed<string[]>(() => {
  66. const urls: string[] = []
  67. for (let i = 0; i < imageList.value.length; i++) {
  68. urls.push(imageList.value[i].url)
  69. }
  70. return urls
  71. })
  72. // 上传图片
  73. const handleUpload = async (filePaths: string[]): Promise<void> => {
  74. try {
  75. uploading.value = true
  76. for (let i = 0; i < filePaths.length; i++) {
  77. const filePath = filePaths[i]
  78. // 上传图片,获取完整的文件信息
  79. const result = await uploadImage(filePath, props.businessType)
  80. // 存储完整的上传响应对象
  81. imageList.value.push(result)
  82. }
  83. emit('update:modelValue', imageList.value)
  84. emit('change', imageList.value)
  85. uni.showToast({
  86. title: '上传成功',
  87. icon: 'success'
  88. })
  89. } catch (e: any) {
  90. uni.showToast({
  91. title: e.message ?? '上传失败',
  92. icon: 'none'
  93. })
  94. } finally {
  95. uploading.value = false
  96. }
  97. }
  98. const openCamera = () => {
  99. const input = createNativeInput()
  100. // 清除事件监听器(避免重复绑定)
  101. input.onchange = null
  102. input.onchange = (event: Event) => {
  103. const target = event.target as HTMLInputElement
  104. if (target.files && target.files.length > 0) {
  105. const file = target.files[0]
  106. handleFile(file)
  107. }
  108. // 清除值以便下次选择
  109. input.value = ''
  110. }
  111. // 触发点击
  112. input.click()
  113. }
  114. const handleFile = (file: File) => {
  115. const blobUrl = URL.createObjectURL(file)
  116. const result = {
  117. tempFilePaths: [blobUrl], // 类似: blob:http://localhost:8080/550e8400-e29b-41d4-a716-446655440000
  118. tempFiles: [{
  119. path: blobUrl,
  120. size: file.size,
  121. name: file.name,
  122. type: file.type,
  123. lastModified: file.lastModified
  124. }]
  125. }
  126. handleUpload(result.tempFilePaths)
  127. }
  128. // 选择图片
  129. const handleChooseImage = (): void => {
  130. // let inputHtml = createNativeInput();
  131. if (imageList.value.length >= props.maxCount) {
  132. uni.showToast({
  133. title: `最多上传${props.maxCount}张图片`,
  134. icon: 'none'
  135. })
  136. return
  137. }
  138. uni.chooseImage({
  139. count: props.maxCount - imageList.value.length,
  140. sizeType: ['compressed'],
  141. sourceType: ['album', 'camera'],
  142. success: (res) => {
  143. const tempFilePaths = res.tempFilePaths as string[]
  144. handleUpload(tempFilePaths)
  145. }
  146. })
  147. }
  148. // 预览图片
  149. const handlePreview = (index: number): void => {
  150. uni.previewImage({
  151. urls: imageUrls.value,
  152. current: index
  153. })
  154. }
  155. // 删除图片
  156. const handleDelete = (index: number): void => {
  157. uni.showModal({
  158. title: '确认删除',
  159. content: '确定要删除这张图片吗?',
  160. success: (res) => {
  161. const confirm = res.confirm as boolean
  162. if (confirm) {
  163. imageList.value.splice(index, 1)
  164. emit('update:modelValue', imageList.value)
  165. emit('change', imageList.value)
  166. }
  167. }
  168. })
  169. }
  170. </script>
  171. <style lang="scss">
  172. .image {
  173. &-list {
  174. flex-direction: row;
  175. flex-wrap: wrap;
  176. }
  177. &-item {
  178. background-color: #f5f7fa;
  179. border-radius: 8rpx;
  180. position: relative;
  181. width: 160rpx;
  182. height: 160rpx;
  183. margin-right: 20rpx;
  184. margin-bottom: 20rpx;
  185. }
  186. width: 160rpx;
  187. height: 160rpx;
  188. border-radius: 8rpx;
  189. &-delete {
  190. position: absolute;
  191. top: 2rpx;
  192. right: 2rpx;
  193. width: 44rpx;
  194. height: 44rpx;
  195. justify-content: center;
  196. align-items: center;
  197. }
  198. }
  199. .delete-icon {
  200. width: 32rpx;
  201. height: 32rpx;
  202. }
  203. .upload {
  204. &-box {
  205. background-color: #f5f7fa;
  206. border-radius: 8rpx;
  207. width: 160rpx;
  208. height: 160rpx;
  209. margin-right: 20rpx;
  210. margin-bottom: 20rpx;
  211. border: 2rpx dashed #d0d0d0;
  212. border-radius: 8rpx;
  213. justify-content: center;
  214. align-items: center;
  215. }
  216. &-icon {
  217. font-size: 60rpx;
  218. color: #999999;
  219. line-height: 60rpx;
  220. margin-bottom: 10rpx;
  221. }
  222. &-text {
  223. font-size: 24rpx;
  224. color: #999999;
  225. }
  226. }
  227. .uploading {
  228. border-style: solid;
  229. border-color: #d0d0d0;
  230. background-color: #f5f7fa;
  231. }
  232. </style>