uploadPay.vue 8.39 KB
<template>
	<view class="page">
		<scroll-view class="scroll" scroll-y>
			<view class="detail-page">
				<!-- 基础信息 -->
				<view class="section">
					<text class="company">{{ form.code || '-' }}</text>
					<view class="row"><text class="label">公司简称</text><text class="value">{{ form.companyShortName || form.companyName || '-' }}</text></view>
					<view class="row"><text class="label">供应商名称</text><text class="value">{{ form.supplierName || '-' }}</text></view>
					<view class="row"><text class="label">采购处</text><text class="value">{{ form.purchaseDepartmentName || form.purchaseDepartment || '-' }}</text></view>
				</view>

				<!-- 请款附件 -->
				<view class="sub-table">
					<view class="sub-header">
						<image class="sub-bar" src="/static/images/title.png" />
						<text class="sub-title">请款附件</text>
						<view class="upload-btn" @click="selectFile">上传文件</view>
					</view>
					<view class="sub-body">
						<view v-if="!fileList.length" class="empty-tip">暂无附件,请上传</view>
						<view v-for="(file, idx) in fileList" :key="file.id || idx" class="file-item">
							<text class="file-name">{{ file.name || '未命名文件' }}</text>
							<text class="file-del" @click="removeFile(idx)">删除</text>
						</view>
					</view>
				</view>
			</view>
		</scroll-view>
		<view class="footer">
			<button class="btn submit" type="primary" :loading="loading" @click="onSubmit">提交</button>
		</view>
	</view>
</template>

<script>
import { procurementPlanDetailApi, procurementPlanUploadRequestAttachmentApi } from '@/api/procure-manage/procurementPlan.js'
import upload from '@/utils/upload'
import { uploadFileApi } from '@/api/base.js'

export default {
	name: 'DomesticPlanUploadPay',
	data() {
		return {
			id: '',
			loading: false,
			form: {},
			// 请款附件列表 [{ id, name }]
			fileList: []
		}
	},
	onLoad(query) {
		this.id = (query && query.id) || ''
		this.loadDetail()
	},
	methods: {
		async loadDetail() {
			if (!this.id) {
				uni.showToast({ title: '缺少单据ID', icon: 'none' })
				return
			}
			uni.showLoading({ title: '加载中...', mask: true })
			try {
				const res = await procurementPlanDetailApi({ id: this.id })
				const m = (res && res.data) || {}
				this.form = m
				// 详情接口返回的请款附件字段为 requestFileIds/requestFileNames
				const ids = String(m.requestFileIds || '').split(',').filter(Boolean)
				const names = String(m.requestFileNames || '').split(',').filter(Boolean)
				this.fileList = ids.map((id, i) => ({ id, name: names[i] || '' }))
			} catch (e) {
				uni.showToast({ title: '加载详情失败', icon: 'none' })
			} finally {
				uni.hideLoading()
			}
		},
		// 选择并上传文件(多文件逐个追加)
		selectFile() {
			if (typeof uni !== 'undefined' && typeof uni.chooseMessageFile === 'function') {
				uni.chooseMessageFile({
					count: 9,
					type: 'all',
					success: async res => {
						const files = (res && res.tempFiles) || []
						if (!files.length) return
						uni.showLoading({ title: '上传中...', mask: true })
						try {
							for (const f of files) {
								const name = f.name || 'file'
								const r = await upload({ url: '/sw/filebox/uploadFile', filePath: f.path, name: 'file' })
								const d = r && r.data ? r.data : r
								const id = (d && (d.id || d.fileId || (d.data && (d.data.id || d.data.fileId))))
									? String(d.id || d.fileId || d.data.id || d.data.fileId)
									: ''
								if (id) this.fileList.push({ id, name })
							}
						} catch (e) {
							uni.showToast({ title: '上传失败', icon: 'none' })
						} finally {
							uni.hideLoading()
						}
					},
					fail: () => {
						uni.showToast({ title: '未选择文件', icon: 'none' })
					}
				})
				return
			}
			// H5 兜底:动态创建原生 input
			try {
				const input = document.createElement('input')
				input.type = 'file'
				input.multiple = true
				input.style.position = 'fixed'
				input.style.left = '-9999px'
				document.body.appendChild(input)
				input.addEventListener('change', this.onFileChange)
				input.click()
				setTimeout(() => { document.body.removeChild(input) }, 1000)
			} catch (e) {
				uni.showToast({ title: '无法打开文件选择', icon: 'none' })
			}
		},
		async onFileChange(e) {
			const files = (e && e.target && e.target.files) || []
			if (!files.length) return
			uni.showLoading({ title: '上传中...', mask: true })
			try {
				for (const file of files) {
					const name = file.name || ''
					const fd = new FormData()
					fd.append('file', file, name)
					const d = await uploadFileApi(fd)
					const data = d && d.data ? d.data : d
					const id = (data && (data.id || data.fileId || (data.data && (data.data.id || data.data.fileId))))
						? String(data.id || data.fileId || data.data.id || data.data.fileId)
						: ''
					if (id) this.fileList.push({ id, name })
				}
			} catch (e2) {
				uni.showToast({ title: '上传失败', icon: 'none' })
			} finally {
				uni.hideLoading()
				if (e && e.target) e.target.value = ''
			}
		},
		removeFile(idx) {
			this.fileList.splice(idx, 1)
		},
		async onSubmit() {
			const planId = (this.form && this.form.id) || this.id
			if (!planId) {
				uni.showToast({ title: '缺少单据id', icon: 'none' })
				return
			}
			const ids = this.fileList.map(it => it.id).filter(Boolean)
			const names = this.fileList.map(it => it.name).filter(Boolean)
			const data = {
				id: planId,
				fileIds: ids.join(','),
				fileNames: names.join(',')
			}
			const confirmRes = await new Promise(resolve => {
				uni.showModal({ title: '提交确认', content: '确定提交请款附件吗?', success: resolve })
			})
			if (!(confirmRes && confirmRes.confirm)) return
			this.loading = true
			try {
				await procurementPlanUploadRequestAttachmentApi(data)
				uni.showToast({ title: '提交成功', icon: 'success' })
				try {
					uni.setStorageSync('DOMESTIC_PLAN_LIST_NEED_REFRESH', '1')
					uni.setStorageSync('DOMESTIC_PLAN_DETAIL_NEED_REFRESH', '1')
				} catch (e) { }
				setTimeout(() => { uni.navigateBack() }, 400)
			} catch (e) {
				uni.showToast({ title: (e && (e.msg || e.message)) || '提交失败', icon: 'none' })
			} finally {
				this.loading = false
			}
		}
	}
}
</script>

