员工权限校验
This commit is contained in:
153
src/utils/limits.js
Normal file
153
src/utils/limits.js
Normal file
@@ -0,0 +1,153 @@
|
||||
import { getHasPermission } from "@/api/shop";
|
||||
import { Notification } from 'element-ui'
|
||||
|
||||
const userInfo = JSON.parse(localStorage.getItem("userInfo"));
|
||||
const $PermissionObj = {
|
||||
data: [{
|
||||
key: 'yun_xu_cha_kan_jing_ying_shu_ju',
|
||||
text: '允许查看经营数据'
|
||||
},
|
||||
{
|
||||
key: 'yun_xu_cha_kan_suo_you_jiao_ban_ji_lu',
|
||||
text: '允许查看所有交班记录'
|
||||
}
|
||||
],
|
||||
default: [{
|
||||
key: 'yun_xu_xia_dan',
|
||||
text: '允许下单'
|
||||
},
|
||||
{
|
||||
key: 'yun_xu_shou_kuan',
|
||||
text: '允许收款'
|
||||
},
|
||||
{
|
||||
key: 'yun_xu_tui_kuan',
|
||||
text: '允许退款'
|
||||
},
|
||||
{
|
||||
key: 'yun_xu_jiao_ban',
|
||||
text: '允许交班'
|
||||
}
|
||||
],
|
||||
goods: [{
|
||||
key: 'yun_xu_xiu_gai_shang_pin',
|
||||
text: '允许修改商品'
|
||||
},
|
||||
{
|
||||
key: 'yun_xu_shang_xia_jia_shang_pin',
|
||||
text: '允许上下架商品'
|
||||
},
|
||||
{
|
||||
key: 'yun_xu_xiu_gai_fen_lei',
|
||||
text: '允许修改分类'
|
||||
},
|
||||
{
|
||||
key: 'yun_xu_xiu_gai_fen_zu',
|
||||
text: '允许修改分组'
|
||||
}
|
||||
],
|
||||
discount:[
|
||||
{
|
||||
key: 'yun_xu_da_zhe',
|
||||
text: '允许打折'
|
||||
}
|
||||
],
|
||||
vip:[
|
||||
{
|
||||
key: 'yun_xu_guan_li_hui_yuan_xin_xi',
|
||||
text: '允许管理会员信息'
|
||||
},
|
||||
{
|
||||
key: 'yun_xu_xiu_gai_hui_yuan_yu_e',
|
||||
text: '允许修改会员余额'
|
||||
}
|
||||
],
|
||||
stock:[
|
||||
{
|
||||
text: '允许提交报损',
|
||||
key: 'yun_xu_ti_jiao_bao_sun'
|
||||
},
|
||||
{
|
||||
text: '允许沽清',
|
||||
key: 'yun_xu_gu_qing'
|
||||
},
|
||||
{
|
||||
text: '允许售罄商品',
|
||||
key: 'yun_xu_shou_qing_shang_pin'
|
||||
},
|
||||
{
|
||||
text:'允许修改商品库存',
|
||||
key:'yun_xu_xiu_gai_shang_pin_ku_cun'
|
||||
},
|
||||
{
|
||||
text: '允许耗材入库',
|
||||
key: 'yun_xu_hao_cai_ru_ku'
|
||||
},
|
||||
{
|
||||
text: '允许耗材出库',
|
||||
key: 'yun_xu_hao_cai_chu_ku'
|
||||
},
|
||||
{
|
||||
text: '允许耗材盘点',
|
||||
key: 'yun_xu_hao_cai_pan_dian'
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
export async function hasPermission (params) {
|
||||
//如果是商户默认拥有全部权限
|
||||
const loginType = localStorage.getItem('loginType')
|
||||
if(loginType=='merchant'){
|
||||
return true
|
||||
}
|
||||
params = returnFormatParams(params)
|
||||
if (!params) {
|
||||
return infoBox.showToast('未找到相关权限,请检查代码或在权限配置文件commons/utils/hasPermission.js文件进行修改或增加')
|
||||
}
|
||||
const option = Object.assign({
|
||||
tips: true,
|
||||
key: '',
|
||||
text: ''
|
||||
}, params)
|
||||
const res = await getHasPermission({
|
||||
userId : userInfo.id,
|
||||
|
||||
code: params.key
|
||||
})
|
||||
if (!res && option.tips) {
|
||||
Notification.error({
|
||||
title: '您没有' + params.text + '权限!',
|
||||
duration: 5000
|
||||
})
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
export function isObjectButNotArray(value) {
|
||||
return typeof value === 'object' && Array.isArray(value) === false;
|
||||
}
|
||||
|
||||
export function findPermissionObj(str) {
|
||||
for (let i in $PermissionObj) {
|
||||
const obj = $PermissionObj[i].find(v => v.key == str || v.text == str)
|
||||
if (obj) {
|
||||
return obj
|
||||
break
|
||||
}
|
||||
}
|
||||
console.error('未找到相关权限配置,请检查权限配置文件commons/utils/hasPermission.js文件进行修改或增加')
|
||||
return false
|
||||
}
|
||||
|
||||
export function returnFormatParams(params) {
|
||||
if (typeof params === 'string') {
|
||||
return findPermissionObj(params)
|
||||
} else {
|
||||
if (isObjectButNotArray(params)) {
|
||||
const obj=findPermissionObj(params.key || params.text)
|
||||
return {...params,...obj}
|
||||
} else {
|
||||
console.error('参数只能是字符串或者对象,不能是数组')
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user