You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

465 lines
13 KiB

3 years ago
<script>
3 years ago
import { save, show, index } from "@/api/system/baseForm";
3 years ago
import { getparameter } from "@/api/system/dictionary";
3 years ago
import { domMap } from "@/const/inputType";
import { addPropsMap } from "@/const/addProps";
3 years ago
export default {
props: {
formInfo: {
type: Array,
default: () => [],
},
3 years ago
tableName: String,
3 years ago
},
render(h) {
return h(
"el-dialog",
{
props: {
title: "新增",
visible: this.dialogVisible,
3 years ago
width: "600px",
3 years ago
},
on: {
"update:visible": (val) => {
this.dialogVisible = val;
},
},
},
[
h(
"el-form",
{
ref: "elForm",
props: {
model: this.form,
labelWidth: "80px",
rules: this.rules,
labelPosition: "right",
3 years ago
size: "small",
3 years ago
},
},
3 years ago
(() => {
let dom = [];
3 years ago
this.formInfo.filter(i => i.form_show).forEach((i, index) => {
dom.push(
h(
"el-form-item",
{
ref: `elFormItem${i.field}`,
style: {
width: "100%",
3 years ago
},
3 years ago
props: {
label: i.name,
prop: i.field,
required:
i.validation instanceof Array
? !!i.validation.find((i) => i === "required")
: false,
},
},
this.$scopedSlots[i.field]
? this.$scopedSlots[i.field]({ fieldInfo: i, form: this.form })
: [
h(
domMap.get(i.edit_input),
{
ref: `elEdit_${i.field}`,
style: {
width: "100%",
},
props: {
...addPropsMap.get(i.edit_input),
...this.extraProps(i),
placeholder: i.help,
value: this.form[i.field],
},
attrs: {
placeholder: i.help || `请填写${i.name}`,
},
on: {
[this.getEventType(i.edit_input)]: (e) => {
if (i.field) {
this.form[i.field] = e;
this.form = Object.assign({}, this.form);
}
3 years ago
},
3 years ago
},
scopedSlots:
i.edit_input === "file" ||
i.edit_input === "files"
? {
file: (scope) => {
let { file } = scope;
return [
h("div", {}, [
h("i", {
class: {
"el-icon-circle-check":
file.status === "success",
"el-icon-loading":
file.status === "uploading",
},
style: {
color:
file.status === "success"
? "green"
: "",
},
}),
h(
"a",
{
attrs: {
href: file.url,
download: file.name,
},
class: {
"uploaded-a":
file.status === "success",
},
3 years ago
style: {
'padding': '0 4px'
},
3 years ago
},
file.name
),
]),
h("i", {
class: "el-icon-close",
on: {
["click"]: () =>
this.fileRemoveHandler(
file,
i.field
),
},
}),
];
},
}
: "",
},
this.optionsRender(h, i)
),
]
)
);
3 years ago
});
return dom;
})()
3 years ago
),
h("template", { slot: "footer" }, [
h(
"el-button",
{
on: {
click: () => (this.dialogVisible = false),
},
},
3 years ago
"取 消"
3 years ago
),
h(
"el-button",
{
props: {
3 years ago
type: "warning",
plain: true,
3 years ago
},
on: {
3 years ago
click: () => this.init(),
3 years ago
},
},
3 years ago
"重 置"
3 years ago
),
h(
"el-button",
{
props: {
type: "primary",
},
on: {
3 years ago
click: this.submit,
3 years ago
},
},
3 years ago
"确 定"
3 years ago
),
]),
]
);
},
data() {
return {
id: "",
type: "add",
dialogVisible: false,
form: {},
rules: {},
3 years ago
file: {},
3 years ago
};
},
methods: {
3 years ago
fileRemoveHandler(file, field) {
this.file[field] = this.file[field].filter((item) => item !== file);
this.file = Object.assign({}, this.file);
},
3 years ago
//on事件类别获取
getEventType(info) {
3 years ago
if (info === "checkbox") {
3 years ago
return "change";
}
return "input";
},
//渲染一些组件内部需要选项等
optionsRender(h, info) {
3 years ago
if (info.edit_input === "checkbox" || info.edit_input === "radio") {
3 years ago
return info._params && info._params instanceof Array
? info._params.map((i) =>
3 years ago
h("el-option", {
props: {
3 years ago
label: i.key || i.value || i.name || i.no || i.mingcheng || i.id,
3 years ago
value: info._relations ? i[info._relations.foreign_key] : i.value,
3 years ago
},
})
3 years ago
)
: [];
}
if (info.edit_input === "file" || info.edit_input === "files") {
return [
h(
"el-button",
{
slot: "trigger",
props: {
size: "small",
type: "primary",
},
},
"选取文件"
),
h(
"el-button",
{
style: {
"margin-left": "10px",
},
props: {
size: "small",
type: "success",
},
on: {
["click"]: (e) => {
this.$refs[`elEdit_${info.field}`].submit();
},
},
},
"上传到服务器"
),
h(
"div",
{
class: "el-upload__tip",
slot: "tip",
},
"文件不超过500kb"
),
];
}
},
extraProps(info) {
let props = {};
if (info.edit_input === "file" || info.edit_input === "files") {
3 years ago
props.fileList = this.file[info.field];
3 years ago
props.beforeUpload = (file) => {
if (file.size / 1000 > 500) {
this.$message({
type: "warning",
message: "上传图片大小超过500kb",
});
return false;
}
};
props.onSuccess = (response, file, fileList) => {
3 years ago
this.file[info.field] = fileList;
3 years ago
};
props.onRemove = (file, fileList) => {
3 years ago
this.file[info.field] = fileList;
3 years ago
};
3 years ago
3 years ago
props.onError = (err, file, fileList) => {
3 years ago
this.file[info.field] = fileList;
this.$message({
type: "warning",
3 years ago
message: err,
});
};
3 years ago
}
return props;
},
3 years ago
init() {
3 years ago
for (let key in this.form) {
if (this.form[key] instanceof Array) {
this.form[key] = [];
} else {
this.form[key] = "";
}
3 years ago
}
3 years ago
this.$refs["elForm"].clearValidate();
3 years ago
},
show() {
this.dialogVisible = true;
},
hidden() {
this.dialogVisible = false;
},
setType(type = "add") {
let types = ["add", "editor"];
if (types.includes(type)) {
this.type = type;
} else {
console.warn("Unknown type: " + type);
}
},
setId(id) {
if (typeof id == "number") {
this.id = id;
} else {
console.error("error typeof id: " + typeof id);
}
},
async getDetail() {
3 years ago
const res = await show({ id: this.id, table_name: this.tableName });
3 years ago
this.$integrateData(this.form, res);
this.form = Object.assign({}, this.form);
3 years ago
this.formInfo.forEach((i) => {
3 years ago
if (i && (i.edit_input === "file" || i.edit_input === 'files')) {
res[i._relations.link_with_name] ? (
this.file[i.field] = res[i._relations.link_with_name] instanceof Array ? res[i._relations.link_with_name].map(i => {
return {
name: i?.original_name,
url: i?.url,
response: i
}
}) : [{
name: res[i._relations.link_with_name]?.original_name,
url: res[i._relations.link_with_name]?.url,
response: res[i._relations.link_with_name]
}]
) : this.file[i.field] = []
3 years ago
}
});
3 years ago
},
submit() {
if (this.type === "add") {
if (this.form.hasOwnProperty("id")) {
delete this.form.id;
}
}
if (this.type === "editor") {
Object.defineProperty(this.form, "id", {
value: this.id,
enumerable: true,
configurable: true,
writable: true,
});
}
3 years ago
this.$refs["elForm"].validate((validate) => {
if (validate) {
this.formInfo.forEach((info) => {
if (info.edit_input === "files") {
3 years ago
this.form[info.field] = this.file[info.field].map(
3 years ago
(i) => i?.response?.id
);
}
3 years ago
if (info.edit_input === "file") {
this.form[info.field] = this.file[info.field][0]?.response?.id;
3 years ago
}
3 years ago
});
console.log(this.form);
save(Object.assign(this.form, { table_name: this.tableName })).then(
(res) => {
this.$Message.success({
content: `${this.type === "add" ? "新增" : "编辑"}成功`,
});
this.$emit("refresh");
this.hidden();
}
);
3 years ago
}
});
3 years ago
},
3 years ago
},
computed: {},
watch: {
formInfo: {
handler: function (newVal) {
this.form = {};
3 years ago
this.rules = {};
3 years ago
this.file = {};
3 years ago
newVal.forEach((i) => {
if (i.field) {
3 years ago
this.form[i.field] = "";
3 years ago
if (
i.validation instanceof Array &&
i.validation.length > 0 &&
!!i.validation.find((i) => i === "required")
) {
3 years ago
this.rules[i.field] = [
{ required: true, message: `请填写${i.name}` },
];
}
3 years ago
if (i.edit_input === "files") {
3 years ago
this.form[i.field] = [];
}
3 years ago
if (i.edit_input === "files" || i.edit_input === "file") {
3 years ago
this.file[i.field] = [];
}
3 years ago
if (i.edit_input === "checkbox") {
this.form[i.field] = [];
3 years ago
}
}
3 years ago
});
3 years ago
},
//immediate: true,
},
dialogVisible(val) {
if (val) {
if (this.type === "editor") {
3 years ago
this.$nextTick(() => this.getDetail());
3 years ago
}
} else {
3 years ago
this.file = {};
3 years ago
this.id = "";
this.type = "";
this.init();
this.$refs["elForm"].clearValidate();
delete this.form.id;
}
3 years ago
},
},
3 years ago
};
</script>
3 years ago
<style scoped lang="scss">
.uploaded-a {
color: red;
text-decoration: none;
transition: all 0.2s;
}
.uploaded-a:hover {
color: red;
text-decoration: underline;
}
</style>