<style lang="scss" scoped>
.page {
	display: flex;
	flex-direction: column;
	height: 100%;
}

.scroll {
	flex: 1;
	padding: 8rpx 0 176rpx;
}

.detail-page {
	background: #f3f3f3;
}

.footer {
	z-index: 2;
	position: fixed;
	left: 0;
	right: 0;
	bottom: 0;
	padding: 32rpx;
	padding-bottom: calc(32rpx + env(safe-area-inset-bottom));
	background: #fff;
	box-shadow: 0 -8rpx 24rpx rgba(0, 0, 0, 0.06);

	.btn {
		height: 80rpx;
		line-height: 80rpx;
		border-radius: 12rpx;
		font-size: 32rpx;
	}

	.submit {
		background: $theme-primary;
		color: #fff;
	}
}

.section {
	padding: 32rpx;
	background: #fff;
	margin-bottom: 20rpx;

	.company {
		display: block;
		font-size: 36rpx;
		font-weight: 600;
		color: rgba(0, 0, 0, 0.9);
		padding: 8rpx 0 24rpx;
		line-height: 50rpx;
	}
}

.row {
	display: flex;
	margin-bottom: 28rpx;

	&:last-child {
		margin-bottom: 0;
	}

	.label {
		max-width: 420rpx;
		margin-right: 20rpx;
		line-height: 32rpx;
		font-size: 28rpx;
		color: rgba(0, 0, 0, 0.6);
	}

	.value {
		flex: 1;
		line-height: 32rpx;
		font-size: 28rpx;
		color: rgba(0, 0, 0, 0.9);
		text-align: right;
		word-break: break-all;
	}
}

.sub-table {
	background: #fff;
	margin-bottom: 20rpx;
}

.sub-header {
	display: flex;
	align-items: center;
	padding: 16rpx 0 16rpx 32rpx;

	.sub-bar {
		width: 6rpx;
		height: 32rpx;
		margin-right: 14rpx;
	}

	.sub-title {
		font-size: 30rpx;
		font-weight: 600;
		color: rgba(0, 0, 0, 0.9);
		flex: 1;
		line-height: 42rpx;
	}

	.upload-btn {
		margin-right: 32rpx;
		height: 56rpx;
		line-height: 56rpx;
		padding: 0 24rpx;
		font-size: 26rpx;
		border-radius: 8rpx;
		background: $theme-primary;
		color: #fff;
	}
}

.sub-body {
	padding: 0 32rpx 24rpx;

	.empty-tip {
		text-align: center;
		padding: 60rpx 0;
		color: rgba(0, 0, 0, 0.4);
		font-size: 28rpx;
	}

	.file-item {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 20rpx 24rpx;
		margin-bottom: 16rpx;
		background: #fafafa;
		border-radius: 8rpx;

		&:last-child {
			margin-bottom: 0;
		}

		.file-name {
			flex: 1;
			font-size: 28rpx;
			color: rgba(0, 0, 0, 0.9);
			word-break: break-all;
			margin-right: 20rpx;
		}

		.file-del {
			flex-shrink: 0;
			font-size: 26rpx;
			color: #d54941;
		}
	}
}
</style>