feat(mes): 添加编码规则分段详情查询 API

pull/871/MERGE
YunaiV 2026-03-06 00:09:34 +08:00
parent c12d7616f2
commit 5960e0102f
6 changed files with 771 additions and 0 deletions

View File

@ -0,0 +1,98 @@
import request from '@/config/axios'
// MES 条码清单 VO
// TODO @AI拆分成 index.ts和 config/index.ts
// TODO @AIWM 前缀,类似别的模块,要添加下;
export interface BarcodeVO {
id: number
configId: number
format: number
bizType: number
content: string
bizId: number
bizCode: string
bizName: string
status: number
remark: string
createTime: string
}
// MES 条码配置 VO
// TODO @AIWM 前缀,类似别的模块,要添加下;
export interface BarcodeConfigVO {
id: number
format: number
bizType: number
contentFormat: string
contentExample: string
autoGenerateFlag: boolean
defaultTemplate: string
status: number
remark: string
createTime: string
}
// MES 条码 API
// TODO @AIWM 前缀,类似别的模块,要添加下;
export const BarcodeApi = {
// 查询条码分页
getBarcodePage: async (params: any) => {
return await request.get({ url: '/mes/wm/barcode/page', params })
},
// 查询条码详情
getBarcode: async (id: number) => {
return await request.get({ url: '/mes/wm/barcode/get?id=' + id })
},
// 根据业务对象获取条码
getBarcodeByBusiness: async (bizType: number, bizId: number) => {
return await request.get({
url: '/mes/wm/barcode/get-by-business',
params: { bizType, bizId }
})
},
// 新增条码
createBarcode: async (data: BarcodeVO) => {
return await request.post({ url: '/mes/wm/barcode/create', data })
},
// 修改条码
updateBarcode: async (data: BarcodeVO) => {
return await request.put({ url: '/mes/wm/barcode/update', data })
},
// 删除条码
deleteBarcode: async (id: number) => {
return await request.delete({ url: '/mes/wm/barcode/delete?id=' + id })
}
}
// MES 条码配置 API
export const BarcodeConfigApi = {
// 查询条码配置分页
getBarcodeConfigPage: async (params: any) => {
return await request.get({ url: '/mes/wm/barcode-config/page', params })
},
// 查询条码配置详情
getBarcodeConfig: async (id: number) => {
return await request.get({ url: '/mes/wm/barcode-config/get?id=' + id })
},
// 新增条码配置
createBarcodeConfig: async (data: BarcodeConfigVO) => {
return await request.post({ url: '/mes/wm/barcode-config/create', data })
},
// 修改条码配置
updateBarcodeConfig: async (data: BarcodeConfigVO) => {
return await request.put({ url: '/mes/wm/barcode-config/update', data })
},
// 删除条码配置
deleteBarcodeConfig: async (id: number) => {
return await request.delete({ url: '/mes/wm/barcode-config/delete?id=' + id })
}
}

View File

