yudao-dev/src/mixins/basic-add.js
2024-04-15 17:22:10 +08:00

123 lines
3.3 KiB
JavaScript
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.

/*
* @Author: zwq
* @Date: 2022-08-24 11:19:43
* @LastEditors: zwq
* @LastEditTime: 2024-04-10 15:46:19
* @Description:
*/
import { listData } from "@/api/system/dict/data"; //数据字典接口
export default {
data() {
/* eslint-disable */
return {
urlOptions: {
createURL: '', //新增接口
updateURL: '', //编辑提交接口
infoURL: '', //编辑时获取单条数据接口
codeURL: '', //获取code接口返回结果为dataForm.code字段
optionArrUrl: [], //需要获取下拉框的方法数组
optionArr: {}, //需要获取下拉框的方法数组的返回结果
dictNameList: [], //数据字典name数组
dictArr: {}, //需要获取数据字典的方法数组的返回结果
},
visible: false,
setData: false, // 是否需要【编辑时获取单条数据接口】返回的数据操作
}
},
created() {
},
activated() {
},
methods: {
init(id) {
this.dataForm.id = id || null;
this.visible = true;
if (this.urlOptions.optionArrUrl.length > 0) {
this.getArr()
}
if (this.urlOptions.dictNameList.length > 0) {
this.getDict()
}
this.$nextTick(() => {
this.$refs["dataForm"].resetFields();
if (this.dataForm.id) {
this.urlOptions.infoURL(id).then(response => {
this.dataForm = response.data;
if (this.setData) {
this.setDataForm()
}
});
} else {
if (this.urlOptions.codeURL) {
this.getCode()
}
}
});
},
/** 获取code */
getCode() {
this.urlOptions.codeURL()
.then(({ data: res }) => {
this.dataForm.code = res;
})
.catch(() => {});
},
/** 获取下拉框数组 */
getArr() {
const params = {
pageSize: 100,
pageNo: 1,
}
this.urlOptions.optionArrUrl.forEach((item, index) => {
item(params).then(({ data: res }) => {
this.$set(this.urlOptions.optionArr, `arr${index}`, res.list)
})
.catch(() => {
});
});
},
/** 查询字典数据列表 */
getDict() {
this.urlOptions.dictNameList.forEach((item,index)=>{
const queryParams = {
pageNo: 1,
pageSize: 99,
dictType: item,
}
listData(queryParams).then(response => {
this.$set(this.urlOptions.dictArr, `dict${index}`, response.data.list)
});
})
},
// 表单提交
dataFormSubmit() {
this.$refs["dataForm"].validate((valid) => {
if (!valid) {
return false;
}
// 修改的提交
if (this.dataForm.id) {
this.urlOptions.updateURL(this.dataForm).then(response => {
this.$modal.msgSuccess("修改成功");
this.visible = false;
this.$emit("refreshDataList");
});
return;
}
// 添加的提交
this.urlOptions.createURL(this.dataForm).then(response => {
this.$modal.msgSuccess("新增成功");
this.visible = false;
this.$emit("refreshDataList");
});
});
},
/** 清空form */
formClear() {
if (this.$refs.dataForm!==undefined) {
this.$refs.dataForm.resetFields();
}
}
}
}