perf: model-box重构

This commit is contained in:
chenjinsong
2020-07-20 11:43:10 +08:00
parent c271cd0112
commit cee6b74779
2 changed files with 212 additions and 291 deletions

View File

@@ -1,93 +1,86 @@
<template>
<transition name="right-box">
<div class="right-box right-box-model" v-if="rightBox.show" v-clickoutside="clickos" @click="inputHandler">
<!-- begin--顶部按钮-->
<div class="right-box-top-btns">
<button id="model-del" type="button" v-if="currentModel.id != '' && rightBox.isEdit" @click="del" class="nz-btn nz-btn-size-normal nz-btn-size-alien nz-btn-style-light nz-btn-min-width-82">
<span class="right-box-top-btn-icon"><i class="el-icon-delete"></i></span>
<span class="right-box-top-btn-txt">{{$t('overall.delete')}}</span>
</button>
<button v-if="!rightBox.isEdit" id="model-save" type="button" @click="saveOrToEdit" class="nz-btn nz-btn-size-normal nz-btn-size-alien nz-btn-style-light nz-btn-min-width-82">
<span class="right-box-top-btn-icon"><i class="nz-icon nz-icon-edit"></i></span>
<span class="right-box-top-btn-txt">{{$t('overall.edit')}}</span>
</button>
</div>
<!-- end--顶部按钮-->
<!-- begin--标题-->
<div class="right-box-title">{{rightBox.title}}</div>
<!-- end--标题-->
<!-- begin--表单-->
<el-scrollbar class="right-box-form-box">
<el-form class="right-box-form right-box-form-left" :model="currentModel" label-position="right" label-width="120px" :rules="rules" ref="modelForm">
<!--model名称-->
<el-form-item :label='$t("overall.name")' prop="name">
<el-input v-if="rightBox.isEdit" placeholder="" maxlength="64" show-word-limit v-model.trim="currentModel.name" size="small"></el-input>
<div v-if="!rightBox.isEdit" class="right-box-form-content-txt">{{currentModel.name}}</div>
</el-form-item>
<!--设备类型-->
<el-form-item :label="$t('config.model.type')" prop="type">
<el-autocomplete
:fetch-suggestions="typeSuggestion"
v-model.trim="currentModel.type.value"
placeholder=""
size="small"
popper-class="no-style-class"
>
<template slot-scope="{ item }">
<div class="autocomplete-dropdown" @mouseenter="dropdownHoverItem = 'type' + item.id" @mouseleave="dropdownHoverItem = ''">
<span v-if="!item.isEdit">{{item.value}}</span>
<span v-else @click.stop="inputHandler(item)"><el-input size="mini" v-model="editingType" @click.stop></el-input></span>
<div class="dropdown-btns" v-show="dropdownHoverItem == 'type' + item.id">
<span class="dropdown-btn dropdown-btn-delete" :id="'model-type-op-del-' + item.id" @click.stop="delType(item.id)"><i class="el-icon-delete"></i></span>
<span v-if="!item.isEdit" class="dropdown-btn dropdown-btn-edit" :id="'model-type-op-edit-' + item.id" @click.stop="editType(item)"><i class="nz-icon nz-icon-edit"></i></span>
<span v-else class="dropdown-btn dropdown-btn-edit" :id="'model-type-op-edit-' + item.id" @click.stop="saveType(item)"><i class="el-icon-check"></i></span>
</div>
</div>
</template>
</el-autocomplete>
</el-form-item>
<!--厂商-->
<el-form-item :label="$t('config.model.vendor')" prop="vendor">
<el-autocomplete
:fetch-suggestions="vendorSuggestion"
v-model.trim="currentModel.vendor.value"
placeholder=""
size="small"
popper-class="no-style-class"
>
<template slot-scope="{ item }">
<div class="autocomplete-dropdown" @mouseenter="dropdownHoverItem = 'vendor' + item.id" @mouseleave="dropdownHoverItem = ''">
<span v-if="!item.isEdit">{{item.value}}</span>
<span v-else @click.stop="inputHandler(item)"><el-input size="mini" v-model="editingVendor"></el-input></span>
<div class="dropdown-btns" v-show="dropdownHoverItem == 'vendor' + item.id">
<span class="dropdown-btn dropdown-btn-delete" :id="'model-vendor-op-del-' + item.id" @click.stop="delVendor(item.id)"><i class="el-icon-delete"></i></span>
<span v-if="!item.isEdit" class="dropdown-btn dropdown-btn-edit" :id="'model-vendor-op-edit-' + item.id" @click.stop="editVendor(item)"><i class="nz-icon nz-icon-edit"></i></span>
<span v-else class="dropdown-btn dropdown-btn-edit" :id="'model-type-op-edit-' + item.id" @click.stop="saveVendor(item)"><i class="el-icon-check"></i></span>
</div>
</div>
</template>
</el-autocomplete>
</el-form-item>
<el-form-item :label="this.$t('asset.createAssetTab.AddCabinetTab.uSize')" prop="uSize">
<el-input v-model.number="currentModel.uSize" :max="47" size="small"></el-input>
</el-form-item>
</el-form>
</el-scrollbar>
<!--底部按钮-->
<div class="right-box-bottom-btns">
<button @click="esc" id="model-box-esc" class="nz-btn nz-btn-size-normal nz-btn-style-light nz-btn-min-width-100">
<span>{{$t('overall.cancel')}}</span>
</button>
<button v-if="rightBox.isEdit" @click="saveOrToEdit" id="model-box-save" class="nz-btn nz-btn-size-normal nz-btn-style-normal nz-btn-min-width-100">
<span>{{$t('overall.save')}}</span>
</button>
</div>
<div class="right-box right-box-model" v-clickoutside="clickOutside" @click="inputHandler">
<!-- begin--顶部按钮-->
<div class="right-box-top-btns">
<button id="model-del" type="button" v-if="editModel.id" @click="del" class="nz-btn nz-btn-size-normal nz-btn-size-alien nz-btn-style-light">
<span class="right-box-top-btn-icon"><i class="el-icon-delete"></i></span>
<span class="right-box-top-btn-txt">{{$t('overall.delete')}}</span>
</button>
</div>
</transition>
<!-- end--顶部按钮-->
<!-- begin--标题-->
<div class="right-box-title">{{editModel.id ? ($t("config.model.editModel") + " ID" + editModel.id) : $t("config.model.createModel")}}</div>
<!-- end--标题-->
<!-- begin--表单-->
<el-scrollbar class="right-box-form-box">
<el-form class="right-box-form right-box-form-left" :model="editModel" label-position="right" label-width="120px" :rules="rules" ref="modelForm">
<!--model名称-->
<el-form-item :label='$t("overall.name")' prop="name">
<el-input placeholder="" maxlength="64" show-word-limit v-model.trim="editModel.name" size="small"></el-input>
</el-form-item>
<!--设备类型-->
<el-form-item :label="$t('config.model.type')" prop="type">
<el-autocomplete
:fetch-suggestions="typeSuggestion"
v-model.trim="editModel.type.value"
placeholder=""
size="small"
popper-class="no-style-class"
>
<template slot-scope="{ item }">
<div class="autocomplete-dropdown" @mouseenter="dropdownHoverItem = 'type' + item.id" @mouseleave="dropdownHoverItem = ''">
<span v-if="!item.isEdit">{{item.value}}</span>
<span v-else @click.stop="inputHandler(item)"><el-input size="mini" v-model="editingType" @click.stop></el-input></span>
<div class="dropdown-btns" v-show="dropdownHoverItem == 'type' + item.id">
<span class="dropdown-btn dropdown-btn-delete" :id="'model-type-op-del-' + item.id" @click.stop="delType(item.id)"><i class="el-icon-delete"></i></span>
<span v-if="!item.isEdit" class="dropdown-btn dropdown-btn-edit" :id="'model-type-op-edit-' + item.id" @click.stop="editType(item)"><i class="nz-icon nz-icon-edit"></i></span>
<span v-else class="dropdown-btn dropdown-btn-edit" :id="'model-type-op-edit-' + item.id" @click.stop="saveType(item)"><i class="el-icon-check"></i></span>
</div>
</div>
</template>
</el-autocomplete>
</el-form-item>
<!--厂商-->
<el-form-item :label="$t('config.model.vendor')" prop="vendor">
<el-autocomplete
:fetch-suggestions="vendorSuggestion"
v-model.trim="editModel.vendor.value"
placeholder=""
size="small"
popper-class="no-style-class"
>
<template slot-scope="{ item }">
<div class="autocomplete-dropdown" @mouseenter="dropdownHoverItem = 'vendor' + item.id" @mouseleave="dropdownHoverItem = ''">
<span v-if="!item.isEdit">{{item.value}}</span>
<span v-else @click.stop="inputHandler(item)"><el-input size="mini" v-model="editingVendor"></el-input></span>
<div class="dropdown-btns" v-show="dropdownHoverItem == 'vendor' + item.id">
<span class="dropdown-btn dropdown-btn-delete" :id="'model-vendor-op-del-' + item.id" @click.stop="delVendor(item.id)"><i class="el-icon-delete"></i></span>
<span v-if="!item.isEdit" class="dropdown-btn dropdown-btn-edit" :id="'model-vendor-op-edit-' + item.id" @click.stop="editVendor(item)"><i class="nz-icon nz-icon-edit"></i></span>
<span v-else class="dropdown-btn dropdown-btn-edit" :id="'model-type-op-edit-' + item.id" @click.stop="saveVendor(item)"><i class="el-icon-check"></i></span>
</div>
</div>
</template>
</el-autocomplete>
</el-form-item>
<el-form-item :label="this.$t('asset.createAssetTab.AddCabinetTab.uSize')" prop="usize">
<el-input v-model.number="editModel.usize" :max="47" size="small"></el-input>
</el-form-item>
</el-form>
</el-scrollbar>
<!--底部按钮-->
<div class="right-box-bottom-btns">
<button @click="esc" id="model-box-esc" class="nz-btn nz-btn-size-normal nz-btn-style-light nz-btn-min-width-100">
<span>{{$t('overall.cancel')}}</span>
</button>
<button @click="save" id="model-box-save" class="nz-btn nz-btn-size-normal nz-btn-style-normal nz-btn-min-width-100">
<span>{{$t('overall.save')}}</span>
</button>
</div>
</div>
</template>
<script>
@@ -96,27 +89,13 @@
export default {
name: "modelBox",
props: {
model: Object
model: Object,
},
data() {
return {
currentModel: {
id: '',
name: '',
vendor: {id: '', value: '', code: '', type: ''},
type: {id: '', value: '', code: '', type: ''},
assetStat: {total: '', inStock: '', outStock: ''},
remark: '',
vendorCode: '',
typeCode: '',
uSize:1,
},
rightBox: {
show: false,
firstShow: false,
title: '',
isEdit: false
},
vendorData: [], //vendor下拉列表的数据
typeData: [], //type下拉列表的数据
editModel: {},
rules: {
name: [
{required: true, message: this.$t('validate.required'), trigger: 'blur'}
@@ -127,24 +106,18 @@
vendor: [
{required: true, message: this.$t('validate.required'), trigger: 'blur'}
],
uSize: [
{type: 'number', min: 1, max: 47,message: this.$t('validate.uSize'),trigger: 'blur'}
usize: [
{type: 'number', min: 1, max: 47,message: this.$t('validate.usize'),trigger: 'blur'}
],
},
vendorData: [], //vendor下拉列表的数据
typeData: [], //type下拉列表的数据
editingVendor: '', //修改状态的vendor值
editingType: '', //修改状态的assetType值
dropdownHoverItem: '', //控制vendor和type下拉列表中操作按钮的展示
}
},
methods: {
show(show, isEdit) {
this.rightBox.show = show;
this.rightBox.isEdit = isEdit;
},
clickos() {
this.rightBox.show = false;
clickOutside() {
this.esc(false);
},
//控制下拉框里input的状态
@@ -170,9 +143,8 @@
}
},
/*关闭弹框*/
esc() {
this.rightBox.show = false;
this.rightBox.firstShow = false;
esc(refresh) {
this.$emit("close", refresh);
},
/*保存*/
@@ -181,106 +153,52 @@
if (valid) {
let errMsg = [];
//vendor和type如果是新记录需要先请求后台新增再获取code
let vendorReady = false;
let vendorCode = this.autocompleteExist('vendor', this.currentModel.vendor.value);
let vendorReady;
let vendorCode = this.autocompleteExist('vendor', this.editModel.vendor.value);
if (vendorCode) {
this.currentModel.vendorCode = vendorCode;
vendorReady = true;
this.editModel.vendorCode = vendorCode;
vendorReady = this.$TOOLS.blankPromise();
} else {
let vendor = {type: 'vendor', value: this.currentModel.vendor.value};
this.$post("sys/dict/save", vendor).then(response => {
if (response.code === 200) {
//保存成功后,重新获取
this.$get('sys/dict/all?type=vendor').then(response => {
if (response.code === 200) {
this.vendorData = response.data.map((item) => {
item.isEdit = false;
return item;
});
this.currentModel.vendorCode = this.autocompleteExist('vendor', this.currentModel.vendor.value);
vendorReady = true;
}
});
} else {
errMsg.push("vendor");
}
});
let vendor = {type: 'vendor', value: this.editModel.vendor.value};
vendorReady = this.saveVendor(vendor);
}
let typeReady = false;
let typeCode = this.autocompleteExist('type', this.currentModel.type.value);
let typeReady;
let typeCode = this.autocompleteExist('type', this.editModel.type.value);
if (typeCode) {
this.currentModel.typeCode = typeCode;
typeReady = true;
this.editModel.typeCode = typeCode;
typeReady = this.$TOOLS.blankPromise();
} else {
let type = {type: 'assetType', value: this.currentModel.type.value};
this.$post("sys/dict/save", type).then(response => {
if (response.code === 200) {
this.$get('sys/dict/all?type=assetType').then(response => {
if (response.code === 200) {
this.typeData = response.data.map((item) => {
item.isEdit = false;
return item;
});
this.currentModel.typeCode = this.autocompleteExist('type', this.currentModel.type.value);
typeReady = true;
}
});
} else {
errMsg.push("type");
}
});
let type = {type: 'assetType', value: this.editModel.type.value};
typeReady = this.saveType(type);
}
let intervalTime = 1; //设置3秒超时
let interval = setInterval(() => {
if (vendorReady && typeReady) {
if (this.currentModel.id) {
this.$put('model', this.currentModel).then(response => {
if (response.code === 200) {
this.$message({duration: 1000, type: 'success', message: this.$t("tip.saveSuccess")});
this.rightBox.show = false;
this.$emit('reload');
} else {
this.$message.error(response.msg);
}
});
} else {
this.$post('model', this.currentModel).then(response => {
if (response.code === 200) {
this.$message({duration: 1000, type: 'success', message: this.$t("tip.saveSuccess")});
this.rightBox.show = false;
this.$emit("reload");
} else {
this.$message.error(response.msg);
}
});
}
clearInterval(interval);
} else if (intervalTime > 15) {
if (errMsg.length > 0) {
this.$message.error(errMsg.join() + " error");
} else {
this.$message.error("Time out");
}
clearInterval(interval);
Promise.all([vendorReady, typeReady]).then(response => {
if (this.editModel.id) {
this.$put('model', this.editModel).then(response => {
if (response.code === 200) {
this.$message({duration: 1000, type: 'success', message: this.$t("tip.saveSuccess")});
this.esc(true);
} else {
this.$message.error(response.msg);
}
});
} else {
intervalTime++;
this.$post('model', this.editModel).then(response => {
if (response.code === 200) {
this.$message({duration: 1000, type: 'success', message: this.$t("tip.saveSuccess")});
this.esc(true);
} else {
this.$message.error(response.msg);
}
});
}
}, 200);
});
} else {
return false;
}
});
},
saveOrToEdit: function() {
if (!this.rightBox.isEdit) {
this.rightBox.isEdit = true;
this.rightBox.title = this.$t("config.model.editModel") + " ID" + this.currentModel.id;
} else {
this.save();
}
},
/*删除*/
del() {
this.$confirm(this.$t("tip.confirmDelete"), {
@@ -288,11 +206,10 @@
cancelButtonText: this.$t("tip.no"),
type: 'warning'
}).then(() => {
this.$delete("model?ids=" + this.currentModel.id).then(response => {
this.$delete("model?ids=" + this.editModel.id).then(response => {
if (response.code === 200) {
this.$message({duration: 1000, type: 'success', message: this.$t("tip.deleteSuccess")});
this.rightBox.show = false;
this.$emit("reload");
this.esc(true);
} else {
this.$message.error(response.msg);
}
@@ -309,6 +226,7 @@
if (response.code === 200) {
this.$message({duration: 1000, type: 'success', message: this.$t("tip.deleteSuccess")});
this.getTypeData();
this.$emit("reload");
} else {
this.$message.error(response.msg);
}
@@ -321,23 +239,25 @@
item.isEdit = true;
},
saveType(item) {
if (this.editingType) {
let temp = Object.assign({}, item);
temp.value = this.editingType;
this.$post('sys/dict/update', temp).then(response => {
if (response.code === 200) {
item.value = this.editingType;
item.isEdit = false;
if (this.currentModel.type.id == item.id) {
this.currentModel.type.value = item.value;
return new Promise(resolve => {
if (this.editingType) {
let temp = Object.assign({}, item);
temp.value = this.editingType;
this.$post('sys/dict/update', temp).then(response => {
if (response.code === 200) {
item.value = this.editingType;
item.isEdit = false;
if (this.editModel.type.id == item.id) {
this.editModel.type.value = item.value;
}
this.$emit("reload");
this.$message({duration: 1000, type: 'success', message: this.$t("tip.saveSuccess")});
} else {
this.$message.error(response.msg);
}
this.$emit("reload");
this.$message({duration: 1000, type: 'success', message: this.$t("tip.saveSuccess")});
} else {
this.$message.error(response.msg);
}
});
}
});
}
});
},
editVendor(item) {
this.editingVendor = item.value;
@@ -345,23 +265,40 @@
item.isEdit = true;
},
saveVendor(item) {
if (this.editingVendor) {
let temp = Object.assign({}, item);
temp.value = this.editingVendor;
this.$post('sys/dict/update', temp).then(response => {
if (response.code === 200) {
item.value = this.editingVendor;
item.isEdit = false;
if (this.currentModel.vendor.id == item.id) {
this.currentModel.vendor.value = item.value;
return new Promise(resolve => {
if (this.editingVendor) {
let temp = Object.assign({}, item);
temp.value = this.editingVendor;
this.$post('sys/dict/update', temp).then(response => {
if (response.code === 200) {
item.value = this.editingVendor;
item.isEdit = false;
if (this.editModel.vendor.id == item.id) {
this.editModel.vendor.value = item.value;
}
this.$emit("reload");
this.$message({duration: 1000, type: 'success', message: this.$t("tip.saveSuccess")});
} else {
this.$message.error(response.msg);
}
this.$emit("reload");
this.$message({duration: 1000, type: 'success', message: this.$t("tip.saveSuccess")});
} else {
this.$message.error(response.msg);
}
});
}
});
} else {
this.$post('sys/dict/save', item).then(response => {
if (response.code === 200) {
item.value = this.editingVendor;
item.isEdit = false;
if (this.editModel.vendor.id == item.id) {
this.editModel.vendor.value = item.value;
}
this.$emit("reload");
this.$message({duration: 1000, type: 'success', message: this.$t("tip.saveSuccess")});
resolve();
} else {
this.$message.error(response.msg);
}
});
}
});
},
delVendor(id) {
this.$confirm(this.$t("tip.confirmDelete"), {
@@ -373,6 +310,7 @@
if (response.code === 200) {
this.$message({duration: 1000, type: 'success', message: this.$t("tip.deleteSuccess")});
this.getVendorData();
this.$emit("reload");
} else {
this.$message.error(response.msg);
}
@@ -453,19 +391,12 @@
this.getVendorData();
this.getTypeData();
},
computed: {
},
watch: {
model: {
immediate: true,
deep:true,
handler(n, o) {
this.currentModel = JSON.parse(JSON.stringify(n));
if (n && n.id) {
this.rightBox.title =this.rightBox.isEdit? this.$t("config.model.editModel") + " ID" + n.id : this.$t("config.model.model") + " ID" + n.id ;
} else {
this.rightBox.title = this.$t("config.model.createModel");
}
this.editModel = JSON.parse(JSON.stringify(n));
}
},
}