<template>
<a-modal
v-if="visible"
:title="title" // 标题
:visible="visible" // 对话框是否可见
@ok="handleOk" //点击确定回调
@cancel="handleCancel" //点击遮罩层或右上角叉或取消按钮的回调
:width="800"
:height="900"
:destoryOnClose="true" //关闭时销毁 Modal 里的子元素
:bodyStyle="{ padding: '0px 10px', minHeight: '500px' }" //Modal body 样式
>
<a-tabs default-active-key="1">
<a-tab-pane key="1" tab="基本信息" style="padding-left: 10px"> //基本信息tab
<a-row>
<a-form-model
ref="createBatchInfoRef" //表单DOM实例
:model="form" //表单数据对象
:labelCol="{ span: 10 }" //label 标签布局
:wrapperCol="{ span: 14 }"
:rules="rules" // 表单验证规则
> // Form 组件提供了表单验证的功能,只需要通过 rules 属性传入约定的验证规则,并将 FormItem 的 prop 属性设置为需校验的字段名即可。
<a-row :gutter="16">
<a-col :span="12">
<a-form-model-item label="种类" prop="frameName">
<a-input disabled v-model="form.frameName"></a-input>
</a-form-model-item>
</a-col>
<a-col :span="12">
<a-form-model-item label="图标" prop="icon">
<a-select v-model="form.icon">
<a-select-option
:value="item"
:key="index"
v-for="(item, index) in schemaIcons"
>
<a-icon :type="item" style="font-size: 16px" />
</a-select-option>
</a-select>
</a-form-model-item>
</a-col>
<a-col :span="12">
<a-form-model-item label="编码" prop="prefix">
<a-input v-model="form.prefix"></a-input>
</a-form-model-item>
</a-col>
<a-col :span="12">
<a-form-model-item label="元件名称" prop="name">
<a-input v-model="form.name"></a-input>
</a-form-model-item>
</a-col>
<!--如果stockFrameInfo.id===3,也就是容器才触发-->
<a-col :span="12" v-if="CONTAINER_TYPE === stockFrameInfo.id">
<a-form-model-item label="单位" prop="unitId">
<a-select
@dropdownVisibleChange="dropdownVisibleChange" //展开下拉菜单的回调
v-model="form.unitId"
placeholder="请选择"
>
<a-select-option
:key="item.id"
:value="item.id"
v-for="item in unitOptions" //拿到数据库中的预选单位
>
{{ item.unitName }}
</a-select-option>
<template v-slot:notFoundContent v-if="spinning">
<a-spin :spinning="spinning"></a-spin>
</template>
</a-select>
</a-form-model-item>
</a-col>
<a-col :span="12" v-if="CONTAINER_TYPE === stockFrameInfo.id">
<a-form-model-item label="容量" prop="capacity">
<a-input v-model="form.capacity"></a-input>
</a-form-model-item>
</a-col>
<a-col
:key="item.id"
:span="12"
v-for="item in stockFrameAttribute" //根据库位种类id获取的库位种类动态字段信息
>
<a-form-model-item :label="item.paramName" :prop="item.id">
// 根据数据类型进行不同的操作
<template
v-if="
paramTypeColumn.text === item.paramType || // 10
paramTypeColumn.integer === item.paramType || // 30
paramTypeColumn.float === item.paramType // 31
"
>
<a-input
v-if="item.isMultipleValue === 2"
v-model="form[item.id][0]"
></a-input>
<a-select
v-else
v-model="form[item.id]"
:dropdownStyle="{ display: 'none' }"
:dropdownMenuStyle="{ display: 'none' }"
mode="tags"
></a-select>
</template>
<template
v-else-if="paramTypeColumn.textArea === item.paramType"
>
<a-input
:auto-size="{ minRows: 3, maxRows: 5 }"
class="default-input"
v-model="form[item.id][0]"
type="textarea"
></a-input>
</template>
<template
v-else-if="paramTypeColumn.radioSelect === item.paramType"
>
<a-select v-model="form[item.id][0]">
<a-select-option
:key="element"
v-for="element in item.preDefinedValues"
>
{{ element }}
</a-select-option>
</a-select>
</template>
<template
v-else-if="paramTypeColumn.multiSelect === item.paramType"
>
<a-select mode="multiple" v-model="form[item.id]">
<a-select-option
:key="element"
v-for="element in item.preDefinedValues"
>
{{ element }}
</a-select-option>
</a-select>
</template>
<template v-else-if="paramTypeColumn.time === item.paramType">
<a-date-picker
v-model="item.paramValues[0]"
:showTime="true"
></a-date-picker>
</template>
<template v-else-if="paramTypeColumn.link === item.paramType">
<span
class="link-type"
:key="ele.referenceId"
v-for="ele in item.paramValues"
@click="showProductLinkDetail(ele)"
><a-icon type="link" /> {{ ele.referenceName }}</span
>
</template>
<template
v-else-if="
paramTypeColumn.container === item.paramType &&
(CONTAINER_TYPE_HE | CONTAINER_TYPE_BAN) ===
stockFrameInfo.id
"
>
<a-select
@dropdownVisibleChange="dropdownVisibleChange2"
v-model="form[item.id][0]"
>
// 要加key、value才行
<a-select-option
:key="element.id"
v-for="element in ContainerOptions"
:value="element.id"
>
{{ element.name }}
</a-select-option>
</a-select>
</template>
</a-form-model-item>
</a-col>
</a-row>
</a-form-model>
</a-row>
<span>元件字段:</span>
<custom-fields
@getData="getCustomFieldsData"
ref="customFieldsRef"
></custom-fields>
<span>库位字段:</span>
<storage-fields
ref="storageFieldsRef"
@getData="getStorageLocationFieldsData"
:showMode="showMode.productKey"
>
</storage-fields>
</a-tab-pane>
<a-tab-pane
key="2"
tab="规则设置"
style="padding-left: 10px"
></a-tab-pane> //规则设置tab
</a-tabs>
</a-modal>
</template>
<script>
import {
queryStockFrameFields,
saveStockSchema,
queryStockSchemasDetail
} from '@/api/material/locationSetting' // 引入API接口
import { getFullUnitInfo } from '@/api/system/unit'
import {
handleValidateCodePlus,
validateInteger,
validateFloat,
validateFloatNumber,
validateArrRequired
} from '@/common/inspectFunction' // 引入规则校验函数
import { paramTypeColumn, schemaIcons, VOLUME_TYPE_UNIT } from '@/common/global'
import CustomFields from '@/views/materialcenter/productManagement/components/ProductCustomFields'
import { CUSTOM_FIELD_SHOW_MODE } from '@/views/materialcenter/productManagement/components/comm/constValues'
const CONTAINER_TYPE = '3'
const CONTAINER_TYPE_HE = '2'
const CONTAINER_TYPE_BAN = '4'
export default {
components: {
CustomFields,
StorageFields: CustomFields,
},
props: {
leftSuccess: Function,
schemaRightSuccess: Function,
},
data() {
return {
schemaIcons: Object.freeze(schemaIcons),
source: '',
stockFrameInfo: {},
visible: false,
title: '新建元件',
paramTypeColumn: Object.freeze(paramTypeColumn), //参数类型
baseInfo: {
frameName: '',
icon: '',
prefix: '',
name: '',
unitId: undefined,
capacity: undefined,
},
form: {
frameName: '',
icon: '',
prefix: '',
name: '',
unitId: undefined,
capacity: undefined,
},
rules: {
prefix: [
{
min: 1,
max: 64,
required: true,
message: '编码的长度在1~64位之间',
trigger: 'change',
},
{
validator: this.handleValidateCodePlus,
message: '编号格式不正确',
trigger: 'change',
},
],
name: [
{
min: 1,
max: 32,
required: true,
message: '元件名称的长度在1~32位之间',
trigger: 'change',
},
],
capacity: [
{
validator: this.validateFloatNumber,
},
{
required: true,
message: '容量为必填',
trigger: 'blur',
},
],
unitId: [
{
required: true,
message: '请填写单位',
trigger: 'blur',
},
],
},
stockFrameAttribute: [], //库位种类属性
// 后端需要的paramType
paramTypeObj: {},
showMode: Object.freeze(CUSTOM_FIELD_SHOW_MODE),
customFieldsData: [],
storageFieldData: [],
unitOptions: [], // 单位选项
CONTAINER_TYPE,
spinning: true,
CONTAINER_TYPE_HE,
CONTAINER_TYPE_BAN,
ContainerOptions: [] //容器选项
}
},
methods: {
dropdownVisibleChange(open) {
// this.spinning = true
open && _.isEmpty(this.unitOptions) && this.getFullUnitInfo()
},
/**
* 获取数据库预选单位
*/
async getFullUnitInfo() {
this.spinning = true
const res = await getFullUnitInfo({
artifacts: [VOLUME_TYPE_UNIT],
pageSize: -1,
}).finally(() => {
this.spinning = false
})
if (res.code === '200') {
this.unitOptions = res.data
}
},
dropdownVisibleChange2(open) {
open && _.isEmpty(this.ContainerOptions) && this.getFullContainerInfo()
},
/**
* 获取数据库容器
*/
async getFullContainerInfo() {
this.spinning = true
const res = await queryStockSchemasDetail({
stockFrameId: '3',
pageSize: -1
}).finally(() => {
this.spinning = false
})
if (res.code === '200') {
this.ContainerOptions = res.data.result
this.ContainerOptions.forEach(item => {
this.$set(this.form, item.id, item.name)
})
}
},
/**
* 自定义编码规则
*/
handleValidateCodePlus,
/**
* 整数自定义验证
*/
validateInteger,
/**
* 浮点数自定义验证
*/
validateFloat,
validateFloatNumber,
/**
* 后端给的值是数组
* 自定义必填
*/
validateArrRequired,
/**
* 动态表单校验
*/
dynamicValidate(element) {
// 必填项
if (element.isRequired === 1) {
const requiredInfo = [
{ validator: this.validateArrRequired, trigger: 'change' },
{ required: true, message: '必填', trigger: 'change' }
]
!this.rules[element.id] && this.$set(this.rules, element.id, requiredInfo)
if (this.rules[element.id] && this.rules[element.id].findIndex(item => item.required) < 0) {
this.rules[element.id].push(...requiredInfo)
}
// this.rules[element.id] = [{ required: true, message: '必填', trigger: 'change' }]
}
// 整数
if (element.paramType === this.paramTypeColumn.integer) {
!this.rules[element.id] &&
this.$set(this.rules, element.id, [
{ validator: this.validateInteger },
])
this.rules[element.id] &&
this.rules[element.id].push({
validator: this.validateInteger,
})
}
// 浮点数
if (element.paramType === this.paramTypeColumn.float) {
!this.rules[element.id] &&
this.$set(this.rules, element.id, [{ validator: this.validateFloat }])
this.rules[element.id] &&
this.rules[element.id].push({
validator: this.validateFloat,
})
}
},
/**
* 获取自定义字段数据
*/
getCustomFieldsData(data) {
this.customFieldsData = data
},
/**
* 获取元件为库位设置的字段信息
*/
getStorageLocationFieldsData(data) {
this.storageFieldData = data
},
/**
* 保存库位元件
*/
async saveStockSchema(data) {
const res = await saveStockSchema(data)
if (res.code === '200') {
this.judgeCallback()
this.handleCancel()
}
},
/**
* 判断需要执行的回调
*/
judgeCallback() {
switch (this.source) {
case 'storageComponentLeft':
this.leftSuccess(this.stockFrameInfo)
break
case 'storageSchemaListRight':
this.schemaRightSuccess()
break
default:
break
}
},
/**
* 获取自定义字段和设置字段的数据
*/
getCustomFieldsDataAndSettingData() {
this.$refs.customFieldsRef.getData()
this.$refs.storageFieldsRef.getData()
},
/**
* 获取种类字段数据
*/
getFrameParams() {
let targetArr = []
const target = _.cloneDeep(this.form)
Object.keys(this.baseInfo).forEach((key) => {
this.baseInfo[key] = target[key]
delete target[key]
})
Object.keys(target).forEach((key) => {
targetArr.push({
id: key,
paramValues: target[key],
paramType: this.paramTypeObj[key],
})
})
return targetArr
},
/**
* TODO
* 模态框确定
*/
handleOk() {
this.$refs.createBatchInfoRef.validate((valid) => {
if (valid) {
this.getCustomFieldsDataAndSettingData()
const frameParams = this.getFrameParams()
this.saveStockSchema({
...this.baseInfo,
frameParams: frameParams,
customParams: {
params: this.customFieldsData,
},
stockParams: {
params: this.storageFieldData,
},
stockFrameId: this.stockFrameInfo.id,
})
}
})
},
/**
* 模态框取消
*/
handleCancel() {
this.visible = false
this.$refs.createBatchInfoRef.resetFields()
this.form = {
frameName: '',
icon: '',
prefix: '',
name: '',
unitId: undefined,
capacity: undefined,
}
this.stockFrameAttribute = []
},
/**
* 根据库位种类id获取库位种类属性信息
*/
async queryStockFrameFields(data) {
const res = await queryStockFrameFields(data) //根据库位种类id获取库位种类动态字段信息
if (res.code === '200') {
this.stockFrameAttribute = res.data //存到这里
this.stockFrameAttribute.forEach((element) => {
this.paramTypeObj[element.id] = element.paramType //取出paramType存到paramTypeObj对象;
this.$set(this.form, element.id, element.paramValues || []) //向form中添加表单动态字段以便在template中显示
// 校验规则
this.dynamicValidate(element)
})
}
},
/**
* 打开模态框
*/
openCreateStockShemaModal(data, source) {
this.source = source || ''
this.visible = true
this.stockFrameInfo = data
this.form.frameName = this.stockFrameInfo.name // 位置、盒、容器、板
this.queryStockFrameFields({ id: this.stockFrameInfo.id }) //id 对应1、2、3、4
},
},
watch: {},
}
</script>
<style lang="scss" scoped>
</style>
const paramTypeColumn = {
text: 10,
textArea: 11,
radioSelect: 20,
multiSelect: 21,
integer: 30,
float: 31,
time: 40,
link: 50,
frame: 51,
entity: 55,
}
添加一个动态表单container:
<template v-else-if="paramTypeColumn.container === item.paramType">
<a-select v-model="form[item.id][0]">
<a-select-option :key="element" v-for="element in item.preDefinedValues">
{{ element }}
</a-select-option>
</a-select>
</template>