/**
* @description 权限组件
*/
Vue.component('component-permission', {
template: `
`,
data() {
return {}
},
methods: {
/**
* @description 获取已被选中的权限ID数组
*/
getCheckedPomission() {
return this.$refs.tree.getCheckedKeys()
},
/**
* @description 节点被点击事件
*/
handleCheckChange(nodeData, isChecked) {
//商户角色设置需要对门店后台权限进行设置
//多级监听的原因是element在初始时,默认情况下未展开过的子节点是不会触发此方法的。无法在父节点变化时监听到子节点的变化。
//而render-after-expand设置false时,是可以初始化监听子节点,但是经过反复测试,在选择时,它的响应速度非常慢,1~2秒的延时。
//所以不需要为了一个监听事件而使所有使用当前组件的交互效果变差,故采用多级监听的方式。
//当它的一级父节点164,二级父节点165编号变化时,记得修改此处代码。
if (['164', '165', '170'].includes(nodeData.id)) {
this.$emit('check-change', nodeData.id, isChecked)
}
}
},
props: {
data: { //权限数据
type: Array,
default() {
return []
}
},
checkedKeys: { //已选择的权限
type: Array,
default() {
return []
}
},
defaultProps: { //默认属性字段名
type: Object,
default() {
return {
children: 'children',
label: 'label'
}
}
}
}
})
/**
* @description 编辑权限
*/
Vue.component('edit-next-role', {
template: `
`,
props: {
'show': {
type: Boolean,
default: false,
},
'type': {
type: Number,
default: 2,
},//1合伙人 2用户 3商户 4门店
'id': {
type: String,
default: ''
}
},
data() {
return {
permissionData: [], //权限数据
permissionCheckedKeys: [], //已选择的权限
permissionLoading: false, //权限提交加载
permissionDialogLogsVisible: false, //权限记录窗口
fatherIds: [], //父级ID列表
roleRequestData: {},
requestUrl: {
get: {
2: '/index.php/Admin/AdminRoles/getAgentRole',
3: '/index.php/Agent/AgentRoles/getMerchantRole',
4: '/index.php/Retail/MerchantRoles/getStoreRole',
},
set: {
2: '/index.php/Admin/AdminRoles/setAgentRole',
3: '/index.php/Agent/AgentRoles/setMerchantRole',
4: '/index.php/Retail/MerchantRoles/setStoreRole',
},
reset: {
2: '/index.php/Admin/AdminRoles/resetUserRole',
3: '/index.php/Agent/AgentRoles/resetUserRole',
4: '/index.php/Retail/MerchantRoles/resetUserRole',
}
},
}
},
methods: {
/**
* @description 用户权限设置窗口关闭事件
*/
permissionDialogClosed() {
this.$emit('update:show', false)
},
/**
* @description 获取用户权限
* @param {Number} agentId 用户ID
*/
getPermission() {
this.permissionLoading = true
this.sendRequest(this.requestUrl.get[this.type], {
data: this.roleRequestData,
success: (response) => {
if (response.status == 1) {
const data = response.data
this.permissionData = data.list || []
this.permissionCheckedKeys = data.checkIds || []
this.saveFatherIds(data.list)
} else {
this.$message.error(response.message)
}
this.permissionLoading = false
}
});
},
/**
* @description 用户权限设置提交
*/
permissionSubmit() {
this.permissionLoading = true
const checkedPomission = this.$refs['component-permission'].getCheckedPomission()
const checkIds = this.getArrDifference(checkedPomission, this.fatherIds);
this.roleRequestData.checkIds = checkIds.join(',')
this.sendRequest(this.requestUrl.set[this.type], {
data: this.roleRequestData,
success: (response) => {
if (response.status == 1) {
this.$message.success(response.message)
this.$emit('end')
this.permissionDialogClosed()
} else {
this.$message.error(response.message)
}
this.permissionLoading = false
}
});
},
getArrDifference(arr1, arr2) {
return arr1.filter(function (v) {
return arr2.indexOf(v) == -1;
});
},
saveFatherIds(list) {
list.forEach((item) => {
if (item.children) {
this.fatherIds.push(item.id)
this.saveFatherIds(item.children)
}
})
},
//重置权限
resetUserRole(){
this.$confirm("您确认将该用户的权限重置为初始状态吗?", "温馨提示", {
confirmButtonText: '确定',
type: 'info'
}).then(() => {
this.sendRequest(this.requestUrl.reset[this.type], {
data: this.roleRequestData,
success: (response) => {
if (response.status == 1) {
this.$message.success(response.message)
this.$emit('end')
this.permissionDialogClosed()
} else {
this.$message.error(response.message)
}
this.permissionLoading = false
}
});
}).catch(() => {
})
},
unitData() {
switch (this.type) {
case 2:
this.roleRequestData = {
agent_id: this.id
}
break
case 3:
this.roleRequestData = {
merchant_id: this.id
}
break
case 4:
this.roleRequestData = {
store_id: this.id
}
break
default:
this.$message.error('角色类型错误!')
return
}
this.getPermission();
}
},
mounted() {
if (this.show) {
this.unitData()
}
},
watch: {
show(n) {
if (n) {
this.unitData()
}
}
}
});
/**
* @description 权限修改记录
*/
Vue.component('component-permission-logs', {
template: `
`,
data() {
return {
logs: [], //权限修改记录
page: 1, //当前页数
limit: 10, //每页数量
count: 0, //记录总数
requestUrl: { //各个角色的记录获取路径
2: 'Admin/AdminRoles/getRoleRecord',
3: 'Agent/AgentRoles/getRoleRecord',
4: 'Retail/MerchantRoles/getRoleRecord'
},
roleRequestData: {}, //角色请求特有数据
operatorId: 0 //操作人ID
}
},
methods: {
search() {
this.page = 1
this.getLogs()
},
/**
* @description 获取记录数据
*/
getLogs() {
const requestData = Object.assign({
page: this.page,
limit: this.limit
}, this.roleRequestData)
if (this.operatorId > 0) {
requestData.operator_id = this.operatorId
}
this.sendRequest(this.requestUrl[this.type], {
data: requestData,
success: (response) => {
if (response.status == 1) {
const data = response.data
this.count = data.count
this.logs = data.list
}
}
});
},
},
created() {
if (!this.id) {
this.$message.error('角色编号错误!')
return
}
switch (this.type) {
case 2:
this.roleRequestData = {
agent_id: this.id
}
break
case 3:
this.roleRequestData = {
merchant_id: this.id
}
break
case 4:
this.roleRequestData = {
store_id: this.id
}
break
default:
this.$message.error('角色类型错误!')
return
}
this.getLogs()
},
//type:角色类型,1合伙人,2用户,3商户 4门店
//id:各个角色的主键ID
props: ['type', 'id']
})