import Mock from 'mockjs'
let catalogData = []
for(let i = 1;i < Math.floor(Math.random() * 10, 2) + 20; i++){
var fileType
if(i % 2 === 0){
fileType = '2'
}else{
fileType = '1'
}
catalogData.push({
id:'' + i,
catalogCode:"CODE000" + i,
catalogName:"人员照片" + i,
fileType:fileType,
})
}
export default ({ mock }) => {
if (!mock) return;
//预置项catalogData查找 获取数据
Mock.mock('/api/blade-business/catalogData', 'get', ({ body }) => {
const data = JSON.parse(body)
let filterCatalogData = [...catalogData]
if(data){
let catalogName = data.catalogName || undefined
if(catalogName){
filterCatalogData = filterCatalogData.filter(item => item.catalogName == catalogName)
}
}
return {
code: 200,
data: filterCatalogData,
msg: '操作成功'
}
});
//新增 复制
Mock.mock('/api/blade-business/addCatalog', 'post', ({ body }) => {
const { data } = JSON.parse(body)
const dataTips = {
catalogName: "文件名称不能为空",
}
for (const key of Object.keys(dataTips)) {
if (!data[key]) {
return { code: 400, message: dataTips[key] }
}
}
catalogData.push({
id:functionData.length + 1,
catalogCode:data.catalogCode,
catalogName:data.catalogName,
fileType:data.fileType,
})
return {
code: 200,
success: true,
msg: '新增成功'
}
});
//删除
Mock.mock('/api/blade-business/deleteCatalog', 'post', ({ body }) => {
const data = JSON.parse(body)
let idArr = data.ids.split(",")
idArr.forEach((id)=>{
catalogData = catalogData.filter(item => item.id != id)
})
return {
code: 200,
success: true,
msg: '删除成功'
}
});
//修改
Mock.mock('/api/blade-business/updateCatalog', 'post', ({ body }) => {
const { data } = JSON.parse(body)
let id = data.id;
let catalogCode = data.catalogCode;
let catalogName = data.catalogName;
let fileType = data.fileType;
catalogData.forEach((item,index)=>{
if(item.id == id){
catalogData[index].catalogCode = catalogCode
catalogData[index].catalogName = catalogName
catalogData[index].fileType = fileType
}
})
return {
code: 200,
success: true,
msg: '修改成功'
}
});
}
使用,根据场景不同做调整,我这是弹窗的
//获取数据,查
const params = {...this.searchForm}
axios.get('/api/blade-business/catalogData',{data:params}).then((res)=>{
this.catalogData = res.data.data
})
//新增 修改
params = {...this.catalogForm}
axios.post('/api/blade-business/addCatalog',{data:params}).then((res)=>{
if(res.data.code == 200){
this.onLoad()
this.catalogdialogIsShow = false;
this.copycatalogDialogIsShow = false
this.catalogForm = {catalogCode:'1'}
}
})
axios.post('/api/blade-business/updateCatalog',{data:params}).then((res)=>{
if(res.data.code == 200){
this.onLoad()
this.catalogdialogIsShow = false;
this.copycatalogDialogIsShow = false
this.catalogForm = {catalogCode:'1'}
}
})
//复制
params = {...this.selectionList[0],catalogName:this.copyCatalogForm.catalogName}
axios.post('/api/blade-business/addCatalog',{data:params}).then((res)=>{
if(res.data.code == 200){
this.onLoad()
this.catalogdialogIsShow = false;
this.copycatalogDialogIsShow = false
this.catalogForm = {catalogCode:'1'}
}
})
//删除,可删除多个
ids(){
let ids = [];
this.selectionList.forEach(ele => {
ids.push(ele.id);
});
return ids.join(",");
},
axios.post('/api/blade-business/deleteCatalog',{ids:this.ids}).then((res)=>{
if(res.data.code == 200){
this.onLoad()
this.$message.success("删除成功!")
}
})