add:[permission]: add checkPermission function

This commit is contained in:
Pan 2018-06-19 17:52:05 +08:00
parent 3f479664b6
commit 03691739e1
2 changed files with 39 additions and 0 deletions

26
src/utils/permission.js Normal file
View file

@ -0,0 +1,26 @@
import store from '@/store'
/**
* @param {Array} value
* @returns {Boolean}
* @example see @/views/permission/directive.vue
*/
export default function checkPermission(value) {
if (value && value instanceof Array && value.length > 0) {
const roles = store.getters && store.getters.roles
const permissionRoles = value
const hasPermission = roles.some(role => {
return permissionRoles.includes(role)
})
if (!hasPermission) {
return false
}
return true
} else {
console.error(`need roles! Like v-permission="['admin','editor']"`)
return false
}
}

View file

@ -16,11 +16,23 @@
<el-tag class="permission-tag" size="small">editor</el-tag> can see this <el-tag class="permission-tag" size="small">editor</el-tag> can see this
</span> </span>
</div> </div>
<div style="margin-top:30px;" :key="'checkPermission'+key">
<code>In some cases it is not suitable to use v-permission, such as element Tab component which can only be achieved by manually setting the v-if.
<br> e.g.
</code>
<el-tabs type="border-card" style="width:500px;">
<el-tab-pane v-if="checkPermission(['admin'])" label="Admin">Admin can see this</el-tab-pane>
<el-tab-pane v-if="checkPermission(['editor'])" label="Editor">Editor can see this</el-tab-pane>
<el-tab-pane v-if="checkPermission(['admin','editor'])" label="Admin-OR-Editor">Both admin or editor can see this</el-tab-pane>
</el-tabs>
</div>
</div> </div>
</template> </template>
<script> <script>
import permission from '@/directive/permission/index.js' // import permission from '@/directive/permission/index.js' //
import checkPermission from '@/utils/permission' //
import SwitchRoles from './components/SwitchRoles' import SwitchRoles from './components/SwitchRoles'
export default{ export default{
@ -33,6 +45,7 @@ export default{
} }
}, },
methods: { methods: {
checkPermission,
handleRolesChange() { handleRolesChange() {
this.key++ this.key++
} }