Files
one-pipe-system/src/components/business/package-silence/SilencePolicyDialog.vue
luo f2614d8efb
All checks were successful
构建并部署前端到测试环境 / deploy (push) Successful in 54s
fix: some
2026-10-28 15:18:44 +08:00

247 lines
8.5 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>
<ElDialog
:model-value="true"
:title="id ? '编辑延期政策' : '新增延期政策'"
width="min(780px, 96vw)"
destroy-on-close
@close="$emit('close')"
>
<ElAlert
title="同级范围及生效时间不能重叠;变更只影响之后发起的业务,不改变历史订单和审批快照。金额输入单位为元。"
type="info"
:closable="false"
/>
<ElAlert v-if="error" :title="error" type="error" :closable="false" />
<ElForm v-loading="loading" label-width="160px" :disabled="saving || loading">
<ElFormItem label="政策名称" required><ElInput v-model="form.name" /></ElFormItem>
<ElFormItem label="适用范围" required
><ElSelect v-model="form.scope_type" @change="form.scope_ids = []"
><ElOption value="all" label="全部套餐" /><ElOption
value="series"
label="套餐系列" /><ElOption value="package" label="指定套餐" /></ElSelect
></ElFormItem>
<ElFormItem v-if="form.scope_type !== 'all'" label="适用系列/套餐" required
><BusinessEntitySelect
v-model="form.scope_ids"
:entity="form.scope_type === 'package' ? 'package' : 'series'"
:allowed="allowed"
multiple
/>
<p>范围优先级:指定套餐 > 套餐系列 > 全部套餐。</p></ElFormItem
>
<ElFormItem label="月单价(元)" required
><ElInput v-model="unitYuan" inputmode="decimal"
/></ElFormItem>
<ElFormItem label="免费延期次数"
><ElInputNumber v-model="form.free_extension_limit" :min="0" :precision="0"
/></ElFormItem>
<ElFormItem label="付费累计最多月数"
><ElInputNumber v-model="form.paid_max_months" :min="0" :precision="0"
/></ElFormItem>
<ElFormItem label="限制单次月数"><ElSwitch v-model="singleLimited" /></ElFormItem>
<ElFormItem v-if="singleLimited" label="单次最多月数"
><ElInputNumber v-model="form.single_max_months" :min="1" :precision="0"
/></ElFormItem>
<ElFormItem label="生效时间" required
><ElDatePicker
v-model="form.effective_from"
type="datetime"
value-format="YYYY-MM-DDTHH:mm:ssZ"
format="YYYY-MM-DD HH:mm:ss"
/></ElFormItem>
<ElFormItem label="失效时间"
><ElDatePicker
v-model="form.effective_to"
type="datetime"
value-format="YYYY-MM-DDTHH:mm:ssZ"
format="YYYY-MM-DD HH:mm:ss"
clearable
/><p>不填表示长期。</p></ElFormItem
>
<ElFormItem label="免审批场景"
><ElSelect
v-model="form.approval_exempt_types"
multiple
filterable
allow-create
default-first-option
placeholder="空表示全部需要审批"
><ElOption value="offline_paid" label="线下付费延期" /><ElOption
value="batch"
label="批量特殊延期" /><ElOption
v-for="value in form.approval_exempt_types.filter(
(x) => !['offline_paid', 'batch'].includes(x)
)"
:key="value"
:value="value"
:label="value" /></ElSelect
><p>接口未枚举完整场景;其他场景须填写后端提供的精确编码,不填写显示文案。</p></ElFormItem
>
<ElFormItem label="启用"
><ElSwitch v-model="form.status" :active-value="1" :inactive-value="0"
/></ElFormItem>
<ElFormItem label="备注"><ElInput v-model="form.remark" type="textarea" /></ElFormItem>
</ElForm>
<template #footer
><ElButton @click="$emit('close')">取消</ElButton
><ElButton
type="primary"
:loading="saving"
:disabled="loading || !allowed || !loaded"
@click="submit"
>保存</ElButton
></template
>
</ElDialog>
</template>
<script setup lang="ts">
import { ref, reactive, onMounted, watch, onBeforeUnmount } from 'vue'
import { ElMessageBox } from 'element-plus'
import { PackageSilenceService as Service } from '@/api/modules/packageSilence'
import BusinessEntitySelect from '@/components/business/BusinessEntitySelect.vue'
import {
silenceData,
silenceDate,
silenceFen,
silenceId,
validateSilencePolicy
} from '@/utils/business/silenceExtension'
import { gatewayError, requireGatewayAccess } from '@/utils/business/gateway'
import { pollingScope } from '@/utils/business/pollingScheduler'
import { useUserStore } from '@/store/modules/user'
import type { SilencePolicyRequest } from '@/types/api/packageSilenceContract'
const props = defineProps<{ id?: number; allowed: boolean }>(),
emit = defineEmits<{ close: []; saved: [] }>(),
user = useUserStore()
const form = reactive({
name: '',
scope_type: 'all',
scope_ids: [] as (number | string)[],
free_extension_limit: 0,
paid_max_months: 0,
single_max_months: 1,
effective_from: '',
effective_to: '' as string | null,
approval_exempt_types: [] as string[],
status: 1,
remark: ''
})
const unitYuan = ref('0.00'),
singleLimited = ref(false),
loading = ref(false),
saving = ref(false),
loaded = ref(!props.id),
error = ref('')
let active = true,
version = 0
watch(
() => [props.id, props.allowed, pollingScope(user)],
() => {
version++
loaded.value = false
error.value = ''
emit('close')
},
{ flush: 'sync' }
)
onMounted(async () => {
if (!props.id || !props.allowed) return
const current = version
loading.value = true
try {
const value = silenceData(await Service.policy(silenceId(props.id)))
if (!active || current !== version || !props.allowed) return
Object.assign(form, {
name: value.name || '',
scope_type: value.scope_type || 'all',
scope_ids: value.scope_ids || [],
free_extension_limit: value.free_extension_limit ?? 0,
paid_max_months: value.paid_max_months ?? 0,
single_max_months: value.single_max_months ?? 1,
effective_from: value.effective_from || '',
effective_to: value.effective_to || null,
approval_exempt_types: value.approval_exempt_types || [],
status: value.status ?? 1,
remark: value.remark || ''
})
unitYuan.value = ((value.unit_price ?? 0) / 100).toFixed(2)
singleLimited.value = value.single_max_months != null
loaded.value = true
} catch (e) {
if (current === version) error.value = gatewayError(e)
} finally {
if (current === version) loading.value = false
}
})
async function submit() {
if (saving.value) return
saving.value = true
const current = version,
scope = pollingScope(user)
try {
requireGatewayAccess(props.allowed && loaded.value)
const body: SilencePolicyRequest = {
...form,
name: form.name.trim(),
scope_ids:
form.scope_type === 'all' ? [] : form.scope_ids.map((value) => silenceId(Number(value))),
unit_price: silenceFen(unitYuan.value),
single_max_months: singleLimited.value ? form.single_max_months : null,
effective_from: silenceDate(form.effective_from),
effective_to: form.effective_to ? silenceDate(form.effective_to) : null,
approval_exempt_types: [...form.approval_exempt_types]
}
validateSilencePolicy(body)
await ElMessageBox.confirm(
'确认保存延期政策?历史订单和已提交审批不受此次修改影响。',
'保存确认',
{ type: 'warning' }
)
requireGatewayAccess(
active && current === version && scope === pollingScope(user) && props.allowed
)
if (props.id) silenceData(await Service.updatePolicy(props.id, { ...body, id: props.id }))
else silenceData(await Service.createPolicy(body))
if (active && current === version && props.allowed) {
emit('saved')
emit('close')
}
} catch (e) {
if (active && current === version && e !== 'cancel' && e !== 'close')
error.value = gatewayError(e)
} finally {
saving.value = false
}
}
onBeforeUnmount(() => {
active = false
version++
})
</script>
<style scoped>
.el-alert {
margin-bottom: 16px;
}
p {
margin: 0;
line-height: 1.5;
}
@media (width <= 640px) {
:deep(.el-form-item) {
flex-wrap: wrap;
}
:deep(.el-form-item__label) {
justify-content: flex-start;
width: 100% !important;
}
:deep(.el-form-item__content) {
flex-basis: 100%;
margin-left: 0 !important;
}
}
</style>