代码生成:重构 vue2 代码生成模版,使用 async await 优化代码层次
This commit is contained in:
parent
3d2022e31f
commit
86160f40de
|
@ -145,19 +145,17 @@
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
/** 打开弹窗 */
|
/** 打开弹窗 */
|
||||||
open(id, ${subJoinColumn.javaField}) {
|
async open(id, ${subJoinColumn.javaField}) {
|
||||||
this.dialogVisible = true;
|
this.dialogVisible = true;
|
||||||
this.reset();
|
this.reset();
|
||||||
const that = this;
|
|
||||||
this.formData.${subJoinColumn.javaField} = ${subJoinColumn.javaField};
|
this.formData.${subJoinColumn.javaField} = ${subJoinColumn.javaField};
|
||||||
// 修改时,设置数据
|
// 修改时,设置数据
|
||||||
if (id) {
|
if (id) {
|
||||||
this.formLoading = true;
|
this.formLoading = true;
|
||||||
try {
|
try {
|
||||||
${simpleClassName}Api.get${subSimpleClassName}(id).then(res=>{
|
const res = await ${simpleClassName}Api.get${subSimpleClassName}(id);
|
||||||
that.formData = res.data;
|
this.formData = res.data;
|
||||||
that.dialogTitle = "修改${subTable.classComment}";
|
this.dialogTitle = "修改${subTable.classComment}";
|
||||||
})
|
|
||||||
} finally {
|
} finally {
|
||||||
this.formLoading = false;
|
this.formLoading = false;
|
||||||
}
|
}
|
||||||
|
@ -165,32 +163,26 @@
|
||||||
this.dialogTitle = "新增${subTable.classComment}";
|
this.dialogTitle = "新增${subTable.classComment}";
|
||||||
},
|
},
|
||||||
/** 提交按钮 */
|
/** 提交按钮 */
|
||||||
submitForm() {
|
async submitForm() {
|
||||||
|
await this.#[[$]]#refs["formRef"].validate();
|
||||||
this.formLoading = true;
|
this.formLoading = true;
|
||||||
try {
|
try {
|
||||||
let data = this.formData;
|
const data = this.formData;
|
||||||
this.#[[$]]#refs["formRef"].validate(valid => {
|
|
||||||
if (!valid) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
// 修改的提交
|
// 修改的提交
|
||||||
if (data.${primaryColumn.javaField}) {
|
if (data.${primaryColumn.javaField}) {
|
||||||
${simpleClassName}Api.update${subSimpleClassName}(data).then(response => {
|
await ${simpleClassName}Api.update${subSimpleClassName}(data);
|
||||||
this.#[[$modal]]#.msgSuccess("修改成功");
|
this.#[[$modal]]#.msgSuccess("修改成功");
|
||||||
this.dialogVisible = false;
|
this.dialogVisible = false;
|
||||||
this.#[[$]]#emit('success');
|
this.#[[$]]#emit('success');
|
||||||
});
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// 添加的提交
|
// 添加的提交
|
||||||
${simpleClassName}Api.create${subSimpleClassName}(data).then(response => {
|
await ${simpleClassName}Api.create${subSimpleClassName}(data);
|
||||||
this.#[[$modal]]#.msgSuccess("新增成功");
|
this.#[[$modal]]#.msgSuccess("新增成功");
|
||||||
this.dialogVisible = false;
|
this.dialogVisible = false;
|
||||||
this.#[[$]]#emit('success');
|
this.#[[$]]#emit('success');
|
||||||
});
|
|
||||||
});
|
|
||||||
}finally {
|
}finally {
|
||||||
this.formLoading = false
|
this.formLoading = false;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
/** 表单重置 */
|
/** 表单重置 */
|
||||||
|
|
|
@ -289,13 +289,14 @@
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
this.formLoading = true;
|
this.formLoading = true;
|
||||||
|
// 这里还是需要获取一下 this 的不然取不到 formData
|
||||||
const that = this;
|
const that = this;
|
||||||
#if ( $subTable.subJoinMany )
|
#if ( $subTable.subJoinMany )
|
||||||
${simpleClassName}Api.get${subSimpleClassName}ListBy${SubJoinColumnName}(val).then(res=>{
|
${simpleClassName}Api.get${subSimpleClassName}ListBy${SubJoinColumnName}(val).then(function (res){
|
||||||
that.formData = res.data;
|
that.formData = res.data;
|
||||||
})
|
})
|
||||||
#else
|
#else
|
||||||
${simpleClassName}Api.get${subSimpleClassName}By${SubJoinColumnName}(val).then(res=>{
|
${simpleClassName}Api.get${subSimpleClassName}By${SubJoinColumnName}(val).then(function (res){
|
||||||
const data = res.data;
|
const data = res.data;
|
||||||
if (!data) {
|
if (!data) {
|
||||||
return
|
return
|
||||||
|
@ -325,21 +326,21 @@
|
||||||
#end
|
#end
|
||||||
#end
|
#end
|
||||||
}
|
}
|
||||||
row.${subJoinColumn.javaField} = this.${subJoinColumn.javaField}
|
row.${subJoinColumn.javaField} = this.${subJoinColumn.javaField};
|
||||||
this.formData.push(row)
|
this.formData.push(row);
|
||||||
},
|
},
|
||||||
/** 删除按钮操作 */
|
/** 删除按钮操作 */
|
||||||
handleDelete(index) {
|
handleDelete(index) {
|
||||||
this.formData.splice(index, 1)
|
this.formData.splice(index, 1);
|
||||||
},
|
},
|
||||||
#end
|
#end
|
||||||
/** 表单校验 */
|
/** 表单校验 */
|
||||||
validate(){
|
validate(){
|
||||||
return this.#[[$]]#refs["formRef"].validate()
|
return this.#[[$]]#refs["formRef"].validate();
|
||||||
},
|
},
|
||||||
/** 表单值 */
|
/** 表单值 */
|
||||||
getData(){
|
getData(){
|
||||||
return this.formData
|
return this.formData;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -111,28 +111,24 @@
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
/** 查询列表 */
|
/** 查询列表 */
|
||||||
getList() {
|
async getList() {
|
||||||
try {
|
try {
|
||||||
this.loading = true;
|
this.loading = true;
|
||||||
const that = this;
|
|
||||||
#if ($table.templateType == 11)
|
#if ($table.templateType == 11)
|
||||||
${simpleClassName}Api.get${subSimpleClassName}Page(this.queryParams).then(response => {
|
const res = await ${simpleClassName}Api.get${subSimpleClassName}Page(this.queryParams);
|
||||||
that.list = response.data.list;
|
this.list = res.data.list;
|
||||||
that.total = response.data.total;
|
this.total = res.data.total;
|
||||||
});
|
|
||||||
#else
|
#else
|
||||||
#if ( $subTable.subJoinMany )
|
#if ( $subTable.subJoinMany )
|
||||||
${simpleClassName}Api.get${subSimpleClassName}ListBy${SubJoinColumnName}(this.${subJoinColumn.javaField}).then(response=>{
|
const res = await ${simpleClassName}Api.get${subSimpleClassName}ListBy${SubJoinColumnName}(this.${subJoinColumn.javaField});
|
||||||
that.list = response.data;
|
this.list = res.data;
|
||||||
})
|
|
||||||
#else
|
#else
|
||||||
${simpleClassName}Api.get${subSimpleClassName}By${SubJoinColumnName}(this.${subJoinColumn.javaField}).then(response=>{
|
const res = await ${simpleClassName}Api.get${subSimpleClassName}By${SubJoinColumnName}(this.${subJoinColumn.javaField});
|
||||||
const data = response.data;
|
const data = res.data;
|
||||||
if (!data) {
|
if (!data) {
|
||||||
return
|
return;
|
||||||
}
|
}
|
||||||
that.list.push(data)
|
this.list.push(data);
|
||||||
})
|
|
||||||
#end
|
#end
|
||||||
#end
|
#end
|
||||||
} finally {
|
} finally {
|
||||||
|
@ -148,22 +144,19 @@
|
||||||
/** 添加/修改操作 */
|
/** 添加/修改操作 */
|
||||||
openForm(id) {
|
openForm(id) {
|
||||||
if (!this.${subJoinColumn.javaField}) {
|
if (!this.${subJoinColumn.javaField}) {
|
||||||
that.#[[$modal]]#.msgError('请选择一个${table.classComment}');
|
this.#[[$modal]]#.msgError('请选择一个${table.classComment}');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.#[[$]]#refs["formRef"].open(id, this.${subJoinColumn.javaField});
|
this.#[[$]]#refs["formRef"].open(id, this.${subJoinColumn.javaField});
|
||||||
},
|
},
|
||||||
/** 删除按钮操作 */
|
/** 删除按钮操作 */
|
||||||
handleDelete(row) {
|
async handleDelete(row) {
|
||||||
const that = this;
|
const ${primaryColumn.javaField} = row.${primaryColumn.javaField};
|
||||||
|
await this.#[[$modal]]#.confirm('是否确认删除${table.classComment}编号为"' + ${primaryColumn.javaField} + '"的数据项?');
|
||||||
try {
|
try {
|
||||||
const ${primaryColumn.javaField} = row.${primaryColumn.javaField};
|
await ${simpleClassName}Api.delete${subSimpleClassName}(${primaryColumn.javaField});
|
||||||
this.#[[$modal]]#.confirm('是否确认删除${table.classComment}编号为"' + ${primaryColumn.javaField} + '"的数据项?').then(()=>{
|
await this.getList();
|
||||||
return ${simpleClassName}Api.delete${subSimpleClassName}(${primaryColumn.javaField});
|
this.#[[$modal]]#.msgSuccess("删除成功");
|
||||||
}).then(() => {
|
|
||||||
that.getList();
|
|
||||||
that.#[[$modal]]#.msgSuccess("删除成功");
|
|
||||||
}).catch(() => {});
|
|
||||||
} catch {}
|
} catch {}
|
||||||
},
|
},
|
||||||
#end
|
#end
|
||||||
|
|
|
@ -195,18 +195,16 @@
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
/** 打开弹窗 */
|
/** 打开弹窗 */
|
||||||
open(id) {
|
async open(id) {
|
||||||
this.dialogVisible = true;
|
this.dialogVisible = true;
|
||||||
this.reset();
|
this.reset();
|
||||||
const that = this;
|
|
||||||
// 修改时,设置数据
|
// 修改时,设置数据
|
||||||
if (id) {
|
if (id) {
|
||||||
this.formLoading = true;
|
this.formLoading = true;
|
||||||
try {
|
try {
|
||||||
${simpleClassName}Api.get${simpleClassName}(id).then(res=>{
|
const res = await ${simpleClassName}Api.get${simpleClassName}(id);
|
||||||
that.formData = res.data;
|
this.formData = res.data;
|
||||||
that.title = "修改${table.classComment}";
|
this.title = "修改${table.classComment}";
|
||||||
})
|
|
||||||
} finally {
|
} finally {
|
||||||
this.formLoading = false;
|
this.formLoading = false;
|
||||||
}
|
}
|
||||||
|
@ -218,111 +216,66 @@
|
||||||
#end
|
#end
|
||||||
},
|
},
|
||||||
/** 提交按钮 */
|
/** 提交按钮 */
|
||||||
submitForm() {
|
async submitForm() {
|
||||||
|
// 校验主表
|
||||||
|
await this.$refs["formRef"].validate();
|
||||||
|
## 特殊:主子表专属逻辑
|
||||||
|
#if ( $table.templateType == 10 || $table.templateType == 12 )
|
||||||
|
#if ( $subTables && $subTables.size() > 0 )
|
||||||
|
// 校验子表
|
||||||
|
#foreach ($subTable in $subTables)
|
||||||
|
#set ($index = $foreach.count - 1)
|
||||||
|
#set ($subClassNameVar = $subClassNameVars.get($index))
|
||||||
|
try {
|
||||||
|
## 代码生成后会替换为正确的 refs
|
||||||
|
await this.refs['${subClassNameVar}FormRef'].validate();
|
||||||
|
} catch (e) {
|
||||||
|
this.subTabsName = '${subClassNameVar}';
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
#end
|
||||||
|
#end
|
||||||
|
#end
|
||||||
this.formLoading = true;
|
this.formLoading = true;
|
||||||
try {
|
try {
|
||||||
const that = this;
|
const data = this.formData;
|
||||||
let data = this.formData;
|
|
||||||
let validate = false;
|
|
||||||
// 校验主表
|
|
||||||
this.getRef("formRef").validate(valid => {
|
|
||||||
validate = valid;
|
|
||||||
});
|
|
||||||
## 特殊:主子表专属逻辑
|
## 特殊:主子表专属逻辑
|
||||||
#if ( $table.templateType == 10 || $table.templateType == 12 )
|
#if ( $table.templateType == 10 || $table.templateType == 12 )
|
||||||
#if ( $subTables && $subTables.size() > 0 )
|
#if ( $subTables && $subTables.size() > 0 )
|
||||||
// 校验子表
|
|
||||||
this.validateSubFrom01().then(() => {
|
|
||||||
// 全部校验通过-拼接子表的数据
|
|
||||||
// 拼接子表的数据
|
// 拼接子表的数据
|
||||||
#foreach ($subTable in $subTables)
|
#foreach ($subTable in $subTables)
|
||||||
#set ($index = $foreach.count - 1)
|
#set ($index = $foreach.count - 1)
|
||||||
#set ($subClassNameVar = $subClassNameVars.get($index))
|
#set ($subClassNameVar = $subClassNameVars.get($index))
|
||||||
data.${subClassNameVar}#if ( $subTable.subJoinMany)s#end = that.getRef('${subClassNameVar}FormRef').getData();
|
data.${subClassNameVar}#if ( $subTable.subJoinMany)s#end = this.refs['${subClassNameVar}FormRef'].getData();
|
||||||
#end
|
#end
|
||||||
}).catch((err) => {
|
|
||||||
validate = false;
|
|
||||||
that.subTabsName = err.replace("FormRef", ""); // 定位到没有校验通过的子表单
|
|
||||||
})
|
|
||||||
#end
|
#end
|
||||||
#end
|
#end
|
||||||
// 所有表单校验通过后方可提交
|
|
||||||
if (!validate) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
// 修改的提交
|
// 修改的提交
|
||||||
if (data.${primaryColumn.javaField}) {
|
if (data.${primaryColumn.javaField}) {
|
||||||
${simpleClassName}Api.update${simpleClassName}(data).then(response => {
|
await ${simpleClassName}Api.update${simpleClassName}(data);
|
||||||
that.#[[$modal]]#.msgSuccess("修改成功");
|
this.#[[$modal]]#.msgSuccess("修改成功");
|
||||||
that.dialogVisible = false;
|
this.dialogVisible = false;
|
||||||
that.#[[$]]#emit('success');
|
this.#[[$]]#emit('success');
|
||||||
});
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// 添加的提交
|
// 添加的提交
|
||||||
${simpleClassName}Api.create${simpleClassName}(data).then(response => {
|
await ${simpleClassName}Api.create${simpleClassName}(data);
|
||||||
that.#[[$modal]]#.msgSuccess("新增成功");
|
this.#[[$modal]]#.msgSuccess("新增成功");
|
||||||
that.dialogVisible = false;
|
this.dialogVisible = false;
|
||||||
that.#[[$]]#emit('success');
|
this.#[[$]]#emit('success');
|
||||||
});
|
|
||||||
}finally {
|
}finally {
|
||||||
this.formLoading = false;
|
this.formLoading = false;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
getRef(refName){
|
|
||||||
return this.#[[$]]#refs[refName];
|
|
||||||
},
|
|
||||||
## 特殊:主子表专属逻辑
|
|
||||||
#if ( $table.templateType == 10 || $table.templateType == 12 )
|
|
||||||
#if ( $subTables && $subTables.size() > 0 )
|
|
||||||
/** 校验子表单 */
|
|
||||||
validateSubFrom(item) {
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
this.getRef(item).validate()
|
|
||||||
.then(() => {
|
|
||||||
resolve();
|
|
||||||
})
|
|
||||||
.catch(() => {
|
|
||||||
reject(item);
|
|
||||||
})
|
|
||||||
})
|
|
||||||
},
|
|
||||||
/** 校验所有子表单 */
|
|
||||||
validateSubFrom01() {
|
|
||||||
// 需要校验的表单 ref
|
|
||||||
const validFormRefArr = [
|
|
||||||
#foreach ($subTable in $subTables)
|
|
||||||
#set ($index = $foreach.count - 1)
|
|
||||||
#set ($subClassNameVar = $subClassNameVars.get($index))
|
|
||||||
"${subClassNameVar}FormRef",
|
|
||||||
#end
|
|
||||||
];
|
|
||||||
const validArr = []; // 校验
|
|
||||||
for (const item of validFormRefArr) {
|
|
||||||
validArr.push(this.validateSubFrom(item));
|
|
||||||
}
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
// 校验所有
|
|
||||||
Promise.all(validArr).then(() => {
|
|
||||||
resolve();
|
|
||||||
}).catch((err) => {
|
|
||||||
reject(err);
|
|
||||||
})
|
|
||||||
})
|
|
||||||
},
|
|
||||||
#end
|
|
||||||
#end
|
|
||||||
## 特殊:树表专属逻辑
|
## 特殊:树表专属逻辑
|
||||||
#if ( $table.templateType == 2 )
|
#if ( $table.templateType == 2 )
|
||||||
/** 获得${table.classComment}树 */
|
/** 获得${table.classComment}树 */
|
||||||
get${simpleClassName}Tree() {
|
async get${simpleClassName}Tree() {
|
||||||
const that = this;
|
this.${classNameVar}Tree = [];
|
||||||
that.${classNameVar}Tree = [];
|
const res = await ${simpleClassName}Api.get${simpleClassName}List();
|
||||||
${simpleClassName}Api.get${simpleClassName}List().then(res=>{
|
const root = { id: 0, name: '顶级${table.classComment}', children: [] };
|
||||||
const root = { id: 0, name: '顶级${table.classComment}', children: [] };
|
root.children = this.handleTree(res.data, 'id', '${treeParentColumn.javaField}')
|
||||||
root.children = this.handleTree(res.data, 'id', '${treeParentColumn.javaField}')
|
this.${classNameVar}Tree.push(root)
|
||||||
that.${classNameVar}Tree.push(root)
|
|
||||||
});
|
|
||||||
},
|
},
|
||||||
#end
|
#end
|
||||||
## 特殊:树表专属逻辑
|
## 特殊:树表专属逻辑
|
||||||
|
@ -361,7 +314,7 @@
|
||||||
#end
|
#end
|
||||||
};
|
};
|
||||||
this.resetForm("formRef");
|
this.resetForm("formRef");
|
||||||
},
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
</script>
|
</script>
|
||||||
|
|
|
@ -262,19 +262,17 @@ export default {
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
/** 查询列表 */
|
/** 查询列表 */
|
||||||
getList() {
|
async getList() {
|
||||||
try {
|
try {
|
||||||
this.loading = true;
|
this.loading = true;
|
||||||
## 特殊:树表专属逻辑(树不需要分页接口)
|
## 特殊:树表专属逻辑(树不需要分页接口)
|
||||||
#if ( $table.templateType == 2 )
|
#if ( $table.templateType == 2 )
|
||||||
${simpleClassName}Api.get${simpleClassName}List(this.queryParams).then(response => {
|
const res = await ${simpleClassName}Api.get${simpleClassName}List(this.queryParams);
|
||||||
this.list = this.handleTree(response.data, 'id', '${treeParentColumn.javaField}');
|
this.list = this.handleTree(res.data, 'id', '${treeParentColumn.javaField}');
|
||||||
})
|
|
||||||
#else
|
#else
|
||||||
${simpleClassName}Api.get${simpleClassName}Page(this.queryParams).then(response => {
|
const res = await ${simpleClassName}Api.get${simpleClassName}Page(this.queryParams);
|
||||||
this.list = response.data.list;
|
this.list = res.data.list;
|
||||||
this.total = response.data.total;
|
this.total = res.data.total;
|
||||||
});
|
|
||||||
#end
|
#end
|
||||||
} finally {
|
} finally {
|
||||||
this.loading = false;
|
this.loading = false;
|
||||||
|
@ -295,31 +293,25 @@ export default {
|
||||||
this.#[[$]]#refs["formRef"].open(id);
|
this.#[[$]]#refs["formRef"].open(id);
|
||||||
},
|
},
|
||||||
/** 删除按钮操作 */
|
/** 删除按钮操作 */
|
||||||
handleDelete(row) {
|
async handleDelete(row) {
|
||||||
const that = this;
|
|
||||||
try {
|
|
||||||
const ${primaryColumn.javaField} = row.${primaryColumn.javaField};
|
const ${primaryColumn.javaField} = row.${primaryColumn.javaField};
|
||||||
this.#[[$modal]]#.confirm('是否确认删除${table.classComment}编号为"' + ${primaryColumn.javaField} + '"的数据项?').then(()=>{
|
await this.#[[$modal]]#.confirm('是否确认删除${table.classComment}编号为"' + ${primaryColumn.javaField} + '"的数据项?')
|
||||||
return ${simpleClassName}Api.delete${simpleClassName}(${primaryColumn.javaField});
|
try {
|
||||||
}).then(() => {
|
await ${simpleClassName}Api.delete${simpleClassName}(${primaryColumn.javaField});
|
||||||
that.getList();
|
this.getList();
|
||||||
that.#[[$modal]]#.msgSuccess("删除成功");
|
this.#[[$modal]]#.msgSuccess("删除成功");
|
||||||
}).catch(() => {});
|
|
||||||
} catch {}
|
} catch {}
|
||||||
},
|
},
|
||||||
/** 导出按钮操作 */
|
/** 导出按钮操作 */
|
||||||
handleExport() {
|
async handleExport() {
|
||||||
const that = this;
|
await this.#[[$modal]]#.confirm('是否确认导出所有${table.classComment}数据项?');
|
||||||
try {
|
try {
|
||||||
this.#[[$modal]]#.confirm('是否确认导出所有${table.classComment}数据项?').then(() => {
|
this.exportLoading = true;
|
||||||
that.exportLoading = true;
|
const res = await ${simpleClassName}Api.export${simpleClassName}Excel(this.queryParams);
|
||||||
return ${simpleClassName}Api.export${simpleClassName}Excel(params);
|
this.#[[$]]#download.excel(res.data, '${table.classComment}.xls');
|
||||||
}).then(response => {
|
|
||||||
that.#[[$]]#download.excel(response, '${table.classComment}.xls');
|
|
||||||
});
|
|
||||||
} catch {
|
} catch {
|
||||||
} finally {
|
} finally {
|
||||||
that.exportLoading = false;
|
this.exportLoading = false;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
## 特殊:主子表专属逻辑
|
## 特殊:主子表专属逻辑
|
||||||
|
|
Loading…
Reference in New Issue