diff --git a/commons/utils/hasPermission.js b/commons/utils/hasPermission.js new file mode 100644 index 0000000..375ee71 --- /dev/null +++ b/commons/utils/hasPermission.js @@ -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 +} +