import fetch from "@/utils/fetch.js";
import { Message } from "element-ui";
export default {
data() {
return {
typeList: [],
typeListObj: {},
typeTree: [],
typeInfo: {
id: null,
pid: 0,
path: "0",
typeIcon: "",
typeName: "",
typeOrder: 0,
typeState: ""
},
formRules: {
typeName: [
{ required: true, message: "请输入分类名称" },
{ min: 1, max: 12, message: "长度1到12字符" }
]
}
};
},
created: function() {
this.load();
},
methods: {
load() {
//初始化数据
this.typeInfo = {
id: null,
pid: 0,
path: "0",
typeIcon: "",
typeName: "",
typeOrder: 0,
typeState: "1"
};
//获取数据
fetch
.get("/api/asarticlestype")
.then(res => {
if (res.code == "0") {
this.typeList = res.data;
this.typeTree = this.toTree(res.data, 0, "id", "typeName", "pid");
this.typeList.forEach(type => {
this.typeListObj[type.id] = type;
});
// console.log(res.data)
// console.log(this.typeListObj)
} else {
Message({
message: res.msg,
type: "error",
duration: 5 * 1000
});
}
})
.catch(error => {
//获取错误
console.log(error);
});
},
addType(pid) {
this.typeInfo = {
id: null,
pid: pid,
path: "0",
typeIcon: "",
typeName: "",
typeOrder: 0,
typeState: "1"
};
},
saveType() {
this.$refs["saveForm"].validate(valid => {
//this.$refs以减少获取dom节点的消耗
//validate确保组件具有有效的布局。此类主要适用于在 Container 实例上进行操作
if (valid) {
var path = this.getPath(this.typeInfo.pid);
this.typeInfo.path = path;
fetch.post("/api/articletype", this.typeInfo).then(res => {
if ((res.code = "0")) {
Message({
message: "操作成功",
type: "success",
duration: 3 * 1000
});
this.load();
} else {
Message({
message: res.msg,
type: "error",
duration: 5 * 1000
});
}
});
}
});
},
getPath(id) {
if (id == "0") {
return id;
} else {
return id + "," + this.getPath(this.typeListObj[id].pid);
}
},
typeTreeNodeClick(node) {
this.typeInfo = node;
},
getPname() {
if (this.typeInfo.id == null || this.typeInfo.pid == "0") {
return "无";
} else {
return this.typeListObj[this.typeInfo.pid].typeName;
}
},
deleteType() {
for (var i in this.typeList) {
var e = this.typeList[i];
console.log(e);
if (e.pid == this.typeInfo.id) {
Message({
message: "该类型下还有活动的子类型,不可删除!",
type: "error",
duration: 5 * 1000
});
return;
}
}
//后台验证该分类是否有文章信息,有则不可删除(待开发)
this.$confirm("是否删除类型", "提示", {
type: "warning",
confirmButtonText: "确定",
cancelButtonText: "取消"
}).then(() => {
fetch
.post("/api/asarticlestype/save", {
id: this.typeInfo.id,
typeState: "0"
})
.then(res => {
if ((res.code = "0")) {
Message({
message: "操作成功",
type: "success",
duration: 3 * 1000
});
this.load();
} else {
Message({
message: res.msg,
type: "error",
duration: 5 * 1000
});
}
});
});
}
}
};