## permission_service.py - init_all_menus: 新增3个采购权限元素 inbound_purchase:unit_price, :total_price, :tax_rate ## purchase.py - _filter_purchase_prices(): 无价格权限则pop价格字段 - GET /purchase (列表): 应用价格过滤 - GET /purchase/<id> (详情): 应用价格过滤 - approved-unstocked: 保留价格(入库数据源,前端按inbound_buy权限控制) ## purchase/index.vue - 列表: 单价/总价/税率列 v-if hasPermission - 详情: 单价/总价 v-if hasPermission
721 lines
27 KiB
Vue
721 lines
27 KiB
Vue
<template>
|
||
<div class="app-container">
|
||
|
||
<!-- 顶部工具栏 -->
|
||
<div class="filter-container">
|
||
<span style="font-weight: bold; font-size: 15px; margin-right: 8px;">审批状态:</span>
|
||
<el-radio-group v-model="filterStatus" size="default" @change="handleStatusChange">
|
||
<el-radio-button label="">全部</el-radio-button>
|
||
<el-radio-button :label="0">待审批</el-radio-button>
|
||
<el-radio-button :label="1">已通过</el-radio-button>
|
||
<el-radio-button :label="2">已驳回</el-radio-button>
|
||
<el-radio-button :label="3">已完成</el-radio-button>
|
||
</el-radio-group>
|
||
<el-button type="primary" :icon="Refresh" @click="fetchData">刷新</el-button>
|
||
<el-button type="success" :icon="Plus" @click="openCreateDialog">
|
||
新建采购申请
|
||
</el-button>
|
||
</div>
|
||
|
||
<!-- 数据表格 -->
|
||
<el-table v-loading="loading" :data="list" border stripe style="margin-top: 16px;" row-key="id">
|
||
<el-table-column prop="request_no" label="申请单号" width="180" />
|
||
<el-table-column prop="name" label="采购物品" min-width="150" show-overflow-tooltip />
|
||
<el-table-column prop="spec_model" label="规格型号" min-width="120" show-overflow-tooltip />
|
||
<el-table-column prop="quantity" label="数量" width="80" align="center" />
|
||
<el-table-column prop="purchase_date" label="采购日期" width="110" />
|
||
<el-table-column v-if="userStore.hasPermission('inbound_purchase:unit_price')" label="含税单价" width="110" align="right">
|
||
<template #default="{ row }">
|
||
{{ row.unit_price ? '¥' + Number(row.unit_price).toFixed(2) : '-' }}
|
||
</template>
|
||
</el-table-column>
|
||
<el-table-column v-if="userStore.hasPermission('inbound_purchase:total_price')" label="含税总价" width="120" align="right">
|
||
<template #default="{ row }">
|
||
{{ row.total_price ? '¥' + Number(row.total_price).toFixed(2) : '-' }}
|
||
</template>
|
||
</el-table-column>
|
||
<el-table-column v-if="userStore.hasPermission('inbound_purchase:tax_rate')" prop="tax_rate" label="税率" width="70" align="center">
|
||
<template #default="{ row }">
|
||
{{ row.tax_rate != null ? row.tax_rate + '%' : '-' }}
|
||
</template>
|
||
</el-table-column>
|
||
<el-table-column prop="requester_name" label="申请人" width="100" />
|
||
<el-table-column prop="approver_name" label="审批人" width="100" />
|
||
<el-table-column label="状态" width="100" align="center">
|
||
<template #default="{ row }">
|
||
<el-tag :type="statusTagType(row.status)" size="small">{{ row.status_text }}</el-tag>
|
||
</template>
|
||
</el-table-column>
|
||
<el-table-column label="操作" width="200" fixed="right" align="center">
|
||
<template #default="{ row }">
|
||
<el-button type="primary" link size="small" @click="openDetailDialog(row)">详情</el-button>
|
||
<template v-if="row.status === 0 && canApprove">
|
||
<el-button type="success" link size="small" @click="handleApprove(row)">通过</el-button>
|
||
<el-button type="danger" link size="small" @click="openRejectDialog(row)">驳回</el-button>
|
||
</template>
|
||
</template>
|
||
</el-table-column>
|
||
</el-table>
|
||
|
||
<!-- 分页 -->
|
||
<el-pagination
|
||
background
|
||
style="margin-top: 16px; justify-content: flex-end; display: flex;"
|
||
v-model:current-page="page"
|
||
v-model:page-size="pageSize"
|
||
:total="total"
|
||
:page-sizes="[10, 20, 50, 100]"
|
||
layout="total, prev, pager, next, sizes"
|
||
@size-change="handleSizeChange"
|
||
@current-change="handlePageChange"
|
||
/>
|
||
|
||
<!-- ========== 新建/编辑弹窗 ========== -->
|
||
<el-dialog v-model="formDialogVisible" :title="dialogTitle" width="750px" destroy-on-close :close-on-click-modal="false" :close-on-press-escape="false">
|
||
<el-form ref="formRef" :model="form" label-width="100px">
|
||
|
||
<!-- ── 物料信息 ── -->
|
||
<div class="form-section">
|
||
<div class="section-title"><span>物料信息</span></div>
|
||
<el-row :gutter="16">
|
||
<el-col :span="24">
|
||
<el-form-item label="采购物品" required>
|
||
<el-select
|
||
v-model="materialBaseId" filterable remote reserve-keyword="true" clearable
|
||
placeholder="输入名称或规格搜索..." :remote-method="handleSearchMaterialDebounced"
|
||
:loading="searchLoading" style="width: 100%" @change="onMaterialSelected"
|
||
default-first-option="true" popper-class="long-dropdown"
|
||
v-loadmore="handleLoadMoreMaterials" @visible-change="onMaterialDropdownVisibleChange"
|
||
>
|
||
<el-option v-for="item in materialOptions" :key="item.id" :label="item.name" :value="item.id">
|
||
<div style="display: flex; justify-content: space-between; align-items: center;">
|
||
<span>{{ item.name }}</span>
|
||
<span style="color: #999; font-size: 12px;">{{ item.spec_model || '-' }}</span>
|
||
</div>
|
||
</el-option>
|
||
<li v-if="loadingMore" class="el-select-dropdown__item loading-more" style="text-align:center;color:#999;cursor:default;"><span>加载中...</span></li>
|
||
</el-select>
|
||
<div v-if="autoFillHint" style="font-size: 12px; color: #67C23A; margin-top: 4px;">{{ autoFillHint }}</div>
|
||
</el-form-item>
|
||
</el-col>
|
||
<el-col :span="8">
|
||
<el-form-item label="规格型号">
|
||
<el-input v-model="form.spec_model" placeholder="自动填充" clearable />
|
||
</el-form-item>
|
||
</el-col>
|
||
<el-col :span="8">
|
||
<el-form-item label="采购数量" required>
|
||
<el-input-number v-model="form.quantity" :min="1" :controls="false" style="width: 100%;" />
|
||
</el-form-item>
|
||
</el-col>
|
||
<el-col :span="8">
|
||
<el-form-item label="采购日期" required>
|
||
<el-date-picker v-model="form.purchase_date" type="date" value-format="YYYY-MM-DD" placeholder="选择日期" style="width: 100%;" />
|
||
</el-form-item>
|
||
</el-col>
|
||
</el-row>
|
||
</div>
|
||
|
||
<!-- ── 价格与审批 ── -->
|
||
<div class="form-section">
|
||
<div class="section-title"><span>价格与审批</span></div>
|
||
<el-row :gutter="16">
|
||
<el-col :span="8">
|
||
<el-form-item label="含税单价">
|
||
<el-input-number v-model="form.unit_price" :min="0" :precision="2" :controls="false" style="width: 100%;" placeholder="¥" @change="onUnitPriceChange" />
|
||
</el-form-item>
|
||
</el-col>
|
||
<el-col :span="8">
|
||
<el-form-item label="含税总价">
|
||
<el-input-number v-model="form.total_price" :min="0" :precision="2" :controls="false" style="width: 100%;" placeholder="¥" @change="onTotalPriceChange" />
|
||
</el-form-item>
|
||
</el-col>
|
||
<el-col :span="8">
|
||
<el-form-item label="税率">
|
||
<el-select v-model="form.tax_rate" style="width: 100%;" @change="onTaxRateChange">
|
||
<el-option label="0%" :value="0" />
|
||
<el-option label="1%" :value="1" />
|
||
<el-option label="6%" :value="6" />
|
||
<el-option label="13%" :value="13" />
|
||
</el-select>
|
||
</el-form-item>
|
||
</el-col>
|
||
<el-col :span="12">
|
||
<el-form-item label="审批人" required>
|
||
<el-select v-model="form.approver_id" placeholder="请选择审批人" style="width: 100%;" filterable>
|
||
<el-option v-for="u in approvers" :key="u.id" :label="u.username" :value="u.id" />
|
||
</el-select>
|
||
</el-form-item>
|
||
</el-col>
|
||
<el-col :span="12">
|
||
<el-form-item label="商家链接">
|
||
<el-input v-model="form.supplier_link" placeholder="https://" clearable />
|
||
</el-form-item>
|
||
</el-col>
|
||
</el-row>
|
||
</div>
|
||
|
||
<!-- ── 备注与图片 ── -->
|
||
<div class="form-section">
|
||
<div class="section-title"><span>备注与凭证</span></div>
|
||
<el-row :gutter="16">
|
||
<el-col :span="24">
|
||
<el-form-item label="备注">
|
||
<el-input v-model="form.remark" type="textarea" :rows="2" placeholder="备注信息(选填)" />
|
||
</el-form-item>
|
||
</el-col>
|
||
<el-col :span="24">
|
||
<el-form-item label="图片凭证" required id="upload-purchase-images">
|
||
<el-upload
|
||
v-model:file-list="fileList"
|
||
:http-request="customUpload"
|
||
:on-remove="handleRemoveImage"
|
||
:before-upload="beforeAvatarUpload"
|
||
accept="image/jpeg,image/png,image/gif,image/webp"
|
||
multiple
|
||
list-type="picture-card"
|
||
>
|
||
<el-icon><Plus /></el-icon>
|
||
</el-upload>
|
||
</el-form-item>
|
||
</el-col>
|
||
</el-row>
|
||
</div>
|
||
|
||
</el-form>
|
||
<template #footer>
|
||
<el-button @click="formDialogVisible = false">取消</el-button>
|
||
<el-button type="primary" :loading="submitLoading" @click="submitForm">确认提交</el-button>
|
||
</template>
|
||
</el-dialog>
|
||
|
||
<!-- ========== 详情弹窗 ========== -->
|
||
<el-dialog v-model="detailDialogVisible" title="采购申请详情" width="700px" destroy-on-close :close-on-click-modal="false" :close-on-press-escape="false">
|
||
<el-descriptions :column="2" border>
|
||
<el-descriptions-item label="申请单号">{{ detail.request_no }}</el-descriptions-item>
|
||
<el-descriptions-item label="状态">
|
||
<el-tag :type="statusTagType(detail.status)" size="small">{{ detail.status_text }}</el-tag>
|
||
</el-descriptions-item>
|
||
<el-descriptions-item label="采购物品">{{ detail.name }}</el-descriptions-item>
|
||
<el-descriptions-item label="规格型号">{{ detail.spec_model || '-' }}</el-descriptions-item>
|
||
<el-descriptions-item label="采购数量">{{ detail.quantity }}</el-descriptions-item>
|
||
<el-descriptions-item label="采购日期">{{ detail.purchase_date }}</el-descriptions-item>
|
||
<el-descriptions-item v-if="userStore.hasPermission('inbound_purchase:unit_price')" label="单价">{{ detail.unit_price || '-' }}</el-descriptions-item>
|
||
<el-descriptions-item v-if="userStore.hasPermission('inbound_purchase:total_price')" label="总价">{{ detail.total_price || '-' }}</el-descriptions-item>
|
||
<el-descriptions-item label="申请人">{{ detail.requester_name }}</el-descriptions-item>
|
||
<el-descriptions-item label="审批人">{{ detail.approver_name || '-' }}</el-descriptions-item>
|
||
<el-descriptions-item label="审批时间">{{ detail.approved_at || '-' }}</el-descriptions-item>
|
||
<el-descriptions-item label="商家链接">
|
||
<a v-if="detail.supplier_link" :href="detail.supplier_link" target="_blank" style="color: #409EFF; word-break: break-all;">
|
||
{{ detail.supplier_link }}
|
||
</a>
|
||
<span v-else>-</span>
|
||
</el-descriptions-item>
|
||
<el-descriptions-item label="备注" :span="2">{{ detail.remark || '-' }}</el-descriptions-item>
|
||
<el-descriptions-item label="驳回原因" :span="2" v-if="detail.reject_reason">
|
||
<span style="color: #F56C6C;">{{ detail.reject_reason }}</span>
|
||
</el-descriptions-item>
|
||
</el-descriptions>
|
||
|
||
<!-- 图片展示 -->
|
||
<div v-if="detail.images && detail.images.length > 0" style="margin-top: 16px;">
|
||
<div style="font-weight: bold; margin-bottom: 8px;">图片</div>
|
||
<div style="display: flex; gap: 8px; flex-wrap: wrap;">
|
||
<el-image
|
||
v-for="(img, idx) in detail.images"
|
||
:key="idx"
|
||
:src="getImageUrl(img)"
|
||
:preview-src-list="detail.images.map(u => getImageUrl(u))"
|
||
fit="cover"
|
||
style="width: 100px; height: 100px; border-radius: 4px; cursor: pointer;"
|
||
/>
|
||
</div>
|
||
</div>
|
||
</el-dialog>
|
||
|
||
<!-- ========== 驳回原因弹窗 ========== -->
|
||
<el-dialog v-model="rejectDialogVisible" title="驳回申请" width="480px" destroy-on-close :close-on-click-modal="false" :close-on-press-escape="false">
|
||
<el-form label-width="80px">
|
||
<el-form-item label="申请单号">
|
||
<span style="font-weight: bold; color: #409EFF;">{{ currentRejectRow?.request_no }}</span>
|
||
</el-form-item>
|
||
<el-form-item label="驳回原因" required>
|
||
<el-input v-model="rejectReason" type="textarea" :rows="4" placeholder="请填写驳回原因(必填)" maxlength="200" show-word-limit />
|
||
</el-form-item>
|
||
</el-form>
|
||
<template #footer>
|
||
<el-button @click="rejectDialogVisible = false">取消</el-button>
|
||
<el-button type="danger" :loading="rejectLoading" @click="confirmReject">确认驳回</el-button>
|
||
</template>
|
||
</el-dialog>
|
||
|
||
</div>
|
||
</template>
|
||
|
||
<script setup lang="ts">
|
||
import { ref, computed, onMounted } from 'vue'
|
||
import { useRoute } from 'vue-router'
|
||
import { Refresh, Plus } from '@element-plus/icons-vue'
|
||
import { ElMessage, ElMessageBox } from 'element-plus'
|
||
import { useUserStore } from '@/stores/user'
|
||
import { searchMaterialPurchase } from '@/api/purchase'
|
||
import {
|
||
getPurchaseList, createPurchase, getPurchaseDetail,
|
||
approvePurchase, getPurchaseApprovers, autoFillPurchase
|
||
} from '@/api/purchase'
|
||
import { uploadFile, deleteFile } from '@/api/common/upload'
|
||
import { usePasteUpload } from '@/hooks/usePasteUpload'
|
||
import type { FormInstance } from 'element-plus'
|
||
|
||
const userStore = useUserStore()
|
||
|
||
// --- 状态 ---
|
||
const list = ref<any[]>([])
|
||
const loading = ref(false)
|
||
const total = ref(0)
|
||
const page = ref(1)
|
||
const pageSize = ref(20)
|
||
const filterStatus = ref<number | ''>(0) // 默认筛选待审批
|
||
const submitLoading = ref(false)
|
||
const rejectLoading = ref(false)
|
||
const fileList = ref<any[]>([])
|
||
|
||
// 驳回
|
||
const rejectDialogVisible = ref(false)
|
||
const currentRejectRow = ref<any>(null)
|
||
const rejectReason = ref('')
|
||
|
||
// 详情
|
||
const detailDialogVisible = ref(false)
|
||
const detail = ref<any>({})
|
||
|
||
// 创建弹窗
|
||
const formDialogVisible = ref(false)
|
||
const dialogTitle = ref('新建采购申请')
|
||
const formRef = ref<FormInstance>()
|
||
const isUploading = ref(false)
|
||
const autoFillHint = ref('')
|
||
|
||
// 审批人
|
||
const approvers = ref<any[]>([])
|
||
|
||
// 物料搜索
|
||
const materialOptions = ref<any[]>([])
|
||
const searchLoading = ref(false)
|
||
const materialBaseId = ref<number | null>(null)
|
||
const searchPage = ref(1)
|
||
const searchKeyword = ref('')
|
||
const hasNextPage = ref(true)
|
||
const loadingMore = ref(false)
|
||
|
||
// v-loadmore 指令:监听 el-select 下拉滚动,滚动到底部时触发回调
|
||
const vLoadmore = {
|
||
mounted(el: any, binding: any) {
|
||
const dropdown = el.querySelector?.('.el-select-dropdown__wrap')
|
||
if (!dropdown) return
|
||
dropdown.addEventListener('scroll', function (this: any) {
|
||
const condition = this.scrollHeight - this.scrollTop <= this.clientHeight + 2
|
||
if (condition) binding.value()
|
||
})
|
||
}
|
||
}
|
||
|
||
// 表单
|
||
const form = ref({
|
||
name: '',
|
||
spec_model: '',
|
||
quantity: 1,
|
||
purchase_date: '',
|
||
supplier_link: '',
|
||
remark: '',
|
||
unit_price: undefined as number | undefined,
|
||
total_price: undefined as number | undefined,
|
||
tax_rate: 0,
|
||
approver_id: undefined as number | undefined,
|
||
images: [] as string[]
|
||
})
|
||
|
||
// --- 计算属性 ---
|
||
// 审批按钮仅对拥有操作权限的用户可见(菜单权限不等于审批权限)
|
||
const canApprove = computed(() => {
|
||
return userStore.role === 'SUPER_ADMIN'
|
||
|| userStore.hasPermission('inbound_purchase:operation')
|
||
})
|
||
|
||
// --- 工具函数 ---
|
||
const statusTagType = (status: number) => {
|
||
const map: Record<number, string> = {
|
||
0: 'warning', 1: 'success', 2: 'danger', 3: 'info'
|
||
}
|
||
return map[status] ?? 'info'
|
||
}
|
||
|
||
const getImageUrl = (url: string) => {
|
||
if (!url) return ''
|
||
if (url.startsWith('http')) return url
|
||
if (url.startsWith('/api/v1/common/files/')) return url
|
||
return `/api/v1/common/files/${url}`
|
||
}
|
||
|
||
const formatDate = (d: string) => d ? d.split(' ')[0] : ''
|
||
|
||
// --- 数据获取 ---
|
||
const fetchData = async () => {
|
||
loading.value = true
|
||
try {
|
||
const params: any = { page: page.value, limit: pageSize.value }
|
||
if (filterStatus.value !== '') params.status = filterStatus.value
|
||
|
||
const res: any = await getPurchaseList(params)
|
||
list.value = res.data?.items || []
|
||
total.value = res.data?.total || 0
|
||
} catch (err: any) {
|
||
ElMessage.error(err?.msg || '加载失败')
|
||
} finally {
|
||
loading.value = false
|
||
}
|
||
}
|
||
|
||
const fetchApprovers = async () => {
|
||
try {
|
||
const res: any = await getPurchaseApprovers()
|
||
approvers.value = res.data || []
|
||
} catch (e) {
|
||
console.error(e)
|
||
}
|
||
}
|
||
|
||
// --- 筛选 & 分页 ---
|
||
const handleStatusChange = () => {
|
||
page.value = 1
|
||
fetchData()
|
||
}
|
||
|
||
const handlePageChange = (p: number) => { page.value = p; fetchData() }
|
||
const handleSizeChange = (s: number) => { pageSize.value = s; page.value = 1; fetchData() }
|
||
|
||
// --- 新建 ---
|
||
const openCreateDialog = (prefill?: { materialId?: number; name?: string; spec?: string; unit?: string }) => {
|
||
dialogTitle.value = '新建采购申请'
|
||
form.value = {
|
||
name: prefill?.name || '',
|
||
spec_model: prefill?.spec || '',
|
||
quantity: 1,
|
||
purchase_date: new Date().toISOString().split('T')[0],
|
||
supplier_link: '', remark: '',
|
||
unit_price: undefined, total_price: undefined, tax_rate: 0,
|
||
approver_id: undefined, images: []
|
||
}
|
||
|
||
if (prefill?.materialId) {
|
||
materialBaseId.value = prefill.materialId
|
||
// 预填物料选项以便显示选中状态
|
||
materialOptions.value = [{
|
||
id: prefill.materialId,
|
||
name: prefill.name || '',
|
||
spec_model: prefill.spec || ''
|
||
}]
|
||
if (prefill.spec) {
|
||
form.value.spec_model = prefill.spec
|
||
}
|
||
autoFillHint.value = `已从基础信息【${prefill.name || ''}】带入,请补充采购信息`
|
||
} else {
|
||
materialBaseId.value = null
|
||
materialOptions.value = []
|
||
autoFillHint.value = ''
|
||
}
|
||
|
||
fileList.value = []
|
||
totalPriceManuallyEdited.value = false
|
||
formDialogVisible.value = true
|
||
|
||
// 异步加载默认审批人(取当前用户上一次采购申请的审批人)
|
||
fetchDefaultApprover()
|
||
}
|
||
|
||
// 获取当前用户上一次采购申请的审批人作为默认值
|
||
const fetchDefaultApprover = async () => {
|
||
try {
|
||
const res: any = await getPurchaseList({ page: 1, limit: 1, status: undefined })
|
||
const items = res?.data?.items || []
|
||
if (items.length > 0 && items[0].approver_id) {
|
||
form.value.approver_id = items[0].approver_id
|
||
}
|
||
} catch (e) {
|
||
// 静默失败,用户可手动选择
|
||
}
|
||
}
|
||
|
||
// --- 物料搜索(分页) ---
|
||
let searchTimer: any = null
|
||
const handleSearchMaterialDebounced = (query: string) => {
|
||
clearTimeout(searchTimer)
|
||
searchTimer = setTimeout(() => handleSearchMaterial(query), 300)
|
||
}
|
||
|
||
const handleSearchMaterial = async (query: string) => {
|
||
// 防御性处理:粘贴场景常混入零宽字符 / 控制字符 / 不可见 Unicode
|
||
const rawQuery = String(query || '')
|
||
const safeQuery = rawQuery.replace(/[\x00-\x1F\x7F-\x9F\u200B-\u200D\uFEFF]/g, '').trim()
|
||
searchLoading.value = true
|
||
searchKeyword.value = safeQuery
|
||
searchPage.value = 1
|
||
materialOptions.value = []
|
||
hasNextPage.value = true
|
||
|
||
try {
|
||
const res: any = await searchMaterialPurchase(safeQuery, 1)
|
||
materialOptions.value = res.data || []
|
||
hasNextPage.value = res.has_next !== false
|
||
} finally {
|
||
searchLoading.value = false
|
||
}
|
||
}
|
||
|
||
const handleLoadMoreMaterials = async () => {
|
||
if (searchLoading.value || loadingMore.value || !hasNextPage.value) return
|
||
loadingMore.value = true
|
||
searchPage.value += 1
|
||
try {
|
||
const res: any = await searchMaterialPurchase(searchKeyword.value, searchPage.value)
|
||
if (res.data && res.data.length > 0) {
|
||
materialOptions.value = [...materialOptions.value, ...res.data]
|
||
hasNextPage.value = res.has_next !== false
|
||
} else {
|
||
hasNextPage.value = false
|
||
}
|
||
} catch {
|
||
searchPage.value -= 1
|
||
} finally {
|
||
loadingMore.value = false
|
||
}
|
||
}
|
||
|
||
const onMaterialDropdownVisibleChange = (visible: boolean) => {
|
||
if (!visible) return
|
||
// 防御性拦截:竞态条件守卫
|
||
// 如果当前已经有搜索关键字(例如用户刚刚粘贴了内容、remote-method 已经设置了 searchKeyword),
|
||
// 绝对不要去请求默认列表,否则会清空 searchKeyword、覆盖正确结果。
|
||
if (searchKeyword.value || materialOptions.value.length > 0) return
|
||
// 打断正在排队的 debounce 定时器,避免与默认请求相互打架
|
||
if (searchTimer) { clearTimeout(searchTimer); searchTimer = null }
|
||
handleSearchMaterial('')
|
||
}
|
||
|
||
const onMaterialSelected = (id: number | null) => {
|
||
if (!id) {
|
||
materialBaseId.value = null
|
||
return
|
||
}
|
||
const item = materialOptions.value.find(i => i.id === id)
|
||
if (item) {
|
||
form.value.name = item.name || item.material_name || ''
|
||
form.value.spec_model = item.spec_model || item.spec || ''
|
||
materialBaseId.value = id
|
||
autoFillHint.value = ''
|
||
}
|
||
}
|
||
|
||
// --- 价格半联动逻辑 ---
|
||
// totalPriceManuallyEdited:标记总价是否被用户手动修改过,修改后不再自动覆盖
|
||
const totalPriceManuallyEdited = ref(false)
|
||
|
||
const onUnitPriceChange = (val: number | undefined) => {
|
||
// 用户修改单价时:如果总价尚未被手动修改,则自动计算总价
|
||
if (val !== undefined && val > 0 && form.value.quantity > 0 && !totalPriceManuallyEdited.value) {
|
||
form.value.total_price = +(val * form.value.quantity).toFixed(2)
|
||
}
|
||
}
|
||
|
||
const onTotalPriceChange = (_val: number | undefined) => {
|
||
totalPriceManuallyEdited.value = true
|
||
}
|
||
|
||
const onTaxRateChange = () => {
|
||
// 税率变化时:保持不含税单价不变,重算不含税总价 = 单价×数量
|
||
if (form.value.unit_price && form.value.quantity) {
|
||
form.value.total_price = Number((form.value.unit_price * form.value.quantity).toFixed(2))
|
||
}
|
||
}
|
||
|
||
// --- 上传 ---
|
||
const beforeAvatarUpload = (rawFile: any) => {
|
||
const isTypeValid = ['image/jpeg', 'image/png', 'image/gif', 'image/webp', 'image/bmp'].includes(rawFile.type)
|
||
if (!isTypeValid) {
|
||
ElMessage.error('仅支持 JPG/PNG/GIF/WEBP 图片')
|
||
return false
|
||
}
|
||
if (rawFile.size / 1024 / 1024 > 10) {
|
||
ElMessage.error('图片不能超过 10MB')
|
||
return false
|
||
}
|
||
return true
|
||
}
|
||
|
||
const customUpload = async (options: any) => {
|
||
const { file, onSuccess, onError } = options
|
||
const formData = new FormData()
|
||
formData.append('file', file)
|
||
isUploading.value = true
|
||
try {
|
||
const res: any = await uploadFile(formData)
|
||
if (res.code === 200) {
|
||
const newUrl = res.data.url
|
||
form.value.images!.push(newUrl)
|
||
// ★ v-model:file-list 已自动管理列表,onSuccess 通知 el-upload 更新状态即可
|
||
// 传入 { url } 让 el-upload 正确设置文件预览 URL
|
||
onSuccess({ url: getImageUrl(newUrl) })
|
||
} else {
|
||
ElMessage.error(res.msg || '上传失败')
|
||
onError(new Error(res.msg))
|
||
}
|
||
} catch (e) {
|
||
ElMessage.error('网络错误')
|
||
onError(e)
|
||
} finally {
|
||
isUploading.value = false
|
||
}
|
||
}
|
||
|
||
const handleRemoveImage = async (uploadFile: any) => {
|
||
const urlToRemove = form.value.images!.find(u => getImageUrl(u) === uploadFile.url) || uploadFile.url
|
||
form.value.images = form.value.images!.filter(u => u !== urlToRemove)
|
||
const filename = urlToRemove.split('/').pop()
|
||
if (filename && !urlToRemove.startsWith('http')) {
|
||
await deleteFile(filename).catch(() => {})
|
||
}
|
||
}
|
||
|
||
// 粘贴上传处理器(PC 端:鼠标悬停 + Ctrl+V 直接粘贴图片)
|
||
const handlePasteLink = (link: string, field: string) => {
|
||
// 采购单没有独立的外链输入框,暂不支持网页图片链接自动填入
|
||
}
|
||
usePasteUpload(customUpload, 'images', '#upload-purchase-images', handlePasteLink)
|
||
|
||
// --- 提交 ---
|
||
const submitForm = async () => {
|
||
if (!form.value.name.trim()) { ElMessage.warning('请选择或填写采购物品'); return }
|
||
if (!form.value.approver_id) { ElMessage.warning('请选择审批人'); return }
|
||
if (!form.value.purchase_date) { ElMessage.warning('请选择采购日期'); return }
|
||
if (!form.value.images || form.value.images.length === 0) { ElMessage.warning('请上传至少一张图片'); return }
|
||
|
||
submitLoading.value = true
|
||
try {
|
||
await createPurchase(form.value as any)
|
||
ElMessage.success('提交成功,审批人将收到邮件通知')
|
||
formDialogVisible.value = false
|
||
await fetchData()
|
||
} catch (err: any) {
|
||
ElMessage.error(err?.msg || '提交失败')
|
||
} finally {
|
||
submitLoading.value = false
|
||
}
|
||
}
|
||
|
||
// --- 详情 ---
|
||
const openDetailDialog = async (row: any) => {
|
||
try {
|
||
const res: any = await getPurchaseDetail(row.id)
|
||
detail.value = res.data || {}
|
||
detailDialogVisible.value = true
|
||
} catch (e) {
|
||
ElMessage.error('获取详情失败')
|
||
}
|
||
}
|
||
|
||
// --- 审批操作 ---
|
||
const handleApprove = async (row: any) => {
|
||
try {
|
||
await ElMessageBox.confirm(`确定要通过采购申请「${row.request_no}」吗?`, '审批确认', {
|
||
confirmButtonText: '确定通过', cancelButtonText: '取消', type: 'info'
|
||
})
|
||
} catch { return }
|
||
|
||
try {
|
||
await approvePurchase(row.id, { action: 'approve' })
|
||
ElMessage.success('已通过')
|
||
await fetchData()
|
||
} catch (err: any) {
|
||
ElMessage.error(err?.msg || '操作失败')
|
||
}
|
||
}
|
||
|
||
const openRejectDialog = (row: any) => {
|
||
currentRejectRow.value = row
|
||
rejectReason.value = ''
|
||
rejectDialogVisible.value = true
|
||
}
|
||
|
||
const confirmReject = async () => {
|
||
if (!rejectReason.value.trim()) { ElMessage.warning('请填写驳回原因'); return }
|
||
rejectLoading.value = true
|
||
try {
|
||
await approvePurchase(currentRejectRow.value.id, {
|
||
action: 'reject', reject_reason: rejectReason.value
|
||
})
|
||
ElMessage.success('已驳回')
|
||
rejectDialogVisible.value = false
|
||
await fetchData()
|
||
} catch (err: any) {
|
||
ElMessage.error(err?.msg || '操作失败')
|
||
} finally {
|
||
rejectLoading.value = false
|
||
}
|
||
}
|
||
|
||
// --- 初始化 ---
|
||
const route = useRoute()
|
||
|
||
onMounted(() => {
|
||
fetchData()
|
||
fetchApprovers()
|
||
|
||
// 从基础信息跳转过来的参数
|
||
const query = route.query
|
||
if (query.material_id) {
|
||
openCreateDialog({
|
||
materialId: Number(query.material_id),
|
||
name: (query.name as string) || '',
|
||
spec: (query.spec as string) || '',
|
||
unit: (query.unit as string) || ''
|
||
})
|
||
}
|
||
})
|
||
</script>
|
||
|
||
<style scoped>
|
||
.app-container { padding: 20px; }
|
||
.filter-container { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
|
||
|
||
/* 表单分组 */
|
||
.form-section {
|
||
background: #fafbfc;
|
||
border: 1px solid #ebeef5;
|
||
border-radius: 6px;
|
||
padding: 12px 16px 4px;
|
||
margin-bottom: 14px;
|
||
}
|
||
.form-section .section-title {
|
||
font-size: 13px;
|
||
font-weight: 600;
|
||
color: #303133;
|
||
margin-bottom: 10px;
|
||
padding-bottom: 8px;
|
||
border-bottom: 1px solid #ebeef5;
|
||
}
|
||
.form-section .section-title span {
|
||
position: relative;
|
||
padding-left: 10px;
|
||
}
|
||
.form-section .section-title span::before {
|
||
content: '';
|
||
position: absolute;
|
||
left: 0;
|
||
top: 2px;
|
||
bottom: 2px;
|
||
width: 3px;
|
||
background: #409EFF;
|
||
border-radius: 2px;
|
||
}
|
||
</style>
|