Files
shanxia2.0/web-admin/src/views/m/pl/plan-cert/CertLinkSheet.vue
T
wuzilongandCursor 522b4be705 feat(pl): 装送货段增加同市标记,并同步额度证件与计划表单。
同市易制毒段只校验购买证;同步 Web/H5 表单、额度统计展示与相关回归测试。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-12 16:34:58 +08:00

443 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>
<van-popup
v-model:show="visible"
position="bottom"
round
teleport="body"
class="mp-cert-link-sheet"
:close-on-click-overlay="!saving"
>
<van-nav-bar
:title="mode === 'create' ? '新增关联证件' : '编辑适用产品'"
left-text="关闭"
@click-left="!saving && (visible = false)"
/>
<div class="mp-cert-link-sheet__body">
<template v-if="mode === 'create'">
<div class="mp-cert-link-kinds">
<button
class="mp-wv-btn"
:class="{ 'mp-wv-btn--primary': certType === 'purchase' }"
type="button"
:disabled="saving"
@click="setType('purchase')"
>
购买证
</button>
<button
class="mp-wv-btn"
:class="{ 'mp-wv-btn--primary': certType === 'transport' }"
type="button"
:disabled="saving"
@click="setType('transport')"
>
运输证
</button>
</div>
<van-search
v-model="keyword"
placeholder="证件编号或品名"
shape="round"
@search="searchCerts(keyword)"
/>
<article v-if="certId" class="mp-cert-link-card">
<strong>{{ certNo || '证号未提供' }}</strong>
<button type="button" :disabled="saving" @click="clearCert">更换证件</button>
</article>
<template v-else>
<p v-if="certError" class="mp-cert-link-note" role="alert">{{ certError }}</p>
<van-loading v-else-if="opening || certLoading" />
<p v-else-if="!certOptions.length" class="mp-cert-link-note">没有可关联的证件。</p>
<button
v-for="item in certOptions"
:key="item.id"
type="button"
class="mp-cert-link-card"
:disabled="saving || certChoiceState(item, certType, takenIds) !== 'open'"
@click="pickCert(item)"
>
{{ certChoiceLabel(item, certType, takenIds) }}
</button>
</template>
</template>
<p v-else class="mp-cert-kind-heading">
<span
class="status-tag mp-cert-kind-tag"
:class="`status-tag--${certKindMobileTone(certType)}`"
>{{ certKindLabel(certType) }}</span
>
<span v-if="certNo">{{ certNo }}</span>
</p>
<template v-if="certId">
<h3>适用产品</h3>
<p class="mp-cert-link-note">已保存的产品不能删除,只能设为不可用。未保存的产品可以移除。</p>
<p v-if="certStatus === 'void'" class="mp-cert-link-note">作废证件不能新增或恢复产品。</p>
<p v-else-if="certStatus === 'closed'" class="mp-cert-link-note">已核销证件不能新增或恢复产品。</p>
<p v-if="linesError" class="mp-cert-link-note" role="alert">{{ linesError }}</p>
<van-loading v-else-if="linesLoading" />
<p v-else-if="!draft.length" class="mp-cert-link-note">还没有适用产品。</p>
<article v-for="product in draft" :key="product.contractProductId" class="mp-cert-link-card">
<strong>{{ productName(product.contractProductId) }}</strong>
<p>{{ product.scopeStatus === 'inactive' ? '不可用' : '可用' }}</p>
<button
v-if="product.id"
class="mp-wv-btn"
type="button"
:disabled="saving || !canRestoreProduct(certStatus, product.scopeStatus)"
@click="toggleProduct(product.contractProductId)"
>
{{ product.scopeStatus === 'inactive' ? '恢复可用' : '设为不可用' }}
</button>
<button v-else class="mp-wv-btn" type="button" :disabled="saving" @click="removeProduct(product.contractProductId)">
移除
</button>
</article>
<button
v-if="certStatus !== 'void' && certStatus !== 'closed'"
class="mp-wv-btn mp-wv-btn--block"
type="button"
:disabled="saving || linesLoading"
@click="openPicker"
>
选择产品
</button>
</template>
</div>
<footer>
<button class="mp-wv-btn mp-wv-btn--primary" type="button" :disabled="!canSave" @click="save">
{{ saving ? '正在保存…' : mode === 'create' ? '保存关联' : '保存产品' }}
</button>
</footer>
</van-popup>
<van-popup v-model:show="pickerVisible" position="bottom" round teleport="body" class="mp-cert-link-sheet">
<van-nav-bar title="选择合同产品" left-text="关闭" @click-left="pickerVisible = false" />
<div class="mp-cert-link-sheet__body">
<p v-if="!pickerRows.length" class="mp-cert-link-note">合同里没有可再添加的产品。</p>
<button
v-for="row in pickerRows"
:key="row.id"
type="button"
class="mp-cert-link-card"
:class="{ 'is-selected': picked.has(row.id) }"
@click="togglePicked(row.id)"
>
<strong>{{ row.productName || '合同产品' }}</strong>
<p>{{ row.model || '—' }} · {{ unitLabel(row.unit) || '—' }}</p>
</button>
</div>
<footer>
<button class="mp-wv-btn" type="button" @click="pickerVisible = false">取消</button>
<button class="mp-wv-btn mp-wv-btn--primary" type="button" :disabled="!picked.size" @click="confirmProducts">
确认添加
</button>
</footer>
</van-popup>
</template>
<script setup lang="ts">
import { ElMessage } from 'element-plus'
import {
quotaCertApi,
type CertBinding,
type CertType,
type ContractProduct,
type QuotaCert,
type ScopeProduct
} from '@/api/pl/quota-cert'
import { certKindLabel, certKindMobileTone } from '@/views/pl/quota-cert/cert-list-face'
import { bindingPayload, replaceCertProducts } from '@/views/pl/quota-cert/model'
import { getProductUnitLabel } from '@/utils/crm-product-display'
import { useDictStore } from '@/store/modules/dict'
import {
addDraftProducts,
canRestoreProduct,
canSaveNewLink,
canSaveProductEdit,
certChoiceLabel,
certChoiceState,
nextScopeStatus,
removeDraftProduct
} from './cert-link-rules'
const emit = defineEmits<{ saved: [] }>()
const visible = ref(false)
const opening = ref(false)
const pickerVisible = ref(false)
const saving = ref(false)
const certLoading = ref(false)
const linesLoading = ref(false)
const certError = ref('')
const linesError = ref('')
const mode = ref<'create' | 'edit'>('create')
const contractId = ref('')
const certType = ref<CertType>('purchase')
const certId = ref('')
const certNo = ref('')
const certStatus = ref('')
const keyword = ref('')
const savedProducts = ref<ScopeProduct[]>([])
const draft = ref<ScopeProduct[]>([])
const certOptions = ref<QuotaCert[]>([])
const lines = ref<ContractProduct[]>([])
const takenIds = ref(new Set<string>())
const picked = ref(new Set<string>())
let epoch = 0
let searchTimer = 0
const pickerRows = computed(() => {
const used = new Set(draft.value.map((product) => product.contractProductId))
return lines.value.filter((row) => row.id && !used.has(row.id))
})
const canSave = computed(() => {
if (saving.value || !contractId.value || !certId.value) return false
if (mode.value === 'create') {
return canSaveNewLink(draft.value) && !takenIds.value.has(`${certType.value}:${certId.value}`)
}
return canSaveProductEdit(savedProducts.value, draft.value)
})
function unitLabel(unit?: string | null) {
const label = getProductUnitLabel(useDictStore().getLabel, unit)
return !label || label === '—' ? '' : label
}
function productName(id: string) {
return lines.value.find((row) => row.id === id)?.productName || '合同产品'
}
function reset() {
epoch++
saving.value = false
opening.value = false
certLoading.value = false
linesLoading.value = false
certError.value = ''
linesError.value = ''
certId.value = ''
certNo.value = ''
certStatus.value = ''
keyword.value = ''
savedProducts.value = []
draft.value = []
certOptions.value = []
lines.value = []
takenIds.value = new Set()
picked.value = new Set()
pickerVisible.value = false
}
async function open(payload: {
contractId: string
mode: 'create' | 'edit'
certType?: CertType
certId?: string
certNo?: string
certStatus?: string
}) {
reset()
const current = ++epoch
mode.value = payload.mode
contractId.value = payload.contractId
certType.value = payload.certType || 'purchase'
certId.value = payload.certId || ''
certNo.value = payload.certNo || ''
certStatus.value = payload.certStatus || ''
visible.value = true
opening.value = true
try {
const [bindings, products] = await Promise.all([
quotaCertApi.bindings(payload.contractId),
quotaCertApi.products(payload.contractId)
])
if (current !== epoch) return
lines.value = products
takenIds.value = new Set(bindings.map((row) => `${row.certType}:${row.certId}`))
if (payload.mode === 'edit' && payload.certType && payload.certId) {
const currentBinding = bindings.find(
(row) => row.certType === payload.certType && row.certId === payload.certId
)
savedProducts.value = (currentBinding?.products || []).map((product) => ({ ...product }))
draft.value = savedProducts.value.map((product) => ({ ...product }))
}
if (payload.mode === 'create') await searchCerts('')
} catch {
if (current === epoch) {
visible.value = false
ElMessage.error('合同用证没有打开,请重试')
}
} finally {
if (current === epoch) opening.value = false
}
}
function setType(kind: CertType) {
if (saving.value || certType.value === kind) return
certType.value = kind
clearCert()
void searchCerts(keyword.value)
}
function clearCert() {
certId.value = ''
certNo.value = ''
certStatus.value = ''
savedProducts.value = []
draft.value = []
}
async function searchCerts(value = '') {
const current = epoch
const kind = certType.value
certOptions.value = []
certLoading.value = true
certError.value = ''
try {
const rows = await quotaCertApi.list(kind, { keyword: value })
if (current !== epoch || kind !== certType.value) return
certOptions.value = rows
} catch {
if (current === epoch) certError.value = '证件列表加载失败,请重试'
} finally {
if (current === epoch) certLoading.value = false
}
}
function pickCert(item: QuotaCert) {
if (certChoiceState(item, certType.value, takenIds.value) !== 'open') return
certId.value = item.id
certNo.value = item.certNo
certStatus.value = item.status
draft.value = []
savedProducts.value = []
}
function toggleProduct(id: string) {
const current = draft.value.find((product) => product.contractProductId === id)
if (!current?.id || !canRestoreProduct(certStatus.value, current.scopeStatus)) return
const status = nextScopeStatus(current.scopeStatus)
draft.value = draft.value.map((product) =>
product.contractProductId === id ? { ...product, scopeStatus: status } : product
)
}
function removeProduct(id: string) {
draft.value = removeDraftProduct(draft.value, id)
}
function openPicker() {
picked.value = new Set()
pickerVisible.value = true
}
function togglePicked(id: string) {
const next = new Set(picked.value)
if (next.has(id)) next.delete(id)
else next.add(id)
picked.value = next
}
function confirmProducts() {
draft.value = addDraftProducts(draft.value, [...picked.value])
pickerVisible.value = false
}
async function save() {
if (!canSave.value) return
if (mode.value === 'create' && takenIds.value.has(`${certType.value}:${certId.value}`)) {
ElMessage.warning('该证件已关联,请在对应证件里编辑')
return
}
const current = epoch
saving.value = true
try {
const existing = await quotaCertApi.bindings(contractId.value)
if (current !== epoch) return
const next: CertBinding[] = replaceCertProducts(
existing,
certType.value,
certId.value,
draft.value.map((product) => ({ ...product }))
)
await quotaCertApi.saveBindings(contractId.value, bindingPayload(next))
if (current !== epoch) return
ElMessage.success(mode.value === 'create' ? '证件已关联' : '适用产品已保存')
visible.value = false
emit('saved')
} catch {
ElMessage.error('没有保存,请重试')
} finally {
if (current === epoch) saving.value = false
}
}
watch(keyword, (value) => {
if (!visible.value || mode.value !== 'create' || certId.value) return
window.clearTimeout(searchTimer)
searchTimer = window.setTimeout(() => void searchCerts(value), 300)
})
defineExpose({ open })
</script>
<style scoped>
.mp-cert-link-sheet {
display: flex;
flex-direction: column;
max-height: 88dvh;
color: #1f2a26;
background: #f5f8f7;
}
.mp-cert-link-sheet__body {
min-height: 0;
overflow: auto;
padding: 0 10px 10px;
}
h3 {
margin: 12px 0 0;
font-size: 15px;
}
.mp-cert-link-kinds,
footer {
display: flex;
gap: 8px;
}
.mp-cert-link-kinds {
padding-top: 8px;
}
.mp-cert-link-kinds .mp-wv-btn,
footer .mp-wv-btn {
flex: 1;
height: 30px;
border-radius: 8px;
}
.mp-cert-link-card {
display: block;
width: 100%;
margin-top: 8px;
padding: 10px 12px;
border: 1px solid #dcebe4;
border-radius: 12px;
background: #fff;
color: #1f2a26;
text-align: left;
}
.mp-cert-link-card strong {
display: block;
}
.mp-cert-link-card p,
.mp-cert-link-note {
margin: 4px 0 0;
color: #6b7b74;
font-size: 13px;
line-height: 1.5;
}
.mp-cert-link-card .mp-wv-btn {
margin-top: 8px;
}
.mp-cert-link-card.is-selected {
border-color: #0a9f6d;
}
.mp-wv-btn--block {
width: 100%;
margin-top: 8px;
}
footer {
flex-shrink: 0;
padding: 10px 12px calc(10px + env(safe-area-inset-bottom));
border-top: 1px solid #dcebe4;
background: white;
}
button:disabled {
opacity: 0.45;
}
</style>