541 lines
12 KiB
Vue
541 lines
12 KiB
Vue
<template>
|
||
<view class="page">
|
||
<view v-if="loading" class="state-box">
|
||
<text class="state-text">加载中...</text>
|
||
</view>
|
||
<template v-else-if="detail">
|
||
<!-- 单据信息 -->
|
||
<view class="detail-card">
|
||
<view class="head-row">
|
||
<text class="head-amount">¥{{ moneyText(detail.total_amount) }}</text>
|
||
<u-tag
|
||
:text="getExpenseStatusLabel(detail.status)"
|
||
:type="getExpenseStatusType(detail.status)"
|
||
size="mini"
|
||
/>
|
||
</view>
|
||
<text v-if="descText" class="head-desc">{{ descText }}</text>
|
||
|
||
<view class="info-row">
|
||
<text class="info-label">申请人</text>
|
||
<text class="info-value">{{ detail.user_name || '-' }}</text>
|
||
</view>
|
||
<view class="info-row">
|
||
<text class="info-label">申请日期</text>
|
||
<text class="info-value">{{ detail.apply_date }}</text>
|
||
</view>
|
||
<view class="info-row">
|
||
<text class="info-label">费用笔数</text>
|
||
<text class="info-value">{{ items.length }} 笔</text>
|
||
</view>
|
||
|
||
<view class="meta-row">
|
||
<text>创建于 {{ formatTime(detail.create_time) }}</text>
|
||
<text v-if="detail.update_time">更新于 {{ formatTime(detail.update_time) }}</text>
|
||
</view>
|
||
</view>
|
||
|
||
<!-- 费用明细 -->
|
||
<view class="block-card">
|
||
<view class="block-head">
|
||
<text class="block-title">费用明细</text>
|
||
<text class="block-total">¥{{ moneyText(detail.total_amount) }}</text>
|
||
</view>
|
||
<view v-if="!items.length" class="items-empty">
|
||
<text>暂无费用明细</text>
|
||
</view>
|
||
<view v-for="item in items" :key="item.id" class="exp-item">
|
||
<view class="exp-item-main">
|
||
<view class="exp-item-line">
|
||
<text class="exp-item-type">{{ item.expense_type || '未分类' }}</text>
|
||
<text class="exp-item-amount">¥{{ moneyText(item.amount) }}</text>
|
||
</view>
|
||
<view class="exp-item-sub">
|
||
<text>{{ item.expense_date }}{{ item.quantity > 1 ? ` × ${item.quantity}` : '' }}</text>
|
||
<text v-if="item.description" class="exp-item-desc">{{ item.description }}</text>
|
||
</view>
|
||
<view v-if="item.receipts?.length" class="receipt-grid">
|
||
<template v-for="(img, index) in item.receipts" :key="img.id || index">
|
||
<image
|
||
v-if="!isPdfReceipt(img)"
|
||
class="receipt-item"
|
||
:src="img.url"
|
||
mode="aspectFill"
|
||
@click="previewReceipt(item.receipts, index)"
|
||
/>
|
||
<view v-else class="receipt-file" @click="openReceiptFile(img)">
|
||
<FaIcon name="file-pdf" color="#e74c3c" :size="26" />
|
||
<text class="receipt-file-name">{{ img.name || 'PDF 文件' }}</text>
|
||
<text class="receipt-file-hint">点击打开</text>
|
||
</view>
|
||
</template>
|
||
</view>
|
||
</view>
|
||
</view>
|
||
</view>
|
||
|
||
<!-- 审批记录 -->
|
||
<view class="block-card">
|
||
<view class="block-head">
|
||
<text class="block-title">审批记录</text>
|
||
</view>
|
||
<view v-if="!records.length" class="items-empty">
|
||
<text>暂无审批记录</text>
|
||
</view>
|
||
<view v-else class="timeline">
|
||
<view v-for="record in records" :key="record.id" class="timeline-item">
|
||
<view class="timeline-dot" />
|
||
<view class="timeline-content">
|
||
<text class="timeline-title">
|
||
{{ getExpenseActionLabel(record.action) }} · {{ record.approver_name || '-' }}
|
||
</text>
|
||
<text v-if="record.comment" class="timeline-comment">{{ record.comment }}</text>
|
||
<text class="timeline-time">{{ formatTime(record.create_time) }}</text>
|
||
</view>
|
||
</view>
|
||
</view>
|
||
</view>
|
||
</template>
|
||
|
||
<!-- 操作按钮(统一放详情页,仅草稿可操作) -->
|
||
<view v-if="detail && isDraft" class="footer">
|
||
<view class="footer-btn">
|
||
<u-button type="error" plain icon="trash" text="删除" @click="onDelete" />
|
||
</view>
|
||
<view class="footer-btn">
|
||
<u-button type="primary" plain icon="edit-pen" text="编辑" @click="openEdit" />
|
||
</view>
|
||
<view class="footer-btn">
|
||
<u-button type="primary" icon="arrow-up" text="提交审批" @click="onSubmit" />
|
||
</view>
|
||
</view>
|
||
</view>
|
||
</template>
|
||
|
||
<script setup>
|
||
import { ref, computed } from 'vue'
|
||
import { onLoad, onShow, onUniAppEvent } from '@dcloudio/uni-app'
|
||
import {
|
||
getExpense,
|
||
deleteExpense,
|
||
submitExpense,
|
||
fetchExpenseRecords,
|
||
getExpenseStatusLabel,
|
||
getExpenseStatusType,
|
||
getExpenseActionLabel,
|
||
isPdfReceipt,
|
||
moneyText,
|
||
EXPENSE_STATUS
|
||
} from '@/api/expense.js'
|
||
|
||
const expenseId = ref('')
|
||
const detail = ref(null)
|
||
const items = ref([])
|
||
const records = ref([])
|
||
const loading = ref(false)
|
||
|
||
const isDraft = computed(() => Number(detail.value?.status) === EXPENSE_STATUS.DRAFT)
|
||
|
||
const descText = computed(() => {
|
||
const desc = (detail.value?.description || '').trim()
|
||
return desc || ''
|
||
})
|
||
|
||
onLoad((query) => {
|
||
if (query?.id) {
|
||
expenseId.value = query.id
|
||
loadDetail()
|
||
}
|
||
})
|
||
|
||
onShow(() => {
|
||
if (expenseId.value) loadDetail()
|
||
})
|
||
|
||
// 监听审批通过后的刷新事件
|
||
onUniAppEvent('expense-updated', (data) => {
|
||
if (data?.id === expenseId.value) {
|
||
// 延迟一点刷新,确保后端数据已同步
|
||
setTimeout(() => loadDetail(), 300)
|
||
}
|
||
})
|
||
|
||
/** 后端时间字段可能为 "YYYY-MM-DD HH:mm:ss" 格式,iOS 需转 T 分隔 */
|
||
function formatTime(value) {
|
||
if (!value) return ''
|
||
return String(value).replace(' ', 'T').slice(0, 16).replace('T', ' ')
|
||
}
|
||
|
||
async function loadDetail() {
|
||
loading.value = true
|
||
try {
|
||
const data = await getExpense(expenseId.value)
|
||
if (!data) {
|
||
uni.showToast({ title: '报销单不存在', icon: 'none' })
|
||
setTimeout(() => uni.navigateBack(), 800)
|
||
return
|
||
}
|
||
detail.value = data
|
||
items.value = data.items || []
|
||
records.value = await fetchExpenseRecords(expenseId.value)
|
||
} catch (e) {
|
||
uni.showToast({ title: '加载失败', icon: 'none' })
|
||
} finally {
|
||
loading.value = false
|
||
}
|
||
}
|
||
|
||
/** 图片预览:只把图片类发票给 previewImage,PDF 不参与 */
|
||
function previewReceipt(receipts, index) {
|
||
const current = receipts?.[index]
|
||
const urls = (receipts || [])
|
||
.filter(img => !isPdfReceipt(img))
|
||
.map(img => img.url)
|
||
.filter(Boolean)
|
||
if (!urls.length) return
|
||
uni.previewImage({ urls, current: current && !isPdfReceipt(current) ? current.url : urls[0] })
|
||
}
|
||
|
||
/** PDF 发票:下载后用文档查看器打开(image 组件无法渲染 PDF,会被 ORB 拦截) */
|
||
function openReceiptFile(file) {
|
||
if (!file?.url) return
|
||
uni.showLoading({ title: '打开中...' })
|
||
uni.downloadFile({
|
||
url: file.url,
|
||
success: (res) => {
|
||
uni.openDocument({
|
||
filePath: res.tempFilePath,
|
||
fileType: 'pdf',
|
||
showMenu: true,
|
||
fail: () => uni.showToast({ title: '当前环境无法打开 PDF', icon: 'none' })
|
||
})
|
||
},
|
||
fail: () => uni.showToast({ title: '文件下载失败', icon: 'none' }),
|
||
complete: () => uni.hideLoading()
|
||
})
|
||
}
|
||
|
||
function openEdit() {
|
||
uni.navigateTo({ url: `/pages/tools/expense/edit?id=${expenseId.value}` })
|
||
}
|
||
|
||
function onSubmit() {
|
||
uni.showModal({
|
||
title: '提交审批',
|
||
content: '提交后将进入审批流程,草稿期间仍可修改,确定提交吗?',
|
||
success: async (res) => {
|
||
if (!res.confirm) return
|
||
try {
|
||
await submitExpense(expenseId.value)
|
||
uni.showToast({ title: '已提交审批', icon: 'success' })
|
||
loadDetail()
|
||
} catch (e) {
|
||
// request 封装已弹出服务端错误提示
|
||
}
|
||
}
|
||
})
|
||
}
|
||
|
||
function onDelete() {
|
||
uni.showModal({
|
||
title: '删除报销单',
|
||
content: '删除后不可恢复,确定删除吗?',
|
||
success: async (res) => {
|
||
if (!res.confirm) return
|
||
try {
|
||
await deleteExpense(expenseId.value)
|
||
uni.showToast({ title: '已删除', icon: 'success' })
|
||
setTimeout(() => uni.navigateBack(), 400)
|
||
} catch (e) {
|
||
// request 封装已弹出服务端错误提示
|
||
}
|
||
}
|
||
})
|
||
}
|
||
</script>
|
||
|
||
<style lang="scss" scoped>
|
||
@import '@/src/styles/page-common.scss';
|
||
|
||
.page {
|
||
min-height: 100vh;
|
||
background: $color-bg-page;
|
||
padding: 24rpx $page-padding-x;
|
||
padding-bottom: calc(180rpx + env(safe-area-inset-bottom));
|
||
overflow-x: hidden;
|
||
}
|
||
|
||
.detail-card,
|
||
.block-card {
|
||
@include card;
|
||
padding: 32rpx 28rpx;
|
||
margin-bottom: 24rpx;
|
||
}
|
||
|
||
.head-row {
|
||
display: flex;
|
||
align-items: center;
|
||
justify-content: space-between;
|
||
gap: 16rpx;
|
||
}
|
||
|
||
.head-amount {
|
||
font-size: 44rpx;
|
||
font-weight: 600;
|
||
color: $color-text;
|
||
}
|
||
|
||
.head-desc {
|
||
display: block;
|
||
margin-top: 14rpx;
|
||
font-size: 28rpx;
|
||
color: $color-text-secondary;
|
||
line-height: 1.7;
|
||
white-space: pre-wrap;
|
||
word-break: break-all;
|
||
}
|
||
|
||
.info-row {
|
||
display: flex;
|
||
align-items: center;
|
||
justify-content: space-between;
|
||
gap: 24rpx;
|
||
padding: 20rpx 0 0;
|
||
margin-top: 20rpx;
|
||
border-top: 1rpx solid $color-divider;
|
||
}
|
||
|
||
.info-label {
|
||
font-size: 26rpx;
|
||
color: $color-text-muted;
|
||
flex-shrink: 0;
|
||
}
|
||
|
||
.info-value {
|
||
font-size: 28rpx;
|
||
color: $color-text;
|
||
text-align: right;
|
||
}
|
||
|
||
.meta-row {
|
||
display: flex;
|
||
flex-direction: column;
|
||
gap: 8rpx;
|
||
padding-top: 24rpx;
|
||
margin-top: 20rpx;
|
||
border-top: 1rpx solid $color-divider;
|
||
font-size: 24rpx;
|
||
color: $color-text-muted;
|
||
}
|
||
|
||
.block-head {
|
||
@include block-head;
|
||
}
|
||
|
||
.block-title {
|
||
font-size: 30rpx;
|
||
font-weight: 600;
|
||
color: $color-text;
|
||
}
|
||
|
||
.block-total {
|
||
font-size: 28rpx;
|
||
font-weight: 600;
|
||
color: #f56c6c;
|
||
}
|
||
|
||
.items-empty {
|
||
padding: 32rpx 0;
|
||
font-size: 26rpx;
|
||
color: $color-text-muted;
|
||
text-align: center;
|
||
}
|
||
|
||
.exp-item {
|
||
padding: 20rpx 0;
|
||
border-top: 1rpx solid $color-divider;
|
||
|
||
&:first-of-type {
|
||
border-top: none;
|
||
padding-top: 4rpx;
|
||
}
|
||
}
|
||
|
||
.exp-item-line {
|
||
display: flex;
|
||
align-items: center;
|
||
justify-content: space-between;
|
||
gap: 16rpx;
|
||
}
|
||
|
||
.exp-item-type {
|
||
font-size: 28rpx;
|
||
color: $color-text;
|
||
}
|
||
|
||
.exp-item-amount {
|
||
font-size: 28rpx;
|
||
font-weight: 600;
|
||
color: $color-text;
|
||
flex-shrink: 0;
|
||
}
|
||
|
||
.exp-item-sub {
|
||
display: flex;
|
||
align-items: center;
|
||
gap: 16rpx;
|
||
margin-top: 8rpx;
|
||
font-size: 24rpx;
|
||
color: $color-text-muted;
|
||
|
||
.exp-item-desc {
|
||
flex: 1;
|
||
min-width: 0;
|
||
overflow: hidden;
|
||
text-overflow: ellipsis;
|
||
white-space: nowrap;
|
||
}
|
||
}
|
||
|
||
.receipt-grid {
|
||
display: flex;
|
||
flex-wrap: wrap;
|
||
gap: 12rpx;
|
||
margin-top: 14rpx;
|
||
}
|
||
|
||
.receipt-item {
|
||
width: 120rpx;
|
||
height: 120rpx;
|
||
border-radius: $radius-md;
|
||
background: $color-bg-page;
|
||
}
|
||
|
||
.receipt-file {
|
||
width: 200rpx;
|
||
height: 120rpx;
|
||
border-radius: $radius-md;
|
||
background: $color-bg-page;
|
||
display: flex;
|
||
flex-direction: column;
|
||
align-items: center;
|
||
justify-content: center;
|
||
gap: 4rpx;
|
||
padding: 0 12rpx;
|
||
box-sizing: border-box;
|
||
|
||
&:active {
|
||
opacity: 0.7;
|
||
}
|
||
}
|
||
|
||
.receipt-file-name {
|
||
max-width: 100%;
|
||
font-size: 22rpx;
|
||
color: $color-text;
|
||
overflow: hidden;
|
||
text-overflow: ellipsis;
|
||
white-space: nowrap;
|
||
}
|
||
|
||
.receipt-file-hint {
|
||
font-size: 20rpx;
|
||
color: $color-text-muted;
|
||
}
|
||
|
||
.timeline {
|
||
padding-top: 4rpx;
|
||
}
|
||
|
||
.timeline-item {
|
||
display: flex;
|
||
gap: 20rpx;
|
||
padding-bottom: 28rpx;
|
||
position: relative;
|
||
|
||
&:last-child {
|
||
padding-bottom: 4rpx;
|
||
|
||
&::after {
|
||
display: none;
|
||
}
|
||
}
|
||
|
||
&::after {
|
||
content: '';
|
||
position: absolute;
|
||
left: 9rpx;
|
||
top: 24rpx;
|
||
bottom: 0;
|
||
width: 2rpx;
|
||
background: $color-divider;
|
||
}
|
||
}
|
||
|
||
.timeline-dot {
|
||
width: 20rpx;
|
||
height: 20rpx;
|
||
border-radius: 50%;
|
||
background: #2563eb;
|
||
flex-shrink: 0;
|
||
margin-top: 4rpx;
|
||
}
|
||
|
||
.timeline-content {
|
||
flex: 1;
|
||
min-width: 0;
|
||
}
|
||
|
||
.timeline-title {
|
||
display: block;
|
||
font-size: 26rpx;
|
||
color: $color-text;
|
||
font-weight: 500;
|
||
}
|
||
|
||
.timeline-comment {
|
||
display: block;
|
||
margin-top: 6rpx;
|
||
font-size: 24rpx;
|
||
color: $color-text-secondary;
|
||
line-height: 1.6;
|
||
word-break: break-all;
|
||
}
|
||
|
||
.timeline-time {
|
||
display: block;
|
||
margin-top: 6rpx;
|
||
font-size: 22rpx;
|
||
color: $color-text-muted;
|
||
}
|
||
|
||
.footer {
|
||
position: fixed;
|
||
left: 0;
|
||
right: 0;
|
||
bottom: 0;
|
||
display: flex;
|
||
gap: 16rpx;
|
||
padding: 20rpx $page-padding-x;
|
||
padding-bottom: calc(20rpx + env(safe-area-inset-bottom));
|
||
background: $color-card;
|
||
border-top: 1rpx solid $color-border;
|
||
box-shadow: 0 -4rpx 20rpx rgba(0, 0, 0, 0.04);
|
||
}
|
||
|
||
.footer-btn {
|
||
flex: 1;
|
||
}
|
||
|
||
.state-box {
|
||
display: flex;
|
||
justify-content: center;
|
||
padding: 160rpx 0;
|
||
}
|
||
|
||
.state-text {
|
||
font-size: 28rpx;
|
||
color: $color-text-muted;
|
||
}
|
||
</style>
|