DocumentAttachmentBody.vue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. <script setup lang="ts">
  2. import { reactive, watch, ref, computed } from 'vue'
  3. import { useUpload, pdfFileToImageFiles } from '@/components/UploadFile/src/useUpload'
  4. const message = useMessage()
  5. const props = withDefaults(defineProps<{
  6. isTextMode?: boolean
  7. modelValue?: { images?: string[] }
  8. title?: string
  9. description?: string
  10. elderId?: string | number
  11. elderName?: string
  12. funName?: string
  13. }>(), {
  14. isTextMode: false,
  15. modelValue: () => ({}),
  16. title: '',
  17. description: '',
  18. elderId: '',
  19. elderName: '',
  20. funName: '附件'
  21. })
  22. const emit = defineEmits<{
  23. (e: 'update:modelValue', val: { images?: string[] }): void
  24. }>()
  25. const form = reactive<{ images: string[] }>({
  26. images: []
  27. })
  28. watch(
  29. () => props.modelValue,
  30. (val) => {
  31. if (!val) return
  32. if (Array.isArray(val.images)) {
  33. form.images = [...val.images]
  34. } else {
  35. form.images = []
  36. }
  37. },
  38. { immediate: true, deep: true }
  39. )
  40. const updateForm = () => {
  41. emit('update:modelValue', {
  42. images: form.images ? [...form.images] : []
  43. })
  44. }
  45. const { httpRequest: uploadFn } = useUpload(props.funName, { elderId: props.elderId, elderName: props.elderName })
  46. const uploadingCount = ref(0)
  47. const uploading = computed(() => uploadingCount.value > 0)
  48. const uploadSingleFile = (file: File) => {
  49. uploadingCount.value++
  50. return uploadFn({ file } as any)
  51. .then((res: any) => {
  52. if (res.code === 0) {
  53. form.images.push(res.data)
  54. updateForm()
  55. }
  56. })
  57. .finally(() => {
  58. uploadingCount.value--
  59. })
  60. }
  61. const handleFileChange = async (e: Event) => {
  62. const target = e.target as HTMLInputElement
  63. const files = target.files
  64. if (!files || files.length === 0) return
  65. for (let i = 0; i < files.length; i++) {
  66. const file = files[i]
  67. if (file.type === 'application/pdf') {
  68. try {
  69. const imageFiles = await pdfFileToImageFiles(file)
  70. for (const imgFile of imageFiles) {
  71. await uploadSingleFile(imgFile)
  72. }
  73. } catch (error) {
  74. console.error('PDF 解析失败', error)
  75. message.error(`PDF 解析失败:${file.name}`)
  76. }
  77. } else {
  78. await uploadSingleFile(file)
  79. }
  80. }
  81. target.value = ''
  82. }
  83. const removeImage = (index: number) => {
  84. form.images.splice(index, 1)
  85. updateForm()
  86. }
  87. const fileInputRef = ref<HTMLInputElement | null>(null)
  88. const triggerUpload = () => {
  89. fileInputRef.value?.click()
  90. }
  91. defineExpose({ form })
  92. </script>
  93. <template>
  94. <div class="document-attachment-body">
  95. <div v-if="title" class="doc-title">{{ title }}</div>
  96. <p v-if="description" class="doc-desc">{{ description }}</p>
  97. <!-- 编辑模式:上传区域 -->
  98. <div v-if="!isTextMode" class="edit-section">
  99. <div class="upload-box" @click="triggerUpload">
  100. <div class="upload-placeholder">
  101. <span class="upload-icon">+</span>
  102. <span class="upload-text">{{ uploading ? '上传中...' : '点击上传图片或 PDF(支持多张)' }}</span>
  103. </div>
  104. <input
  105. ref="fileInputRef"
  106. type="file"
  107. accept="image/*,application/pdf"
  108. multiple
  109. style="display: none"
  110. @change="handleFileChange"
  111. />
  112. </div>
  113. <div v-if="form.images && form.images.length > 0" class="image-list">
  114. <div v-for="(img, idx) in form.images" :key="idx" class="image-item">
  115. <img :src="img" class="image-thumb" />
  116. <button class="remove-btn" @click.stop="removeImage(idx)">×</button>
  117. </div>
  118. </div>
  119. </div>
  120. <!-- 文本/打印模式:仅展示图片 -->
  121. <div v-else class="preview-section">
  122. <div v-if="form.images && form.images.length > 0" class="image-preview-list">
  123. <img
  124. v-for="(img, idx) in form.images"
  125. :key="idx"
  126. :src="img"
  127. class="preview-img"
  128. />
  129. </div>
  130. <div v-else class="empty-box">
  131. <span>(暂无图片)</span>
  132. </div>
  133. </div>
  134. </div>
  135. </template>
  136. <style scoped lang="scss">
  137. .document-attachment-body {
  138. width: 210mm;
  139. max-width: 100%;
  140. margin: 0 auto;
  141. font-family: 'SimSun', 'Microsoft YaHei', serif;
  142. font-size: 14px;
  143. color: #333;
  144. background: #fff;
  145. padding: 5mm;
  146. @media print {
  147. width: 210mm !important;
  148. padding: 0;
  149. border: none;
  150. margin: 0;
  151. box-sizing: border-box;
  152. }
  153. }
  154. .doc-title {
  155. text-align: center;
  156. font-size: 20px;
  157. font-weight: bold;
  158. margin-bottom: 16px;
  159. }
  160. .doc-desc {
  161. text-indent: 2em;
  162. margin: 0 0 16px;
  163. line-height: 1.8;
  164. }
  165. .edit-section {
  166. .upload-box {
  167. width: 100%;
  168. height: 160px;
  169. border: 2px dashed #ccc;
  170. border-radius: 4px;
  171. display: flex;
  172. align-items: center;
  173. justify-content: center;
  174. cursor: pointer;
  175. transition: border-color 0.2s;
  176. margin-bottom: 16px;
  177. &:hover {
  178. border-color: #409eff;
  179. }
  180. }
  181. .upload-placeholder {
  182. display: flex;
  183. flex-direction: column;
  184. align-items: center;
  185. color: #999;
  186. .upload-icon {
  187. font-size: 32px;
  188. line-height: 1;
  189. margin-bottom: 8px;
  190. }
  191. .upload-text {
  192. font-size: 14px;
  193. }
  194. }
  195. .image-list {
  196. display: flex;
  197. flex-direction: column;
  198. gap: 16px;
  199. }
  200. .image-item {
  201. position: relative;
  202. width: 100%;
  203. border: 1px solid #e4e7ed;
  204. border-radius: 4px;
  205. overflow: hidden;
  206. .image-thumb {
  207. width: 100%;
  208. display: block;
  209. object-fit: contain;
  210. }
  211. .remove-btn {
  212. position: absolute;
  213. top: 8px;
  214. right: 8px;
  215. width: 28px;
  216. height: 28px;
  217. border: none;
  218. border-radius: 50%;
  219. background: rgba(0, 0, 0, 0.5);
  220. color: #fff;
  221. font-size: 18px;
  222. line-height: 1;
  223. cursor: pointer;
  224. display: flex;
  225. align-items: center;
  226. justify-content: center;
  227. &:hover {
  228. background: rgba(0, 0, 0, 0.7);
  229. }
  230. }
  231. }
  232. }
  233. .preview-section {
  234. .image-preview-list {
  235. display: flex;
  236. flex-direction: column;
  237. gap: 16px;
  238. }
  239. .preview-img {
  240. width: 100%;
  241. display: block;
  242. object-fit: contain;
  243. border: 1px solid #333;
  244. box-sizing: border-box;
  245. }
  246. .empty-box {
  247. width: 100%;
  248. min-height: 400px;
  249. border: 1px solid #333;
  250. padding: 12px;
  251. box-sizing: border-box;
  252. color: #999;
  253. }
  254. }
  255. </style>