WEB/src/views/storage/stockcheck/pages/CheckPatternDialog.vue
liufumingren 4dc90ac539 1
2026-10-01 09:07:35 +08:00

276 lines
8.3 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.

<!--
CheckPatternDialog — 快速创建盘点任务(stock.check.patternList + stock.check.patternCreate)
与 CheckFormDialog(stock.check.create)的区别:
- 无需手选盘点范围:直接选「盘点定式」(后端代码注册的 5 个内置定式),
由后端按定式收集盘点对象(goods 型 → 商品行明细 / location 型 → 库位行明细)
- 无需传入盘点日期(check_date 缺省今天,接口可不传)
- 创建后主表状态直接为 submitted(待审核),明细进度 none_checked,不需要再手动加明细
入参:pattern_id(必填,兼容 patternId 驼峰)+ title/remark(均可选,title 缺省取定式 name)
不传 check_date(缺省今天,弹窗不提供日期选择),也不传 mode(由后端按定式决定)。
warehouse_id 与创建人由服务端从登录上下文注入,前端不传。
-->
<template>
<el-dialog
:model-value="visible"
title="快速创建盘点单"
width="min(560px, 94vw)"
align-center
destroy-on-close
:close-on-click-modal="false"
@update:model-value="handleVisibleChange"
@closed="handleClosed"
>
<el-form
ref="formRef"
:model="form"
:rules="rules"
label-width="90px"
size="small"
@submit.prevent
>
<el-form-item label="盘点定式" prop="pattern_id">
<el-select
v-model="form.pattern_id"
:loading="patternLoading"
filterable
clearable
placeholder="输入定式名称筛选,如:临期盘点"
style="width: 100%"
>
<el-option
v-for="opt in patternOptions"
:key="opt.value"
:label="opt.label"
:value="opt.value"
/>
</el-select>
<div class="type-tip">
{{ selectedPattern
? (selectedPattern.description || '按该定式自动收集盘点对象,创建后进入待审核。')
: '选择定式后由后端自动收集盘点对象(无需手动添加明细)。' }}
</div>
</el-form-item>
<el-form-item label="盘点名称" prop="title">
<el-input
v-model="form.title"
maxlength="50"
show-word-limit
:placeholder="titlePlaceholder"
/>
</el-form-item>
<el-form-item label="备注" prop="remark">
<el-input v-model="form.remark" type="textarea" :rows="3" maxlength="200" show-word-limit placeholder="可留空" />
</el-form-item>
<el-alert
type="info"
:closable="false"
show-icon
:title="createAlertText"
/>
</el-form>
<template #footer>
<el-button size="small" @click="handleVisibleChange(false)">取消</el-button>
<el-button
size="small"
type="primary"
:loading="submitting"
:disabled="!form.pattern_id"
@click="handleSubmit"
>
确认创建
</el-button>
</template>
</el-dialog>
</template>
<script setup lang="ts">
import { computed, reactive, ref, watch } from 'vue'
import { ElMessage } from 'element-plus'
import type { FormInstance, FormRules } from 'element-plus'
import { isCheckApiSuccess } from '../config/template-config'
import {
stockCheckPatternCreate,
stockCheckPatternList,
type StockCheckPattern
} from '@/api/stock.check/stock.check'
defineOptions({ name: 'CheckPatternDialog' })
const props = defineProps<{ visible: boolean }>()
const emit = defineEmits<{
(e: 'update:visible', value: boolean): void
/** 创建成功(携创建出的盘点单关键信息;type 用于决定后续添加「商品」还是「库位」) */
(e: 'success', task: { id: number; check_no: string; title: string; type: string }): void
}>()
const formRef = ref<FormInstance>()
const submitting = ref(false)
// ===== 定式清单(只拉一次,弹窗间复用) =====
const patterns = ref<StockCheckPattern[]>([])
const patternLoading = ref(false)
let patternLoaded = false
const patternOptions = computed(() =>
patterns.value.map((p) => ({
value: String(p.id ?? ''),
label: String(p.name ?? p.id ?? ''),
description: String(p.description ?? '')
}))
)
const selectedPattern = computed<StockCheckPattern | undefined>(() =>
patterns.value.find((p) => String(p.id ?? '') === form.pattern_id)
)
const titlePlaceholder = computed(() =>
selectedPattern.value?.name ? `留空默认取定式名称:${selectedPattern.value.name}` : '留空默认取定式名称'
)
/** 创建后由后端自动收集盘点对象并进入待审核(与 create 的「空盘点单」不同) */
const isLocationPattern = computed(() => String(selectedPattern.value?.type ?? '') === 'location')
const createAlertText = computed(() =>
isLocationPattern.value
? '创建后按定式自动收集「库位」盘点对象,盘点单直接进入待审核;无符合条件对象时后端会提示失败。'
: '创建后按定式自动收集「商品」盘点对象,盘点单直接进入待审核;无符合条件对象时后端会提示失败。'
)
const form = reactive<{
pattern_id: string
title: string
remark: string
}>({
pattern_id: '',
title: '',
remark: ''
})
const rules: FormRules = {
pattern_id: [{ required: true, message: '请选择盘点定式', trigger: 'change' }]
}
function handleVisibleChange(value: boolean) {
emit('update:visible', value)
}
function handleClosed() {
formRef.value?.clearValidate()
submitting.value = false
}
function resetForm() {
form.pattern_id = ''
form.title = ''
form.remark = ''
}
/** 拉取定式清单,兼容 {patterns}/{list}/{data:[...]} 等回包形态 */
async function loadPatterns() {
if (patternLoaded || patternLoading.value) return
patternLoading.value = true
try {
const res: any = await stockCheckPatternList()
if (!isCheckApiSuccess(res)) return
const raw: any =
res?.patterns ??
res?.list ??
res?.data?.patterns ??
res?.data?.list ??
(Array.isArray(res?.data) ? res.data : [])
patterns.value = Array.isArray(raw)
? raw
.map((p: any) => ({
...p,
id: String(p?.id ?? p?.pattern_id ?? ''),
name: String(p?.name ?? p?.title ?? p?.id ?? ''),
description: String(p?.description ?? p?.desc ?? '')
}))
.filter((p: StockCheckPattern) => Boolean(p.id))
: []
} catch (e) {
console.error('[stockcheck] 加载盘点定式失败', e)
} finally {
patternLoading.value = false
patternLoaded = true
}
}
watch(
() => props.visible,
(visible) => {
if (!visible) return
resetForm()
formRef.value?.clearValidate()
void loadPatterns()
}
)
/** 定式 type → 盘点范围 type(goods=商品行 / location=库位行) */
function resolveTaskType(payload: any): string {
const fromResponse = String(payload?.type ?? '')
if (fromResponse) return fromResponse
return isLocationPattern.value ? 'location' : 'goods'
}
async function handleSubmit() {
if (!formRef.value) return
try {
await formRef.value.validate()
} catch {
return
}
submitting.value = true
try {
const res: any = await stockCheckPatternCreate({
pattern_id: form.pattern_id,
title: form.title.trim() || undefined,
remark: form.remark.trim() || undefined
})
if (!isCheckApiSuccess(res)) {
throw new Error(res?.message || '快速创建盘点单失败')
}
const data: any = res?.data && (res.data.id || res.data.check_no || res.data.check) ? res.data : res
const payload: any = data?.check ?? data
const task = {
id: Number(payload?.id) || 0,
check_no: String(payload?.check_no ?? ''),
title: String(payload?.title ?? form.title ?? selectedPattern.value?.name ?? ''),
type: resolveTaskType(payload)
}
ElMessage.success(task.check_no ? `盘点单 ${task.check_no} 创建成功` : '盘点单创建成功')
emit('update:visible', false)
emit('success', task)
} catch (e: any) {
console.error('[stockcheck] 快速创建盘点单失败', e)
ElMessage.error(e?.message || '快速创建盘点单失败')
} finally {
submitting.value = false
}
}
</script>
<style lang="scss" scoped>
.form-tip {
margin-left: 10px;
font-size: 12px;
color: #e6a23c;
}
.type-tip {
width: 100%;
margin-top: 2px;
font-size: 12px;
line-height: 1.5;
color: #909399;
}
</style>