gaoluyang
3 天以前 92230c9a97dc9ce9df3313d11d26999c04bb6b26
src/utils/permission.ts
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,51 @@
import useUserStore from '@/store/modules/user'
/**
 * å­—符权限校验
 * @param {Array} value æ ¡éªŒå€¼
 * @returns {Boolean}
 */
export function checkPermi(value:Array<string>) {
  if (value && value instanceof Array && value.length > 0) {
    const permissions:Array<string> = useUserStore().permissions
    const permissionDatas = value
    const all_permission = "*:*:*"
    const hasPermission = permissions.some(permission => {
      return all_permission === permission || permissionDatas.includes(permission)
    })
    if (!hasPermission) {
      return false
    }
    return true
  } else {
    console.error(`need roles! Like checkPermi="['system:user:add','system:user:edit']"`)
    return false
  }
}
/**
 * è§’色权限校验
 * @param {Array} value æ ¡éªŒå€¼
 * @returns {Boolean}
 */
export function checkRole(value:Array<string>) {
  if (value && value instanceof Array && value.length > 0) {
    const roles:Array<string> = useUserStore().roles
    const permissionRoles = value
    const super_admin = "admin"
    const hasRole = roles.some(role => {
      return super_admin === role || permissionRoles.includes(role)
    })
    if (!hasRole) {
      return false
    }
    return true
  } else {
    console.error(`need roles! Like checkRole="['admin','editor']"`)
    return false
  }
}