Files
shanxia2.0/web-admin/src/views/crm/contract/components/ApprovalDialog.vue
T
2026-08-01 17:29:37 +08:00

515 lines
14 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<template>
<el-dialog
v-model="dialogVisible"
:title="dialogTitle"
width="640px"
align-center
append-to-body
destroy-on-close
class="dialog-form-layout"
modal-class="order-module-dialog-mask"
:close-on-click-modal="false"
>
<el-alert
v-if="isBatchMode && batchCount > 0"
type="success"
:closable="false"
show-icon
class="batch-action-alert batch-action-alert--success"
:title="batchInfoTitle"
/>
<el-alert
v-if="skippedCount > 0"
type="error"
:closable="false"
show-icon
class="batch-action-alert batch-action-alert--danger"
:title="`已跳过 ${skippedCount} 条不可处理的合同`"
/>
<el-form
ref="formRef"
:model="form"
:rules="rules"
label-position="top"
class="crm-dialog-form"
scroll-to-error
:scroll-into-view-options="{ behavior: 'smooth', block: 'center' }"
>
<div class="form-grid-2">
<el-form-item v-if="actionType === 'submit'" label="审批类型" class="form-col-span-2">
<el-radio-group v-model="form.approvalType">
<el-radio value="sequential">顺序审批</el-radio>
<el-radio value="parallel">会签审批</el-radio>
<el-radio value="or">或签审批</el-radio>
</el-radio-group>
<div class="form-label-tip form-tip-block">
顺序审批:按顺序依次审批;会签:所有人都需审批;或签:任一人审批即可
</div>
</el-form-item>
<el-form-item v-if="actionType === 'submit'" prop="approverIds" class="form-col-span-2">
<template #label>
选择审批人<span class="form-label-tip">支持搜索用户姓名</span>
</template>
<el-select
v-model="form.approverIds"
multiple
filterable
remote
reserve-keyword
placeholder="请选择审批人"
:remote-method="handleSearchUser"
:loading="userLoading"
style="width: 100%"
>
<el-option
v-for="user in approverList"
:key="user.userId"
:label="`${user.nickName}(${user.userName})`"
:value="user.userId"
/>
</el-select>
</el-form-item>
<el-form-item
v-if="actionType === 'approve' || actionType === 'reject'"
label="审批意见"
prop="comment"
class="form-col-span-2"
>
<el-input
v-model="form.comment"
type="textarea"
:rows="5"
placeholder="请输入审批意见"
maxlength="500"
show-word-limit
/>
</el-form-item>
<el-form-item v-if="actionType === 'submit'" prop="remark" class="form-col-span-2">
<template #label>
提交说明<span class="form-label-tip">可选</span>
</template>
<el-input
v-model="form.remark"
type="textarea"
:rows="5"
placeholder="请输入提交说明"
maxlength="500"
show-word-limit
/>
</el-form-item>
<el-form-item v-if="actionType === 'withdraw'" label="撤回原因" prop="reason" class="form-col-span-2">
<el-input
v-model="form.reason"
type="textarea"
:rows="5"
placeholder="请输入撤回原因"
maxlength="500"
show-word-limit
/>
</el-form-item>
</div>
</el-form>
<section v-if="actionType === 'submit' && form.approverIds.length > 0" class="approval-dialog-section">
<h3 class="approval-section-title">审批流程预览</h3>
<div class="approval-flow">
<el-steps :active="0" align-center>
<el-step
v-for="(approverId, index) in form.approverIds"
:key="index"
:title="getApproverName(approverId)"
/>
</el-steps>
</div>
</section>
<template #footer>
<div class="approval-dialog-actions">
<el-button @click="handleClose">
<el-icon><Close /></el-icon>取消
</el-button>
<el-button type="primary" :loading="submitLoading" @click="handleConfirm">
<el-icon><Check /></el-icon>{{ confirmButtonText }}
</el-button>
</div>
</template>
</el-dialog>
</template>
<script setup lang="ts" name="ApprovalDialog">
import { ref, computed } from 'vue'
import { Check, Close } from '@element-plus/icons-vue'
import {
submitContract,
batchSubmitContract,
approveContract,
rejectContract,
withdrawContract,
getContractApproverSetting
} from '@/api/crm/contract'
import { fetchUserOptions } from '@/api/admin'
import { $modal } from '@/utils/modal'
interface ApproverUser {
userId: string
nickName: string
userName: string
}
const emit = defineEmits(['success'])
const dialogVisible = ref(false)
const actionType = ref<'submit' | 'approve' | 'reject' | 'withdraw'>('submit')
const contractId = ref<string | number>()
const contractIds = ref<(string | number)[]>()
const skippedCount = ref(0)
const form = ref({
approvalType: 'sequential',
approverIds: [] as string[],
comment: '',
remark: '',
reason: ''
})
const approverList = ref<ApproverUser[]>([])
const submitLoading = ref(false)
const userLoading = ref(false)
const formRef = ref<ElFormInstance>()
const isBatchMode = computed(() => !!(contractIds.value && contractIds.value.length > 0))
const batchCount = computed(() => contractIds.value?.length || 0)
const rules = computed(() => {
const baseRules: any = {}
if (actionType.value === 'submit') {
baseRules.approverIds = [{ required: true, message: '请选择审批人', trigger: 'change' }]
}
if (actionType.value === 'approve' || actionType.value === 'reject') {
baseRules.comment = [{ required: true, message: '请输入审批意见', trigger: 'blur' }]
}
if (actionType.value === 'withdraw') {
baseRules.reason = [{ required: true, message: '请输入撤回原因', trigger: 'blur' }]
}
return baseRules
})
const dialogTitle = computed(() => {
const titleMap = {
submit: '提交审批',
approve: '审批通过',
reject: '审批驳回',
withdraw: '撤回审批'
}
let title = titleMap[actionType.value]
if (isBatchMode.value) {
title += `(${batchCount.value}条)`
}
return title
})
const batchInfoTitle = computed(() => {
if (actionType.value === 'submit') {
return `将提交 ${batchCount.value} 条草稿合同(使用同一组审批人与说明)`
}
if (actionType.value === 'approve') {
return `将审批通过 ${batchCount.value} 条合同(使用同一审批意见)`
}
if (actionType.value === 'reject') {
return `将审批驳回 ${batchCount.value} 条合同(使用同一审批意见)`
}
return `将处理 ${batchCount.value} 条合同`
})
const confirmButtonText = computed(() => {
const textMap = {
submit: '提交',
approve: '通过',
reject: '驳回',
withdraw: '撤回'
}
return textMap[actionType.value]
})
const open = async (id: string | number, type: 'submit' | 'approve' | 'reject' | 'withdraw' = 'submit') => {
contractId.value = id
contractIds.value = undefined
skippedCount.value = 0
actionType.value = type
dialogVisible.value = true
resetForm()
if (type === 'submit') {
await loadApproverList()
await applyPrefillApprovers()
}
}
const openBatch = async (
idList: (string | number)[],
type: 'submit' | 'approve' | 'reject' | 'withdraw' = 'submit',
options?: { skippedCount?: number }
) => {
contractIds.value = idList
contractId.value = undefined
skippedCount.value = options?.skippedCount || 0
actionType.value = type
dialogVisible.value = true
resetForm()
if (type === 'submit') {
await loadApproverList()
await applyPrefillApprovers()
}
}
const showBatchResultMessage = (options: {
actionLabel: string
successCount: number
failedCount: number
skippedCount?: number
}) => {
const { actionLabel, successCount, failedCount, skippedCount: skip = 0 } = options
const skipText = skip > 0 ? `,跳过 ${skip} 条` : ''
if (successCount === 0 && failedCount === 0) {
$modal.msgWarning(`没有可${actionLabel}的合同${skipText}`)
return
}
if (successCount === 0) {
$modal.msgError(`${actionLabel}失败${skipText}`)
return
}
if (failedCount > 0) {
$modal.msgWarning(`成功${actionLabel} ${successCount} 条,失败 ${failedCount} 条${skipText}`)
return
}
if (successCount === 1 && skip === 0) {
$modal.msgSuccess(`${actionLabel}成功`)
return
}
$modal.msgSuccess(`成功${actionLabel} ${successCount} 条${skipText}`)
}
const loadApproverList = async (nickName?: string) => {
userLoading.value = true
try {
const res = await fetchUserOptions()
const options = (res as any)?.data ?? res ?? []
let list = (Array.isArray(options) ? options : [])
.map((item: any) => ({
userId: String(item.value ?? item.userId ?? item.id ?? ''),
nickName: item.label ?? item.nickName ?? item.userName ?? '',
userName: item.userNo ?? item.userName ?? ''
}))
.filter((u) => u.userId)
if (nickName) {
const q = nickName.toLowerCase()
list = list.filter(
(u) => u.nickName.toLowerCase().includes(q) || String(u.userName).toLowerCase().includes(q)
)
}
approverList.value = list
} catch (error) {
console.error('获取审批人列表失败:', error)
$modal.msgError('获取审批人列表失败')
approverList.value = []
} finally {
userLoading.value = false
}
}
async function resolvePrefillApproverIds(userList: ApproverUser[]) {
try {
const { data } = await getContractApproverSetting()
if (!data || String(data.status) !== '0') return [] as string[]
const valid = new Set(userList.map((u) => String(u.userId)))
return (data.approverIds || []).map(String).filter((id) => valid.has(id))
} catch {
return []
}
}
async function applyPrefillApprovers() {
form.value.approverIds = await resolvePrefillApproverIds(approverList.value)
}
const handleSearchUser = (query: string) => {
if (query) {
loadApproverList(query)
} else {
loadApproverList()
}
}
const getApproverName = (approverId: string | number): string => {
const approver = approverList.value.find((item) => item.userId == approverId)
return approver ? approver.nickName : ''
}
const handleConfirm = async () => {
await formRef.value?.validate()
const isBatch = isBatchMode.value
if (!isBatch && (contractId.value == null || contractId.value === '')) {
$modal.msgError('合同 ID 缺失,请关闭后重新打开')
return
}
submitLoading.value = true
try {
let shouldClose = true
if (actionType.value === 'submit') {
if (isBatch) {
const count = await batchSubmitContract(contractIds.value!, form.value.approverIds, form.value.remark)
showBatchResultMessage({
actionLabel: '提交',
successCount: count ?? contractIds.value!.length,
failedCount: 0,
skippedCount: skippedCount.value
})
} else {
await submitContract(contractId.value!, form.value.approverIds, form.value.remark)
$modal.msgSuccess('提交成功')
}
} else if (actionType.value === 'approve') {
if (isBatch) {
const results = await Promise.allSettled(
contractIds.value!.map((id) => approveContract(id, form.value.comment))
)
const successCount = results.filter((item) => item.status === 'fulfilled').length
const failedCount = results.length - successCount
showBatchResultMessage({
actionLabel: '审批通过',
successCount,
failedCount,
skippedCount: skippedCount.value
})
shouldClose = successCount > 0
} else {
await approveContract(contractId.value!, form.value.comment)
$modal.msgSuccess('审批通过')
}
} else if (actionType.value === 'reject') {
if (isBatch) {
const results = await Promise.allSettled(
contractIds.value!.map((id) => rejectContract(id, form.value.comment))
)
const successCount = results.filter((item) => item.status === 'fulfilled').length
const failedCount = results.length - successCount
showBatchResultMessage({
actionLabel: '审批驳回',
successCount,
failedCount,
skippedCount: skippedCount.value
})
shouldClose = successCount > 0
} else {
await rejectContract(contractId.value!, form.value.comment)
$modal.msgSuccess('已驳回')
}
} else if (actionType.value === 'withdraw') {
await withdrawContract(contractId.value!, form.value.reason)
$modal.msgSuccess('已撤回')
}
if (shouldClose) {
dialogVisible.value = false
emit('success')
}
} finally {
submitLoading.value = false
}
}
const handleClose = () => {
dialogVisible.value = false
resetForm()
}
const defaultComment = (type: typeof actionType.value) => {
if (type === 'approve') return '审批通过'
if (type === 'reject') return '审批驳回'
return ''
}
const resetForm = () => {
form.value = {
approvalType: 'sequential',
approverIds: [],
comment: defaultComment(actionType.value),
remark: '',
reason: ''
}
formRef.value?.clearValidate()
}
defineExpose({
open,
openBatch
})
</script>
<style scoped lang="scss">
.batch-action-alert {
margin-bottom: 12px;
&--success {
:deep(.el-alert__title),
:deep(.el-alert__icon) {
color: var(--primary, #10b981);
}
}
&--danger {
:deep(.el-alert__title),
:deep(.el-alert__icon) {
color: #ef4444;
}
}
}
.form-tip-block {
display: block;
margin-top: 6px;
line-height: 1.5;
}
.approval-dialog-section {
margin-top: 16px;
}
.approval-section-title {
margin: 0 0 12px;
font-size: 15px;
font-weight: 600;
color: var(--section-bar-title-color, var(--primary-dark, #059669));
&::before {
content: '';
display: inline-block;
width: 3px;
height: 14px;
margin-right: 8px;
vertical-align: -2px;
background: var(--primary, #1ca851);
border-radius: 2px;
}
}
.approval-flow {
padding: 16px;
background: var(--primary-bg, #e6f8ed);
border: 1px solid var(--border, #d9e8e2);
border-radius: 8px;
}
.approval-dialog-actions {
display: flex;
flex-wrap: wrap;
gap: 4px 16px;
justify-content: flex-end;
width: 100%;
}
</style>