@ -0,0 +1,122 @@
<script lang="ts" setup>
import { computed, nextTick, ref, unref, watch } from 'vue'
import QRCode, { QRCodeRenderersOptions } from 'qrcode'
import JsBarcode from 'jsbarcode'
import { propTypes } from '@/utils/propTypes'
import { useDesign } from '@/hooks/web/useDesign'
import { BarcodeFormatEnum, BARCODE_FORMAT_MAP } from '@/views/mes/wm/barcode/constants/BarcodeConstants'
defineOptions({ name: 'Barcode' })
const props = defineProps({
content: propTypes.string.def(''), //
format: propTypes.number.def(BarcodeFormatEnum.QR_CODE), // : 1=QR_CODE, 2=EAN13, 3=CODE39, 4=UPC_ATODO @AI @ ok
width: propTypes.number.def(200), //
height: propTypes.number.def(100), // 使
displayValue: propTypes.bool.def(true) //
})
const emit = defineEmits(['done'])
const { getPrefixCls } = useDesign()
const prefixCls = getPrefixCls('barcode')
const loading = ref(true)
const canvasRef = ref<Nullable<HTMLCanvasElement>>(null)
const imgRef = ref<Nullable<HTMLImageElement>>(null)
const isQRCode = computed(() => props.format === BarcodeFormatEnum.QR_CODE) //
const wrapStyle = computed(() => {
if (isQRCode.value) {
return {
width: props.width + 'px',
height: props.width + 'px'
}
}
return {
width: props.width + 'px'
}
})
const generateBarcode = async () => {
if (!props.content) {
loading.value = false
return
}
await nextTick()
loading.value = true
try {
if (isQRCode.value) {
//
const options: QRCodeRenderersOptions = {
errorCorrectionLevel: 'M',
width: props.width
}
await QRCode.toCanvas(unref(canvasRef) as HTMLCanvasElement, props.content, options)
emit('done', (unref(canvasRef) as HTMLCanvasElement).toDataURL())
} else {
//
const format = BARCODE_FORMAT_MAP[props.format] || 'CODE39'
JsBarcode(unref(imgRef) as HTMLImageElement, props.content, {
format: format,
width: 2,
height: props.height,
displayValue: props.displayValue,
margin: 10
})
emit('done', (unref(imgRef) as HTMLImageElement).src)
}
} catch (error) {
console.error('生成条码失败:', error)
} finally {
loading.value = false
}
}
watch(
() => [props.content, props.format],
() => {
if (props.content) {
generateBarcode()
}
},
{
immediate: true
}
)
/** 获取条码 Base64 数据 */
const getImageBase64 = (): string => {
if (isQRCode.value) {
return (unref(canvasRef) as HTMLCanvasElement)?.toDataURL() || ''
} else {
return (unref(imgRef) as HTMLImageElement)?.src || ''
}
}
// TODO @AI scss 使 unocss
defineExpose({
getImageBase64
})
</script>
<template>
<div v-loading="loading" :class="[prefixCls, 'inline-block']" :style="wrapStyle">
<canvas v-if="isQRCode" ref="canvasRef" ></canvas>
<img v-else ref="imgRef" alt="barcode" />
</div>
</template>
<style scoped lang="scss">
$prefix-cls: #{$namespace}-barcode;
.#{$prefix-cls} {
canvas,
img {
display: block;
max-width: 100%;
}
}
</style>

View File

@ -0,0 +1,181 @@
<script setup lang="ts">
import { computed, onMounted, ref, watch } from 'vue'
import { useMessage } from '@/hooks/web/useMessage'
import { propTypes } from '@/utils/propTypes'
import Barcode from './Barcode.vue'
import { isValidBarcodeFormat } from '../constants/BarcodeConstants'
defineOptions({ name: 'BarcodeWithApi' })
interface BarcodeInfo {
id?: number
format: number //
content: string //
bizType?: number
bizCode?: string
bizName?: string
status?: number
}
const props = defineProps({
businessType: propTypes.string.isRequired, //
businessId: propTypes.number.isRequired, // ID
width: propTypes.number.def(200), //
height: propTypes.number.def(100), //
autoLoad: propTypes.bool.def(true) //
})
const emit = defineEmits<{
loaded: [data: BarcodeInfo] //
error: [error: Error] //
}>()
const message = useMessage()
const barcodeRef = ref()
const barcodeData = ref<BarcodeInfo | null>(null)
const loading = ref(false)
const error = ref<string | null>(null)
//
const isLoaded = computed(() => !!barcodeData.value)
/**
* 加载条码数据
*/
const loadBarcode = async () => {
if (!props.businessType || !props.businessId) {
error.value = '业务类型或业务 ID 不能为空'
return
}
loading.value = true
error.value = null
try {
// TODO: API
// const response = await getBarcodeByBusinessApi({
// type: props.businessType,
// businessId: props.businessId
// })
//
const response: BarcodeInfo = {
format: 1,
content: `${props.businessType}-${props.businessId}`,
bizType: 101,
bizCode: `CODE-${props.businessId}`,
bizName: `业务-${props.businessId}`,
status: 0
}
if (!response || !isValidBarcodeFormat(response.format)) {
throw new Error('条码数据无效')
}
barcodeData.value = response
emit('loaded', response)
} catch (err) {
const errorMsg = err instanceof Error ? err.message : '加载条码失败'
error.value = errorMsg
console.error('加载条码失败:', err)
emit('error', err instanceof Error ? err : new Error(errorMsg))
message.error(errorMsg)
} finally {
loading.value = false
}
}
/**
* 重新加载条码
*/
const reload = async () => {
await loadBarcode()
}
/**
* 获取条码 Base64
*/
const getImageBase64 = (): string => {
return barcodeRef.value?.getImageBase64?.() || ''
}
// ID
watch(
() => props.businessId,
() => {
if (props.autoLoad) {
loadBarcode()
}
}
)
//
onMounted(() => {
if (props.autoLoad) {
loadBarcode()
}
})
//
defineExpose({
loadBarcode,
reload,
getImageBase64,
isLoaded
})
</script>
<template>
<div class="barcode-with-api">
<!-- 加载中 -->
<el-skeleton v-if="loading" :rows="3" animated />
<!-- 错误状态 -->
<el-alert
v-else-if="error"
:title="error"
type="error"
:closable="false"
class="mb-12px"
/>
<!-- 条码显示 -->
<div v-else-if="isLoaded && barcodeData" class="barcode-content">
<Barcode
ref="barcodeRef"
:content="barcodeData.content"
:format="barcodeData.format"
:width="width"
:height="height"
/>
</div>
<!-- 空状态 -->
<el-empty v-else description="暂无条码数据" />
<!-- 操作按钮 -->
<div v-if="isLoaded" class="barcode-actions mt-12px">
<el-button size="small" @click="reload">
<Icon icon="ep:refresh" class="mr-4px" /> 刷新
</el-button>
</div>
</div>
</template>
<style scoped lang="scss">
.barcode-with-api {
.barcode-content {
display: flex;
justify-content: center;
padding: 12px;
background-color: #f5f7fa;
border-radius: 4px;
}
.barcode-actions {
display: flex;
justify-content: center;
gap: 8px;
}
}
</style>

