(function (vc) {
|
vc.extends({
|
data: {
|
roleAInfo: {
|
all: false,
|
button: false,
|
header: false
|
},
|
pgId: '', // 保存角色编码
|
privilegeData: [], // 保存权限数据
|
pName: '全部', // 保存权限名称,默认为"全部"
|
buttonPrivileges: [], // 保存按钮权限
|
labelPrivileges: [], // 保存标签权限
|
dataPrivileges: [], // 保存数据权限
|
},
|
_initMethod: function () {
|
// 获取URL参数
|
let pId = '';
|
let pgId = '';
|
let communityId = '';
|
let type = '';
|
let pName = '全部'; // 默认值为"全部"
|
|
// 优先从 hash 中获取参数(单页应用使用 hash 路由)
|
const hash = location.hash;
|
const search = location.search;
|
|
// 先尝试从 hash 中获取
|
if (hash && hash.indexOf('?') !== -1) {
|
const hashParams = hash.substring(hash.indexOf('?') + 1);
|
const params = hashParams.split('&');
|
for (let i = 0; i < params.length; i++) {
|
const param = params[i].split('=');
|
const key = param[0];
|
const value = decodeURIComponent(param[1] || '');
|
if (key === 'pId') {
|
pId = value;
|
} else if (key === 'pgId') {
|
pgId = value;
|
} else if (key === 'communityId') {
|
communityId = value;
|
} else if (key === 'type') {
|
type = value;
|
} else if (key === 'pName') {
|
pName = value;
|
}
|
}
|
}
|
|
// 如果 hash 中没有,尝试从 search 中获取
|
if (!pId) {
|
pId = vc.getParam('pId') || '';
|
}
|
if (!pgId) {
|
pgId = vc.getParam('pgId') || '';
|
}
|
if (!communityId) {
|
communityId = vc.getParam('communityId') || '';
|
}
|
if (!type) {
|
type = vc.getParam('type') || '';
|
}
|
if (pName === '全部') {
|
pName = vc.getParam('pName') || '全部';
|
}
|
|
// 如果缺少必要参数,使用默认值或从当前社区获取
|
if (!communityId) {
|
communityId = vc.getCurrentCommunity().communityId;
|
}
|
|
// 保存 pgId、pId 和 pName 到 data 中
|
vc.component.pgId = pgId;
|
vc.component.pId = pId;
|
vc.component.pName = pName;
|
|
// 更新页面标题
|
const titleElement = document.querySelector('.role-a-container h3');
|
if (titleElement) {
|
titleElement.textContent = pName;
|
}
|
|
// 调用接口
|
if (pgId && communityId) {
|
let param = {
|
params: {
|
pgId: pgId,
|
communityId: communityId,
|
pId: pId,
|
type: 'all'
|
}
|
};
|
|
vc.http.apiGet('/query.privilegeGroup.noAddPrivilege',
|
param,
|
function (json) {
|
// 接口调用成功处理
|
try {
|
// 判断 json 的类型,如果已经是对象则直接使用,否则进行解析
|
let data = typeof json === 'string' ? JSON.parse(json) : json;
|
vc.component.privilegeData = data;
|
vc.component.buttonPrivileges = data.filter(item => item.type === '按钮');
|
vc.component.labelPrivileges = data.filter(item => item.type === '标签');
|
vc.component.dataPrivileges = data.filter(item => item.type === '数据');
|
vc.component.renderPrivileges(data);
|
vc.component.initCheckboxEvents();
|
} catch (e) {
|
console.error('解析数据失败:', e);
|
console.error('原始数据:', json);
|
console.error('数据类型:', typeof json);
|
vc.toast('数据解析失败');
|
}
|
},
|
function () {
|
console.log('请求失败处理');
|
vc.toast('加载数据失败');
|
}
|
);
|
} else {
|
console.warn('缺少必要参数,无法调用接口');
|
}
|
},
|
methods: {
|
goBack: function () {
|
vc.goBack();
|
},
|
// 渲染权限复选框
|
renderPrivileges: function (data) {
|
const buttonContainer = document.getElementById('button-container');
|
const labelContainer = document.getElementById('label-container');
|
const dataContainer = document.getElementById('data-container');
|
|
// 清空容器
|
if (buttonContainer) buttonContainer.innerHTML = '';
|
if (labelContainer) labelContainer.innerHTML = '';
|
if (dataContainer) dataContainer.innerHTML = '';
|
|
// 遍历数据,根据 type 创建对应的复选框
|
data.forEach(function (item) {
|
const checkboxDiv = document.createElement('div');
|
checkboxDiv.className = 'role-a-checkbox';
|
checkboxDiv.style.gap = '10px';
|
checkboxDiv.style.display = 'flex';
|
checkboxDiv.style.alignItems = 'center';
|
|
const label = document.createElement('label');
|
label.textContent = item.pName;
|
label.style.paddingTop = '8px';
|
|
const checkbox = document.createElement('input');
|
checkbox.type = 'checkbox';
|
checkbox.name = 'privilege';
|
checkbox.value = item.pId;
|
checkbox.setAttribute('data-pid', item.pId);
|
checkbox.setAttribute('data-type', item.type);
|
checkbox.checked = true;
|
|
checkboxDiv.appendChild(checkbox);
|
checkboxDiv.appendChild(label);
|
|
// 根据 type 添加到对应容器
|
if (item.type === '按钮') {
|
if (buttonContainer) {
|
buttonContainer.appendChild(checkboxDiv);
|
}
|
} else if (item.type === '标签') {
|
if (labelContainer) {
|
labelContainer.appendChild(checkboxDiv);
|
}
|
} else if (item.type === '数据') {
|
if (dataContainer) {
|
dataContainer.appendChild(checkboxDiv);
|
}
|
}
|
});
|
},
|
// 初始化复选框事件
|
initCheckboxEvents: function () {
|
const self = this;
|
|
// 全部复选框
|
const allCheckbox = document.getElementById('all');
|
if (allCheckbox) {
|
allCheckbox.addEventListener('change', function () {
|
const isChecked = this.checked;
|
// 选中所有复选框
|
const allCheckboxes = document.querySelectorAll('input[type="checkbox"][name="privilege"]');
|
allCheckboxes.forEach(function (cb) {
|
cb.checked = isChecked;
|
});
|
// 同时更新按钮和数据区域的"全部"复选框
|
const buttonAll = document.getElementById('button-all');
|
const labelAll = document.getElementById('label-all');
|
const dataAll = document.getElementById('data-all');
|
if (buttonAll) buttonAll.checked = isChecked;
|
if (labelAll) labelAll.checked = isChecked;
|
if (dataAll) dataAll.checked = isChecked;
|
});
|
}
|
|
// 按钮区域全部复选框
|
const buttonAllCheckbox = document.getElementById('button-all');
|
if (buttonAllCheckbox) {
|
buttonAllCheckbox.addEventListener('change', function () {
|
const isChecked = this.checked;
|
// 选中按钮区域所有复选框
|
const buttonCheckboxes = document.querySelectorAll('#button-container input[type="checkbox"][name="privilege"]');
|
buttonCheckboxes.forEach(function (cb) {
|
cb.checked = isChecked;
|
});
|
// 检查是否需要更新"全部"复选框
|
self.updateAllCheckbox();
|
});
|
}
|
|
// 标签区域全部复选框
|
const labelAllCheckbox = document.getElementById('label-all');
|
if (labelAllCheckbox) {
|
labelAllCheckbox.addEventListener('change', function () {
|
const isChecked = this.checked;
|
// 选中标签区域所有复选框
|
const labelCheckboxes = document.querySelectorAll('#label-container input[type="checkbox"][name="privilege"]');
|
labelCheckboxes.forEach(function (cb) {
|
cb.checked = isChecked;
|
});
|
// 检查是否需要更新"全部"复选框
|
self.updateAllCheckbox();
|
});
|
}
|
|
// 数据区域全部复选框
|
const dataAllCheckbox = document.getElementById('data-all');
|
if (dataAllCheckbox) {
|
dataAllCheckbox.addEventListener('change', function () {
|
const isChecked = this.checked;
|
// 选中数据区域所有复选框
|
const dataCheckboxes = document.querySelectorAll('#data-container input[type="checkbox"][name="privilege"]');
|
dataCheckboxes.forEach(function (cb) {
|
cb.checked = isChecked;
|
});
|
// 检查是否需要更新"全部"复选框
|
self.updateAllCheckbox();
|
});
|
}
|
|
// 监听所有权限复选框的变化,更新"全部"复选框状态
|
const privilegeCheckboxes = document.querySelectorAll('input[type="checkbox"][name="privilege"]');
|
privilegeCheckboxes.forEach(function (checkbox) {
|
checkbox.addEventListener('change', function () {
|
self.updateAllCheckbox();
|
self.updateSectionAllCheckbox();
|
});
|
});
|
|
// 保存按钮事件
|
const saveButton = document.getElementById('save');
|
if (saveButton) {
|
saveButton.addEventListener('click', function () {
|
self.savePrivileges();
|
});
|
}
|
|
// 重置按钮事件
|
const resetButton = document.getElementById('reset');
|
if (resetButton) {
|
resetButton.addEventListener('click', function () {
|
// 选中所有权限复选框
|
const allCheckboxes = document.querySelectorAll('input[type="checkbox"][name="privilege"]');
|
allCheckboxes.forEach(function (cb) {
|
cb.checked = true;
|
});
|
// 选中所有"全部"复选框
|
const allCheckbox = document.getElementById('all');
|
const buttonAll = document.getElementById('button-all');
|
const labelAll = document.getElementById('label-all');
|
const dataAll = document.getElementById('data-all');
|
if (allCheckbox) allCheckbox.checked = true;
|
if (buttonAll) buttonAll.checked = true;
|
if (labelAll) labelAll.checked = true;
|
if (dataAll) dataAll.checked = true;
|
});
|
}
|
},
|
// 更新"全部"复选框状态
|
updateAllCheckbox: function () {
|
const allCheckboxes = document.querySelectorAll('input[type="checkbox"][name="privilege"]');
|
const allChecked = Array.from(allCheckboxes).every(function (cb) {
|
return cb.checked;
|
});
|
const allCheckbox = document.getElementById('all');
|
if (allCheckbox) {
|
allCheckbox.checked = allChecked;
|
}
|
},
|
// 更新区域"全部"复选框状态
|
updateSectionAllCheckbox: function () {
|
// 更新按钮区域
|
const buttonCheckboxes = document.querySelectorAll('#button-container input[type="checkbox"][name="privilege"]');
|
const buttonAllChecked = buttonCheckboxes.length > 0 && Array.from(buttonCheckboxes).every(function (cb) {
|
return cb.checked;
|
});
|
const buttonAll = document.getElementById('button-all');
|
if (buttonAll) {
|
buttonAll.checked = buttonAllChecked;
|
}
|
|
// 更新标签区域
|
const labelCheckboxes = document.querySelectorAll('#label-container input[type="checkbox"][name="privilege"]');
|
const labelAllChecked = labelCheckboxes.length > 0 && Array.from(labelCheckboxes).every(function (cb) {
|
return cb.checked;
|
});
|
const labelAll = document.getElementById('label-all');
|
if (labelAll) {
|
labelAll.checked = labelAllChecked;
|
}
|
|
// 更新数据区域
|
const dataCheckboxes = document.querySelectorAll('#data-container input[type="checkbox"][name="privilege"]');
|
const dataAllChecked = dataCheckboxes.length > 0 && Array.from(dataCheckboxes).every(function (cb) {
|
return cb.checked;
|
});
|
const dataAll = document.getElementById('data-all');
|
if (dataAll) {
|
dataAll.checked = dataAllChecked;
|
}
|
},
|
// 保存权限
|
savePrivileges: function () {
|
const self = this;
|
// 获取所有选中的复选框
|
const checkedCheckboxes = document.querySelectorAll('input[type="checkbox"][name="privilege"]:checked');
|
|
// 构建请求数据
|
const pIds = [];
|
checkedCheckboxes.forEach(function (checkbox) {
|
pIds.push({
|
pId: checkbox.value
|
});
|
});
|
|
const requestData = {
|
pgId: vc.component.pgId,
|
parentId: vc.component.pId,
|
pIds: pIds
|
};
|
|
// 调用保存接口
|
vc.http.apiPost('/add.privilege.PrivilegeGroup',
|
requestData,
|
{},
|
function (json) {
|
try {
|
// 判断 json 的类型,如果已经是对象则直接使用,否则进行解析
|
let result = typeof json === 'string' ? JSON.parse(json) : json;
|
vc.toast('保存成功');
|
console.log('保存成功:', result);
|
// 保存成功后返回上一页
|
setTimeout(function () {
|
vc.goBack();
|
}, 1000);
|
} catch (e) {
|
console.error('解析响应失败:', e);
|
vc.toast('保存成功');
|
// 保存成功后返回上一页
|
setTimeout(function () {
|
vc.goBack();
|
}, 1000);
|
}
|
},
|
function (error) {
|
console.error('保存失败:', error);
|
vc.toast('保存失败');
|
}
|
);
|
}
|
}
|
});
|
})(window.vc);
|