优化小程序报销模块

This commit is contained in:
hero920103 committed 2026-10-10 14:24:44 +08:00
1 parent 1e9666cefd
commit 48f3bd997c
21 files changed
+5738 -1750

No files matched your search

+532
View File
@@ -0,0 +1,532 @@
<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 } 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()
})
/** 后端时间字段可能为 "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>
+746
View File
@@ -0,0 +1,746 @@
<template>
<view class="page">
<!-- 基本信息 -->
<view class="form-card">
<view class="field">
<text class="field-label">申请日期 <text class="required">*</text></text>
<picker mode="date" :value="form.applyDate" @change="onDateChange">
<view class="picker-value">
<text>{{ form.applyDate }}</text>
<FaIcon name="calendar-days" color="#909399" :size="16" />
</view>
</picker>
</view>
<view class="field">
<text class="field-label">报销说明</text>
<u-textarea
v-model="form.description"
placeholder="填写报销事由,便于审批人了解(选填)"
:maxlength="500"
count
height="100"
border="none"
/>
</view>
</view>
<!-- 费用明细 -->
<view class="items-card">
<view class="items-head">
<text class="items-title">费用明细</text>
<text class="items-total">合计 ¥{{ totalText }}</text>
</view>
<view v-if="!items.length" class="items-empty">
<text>尚未添加费用明细</text>
</view>
<view
v-for="(item, index) in items"
:key="index"
class="item-row"
@tap="openItemEditor(index)"
>
<view class="item-main">
<view class="item-line">
<text class="item-type">{{ item.expenseType || '未分类' }}</text>
<text class="item-amount">¥{{ moneyText(item.amount) }}</text>
</view>
<view class="item-sub">
<text>{{ item.expenseDate }}</text>
<text v-if="item.receipts?.length" class="item-receipt">发票 ×{{ item.receipts.length }}</text>
</view>
</view>
<view class="item-del" @tap.stop="onRemoveItem(index)">
<FaIcon name="trash-can" color="#f56c6c" :size="18" />
</view>
</view>
<view class="item-add" @tap="openItemEditor(-1)">
<FaIcon name="plus" color="#2563eb" :size="16" />
<text>添加费用明细</text>
</view>
</view>
<view class="footer">
<u-button
type="primary"
color="#2563eb"
shape="circle"
:loading="saving"
loading-text="保存中..."
text="保存"
@click="onSave"
/>
</view>
<!-- 明细编辑弹层 -->
<view v-if="itemEditorVisible" class="mask" @tap="closeItemEditor">
<view class="sheet" @tap.stop>
<view class="sheet-head">
<text class="sheet-cancel" @tap="closeItemEditor">取消</text>
<text class="sheet-title">{{ editingIndex >= 0 ? '编辑费用' : '添加费用' }}</text>
<text class="sheet-save" @tap="confirmItem">确定</text>
</view>
<scroll-view class="sheet-body" scroll-y>
<view class="field">
<text class="field-label">费用类型 <text class="required">*</text></text>
<picker :range="typeNames" :value="typeIndex" @change="onTypeChange">
<view class="picker-value">
<text>{{ typeNames[typeIndex] || '选择费用类型' }}</text>
<FaIcon name="layer-group" color="#909399" :size="16" />
</view>
</picker>
</view>
<view class="field">
<text class="field-label">费用日期 <text class="required">*</text></text>
<picker mode="date" :value="itemForm.expenseDate" @change="onItemDateChange">
<view class="picker-value">
<text>{{ itemForm.expenseDate }}</text>
<FaIcon name="calendar-days" color="#909399" :size="16" />
</view>
</picker>
</view>
<view class="field">
<text class="field-label">金额 <text class="required">*</text></text>
<u-input
v-model="itemForm.amount"
type="digit"
placeholder="0.00"
border="none"
/>
</view>
<view class="field">
<text class="field-label">数量</text>
<u-input
v-model="itemForm.quantity"
type="number"
placeholder="1"
border="none"
/>
</view>
<view class="field">
<text class="field-label">说明</text>
<u-input
v-model="itemForm.description"
placeholder="费用用途说明(选填)"
:maxlength="200"
border="none"
/>
</view>
<view class="field">
<view class="receipt-head">
<text class="field-label no-margin">发票附件</text>
<text v-if="itemForm.receipts.length" class="receipt-hint">长按可删除</text>
</view>
<view class="receipt-grid">
<template v-for="(img, index) in itemForm.receipts" :key="img.id || index">
<image
v-if="!isPdfReceipt(img)"
class="receipt-item"
:src="img.url"
mode="aspectFill"
@click="previewReceipt(index)"
@longpress="onRemoveReceipt(index)"
/>
<view v-else class="receipt-file" @longpress="onRemoveReceipt(index)">
<FaIcon name="file-pdf" color="#e74c3c" :size="26" />
<text class="receipt-file-name">{{ img.name || 'PDF 文件' }}</text>
</view>
</template>
<view class="receipt-add" @tap="onUploadReceipt">
<FaIcon name="camera" color="#94a3b8" :size="20" />
<text>{{ uploading ? '上传中...' : '上传' }}</text>
</view>
</view>
</view>
</scroll-view>
</view>
</view>
</view>
</template>
<script setup>
import { ref, reactive, computed } from 'vue'
import { onLoad } from '@dcloudio/uni-app'
import {
getExpense,
createExpense,
updateExpense,
addExpenseItem,
deleteExpenseItem,
fetchExpenseTypes,
uploadExpenseReceipt,
resolveImageUrl,
isPdfReceipt,
moneyText,
EXPENSE_STATUS
} from '@/api/expense.js'
import { formatDate } from '@/utils/date.js'
const expenseId = ref('')
const isDraft = ref(true)
const saving = ref(false)
const uploading = ref(false)
const form = reactive({
applyDate: '',
description: ''
})
/** 本地明细列表:编辑已有明细时带 id,保存时全量重建 */
const items = ref([])
const typeOptions = ref([])
const typeNames = computed(() => typeOptions.value.map(t => t.name))
const itemEditorVisible = ref(false)
const editingIndex = ref(-1)
const typeIndex = ref(0)
const itemForm = reactive({
expenseType: '',
expenseDate: '',
amount: '',
quantity: '1',
description: '',
receipts: []
})
const totalText = computed(() =>
moneyText(items.value.reduce((sum, item) => sum + (Number(item.amount) || 0), 0))
)
onLoad(async (query) => {
form.applyDate = formatDate(new Date())
loadTypes()
if (query?.id) {
expenseId.value = query.id
uni.setNavigationBarTitle({ title: '编辑报销单' })
await loadDetail(query.id)
} else {
uni.setNavigationBarTitle({ title: '新建报销单' })
}
})
async function loadTypes() {
try {
typeOptions.value = await fetchExpenseTypes()
if (!typeOptions.value.length) typeOptions.value = [{ id: 0, name: '其他' }]
} catch (e) {
typeOptions.value = [{ id: 0, name: '其他' }]
}
}
async function loadDetail(id) {
try {
const data = await getExpense(id)
if (!data) {
uni.showToast({ title: '报销单不存在', icon: 'none' })
setTimeout(() => uni.navigateBack(), 800)
return
}
isDraft.value = Number(data.status) === EXPENSE_STATUS.DRAFT
if (!isDraft.value) {
uni.showToast({ title: '只有草稿可以编辑', icon: 'none' })
setTimeout(() => uni.navigateBack(), 800)
return
}
form.applyDate = data.apply_date || formatDate(new Date())
form.description = data.description || ''
items.value = (data.items || []).map(item => ({
id: item.id,
expenseType: item.expense_type,
expenseDate: item.expense_date,
amount: String(item.amount ?? ''),
quantity: String(item.quantity || 1),
description: item.description || '',
receipts: item.receipts || []
}))
} catch (e) {
uni.showToast({ title: '加载失败', icon: 'none' })
}
}
function onDateChange(e) {
form.applyDate = e.detail.value
}
function openItemEditor(index) {
editingIndex.value = index
if (index >= 0) {
const item = items.value[index]
Object.assign(itemForm, {
expenseType: item.expenseType,
expenseDate: item.expenseDate,
amount: item.amount,
quantity: item.quantity,
unitPrice: item.unitPrice,
description: item.description,
receipts: [...(item.receipts || [])]
})
typeIndex.value = Math.max(
typeOptions.value.findIndex(t => t.name === item.expenseType),
0
)
} else {
Object.assign(itemForm, {
expenseType: typeOptions.value[0]?.name || '',
expenseDate: form.applyDate,
amount: '',
quantity: '1',
description: '',
receipts: []
})
typeIndex.value = 0
}
itemEditorVisible.value = true
}
function closeItemEditor() {
itemEditorVisible.value = false
}
function onTypeChange(e) {
typeIndex.value = Number(e.detail.value)
itemForm.expenseType = typeOptions.value[typeIndex.value]?.name || ''
}
function onItemDateChange(e) {
itemForm.expenseDate = e.detail.value
}
function confirmItem() {
const amount = Number(itemForm.amount)
if (!itemForm.expenseType) {
uni.showToast({ title: '请选择费用类型', icon: 'none' })
return
}
if (!itemForm.expenseDate) {
uni.showToast({ title: '请选择费用日期', icon: 'none' })
return
}
if (!amount || amount <= 0) {
uni.showToast({ title: '费用金额必须大于0', icon: 'none' })
return
}
if (editingIndex.value >= 0) {
const old = items.value[editingIndex.value]
Object.assign(old, {
expenseType: itemForm.expenseType,
expenseDate: itemForm.expenseDate,
amount: amount,
quantity: Math.max(Number(itemForm.quantity) || 1, 1),
description: itemForm.description.trim(),
receipts: [...itemForm.receipts]
})
} else {
items.value.push({
expenseType: itemForm.expenseType,
expenseDate: itemForm.expenseDate,
amount: amount,
quantity: Math.max(Number(itemForm.quantity) || 1, 1),
description: itemForm.description.trim(),
receipts: [...itemForm.receipts]
})
}
itemEditorVisible.value = false
}
function onRemoveItem(index) {
items.value.splice(index, 1)
}
function previewReceipt(index) {
const current = itemForm.receipts[index]
const urls = itemForm.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] })
}
function onRemoveReceipt(index) {
itemForm.receipts.splice(index, 1)
}
/** 上传发票:选择图片(拍照/相册)或从聊天文件选 PDF */
function onUploadReceipt() {
if (uploading.value) return
uni.showActionSheet({
itemList: ['拍摄或从相册选择', '从聊天文件选择'],
success: (res) => {
if (res.tapIndex === 0) {
chooseImageReceipt()
} else if (res.tapIndex === 1) {
chooseFileReceipt()
}
}
})
}
function chooseImageReceipt() {
uni.chooseImage({
count: 9,
success: async (res) => {
await uploadReceipts(res.tempFilePaths || [])
}
})
}
/** 小程序沙箱内选择本地 PDF 只能走微信会话文件(chooseMessageFile) */
function chooseFileReceipt() {
if (typeof uni.chooseMessageFile !== 'function') {
uni.showToast({ title: '当前环境不支持选择文件', icon: 'none' })
return
}
uni.chooseMessageFile({
count: 9,
type: 'file',
extension: ['pdf', 'jpg', 'jpeg', 'png', 'gif', 'webp', 'bmp'],
success: async (res) => {
const paths = (res.tempFiles || []).map(f => f.path).filter(Boolean)
if (!paths.length) return
await uploadReceipts(paths)
},
fail: () => {
// 用户取消选择,静默处理
}
})
}
async function uploadReceipts(paths) {
if (!paths.length || uploading.value) return
uploading.value = true
try {
for (const p of paths) {
const uploaded = await uploadExpenseReceipt(p)
itemForm.receipts.push({ ...uploaded, url: resolveImageUrl(uploaded.url) })
}
uni.showToast({ title: '上传成功', icon: 'success' })
} catch (e) {
// 上传封装已提示错误
} finally {
uploading.value = false
}
}
async function onSave() {
if (saving.value) return
if (!form.applyDate) {
uni.showToast({ title: '请选择申请日期', icon: 'none' })
return
}
saving.value = true
try {
let id = expenseId.value
const payload = { applyDate: form.applyDate, description: form.description }
if (id) {
await updateExpense(id, payload)
// 明细全量重建:先删旧再逐条新增(草稿单,明细量少)
const oldItems = await getExpense(id)
for (const item of oldItems?.items || []) {
await deleteExpenseItem(id, item.id)
}
} else {
id = await createExpense(payload)
}
for (const item of items.value) {
await addExpenseItem(id, item)
}
uni.showToast({ title: '保存成功', icon: 'success' })
setTimeout(() => uni.navigateBack(), 400)
} catch (e) {
// request 封装已弹出服务端错误提示
} finally {
saving.value = false
}
}
</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));
}
.form-card,
.items-card {
@include card;
padding: 8rpx 0;
margin-bottom: 24rpx;
}
.field {
padding: 24rpx 28rpx;
& + & {
border-top: 1rpx solid $color-divider;
}
}
.field-label {
display: block;
font-size: 24rpx;
color: $color-text-muted;
margin-bottom: 12rpx;
&.no-margin {
margin-bottom: 0;
}
}
.required {
color: #f56c6c;
}
.picker-value {
display: flex;
align-items: center;
justify-content: space-between;
height: 72rpx;
padding: 0 20rpx;
background: $color-bg-page;
border-radius: $radius-md;
font-size: 28rpx;
color: $color-text;
}
.items-head {
display: flex;
align-items: center;
justify-content: space-between;
padding: 28rpx 28rpx 8rpx;
}
.items-title {
font-size: 30rpx;
font-weight: 600;
color: $color-text;
}
.items-total {
font-size: 26rpx;
color: #f56c6c;
font-weight: 600;
}
.items-empty {
padding: 32rpx 28rpx;
font-size: 26rpx;
color: $color-text-muted;
text-align: center;
}
.item-row {
display: flex;
align-items: center;
padding: 24rpx 28rpx;
border-top: 1rpx solid $color-divider;
&:active {
background: $color-bg-page;
}
}
.item-main {
flex: 1;
min-width: 0;
}
.item-line {
display: flex;
align-items: center;
justify-content: space-between;
gap: 16rpx;
}
.item-type {
font-size: 28rpx;
color: $color-text;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.item-amount {
font-size: 28rpx;
font-weight: 600;
color: $color-text;
flex-shrink: 0;
}
.item-sub {
display: flex;
align-items: center;
gap: 20rpx;
margin-top: 8rpx;
font-size: 24rpx;
color: $color-text-muted;
}
.item-receipt {
color: #2563eb;
}
.item-del {
padding: 12rpx 0 12rpx 24rpx;
flex-shrink: 0;
}
.item-add {
display: flex;
align-items: center;
justify-content: center;
gap: 8rpx;
margin: 16rpx 28rpx 28rpx;
height: 80rpx;
border: 1rpx dashed $color-border;
border-radius: $radius-md;
font-size: 26rpx;
color: #2563eb;
&:active {
opacity: 0.7;
}
}
.footer {
position: fixed;
left: 0;
right: 0;
bottom: 0;
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);
}
.mask {
position: fixed;
left: 0;
top: 0;
right: 0;
bottom: 0;
background: rgba(15, 23, 42, 0.45);
z-index: 100;
display: flex;
align-items: flex-end;
@include mask-responsive;
}
.sheet {
width: 100%;
background: $color-card;
border-radius: 28rpx 28rpx 0 0;
display: flex;
flex-direction: column;
max-height: 80vh;
padding-bottom: env(safe-area-inset-bottom);
@include sheet-responsive;
}
.sheet-head {
display: flex;
align-items: center;
justify-content: space-between;
padding: 28rpx $page-padding-x;
border-bottom: 1rpx solid $color-divider;
}
.sheet-title {
font-size: 30rpx;
font-weight: 600;
color: $color-text;
}
.sheet-cancel {
font-size: 28rpx;
color: $color-text-muted;
}
.sheet-save {
font-size: 28rpx;
font-weight: 600;
color: $color-primary;
}
.sheet-body {
flex: 1;
min-height: 0;
max-height: 60vh;
}
.receipt-head {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 12rpx;
}
.receipt-hint {
font-size: 22rpx;
color: $color-text-muted;
}
.receipt-grid {
display: flex;
flex-wrap: wrap;
gap: 12rpx;
}
.receipt-item {
width: 140rpx;
height: 140rpx;
border-radius: $radius-md;
background: $color-bg-page;
}
.receipt-file {
width: 200rpx;
height: 140rpx;
border-radius: $radius-md;
background: $color-bg-page;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
gap: 6rpx;
padding: 0 12rpx;
box-sizing: border-box;
}
.receipt-file-name {
max-width: 100%;
font-size: 22rpx;
color: $color-text;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.receipt-add {
width: 140rpx;
height: 140rpx;
border: 1rpx dashed $color-border;
border-radius: $radius-md;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
gap: 6rpx;
font-size: 22rpx;
color: $color-text-muted;
&:active {
opacity: 0.7;
}
}
</style>
+313
View File
@@ -0,0 +1,313 @@
<template>
<view class="page">
<!-- 搜索 + 状态筛选 -->
<view class="filter-bar">
<view class="search-box">
<FaIcon name="magnifying-glass" color="#94a3b8" :size="16" />
<input
class="search-input"
v-model="keyword"
placeholder="搜索报销说明"
confirm-type="search"
@confirm="onSearch"
/>
<text v-if="keyword" class="search-clear" @tap="clearKeyword">清空</text>
</view>
<scroll-view class="tab-scroll" scroll-x :show-scrollbar="false">
<view class="tab-list">
<view
v-for="tab in tabs"
:key="String(tab.value)"
class="tab-item"
:class="{ active: currentStatus === tab.value }"
@tap="onTabChange(tab.value)"
>
<text>{{ tab.label }}</text>
</view>
</view>
</scroll-view>
</view>
<!-- 报销单列表 -->
<view v-if="loading && !list.length" class="state-box">
<text class="state-text">加载中...</text>
</view>
<view v-else-if="!list.length" class="state-box">
<u-empty mode="list" text="暂无报销单" />
</view>
<template v-else>
<view
v-for="row in list"
:key="row.id"
class="expense-card"
@tap="openDetail(row)"
>
<view class="card-top">
<text class="card-amount">¥{{ moneyText(row.total_amount) }}</text>
<u-tag
:text="getExpenseStatusLabel(row.status)"
:type="getExpenseStatusType(row.status)"
size="mini"
/>
</view>
<text class="card-desc">{{ descText(row) }}</text>
<view class="card-meta">
<text>申请日期 {{ row.apply_date }}</text>
<FaIcon name="chevron-right" color="#cbd5e1" :size="14" />
</view>
</view>
<view v-if="finished" class="load-done">
<text>{{ list.length ? '没有更多了' : '' }}</text>
</view>
</template>
<!-- 新建入口 -->
<view class="fab" @tap="openCreate">
<FaIcon name="plus" color="#ffffff" :size="22" />
<text>新建报销</text>
</view>
</view>
</template>
<script setup>
import { ref } from 'vue'
import { onLoad, onShow, onReachBottom } from '@dcloudio/uni-app'
import {
fetchExpenses,
getExpenseStatusLabel,
getExpenseStatusType,
moneyText,
EXPENSE_STATUS_OPTIONS
} from '@/api/expense.js'
const PAGE_SIZE = 20
const tabs = [{ value: '', label: '全部' }, ...EXPENSE_STATUS_OPTIONS]
const list = ref([])
const total = ref(0)
const page = ref(1)
const loading = ref(false)
const finished = ref(false)
const keyword = ref('')
const currentStatus = ref('')
onShow(() => {
refresh()
})
onReachBottom(() => {
if (finished.value || loading.value) return
loadMore()
})
async function refresh() {
page.value = 1
finished.value = false
list.value = []
await loadPage()
}
async function loadMore() {
page.value += 1
await loadPage()
}
async function loadPage() {
loading.value = true
try {
const data = await fetchExpenses({
page: page.value,
pageSize: PAGE_SIZE,
keyword: keyword.value.trim(),
status: currentStatus.value
})
const rows = data.list || []
list.value = page.value === 1 ? rows : [...list.value, ...rows]
total.value = data.total || 0
finished.value = list.value.length >= total.value
} catch (e) {
if (page.value > 1) page.value -= 1
uni.showToast({ title: '加载失败', icon: 'none' })
} finally {
loading.value = false
}
}
function onSearch() {
refresh()
}
function clearKeyword() {
keyword.value = ''
refresh()
}
function onTabChange(value) {
if (currentStatus.value === value) return
currentStatus.value = value
refresh()
}
function descText(row) {
const desc = (row.description || '').trim()
return desc || '无报销说明'
}
function openDetail(row) {
uni.navigateTo({ url: `/pages/tools/expense/detail?id=${row.id}` })
}
function openCreate() {
uni.navigateTo({ url: '/pages/tools/expense/edit' })
}
</script>
<style lang="scss" scoped>
@import '@/src/styles/page-common.scss';
.page {
min-height: 100vh;
background: $color-bg-page;
padding-bottom: calc(160rpx + env(safe-area-inset-bottom));
}
.filter-bar {
position: sticky;
top: 0;
z-index: 10;
background: $color-bg-page;
padding: 20rpx $page-padding-x 8rpx;
}
.search-box {
display: flex;
align-items: center;
gap: 12rpx;
height: 72rpx;
padding: 0 24rpx;
background: $color-card;
border-radius: $radius-full;
box-shadow: $shadow-card;
.search-input {
flex: 1;
font-size: 26rpx;
color: $color-text;
}
.search-clear {
font-size: 24rpx;
color: $color-text-muted;
}
}
.tab-scroll {
margin-top: 20rpx;
white-space: nowrap;
}
.tab-list {
display: inline-flex;
gap: 16rpx;
padding: 4rpx 0 12rpx;
}
.tab-item {
padding: 10rpx 28rpx;
border-radius: $radius-full;
background: $color-card;
font-size: 26rpx;
color: $color-text-secondary;
box-shadow: $shadow-card;
&.active {
background: #2563eb;
color: #ffffff;
font-weight: 500;
}
}
.expense-card {
@include card;
margin: 0 $page-padding-x 24rpx;
padding: 28rpx;
&:active {
opacity: 0.85;
}
}
.card-top {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 14rpx;
}
.card-amount {
font-size: 36rpx;
font-weight: 600;
color: $color-text;
}
.card-desc {
display: block;
font-size: 26rpx;
color: $color-text-secondary;
line-height: 1.6;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.card-meta {
display: flex;
align-items: center;
justify-content: space-between;
margin-top: 16rpx;
font-size: 24rpx;
color: $color-text-muted;
}
.state-box {
display: flex;
justify-content: center;
padding: 140rpx 0;
}
.state-text {
font-size: 28rpx;
color: $color-text-muted;
}
.load-done {
display: flex;
justify-content: center;
padding: 8rpx 0 24rpx;
font-size: 24rpx;
color: $color-text-muted;
}
.fab {
position: fixed;
left: 50%;
transform: translateX(-50%);
bottom: calc(48rpx + env(safe-area-inset-bottom));
display: flex;
align-items: center;
gap: 10rpx;
height: 88rpx;
padding: 0 48rpx;
border-radius: $radius-full;
background: #2563eb;
color: #ffffff;
font-size: 28rpx;
font-weight: 500;
box-shadow: 0 8rpx 24rpx rgba(37, 99, 235, 0.4);
&:active {
opacity: 0.85;
}
}
</style>