View File

@ -0,0 +1,2 @@
export { default as Barcode } from './Barcode.vue'
export { default as BarcodeWithApi } from './BarcodeWithApi.vue'

View File

@ -0,0 +1,206 @@
<!-- TODO @AI /Users/yunai/Java/yudao-all-in-one/yudao-ui-admin-vue3/src/views/mes/wm/barcode/BarcodeConfig.vue
/Users/yunai/Java/yudao-all-in-one/yudao-ui-admin-vue3/src/views/mes/wm/barcode/BarcodeConfigForm.vue /Users/yunai/Java/yudao-all-in-one/yudao-ui-admin-vue3/src/views/mes/wm/barcode/config index.vue -->
<template>
<ContentWrap>
<!-- 搜索工作栏 -->
<el-form
class="-mb-15px"
:model="queryParams"
ref="queryFormRef"
:inline="true"
label-width="68px"
>
<el-form-item label="条码格式" prop="format">
<el-select
v-model="queryParams.format"
placeholder="请选择条码格式"
clearable
class="!w-240px"
>
<el-option
v-for="dict in getIntDictOptions(DICT_TYPE.MES_BARCODE_FORMAT)"
:key="dict.value"
:label="dict.label"
:value="dict.value"
/>
</el-select>
</el-form-item>
<el-form-item label="业务类型" prop="bizType">
<el-select
v-model="queryParams.bizType"
placeholder="请选择业务类型"
clearable
class="!w-240px"
>
<el-option
v-for="dict in getIntDictOptions(DICT_TYPE.MES_BARCODE_BIZ_TYPE)"
:key="dict.value"
:label="dict.label"
:value="dict.value"
/>
</el-select>
</el-form-item>
<el-form-item>
<el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
<el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
<el-button
type="primary"
plain
@click="openForm('create')"
v-hasPermi="['mes:wm-barcode-config:create']"
>
<Icon icon="ep:plus" class="mr-5px" /> 新增
</el-button>
</el-form-item>
</el-form>
</ContentWrap>
<!-- 列表 -->
<ContentWrap>
<el-table v-loading="loading" :data="list">
<el-table-column label="编号" align="center" prop="id" />
<el-table-column label="条码格式" align="center" prop="format">
<template #default="scope">
<dict-tag :type="DICT_TYPE.MES_BARCODE_FORMAT" :value="scope.row.format" />
</template>
</el-table-column>
<el-table-column label="业务类型" align="center" prop="bizType">
<template #default="scope">
<dict-tag :type="DICT_TYPE.MES_BARCODE_BIZ_TYPE" :value="scope.row.bizType" />
</template>
</el-table-column>
<el-table-column label="内容格式" align="center" prop="contentFormat" show-overflow-tooltip />
<el-table-column label="内容样例" align="center" prop="contentExample" />
<el-table-column label="自动生成" align="center" prop="autoGenerateFlag">
<template #default="scope">
<el-switch
v-model="scope.row.autoGenerateFlag"
@change="handleAutoGenerateChange(scope.row)"
/>
</template>
</el-table-column>
<el-table-column label="默认打印模板" align="center" prop="defaultTemplate" />
<el-table-column label="状态" align="center" prop="status">
<template #default="scope">
<dict-tag :type="DICT_TYPE.COMMON_STATUS" :value="scope.row.status" />
</template>
</el-table-column>
<el-table-column
label="创建时间"
align="center"
prop="createTime"
:formatter="dateFormatter"
width="180px"
/>
<el-table-column label="操作" align="center" width="150px" fixed="right">
<template #default="scope">
<el-button
link
type="primary"
@click="openForm('update', scope.row.id)"
v-hasPermi="['mes:wm-barcode-config:update']"
>
编辑
</el-button>
<el-button
link
type="danger"
@click="handleDelete(scope.row.id)"
v-hasPermi="['mes:wm-barcode-config:delete']"
>
删除
</el-button>
</template>
</el-table-column>
</el-table>
<!-- 分页 -->
<Pagination
:total="total"
v-model:page="queryParams.pageNo"
v-model:limit="queryParams.pageSize"
@pagination="getList"
/>
</ContentWrap>
<!-- 表单弹窗添加/修改 -->
<BarcodeConfigForm ref="formRef" @success="getList" />
</template>
<script setup lang="ts">
import { getIntDictOptions, DICT_TYPE } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import { BarcodeConfigApi } from '@/api/mes/wm/barcode'
import BarcodeConfigForm from './BarcodeConfigForm.vue'
defineOptions({ name: 'MesWmBarcodeConfig' })
const message = useMessage()
const { t } = useI18n()
const loading = ref(true)
const list = ref([])
const total = ref(0)
const queryParams = reactive({
pageNo: 1,
pageSize: 10,
format: undefined,
bizType: undefined
})
const queryFormRef = ref()
/** 查询列表 */
const getList = async () => {
loading.value = true
try {
const data = await BarcodeConfigApi.getBarcodeConfigPage(queryParams)
list.value = data.list
total.value = data.total
} finally {
loading.value = false
}
}
/** 搜索按钮操作 */
const handleQuery = () => {
queryParams.pageNo = 1
getList()
}
/** 重置按钮操作 */
const resetQuery = () => {
queryFormRef.value.resetFields()
handleQuery()
}
/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
formRef.value.open(type, id)
}
/** 删除按钮操作 */
const handleDelete = async (id: number) => {
try {
await message.delConfirm()
await BarcodeConfigApi.deleteBarcodeConfig(id)
message.success(t('common.delSuccess'))
await getList()
} catch {}
}
/** 自动生成开关变更 */
const handleAutoGenerateChange = async (row: any) => {
const text = row.autoGenerateFlag ? '启用' : '停用'
try {
await message.confirm(`确认要${text}自动生成吗?`)
await BarcodeConfigApi.updateBarcodeConfig(row)
message.success(`${text}成功`)
} catch {
row.autoGenerateFlag = !row.autoGenerateFlag
}
}
onMounted(() => {
getList()
})
</script>

