OaBankReceiptDialog.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. <template>
  2. <el-dialog
  3. :model-value="modelValue"
  4. title="上传银行回执单"
  5. width="720px"
  6. class="dialog-form-layout oa-bank-receipt-dialog"
  7. align-center
  8. append-to-body
  9. destroy-on-close
  10. modal-class="protocol-dialog-mask"
  11. :close-on-click-modal="false"
  12. @update:model-value="emit('update:modelValue', $event)"
  13. >
  14. <div v-loading="loading" class="receipt-body">
  15. <div class="meta">
  16. <div>
  17. <div class="meta-label">流程名称</div>
  18. <div class="meta-value">{{ meta?.appName || '—' }}</div>
  19. </div>
  20. <div>
  21. <div class="meta-label">申请人</div>
  22. <div class="meta-value">{{ meta?.initiatorUserName || '—' }}</div>
  23. </div>
  24. <div>
  25. <div class="meta-label">申请时间</div>
  26. <div class="meta-value">{{ meta?.applyTime || '—' }}</div>
  27. </div>
  28. <div>
  29. <div class="meta-label">已上传</div>
  30. <div class="meta-value">{{ attachments.length }} 张回执单</div>
  31. </div>
  32. </div>
  33. <p class="section-label">银行回执单</p>
  34. <div
  35. class="attachment-upload"
  36. :class="{ 'is-dragover': isAttachDragover }"
  37. v-bind="attachDropHandlers"
  38. >
  39. <div class="attachment-list">
  40. <el-upload
  41. ref="uploadRef"
  42. :auto-upload="false"
  43. :show-file-list="false"
  44. :multiple="true"
  45. accept="image/*,video/*,.pdf,.doc,.docx,.xls,.xlsx,.ppt,.pptx,.txt,.zip,.rar"
  46. @change="handleAttachmentChange"
  47. >
  48. <div class="upload-card">
  49. <el-icon :size="28" color="#10b981"><Plus /></el-icon>
  50. <span class="upload-card-text">上传附件</span>
  51. </div>
  52. </el-upload>
  53. <div
  54. v-for="(file, index) in attachments"
  55. :key="file.id || index"
  56. class="attachment-item"
  57. :class="{ 'is-uploading': file._uploading }"
  58. >
  59. <div
  60. class="attachment-preview"
  61. @click="isImageFile(file) && file.fileUrl ? handlePreview(file) : undefined"
  62. >
  63. <template v-if="isImageFile(file) && file.fileUrl">
  64. <img :src="fullFileUrl(file.fileUrl)" :alt="file.fileName" />
  65. </template>
  66. <template v-else-if="isImageFile(file) && !file.fileUrl">
  67. <el-icon :size="24" color="#94a3b8"><Loading /></el-icon>
  68. </template>
  69. <template v-else>
  70. <div class="attachment-preview__icon attachment-preview__icon--doc">
  71. <el-icon :size="28"><Document /></el-icon>
  72. </div>
  73. </template>
  74. </div>
  75. <div class="attachment-footer">
  76. <span class="attachment-name" :title="file.fileName">{{ file.fileName }}</span>
  77. <div class="attachment-actions">
  78. <el-icon
  79. class="action-btn"
  80. title="下载"
  81. @click="handleDownload(file)"
  82. >
  83. <Download />
  84. </el-icon>
  85. <el-icon
  86. class="action-btn action-btn--danger"
  87. title="删除"
  88. @click="removeAttachment(index)"
  89. >
  90. <Delete />
  91. </el-icon>
  92. </div>
  93. </div>
  94. </div>
  95. </div>
  96. <span class="upload-hint">
  97. 支持拖拽或点击上传图片、PDF 等文件,单文件不超过 20MB;可上传多张回执单
  98. </span>
  99. </div>
  100. </div>
  101. <template #footer>
  102. <el-button class="btn-ghost" @click="emit('update:modelValue', false)">
  103. <el-icon><Close /></el-icon>
  104. 取消
  105. </el-button>
  106. <el-button class="btn-main" type="primary" :loading="saving" @click="handleSave">
  107. <el-icon><Check /></el-icon>
  108. 确定
  109. </el-button>
  110. </template>
  111. <el-dialog
  112. v-model="previewVisible"
  113. title="预览"
  114. width="800px"
  115. append-to-body
  116. destroy-on-close
  117. modal-class="protocol-dialog-mask"
  118. >
  119. <img v-if="previewUrl" :src="previewUrl" alt="preview" class="preview-img" />
  120. </el-dialog>
  121. </el-dialog>
  122. </template>
  123. <script setup lang="ts">
  124. import { computed, nextTick, ref, watch } from 'vue'
  125. import { ElMessage } from 'element-plus'
  126. import {
  127. Check,
  128. Close,
  129. Delete,
  130. Document,
  131. Download,
  132. Loading,
  133. Plus
  134. } from '@element-plus/icons-vue'
  135. import {
  136. fetchOaBankReceipts,
  137. fetchReplaceOaBankReceipts,
  138. type OaBankReceiptItem
  139. } from '@/api/oaApproval'
  140. import { fetchUploadAttachment } from '@/api/business'
  141. import { useAttachmentDropZone } from '@/composables/useAttachmentDropZone'
  142. import { downloadAttachmentFile } from '@/utils/download-attachment'
  143. defineOptions({ name: 'OaBankReceiptDialog' })
  144. type AttachmentItem = OaBankReceiptItem & { _uploading?: boolean }
  145. const props = defineProps<{
  146. modelValue: boolean
  147. instanceId: string
  148. meta?: {
  149. appName?: string
  150. initiatorUserName?: string
  151. applyTime?: string
  152. }
  153. }>()
  154. const emit = defineEmits<{
  155. 'update:modelValue': [boolean]
  156. saved: []
  157. }>()
  158. const loading = ref(false)
  159. const saving = ref(false)
  160. const attachments = ref<AttachmentItem[]>([])
  161. const uploadRef = ref()
  162. const previewVisible = ref(false)
  163. const previewUrl = ref('')
  164. const minioBaseUrl = (import.meta.env.VITE_MINIO_BASE_URL || '').replace(/\/$/, '')
  165. function fullFileUrl(suffix: string) {
  166. const s = String(suffix || '').trim()
  167. if (!s) return ''
  168. if (/^https?:\/\//i.test(s)) return s
  169. return minioBaseUrl ? `${minioBaseUrl}/${s.replace(/^\//, '')}` : s
  170. }
  171. function isImageFile(file: AttachmentItem) {
  172. const name = String(file.fileName || file.fileUrl || '').toLowerCase()
  173. const type = String(file.fileType || '').toLowerCase()
  174. if (type.startsWith('image/')) return true
  175. return /\.(png|jpe?g|gif|webp|bmp|svg)$/i.test(name)
  176. }
  177. async function loadReceipts() {
  178. const id = String(props.instanceId || '').trim()
  179. if (!id) {
  180. attachments.value = []
  181. return
  182. }
  183. loading.value = true
  184. try {
  185. const res = await fetchOaBankReceipts(id)
  186. attachments.value = (res?.items || []).map((it) => ({
  187. id: it.id,
  188. fileName: it.fileName || '',
  189. fileUrl: it.fileUrl || '',
  190. fileType: it.fileType,
  191. fileSize: it.fileSize,
  192. createTime: it.createTime,
  193. _uploading: false
  194. }))
  195. } catch {
  196. attachments.value = []
  197. ElMessage.error('加载银行回执单失败')
  198. } finally {
  199. loading.value = false
  200. }
  201. }
  202. watch(
  203. () => [props.modelValue, props.instanceId] as const,
  204. ([visible]) => {
  205. if (visible) void loadReceipts()
  206. }
  207. )
  208. async function handleAttachmentChange(uploadFile: { raw?: File }) {
  209. const file = uploadFile.raw
  210. if (!file) return
  211. if (file.size > 20 * 1024 * 1024) {
  212. ElMessage.error('单文件不超过 20MB')
  213. await nextTick()
  214. uploadRef.value?.clearFiles?.()
  215. return
  216. }
  217. const tempId = `temp_${Date.now()}_${Math.random().toString(16).slice(2, 6)}`
  218. attachments.value.push({
  219. id: tempId,
  220. fileName: file.name,
  221. fileUrl: '',
  222. fileType: file.type,
  223. fileSize: file.size,
  224. _uploading: true
  225. })
  226. const idx = attachments.value.length - 1
  227. try {
  228. const fd = new FormData()
  229. fd.append('folder', 'oa-bank-receipt')
  230. fd.append('file', file)
  231. const res: any = await fetchUploadAttachment(fd)
  232. attachments.value[idx] = {
  233. id: res?.id || tempId,
  234. fileName: file.name || res?.fileName || '',
  235. fileUrl: res?.fileUrl || '',
  236. fileType: res?.fileType || file.type,
  237. fileSize: res?.fileSize ?? file.size,
  238. _uploading: false
  239. }
  240. } catch {
  241. ElMessage.error('文件上传失败')
  242. attachments.value.splice(idx, 1)
  243. } finally {
  244. await nextTick()
  245. uploadRef.value?.clearFiles?.()
  246. }
  247. }
  248. function removeAttachment(index: number) {
  249. attachments.value.splice(index, 1)
  250. }
  251. function handleDownload(file: AttachmentItem) {
  252. if (!file.fileUrl) return
  253. downloadAttachmentFile(file, { fullFileUrl })
  254. }
  255. function handlePreview(file: AttachmentItem) {
  256. if (!file.fileUrl) return
  257. previewUrl.value = fullFileUrl(file.fileUrl)
  258. previewVisible.value = true
  259. }
  260. const { isDragover: isAttachDragover, dropZoneHandlers: attachDropHandlers } =
  261. useAttachmentDropZone({
  262. onFiles: (files) => {
  263. for (const file of files) {
  264. void handleAttachmentChange({ raw: file })
  265. }
  266. }
  267. })
  268. async function handleSave() {
  269. const id = String(props.instanceId || '').trim()
  270. if (!id) return
  271. if (attachments.value.some((f) => f._uploading)) {
  272. ElMessage.warning('附件上传中,请稍候')
  273. return
  274. }
  275. saving.value = true
  276. try {
  277. const items = attachments.value
  278. .filter((f) => f.fileUrl && !f._uploading)
  279. .map((f) => ({
  280. fileUrl: f.fileUrl,
  281. fileName: f.fileName,
  282. fileType: f.fileType,
  283. fileSize: f.fileSize
  284. }))
  285. await fetchReplaceOaBankReceipts(id, items)
  286. ElMessage.success('银行回执单已保存')
  287. emit('saved')
  288. emit('update:modelValue', false)
  289. } catch {
  290. // request 层通常已提示
  291. } finally {
  292. saving.value = false
  293. }
  294. }
  295. const meta = computed(() => props.meta || {})
  296. </script>
  297. <style scoped lang="scss">
  298. .receipt-body {
  299. min-height: 120px;
  300. }
  301. .meta {
  302. display: grid;
  303. grid-template-columns: 1fr 1fr;
  304. gap: 12px 16px;
  305. margin-bottom: 20px;
  306. padding: 12px 14px;
  307. background: #f8fafc;
  308. border: 1px solid #e2e8f0;
  309. border-radius: 8px;
  310. }
  311. .meta-label {
  312. margin-bottom: 4px;
  313. font-size: 11px;
  314. color: #94a3b8;
  315. }
  316. .meta-value {
  317. font-size: 13px;
  318. font-weight: 500;
  319. color: #334155;
  320. }
  321. .section-label {
  322. margin: 0 0 10px;
  323. font-size: 13px;
  324. font-weight: 600;
  325. color: #0f172a;
  326. }
  327. .attachment-upload {
  328. width: 100%;
  329. }
  330. .attachment-list {
  331. display: flex;
  332. flex-wrap: wrap;
  333. gap: 12px;
  334. }
  335. .upload-card {
  336. display: flex;
  337. flex-direction: column;
  338. gap: 6px;
  339. align-items: center;
  340. justify-content: center;
  341. width: 140px;
  342. height: 96px;
  343. cursor: pointer;
  344. background: #f0fdf4;
  345. border: 1px dashed #a7f3d0;
  346. border-radius: 8px;
  347. &:hover {
  348. background: #ecfdf5;
  349. border-color: #10b981;
  350. border-style: solid;
  351. }
  352. }
  353. .upload-card-text {
  354. font-size: 12px;
  355. font-weight: 500;
  356. line-height: 1;
  357. color: #10b981;
  358. }
  359. .upload-hint {
  360. display: block;
  361. margin-top: 8px;
  362. font-size: 11px;
  363. line-height: 1.4;
  364. color: #94a3b8;
  365. }
  366. .attachment-item {
  367. width: 140px;
  368. overflow: hidden;
  369. background: #fff;
  370. border: 1px solid #e2e8f0;
  371. border-radius: 8px;
  372. &.is-uploading {
  373. pointer-events: none;
  374. opacity: 0.6;
  375. }
  376. &:hover {
  377. border-color: #10b981;
  378. box-shadow: 0 2px 8px rgba(16, 185, 129, 0.1);
  379. }
  380. }
  381. .attachment-preview {
  382. display: flex;
  383. align-items: center;
  384. justify-content: center;
  385. width: 100%;
  386. height: 96px;
  387. overflow: hidden;
  388. cursor: pointer;
  389. background: #f8fafc;
  390. img {
  391. display: block;
  392. width: 100%;
  393. height: 100%;
  394. object-fit: cover;
  395. }
  396. }
  397. .attachment-preview__icon {
  398. display: flex;
  399. align-items: center;
  400. justify-content: center;
  401. width: 48px;
  402. height: 48px;
  403. border-radius: 10px;
  404. }
  405. .attachment-preview__icon--doc {
  406. color: #0284c7;
  407. background: #e0f2fe;
  408. }
  409. .attachment-footer {
  410. padding: 6px 8px 8px;
  411. }
  412. .attachment-name {
  413. display: -webkit-box;
  414. min-height: 30px;
  415. margin-bottom: 4px;
  416. overflow: hidden;
  417. -webkit-line-clamp: 2;
  418. font-size: 11px;
  419. line-height: 1.35;
  420. color: #334155;
  421. -webkit-box-orient: vertical;
  422. }
  423. .attachment-actions {
  424. display: flex;
  425. gap: 12px;
  426. justify-content: center;
  427. }
  428. .action-btn {
  429. width: 22px;
  430. height: 22px;
  431. color: #64748b;
  432. cursor: pointer;
  433. border-radius: 4px;
  434. &:hover {
  435. color: #10b981;
  436. background: #ecfdf5;
  437. }
  438. &.action-btn--danger:hover {
  439. color: #ef4444;
  440. background: #fef2f2;
  441. }
  442. }
  443. .preview-img {
  444. display: block;
  445. max-width: 100%;
  446. margin: 0 auto;
  447. }
  448. </style>