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.

103 lines
2.5 KiB

3 years ago
import { save, index, destroy } from "@/api/system/customFormField";
import { Message } from "element-ui";
const state = {
formList: [], //更个表单配置信息
copyFormListId: [], //备份原始的字段id数组,以做删除
selectedForm: null, //当前编辑的表单字段配置
selectedIndex: null,
};
const mutations = {
SET_COPY_FORM_LIST_ID: (state, arr) => {
state.copyFormListId = arr;
},
SET_SELECTED_INDEX: (state, index) => {
state.selectedIndex = index;
},
CLEAR_SELECTED_INDEX: (state) => {
state.selectedIndex = null;
},
SET_SELECTED: (state, value) => {
state.selectedForm = value;
},
CLEAR_SELECTED: (state, value) => {
state.selectedForm = null;
},
SET_FORM_LIST: (state, list) => {
state.formList = list;
},
SPLICE_FORM_LIST: (state, info) => {
const { index, length, config } = info;
if (config) {
state.formList.splice(index, length || 0, config || state.selectedForm);
} else {
state.formList.splice(index, length || 0);
}
},
};
const actions = {
submit: ({ state, commit }) => {
return new Promise((resolve, reject) => {
state.formList.forEach((item,index) => {
item.sort = index + 1
})
let formListId = state.formList.filter((i) => !!i.id).map((i) => i.id);
let deleteIds = state.copyFormListId.filter(
(i) => !formListId.includes(i)
);
let promiseAll = [
...state.formList.map((i) => save(i)),
...deleteIds.map((i) => destroy({ id: i })),
];
Promise.all(promiseAll)
.then((res) => {
Message({
type: "success",
message: "保存成功",
});
resolve(res);
})
.catch((err) => {
reject(err);
});
});
},
getFormList: ({ state, commit }, custom_form_id) => {
return new Promise((resolve, reject) => {
index(
{
page: 1,
page_size: 999,
sort_name: "sort",
sort_type: "asc",
custom_form_id,
},
true
)
.then((res) => {
resolve(res);
if (res?.data instanceof Array) {
commit("SET_FORM_LIST", res.data);
commit(
"SET_COPY_FORM_LIST_ID",
res.data.map((i) => i.id)
);
} else {
console.warn("res.data not Array");
}
})
.catch((err) => {
reject(err);
});
});
},
};
export default {
namespaced: true,
state,
mutations,
actions,
};