View File

@ -0,0 +1,162 @@
<template>
<Dialog :title="dialogTitle" v-model="dialogVisible">
<el-form
ref="formRef"
:model="formData"
:rules="formRules"
label-width="120px"
v-loading="formLoading"
>
<el-form-item label="条码格式" prop="format">
<el-select v-model="formData.format" placeholder="请选择条码格式" class="!w-240px">
<el-option
v-for="dict in getIntDictOptions(DICT_TYPE.MES_BARCODE_FORMAT)"
:key="dict.value"
:label="dict.label"
:value="dict.value"
/>
</el-select>
</el-form-item>
<el-form-item label="业务类型" prop="bizType">
<el-select
v-model="formData.bizType"
placeholder="请选择业务类型"
class="!w-240px"
:disabled="formType === 'update'"
>
<el-option
v-for="dict in getIntDictOptions(DICT_TYPE.MES_BARCODE_BIZ_TYPE)"
:key="dict.value"
:label="dict.label"
:value="dict.value"
/>
</el-select>
</el-form-item>
<el-form-item label="内容格式模板" prop="contentFormat">
<el-input
v-model="formData.contentFormat"
placeholder="支持{BUSINESSCODE}占位符WH-{BUSINESSCODE}"
class="!w-400px"
/>
</el-form-item>
<el-form-item label="内容样例" prop="contentExample">
<el-input
v-model="formData.contentExample"
placeholder="如WH-WH001"
class="!w-400px"
/>
</el-form-item>
<el-form-item label="自动生成" prop="autoGenerateFlag">
<el-switch v-model="formData.autoGenerateFlag" />
</el-form-item>
<el-form-item label="默认打印模板" prop="defaultTemplate">
<el-input v-model="formData.defaultTemplate" placeholder="请输入默认打印模板" />
</el-form-item>
<el-form-item label="状态" prop="status">
<el-radio-group v-model="formData.status">
<el-radio
v-for="dict in getIntDictOptions(DICT_TYPE.COMMON_STATUS)"
:key="dict.value"
:label="dict.value"
>
{{ dict.label }}
</el-radio>
</el-radio-group>
</el-form-item>
<el-form-item label="备注" prop="remark">
<el-input v-model="formData.remark" type="textarea" placeholder="请输入备注" />
</el-form-item>
</el-form>
<template #footer>
<el-button @click="submitForm" type="primary" :disabled="formLoading"> </el-button>
<el-button @click="dialogVisible = false"> </el-button>
</template>
</Dialog>
</template>
<script setup lang="ts">
import { getIntDictOptions, DICT_TYPE } from '@/utils/dict'
import { BarcodeConfigApi, BarcodeConfigVO } from '@/api/mes/wm/barcode'
defineOptions({ name: 'BarcodeConfigForm' })
const { t } = useI18n()
const message = useMessage()
const dialogVisible = ref(false)
const dialogTitle = ref('')
const formLoading = ref(false)
const formType = ref('')
const formData = ref({
id: undefined,
format: undefined,
bizType: undefined,
contentFormat: '',
contentExample: '',
autoGenerateFlag: true,
defaultTemplate: '',
status: 0,
remark: ''
})
const formRules = reactive({
format: [{ required: true, message: '条码格式不能为空', trigger: 'change' }],
bizType: [{ required: true, message: '业务类型不能为空', trigger: 'change' }],
contentFormat: [{ required: true, message: '内容格式模板不能为空', trigger: 'blur' }],
autoGenerateFlag: [{ required: true, message: '是否自动生成不能为空', trigger: 'blur' }]
})
const formRef = ref()
/** 打开弹窗 */
const open = async (type: string, id?: number) => {
dialogVisible.value = true
dialogTitle.value = type === 'create' ? '新增条码配置' : '修改条码配置'
formType.value = type
resetForm()
if (id) {
formLoading.value = true
try {
formData.value = await BarcodeConfigApi.getBarcodeConfig(id)
} finally {
formLoading.value = false
}
}
}
defineExpose({ open })
/** 提交表单 */
const emit = defineEmits(['success'])
const submitForm = async () => {
await formRef.value.validate()
formLoading.value = true
try {
const data = formData.value as unknown as BarcodeConfigVO
if (formType.value === 'create') {
await BarcodeConfigApi.createBarcodeConfig(data)
message.success(t('common.createSuccess'))
} else {
await BarcodeConfigApi.updateBarcodeConfig(data)
message.success(t('common.updateSuccess'))
}
dialogVisible.value = false
emit('success')
} finally {
formLoading.value = false
}
}
/** 重置表单 */
const resetForm = () => {
formData.value = {
id: undefined,
format: undefined,
bizType: undefined,
contentFormat: '',
contentExample: '',
autoGenerateFlag: true,
defaultTemplate: '',
status: 0,
remark: ''
}
formRef.value?.resetFields()
}
</script>