增加全局权限验证方法

This commit is contained in:
YeMingfei666 2024-10-09 17:30:18 +08:00
parent 01610734c7
commit 1f499a3bd6
1 changed files with 163 additions and 0 deletions

View File

@ -0,0 +1,163 @@
import {
$hasPermission
} from '@/http/yskApi/shop.js'
import infoBox from '@/commons/utils/infoBox.js'
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: '允许管理会员信息',
text: 'yun_xu_guan_li_hui_yuan_xin_xi'
},
{
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'
}
]
}
function isChinese(str) {
for (var i = 0; i < str.length; i++) {
if (str.charCodeAt(i) >= 0x4E00 && str.charCodeAt(i) <= 0x9FFF) {
return true; // 是中文
}
}
return false; // 不是中文,全是英文或其他
}
function isObjectButNotArray(value) {
return typeof value === 'object' && Array.isArray(value) === false;
}
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
}
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('参数只能是字符串或者对象,不能是数组')
}
}
}
/**
* 判断是否有某权限
* @param {Object|String} params
*/
export async function hasPermission(params) {
//如果是商户默认拥有全部权限
const loginType=uni.getStorageSync('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 $hasPermission({
code: params.key
})
if (!res && option.tips) {
infoBox.showToast('您没有' + params.text + '权限!')
}
return res
}