w-vue-middle
Version:
统一公共服务组件
340 lines (328 loc) • 10.2 kB
JavaScript
/*
* @Author: Jason Liu
* @Date: 2022-10-12 13:15:29
* @Desc: 权限分配
*/
const $apps = require('w-vue-middle/api/apps');
export default {
name: 'permissionAssignment',
props: {
value: {
//是否显示
type: Boolean,
default() {
return false;
},
},
roleId: {
//角色ID
type: [Number, String],
},
asName: {
//授权名称
type: String,
},
},
data() {
return {
defaultMenu: [],
defaultOpen: [],
appList: [], //应用菜单列表
viewAppList: [],
loading: false,
show: this.value,
checkRowKeys: [],
tabKey: 0,
searchName: undefined, //搜索名称
buttonList: [], //按钮组
viewSetButton: false,
};
},
created() {},
methods: {
/**
* @Author: Jason Liu
* @description: 获取应用信息
*/
getAppMenus() {
this.loading = true;
Promise.all([$apps.getAllApp(), $apps.getRoleApps(this.roleId)])
.then((reqs) => {
let menu = [];
let authoritys = reqs[1].data;
let buttonList = [];
let appList = reqs[0].data.map((item) => {
let app = {
id: item.app.appId,
name: item.app.appName,
type: 1,
parentId: item.app.parentId,
};
if (item.menus.length > 0) {
const getMenu = (parentId = undefined) => {
let last = true;
item.menus.forEach((el) => {
if (el.menu.menuType == 'F') {
let button = { checked: false, ...el.menu };
let index = buttonList.findIndex((cc) => {
return cc.menuMapId == button.menuMapId;
});
if (index < 0) {
authoritys.forEach((t) => {
if (t.app.appId == item.app.appId) {
t.menus.forEach((m) => {
if (m.menu.menuMapId == button.menuMapId) {
button.checked = true;
}
});
}
});
buttonList.push(button);
}
} else if (el.menu.parentId == parentId) {
last = false;
let info = {
id: el.menu.menuMapId,
name: el.menu.menuName,
parentId: parentId || item.app.appId,
appId: item.app.appId,
icon: el.menu.icon,
};
menu.push(info);
let isLast = getMenu(el.menu.menuMapId);
if (isLast) {
try {
//提高查询效率
authoritys.forEach((t) => {
if (t.app.appId == item.app.appId) {
t.menus.forEach((m, index) => {
if (m.menu.menuMapId == info.id) {
this.checkRowKeys.push(m.menu.menuMapId);
//authoritys.splice(index, 1);
throw false;
}
});
}
});
} catch (error) {}
}
}
});
return last;
};
getMenu();
} else {
let subApps = reqs[0].data.filter((sub) => {
return sub.app.parentId == app.id;
});
if (subApps.length == 0) {
try {
//提高查询效率
authoritys.forEach((t, index) => {
if (t.app.appId == app.id) {
this.checkRowKeys.push(app.id);
throw false;
}
});
} catch (error) {}
}
}
return app;
});
this.appList = appList.concat(menu);
this.buttonList = buttonList;
this.onSearch();
//this.$refs.appList.loadData(this.appList);
})
.finally(() => {
// setTimeout(() => {
// this.checkRow.forEach(row => {
// this.$refs.appList.toggleCheckboxRow(row);
// });
// }, 1000);
this.loading = false;
});
},
/**
* @Author: Jason Liu
* @description: 复选框修改事件
*/
selectChangeEvent(e) {
if (e.rowid) {
let clearIds = [];
const setChildren = (row) => {
clearIds.push(row.id);
if (row.children && row.children.length > 0) {
row.children.forEach((item) => {
setChildren(item);
});
} else {
if (e.checked) {
this.checkRowKeys.push(row.id);
}
}
};
setChildren(e.row);
if (!e.checked) {
const clearParent = (parentId = undefined) => {
if (parentId) {
clearIds.push(parentId);
try {
this.viewAppList.forEach((item) => {
if (item.id == parentId) {
clearParent(item.parentId);
throw false;
}
});
} catch (error) {}
}
};
clearParent(e.row.parentId);
this.checkRowKeys = this.checkRowKeys.filter((item) => {
return !(clearIds.indexOf(item) > -1);
});
}
} else {
if (e.checked) {
e.records.forEach((item) => {
this.checkRowKeys.push(item.id);
});
} else {
this.checkRowKeys = this.checkRowKeys.filter((item) => {
return !(
this.viewAppList.findIndex((app) => {
return app.id == item;
}) > -1
);
});
}
}
},
/**
* @Author: Jason Liu
* @description: 提交授权请求
*/
submit() {
this.loading = true;
this.searchName = undefined;
this.onSearch();
setTimeout(() => {
let menus = this.$refs.appList.getCheckboxRecords();
let apps = [];
menus.forEach((menu) => {
let pass = true;
let app = apps.find((item) => {
return item.appId == menu.appId;
});
if (app) {
pass = false;
} else {
if (menu.type && menu.parentId) {
let parentApp = apps.find((item) => {
return item.appId == menu.parentId;
});
if (!parentApp) {
apps.push({
appId: menu.parentId,
menuMapIds: [menu.parentId],
});
}
}
app = {
appId: menu.appId || menu.id,
menuMapIds: [],
};
}
if (menu.appId != menu.parentId) {
if (app.menuMapIds.indexOf(menu.parentId) < 0) {
app.menuMapIds.push(menu.parentId);
}
}
if (app.menuMapIds.indexOf(menu.id) < 0) {
app.menuMapIds.push(menu.id); //上级菜单授权
this.buttonList.forEach((button) => {
if (button.checked && button.parentId == menu.id) {
app.menuMapIds.push(button.menuMapId);
}
});
}
if (pass) {
apps.push(app);
}
});
$apps
.saveRoleApps(this.roleId, apps)
.then((req) => {
this.$message.success($t('角色授权成功!'));
})
.finally(() => {
this.loading = false;
});
}, 100);
},
/**
* @Author: Jason Liu
* @description: 搜索菜单名称
*/
onSearch(val = undefined) {
if (val) {
let parentDatas = [];
const getPids = (parentId = undefined) => {
if (parentId) {
try {
this.appList.forEach((item) => {
let has = item.id == parentId;
if (has) {
parentDatas.push(item);
getPids(item.parentId);
throw false;
}
});
} catch (error) {}
}
};
let appView = this.appList.filter((item) => {
let has = item.name.indexOf(val) > -1;
if (has) {
getPids(item.parentId);
}
return has;
});
this.viewAppList = appView.concat(parentDatas);
} else {
this.viewAppList = this.appList;
}
// const xTable = this.$refs.appList;
// const isNoll = xTable.getColumnByField('name').filters[0];
// isNoll.data = this.searchName;
// isNoll.checked = !!this.searchName
// // 修改条件之后,需要手动调用 updateData 处理表格数据
// xTable.updateData();
this.tabKey++;
},
/**
* @Author: Jason Liu
* @description: 查询设置
*/
filterOutTableName({ option, row }) {
return row.name.indexOf(option.data) > -1;
},
},
watch: {
value(val) {
this.show = val;
if (val) {
this.searchName = undefined;
this.viewSetButton = $storage.get('system_authbutton_enable') == '1';
this.getAppMenus();
} else {
this.appMenus = [];
this.checkRowKeys = [];
this.buttonList = [];
}
},
show(val) {
this.$emit('input', val);
},
},
};