(function () {
|
function authHeaders() {
|
return {
|
token: localStorage.getItem('token') || ''
|
};
|
}
|
|
function isForbidden(res) {
|
return res && Number(res.code) === 403;
|
}
|
|
function isOk(res) {
|
return res && Number(res.code) === 200;
|
}
|
|
function normalizeId(value) {
|
if (value === null || value === undefined || value === '') {
|
return null;
|
}
|
var numberValue = Number(value);
|
return isNaN(numberValue) ? value : numberValue;
|
}
|
|
function normalizeNumber(value, fallback) {
|
if (value === null || value === undefined || value === '') {
|
return fallback;
|
}
|
var numberValue = Number(value);
|
return isNaN(numberValue) ? fallback : numberValue;
|
}
|
|
function createPermissionForm() {
|
return {
|
id: null,
|
name: '',
|
action: '',
|
resourceId: null,
|
status: 1
|
};
|
}
|
|
function escapeHtml(value) {
|
return String(value == null ? '' : value)
|
.replace(/&/g, '&')
|
.replace(/</g, '<')
|
.replace(/>/g, '>')
|
.replace(/"/g, '"')
|
.replace(/'/g, ''');
|
}
|
|
function downloadExcel(filename, headers, rows) {
|
var headHtml = headers.map(function (title) {
|
return '<th style="mso-number-format:\\@;">' + escapeHtml(title) + '</th>';
|
}).join('');
|
var bodyHtml = rows.map(function (row) {
|
return '<tr>' + row.map(function (cell) {
|
return '<td style="mso-number-format:\\@;">' + escapeHtml(cell) + '</td>';
|
}).join('') + '</tr>';
|
}).join('');
|
var workbook = [
|
'<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel">',
|
'<head><meta charset="UTF-8"></head>',
|
'<body><table border="1"><tr>',
|
headHtml,
|
'</tr>',
|
bodyHtml,
|
'</table></body></html>'
|
].join('');
|
var blob = new Blob([workbook], {type: 'application/vnd.ms-excel;charset=utf-8;'});
|
var link = document.createElement('a');
|
link.href = URL.createObjectURL(blob);
|
link.download = filename;
|
document.body.appendChild(link);
|
link.click();
|
document.body.removeChild(link);
|
URL.revokeObjectURL(link.href);
|
}
|
|
new Vue({
|
el: '#app',
|
data: function () {
|
return {
|
loading: false,
|
exportLoading: false,
|
resourceSearchLoading: false,
|
dialogResourceLoading: false,
|
tableHeight: Math.max(window.innerHeight - 220, 360),
|
tableData: [],
|
selection: [],
|
pagination: {
|
curr: 1,
|
limit: 16,
|
total: 0
|
},
|
searchForm: {
|
id: '',
|
resourceId: null
|
},
|
resourceSearchOptions: [],
|
permissionDialog: {
|
visible: false,
|
mode: 'create',
|
readonly: false,
|
submitting: false
|
},
|
permissionForm: createPermissionForm(),
|
permissionRules: {
|
name: [
|
{required: true, message: '请输入权限名称', trigger: 'blur'}
|
],
|
action: [
|
{required: true, message: '请输入接口地址', trigger: 'blur'}
|
],
|
status: [
|
{required: true, message: '请选择状态', trigger: 'change'}
|
]
|
},
|
dialogResourceOptions: [],
|
resourceDetail: {
|
visible: false,
|
data: {}
|
}
|
};
|
},
|
created: function () {
|
this.loadList();
|
},
|
mounted: function () {
|
window.addEventListener('resize', this.handleResize);
|
},
|
beforeDestroy: function () {
|
window.removeEventListener('resize', this.handleResize);
|
},
|
methods: {
|
handleResize: function () {
|
this.tableHeight = Math.max(window.innerHeight - 220, 360);
|
this.refreshTableLayout();
|
},
|
handleForbidden: function (res) {
|
if (isForbidden(res)) {
|
top.location.href = baseUrl + '/';
|
return true;
|
}
|
return false;
|
},
|
refreshTableLayout: function () {
|
var vm = this;
|
this.$nextTick(function () {
|
if (vm.$refs.dataTable && vm.$refs.dataTable.doLayout) {
|
vm.$refs.dataTable.doLayout();
|
}
|
});
|
},
|
currentQuery: function () {
|
return {
|
curr: this.pagination.curr,
|
limit: this.pagination.limit,
|
id: $.trim(this.searchForm.id),
|
resourceId: this.searchForm.resourceId
|
};
|
},
|
loadList: function () {
|
var vm = this;
|
vm.loading = true;
|
$.ajax({
|
url: baseUrl + '/permission/list/auth',
|
method: 'GET',
|
headers: authHeaders(),
|
data: vm.currentQuery(),
|
success: function (res) {
|
vm.loading = false;
|
if (vm.handleForbidden(res)) {
|
return;
|
}
|
if (!isOk(res)) {
|
vm.$message.error(res && res.msg ? res.msg : '权限加载失败');
|
return;
|
}
|
var data = res.data || {};
|
vm.tableData = data.records || [];
|
vm.pagination.total = data.total || 0;
|
vm.selection = [];
|
vm.refreshTableLayout();
|
},
|
error: function () {
|
vm.loading = false;
|
vm.$message.error('权限加载失败');
|
}
|
});
|
},
|
fetchResourceOptions: function (keyword, target) {
|
var vm = this;
|
var loadingKey = target === 'search' ? 'resourceSearchLoading' : 'dialogResourceLoading';
|
vm[loadingKey] = true;
|
$.ajax({
|
url: baseUrl + '/resourceQuery/auth',
|
method: 'GET',
|
headers: authHeaders(),
|
data: {
|
condition: keyword || ''
|
},
|
success: function (res) {
|
vm[loadingKey] = false;
|
if (vm.handleForbidden(res)) {
|
return;
|
}
|
if (!isOk(res)) {
|
vm.$message.error(res && res.msg ? res.msg : '菜单查询失败');
|
return;
|
}
|
if (target === 'search') {
|
vm.resourceSearchOptions = res.data || [];
|
} else {
|
vm.dialogResourceOptions = res.data || [];
|
}
|
},
|
error: function () {
|
vm[loadingKey] = false;
|
vm.$message.error('菜单查询失败');
|
}
|
});
|
},
|
searchResourceOptions: function (keyword) {
|
this.fetchResourceOptions(keyword, 'search');
|
},
|
searchDialogResourceOptions: function (keyword) {
|
this.fetchResourceOptions(keyword, 'dialog');
|
},
|
ensureDialogResourceOption: function (id, label) {
|
if (!id || !label) {
|
return;
|
}
|
var exists = this.dialogResourceOptions.some(function (item) {
|
return normalizeId(item.id) === normalizeId(id);
|
});
|
if (!exists) {
|
this.dialogResourceOptions = [{
|
id: normalizeId(id),
|
value: label
|
}].concat(this.dialogResourceOptions);
|
}
|
},
|
handleSelectionChange: function (rows) {
|
this.selection = rows || [];
|
},
|
handleSearch: function () {
|
this.pagination.curr = 1;
|
this.loadList();
|
},
|
handleReset: function () {
|
this.searchForm.id = '';
|
this.searchForm.resourceId = null;
|
this.resourceSearchOptions = [];
|
this.pagination.curr = 1;
|
this.loadList();
|
},
|
handleCurrentChange: function (page) {
|
this.pagination.curr = page;
|
this.loadList();
|
},
|
handleSizeChange: function (size) {
|
this.pagination.limit = size;
|
this.pagination.curr = 1;
|
this.loadList();
|
},
|
resetPermissionDialog: function () {
|
this.permissionForm = createPermissionForm();
|
this.dialogResourceOptions = [];
|
var vm = this;
|
this.$nextTick(function () {
|
if (vm.$refs.permissionForm) {
|
vm.$refs.permissionForm.clearValidate();
|
}
|
});
|
},
|
openCreateDialog: function () {
|
this.permissionDialog.mode = 'create';
|
this.permissionDialog.readonly = false;
|
this.permissionDialog.visible = true;
|
this.resetPermissionDialog();
|
},
|
openEditDialog: function (row) {
|
this.permissionDialog.mode = 'edit';
|
this.permissionDialog.readonly = false;
|
this.permissionDialog.visible = true;
|
this.permissionForm = {
|
id: normalizeId(row.id),
|
name: row.name || '',
|
action: row.action || '',
|
resourceId: normalizeId(row.resourceId),
|
status: normalizeNumber(row.status, 1)
|
};
|
this.dialogResourceOptions = [];
|
this.ensureDialogResourceOption(row.resourceId, row.resourceName);
|
var vm = this;
|
this.$nextTick(function () {
|
if (vm.$refs.permissionForm) {
|
vm.$refs.permissionForm.clearValidate();
|
}
|
});
|
},
|
openDetailDialog: function (row) {
|
this.permissionDialog.mode = 'edit';
|
this.permissionDialog.readonly = true;
|
this.permissionDialog.visible = true;
|
this.permissionForm = {
|
id: normalizeId(row.id),
|
name: row.name || '',
|
action: row.action || '',
|
resourceId: normalizeId(row.resourceId),
|
status: normalizeNumber(row.status, 1)
|
};
|
this.dialogResourceOptions = [];
|
this.ensureDialogResourceOption(row.resourceId, row.resourceName);
|
var vm = this;
|
this.$nextTick(function () {
|
if (vm.$refs.permissionForm) {
|
vm.$refs.permissionForm.clearValidate();
|
}
|
});
|
},
|
submitPermission: function () {
|
var vm = this;
|
if (!vm.$refs.permissionForm) {
|
return;
|
}
|
vm.$refs.permissionForm.validate(function (valid) {
|
if (!valid) {
|
return false;
|
}
|
vm.permissionDialog.submitting = true;
|
$.ajax({
|
url: baseUrl + '/permission/' + (vm.permissionDialog.mode === 'create' ? 'add' : 'update') + '/auth',
|
method: 'POST',
|
headers: authHeaders(),
|
data: {
|
id: vm.permissionDialog.mode === 'edit' ? normalizeId(vm.permissionForm.id) : null,
|
name: $.trim(vm.permissionForm.name),
|
action: $.trim(vm.permissionForm.action),
|
resourceId: normalizeId(vm.permissionForm.resourceId),
|
status: normalizeNumber(vm.permissionForm.status, 1)
|
},
|
success: function (res) {
|
vm.permissionDialog.submitting = false;
|
if (vm.handleForbidden(res)) {
|
return;
|
}
|
if (!isOk(res)) {
|
vm.$message.error(res && res.msg ? res.msg : '保存失败');
|
return;
|
}
|
vm.$message.success(res.msg || '保存成功');
|
vm.permissionDialog.visible = false;
|
vm.loadList();
|
},
|
error: function () {
|
vm.permissionDialog.submitting = false;
|
vm.$message.error('保存失败');
|
}
|
});
|
return true;
|
});
|
},
|
removeSelection: function () {
|
var vm = this;
|
if (!vm.selection.length) {
|
vm.$message.warning('请选择数据');
|
return;
|
}
|
var ids = vm.selection.map(function (row) {
|
return normalizeId(row.id);
|
}).filter(function (id) {
|
return id !== null;
|
});
|
vm.$confirm('确定删除选中权限吗?', '提示', {
|
confirmButtonText: '确定',
|
cancelButtonText: '取消',
|
type: 'warning'
|
}).then(function () {
|
$.ajax({
|
url: baseUrl + '/permission/delete/auth',
|
method: 'POST',
|
headers: authHeaders(),
|
traditional: true,
|
data: {
|
ids: ids
|
},
|
success: function (res) {
|
if (vm.handleForbidden(res)) {
|
return;
|
}
|
if (!isOk(res)) {
|
vm.$message.error(res && res.msg ? res.msg : '删除失败');
|
return;
|
}
|
vm.$message.success(res.msg || '删除成功');
|
if (vm.tableData.length === ids.length && vm.pagination.curr > 1) {
|
vm.pagination.curr -= 1;
|
}
|
vm.loadList();
|
},
|
error: function () {
|
vm.$message.error('删除失败');
|
}
|
});
|
}).catch(function () {
|
});
|
},
|
exportRows: function () {
|
var vm = this;
|
vm.exportLoading = true;
|
$.ajax({
|
url: baseUrl + '/permission/export/auth',
|
method: 'POST',
|
headers: authHeaders(),
|
dataType: 'json',
|
contentType: 'application/json;charset=UTF-8',
|
data: JSON.stringify({
|
permission: {
|
id: $.trim(vm.searchForm.id),
|
resourceId: vm.searchForm.resourceId
|
},
|
fields: ['id', 'name', 'action', 'resourceName', 'status$']
|
}),
|
success: function (res) {
|
vm.exportLoading = false;
|
if (vm.handleForbidden(res)) {
|
return;
|
}
|
if (!isOk(res)) {
|
vm.$message.error(res && res.msg ? res.msg : '导出失败');
|
return;
|
}
|
downloadExcel('权限管理.xls', ['ID', '权限名称', '接口地址', '所属菜单', '状态'], res.data || []);
|
},
|
error: function () {
|
vm.exportLoading = false;
|
vm.$message.error('导出失败');
|
}
|
});
|
},
|
openResourceDetail: function (row) {
|
var vm = this;
|
if (!row.resourceId) {
|
vm.$message.warning('无所属菜单');
|
return;
|
}
|
$.ajax({
|
url: baseUrl + '/resource/' + row.resourceId + '/auth',
|
method: 'GET',
|
headers: authHeaders(),
|
success: function (res) {
|
if (vm.handleForbidden(res)) {
|
return;
|
}
|
if (!isOk(res)) {
|
vm.$message.error(res && res.msg ? res.msg : '菜单详情加载失败');
|
return;
|
}
|
vm.resourceDetail.data = res.data || {};
|
vm.resourceDetail.visible = true;
|
},
|
error: function () {
|
vm.$message.error('菜单详情加载失败');
|
}
|
});
|
}
|
},
|
watch: {
|
'permissionDialog.visible': function (visible) {
|
if (!visible) {
|
this.permissionDialog.submitting = false;
|
this.permissionDialog.readonly = false;
|
this.resetPermissionDialog();
|
}
|
},
|
'resourceDetail.visible': function (visible) {
|
if (!visible) {
|
this.resourceDetail.data = {};
|
}
|
}
|
}
|
});
|
})();
|