24 changed files with 313 additions and 209 deletions
@ -0,0 +1,35 @@
@@ -0,0 +1,35 @@
|
||||
import type { AppRouteModule } from '/@/router/types'; |
||||
|
||||
import { getParentLayout, LAYOUT } from '/@/router/constant'; |
||||
import { t } from '/@/hooks/web/useI18n'; |
||||
|
||||
const permission: AppRouteModule = { |
||||
path: '/permission', |
||||
name: 'Permission', |
||||
component: LAYOUT, |
||||
redirect: '/permission/front/page', |
||||
meta: { |
||||
icon: 'ion:key-outline', |
||||
title: t('routes.demo.permission.permission'), |
||||
}, |
||||
children: [ |
||||
{ |
||||
path: 'page', |
||||
name: 'BackAuthPage', |
||||
component: () => import('/@/views/demo/permission/index.vue'), |
||||
meta: { |
||||
title: t('routes.demo.permission.backPage'), |
||||
}, |
||||
}, |
||||
{ |
||||
path: 'btn', |
||||
name: 'BackAuthBtn', |
||||
component: () => import('/@/views/demo/permission/Btn.vue'), |
||||
meta: { |
||||
title: t('routes.demo.permission.backBtn'), |
||||
}, |
||||
}, |
||||
], |
||||
}; |
||||
|
||||
export default permission; |
@ -0,0 +1,95 @@
@@ -0,0 +1,95 @@
|
||||
<template> |
||||
<PageWrapper contentBackground title="按钮权限控制" contentClass="p-4"> |
||||
<p> |
||||
当前拥有的code列表: <a> {{ userStore.getPermissions }} </a> |
||||
</p> |
||||
<Divider/> |
||||
<Alert |
||||
class="mt-4" |
||||
type="info" |
||||
message="点击后请查看按钮变化(必须处于后台权限模式才可测试此页面所展示的功能)" |
||||
show-icon |
||||
/> |
||||
<Divider/> |
||||
<a-button type="primary" class="mr-2" @click="switchToken(2)"> |
||||
点击切换按钮权限(用户id为2) |
||||
</a-button> |
||||
<a-button type="primary" @click="switchToken(1)"> |
||||
点击切换按钮权限(用户id为1,默认) |
||||
</a-button> |
||||
|
||||
<template> |
||||
<Divider>组件方式判断权限</Divider> |
||||
<Authority :value="'1000'"> |
||||
<a-button type="primary" class="mx-4"> 拥有code ['1000']权限可见 </a-button> |
||||
</Authority> |
||||
|
||||
<Authority :value="'2000'"> |
||||
<a-button color="success" class="mx-4"> 拥有code ['2000']权限可见 </a-button> |
||||
</Authority> |
||||
|
||||
<Authority :value="['1000', '2000']"> |
||||
<a-button color="error" class="mx-4"> 拥有code ['1000','2000']角色权限可见 </a-button> |
||||
</Authority> |
||||
|
||||
<Divider>函数方式方式判断权限</Divider> |
||||
<a-button v-if="hasPermission('1000')" type="primary" class="mx-4"> |
||||
拥有code ['1000']权限可见 |
||||
</a-button> |
||||
|
||||
<a-button v-if="hasPermission('2000')" color="success" class="mx-4"> |
||||
拥有code ['2000']权限可见 |
||||
</a-button> |
||||
|
||||
<a-button v-if="hasPermission(['1000', '2000'])" color="error" class="mx-4"> |
||||
拥有code ['1000','2000']角色权限可见 |
||||
</a-button> |
||||
|
||||
<Divider>指令方式方式判断权限(该方式不能动态修改权限.)</Divider> |
||||
<a-button v-auth="'1000'" type="primary" class="mx-4"> 拥有code ['1000']权限可见 </a-button> |
||||
|
||||
<a-button v-auth="'2000'" color="success" class="mx-4"> 拥有code ['2000']权限可见 </a-button> |
||||
|
||||
<a-button v-auth="['1000', '2000']" color="error" class="mx-4"> |
||||
拥有code ['1000','2000']角色权限可见 |
||||
</a-button> |
||||
</template> |
||||
</PageWrapper> |
||||
</template> |
||||
<script lang="ts"> |
||||
import { defineComponent } from 'vue'; |
||||
import { Alert, Divider } from 'ant-design-vue'; |
||||
import { usePermission } from '/@/hooks/web/usePermission'; |
||||
import { Authority } from '/@/components/Authority'; |
||||
import { PageWrapper } from '/@/components/Page'; |
||||
import { useUserStore } from '/@/store/modules/user'; |
||||
|
||||
export default defineComponent({ |
||||
components: { Alert, PageWrapper, Divider, Authority }, |
||||
setup() { |
||||
const { hasPermission } = usePermission(); |
||||
const userStore = useUserStore(); |
||||
|
||||
async function switchToken(userId: number) { |
||||
// 本函数切换用户登录Token的部分仅用于演示,实际生产时切换身份应当重新登录(目前处于待集成demo) |
||||
// 具体后端代码请参考com.cloud.kicc.common.data.entity.CasUser roleId切换字段 |
||||
const token = '46cdd008-4129-4d88-b845-a52d78b163db'; |
||||
userStore.setAccessToken(token); |
||||
|
||||
// 重新获取用户信息和菜单 |
||||
userStore.getUserInfoAction(); |
||||
} |
||||
|
||||
return { |
||||
hasPermission, |
||||
userStore, |
||||
switchToken, |
||||
}; |
||||
}, |
||||
}); |
||||
</script> |
||||
<style lang="less" scoped> |
||||
.demo { |
||||
background-color: @component-background; |
||||
} |
||||
</style> |
@ -0,0 +1,61 @@
@@ -0,0 +1,61 @@
|
||||
<template> |
||||
<PageWrapper |
||||
title="后台权限示例 用户角色切换" |
||||
contentBackground |
||||
contentClass="p-4" |
||||
content="目前mock了两组数据, id为1 和 2 具体返回的菜单可以在mock/sys/menu.ts内查看" |
||||
> |
||||
<Alert class="mt-4" |
||||
type="info" |
||||
message="点击后请查看左侧菜单变化" |
||||
show-icon |
||||
/> |
||||
<div class="mt-4"> |
||||
权限切换(请先切换权限模式为后台权限模式): |
||||
<Space> |
||||
<a-button @click="switchToken(1)"> |
||||
获取用户id为1的菜单 |
||||
</a-button> |
||||
<a-button @click="switchToken(2)"> |
||||
获取用户id为2的菜单 |
||||
</a-button> |
||||
</Space> |
||||
</div> |
||||
</PageWrapper> |
||||
</template> |
||||
<script lang="ts"> |
||||
import { defineComponent } from 'vue'; |
||||
import { usePermission } from '/@/hooks/web/usePermission'; |
||||
import { useUserStore } from '/@/store/modules/user'; |
||||
import { PageWrapper } from '/@/components/Page'; |
||||
import { Alert, Space } from 'ant-design-vue'; |
||||
|
||||
export default defineComponent({ |
||||
components: { Space, Alert, PageWrapper }, |
||||
setup() { |
||||
const { refreshMenu } = usePermission(); |
||||
const userStore = useUserStore(); |
||||
|
||||
async function switchToken(userId: number) { |
||||
// 本函数切换用户登录Token的部分仅用于演示,实际生产时切换身份应当重新登录 (目前处于待集成demo) |
||||
// 具体后端代码请参考com.cloud.kicc.common.data.entity.CasUser roleId切换字段 |
||||
const token = '46cdd008-4129-4d88-b845-a52d78b163db'; |
||||
userStore.setAccessToken(token); |
||||
|
||||
// 重新获取用户信息和菜单 |
||||
userStore.getUserInfoAction(); |
||||
refreshMenu(); |
||||
} |
||||
|
||||
return { |
||||
refreshMenu, |
||||
switchToken, |
||||
}; |
||||
}, |
||||
}); |
||||
</script> |
||||
<style lang="less" scoped> |
||||
.demo { |
||||
background-color: @component-background; |
||||
} |
||||
</style> |
@ -1,96 +0,0 @@
@@ -1,96 +0,0 @@
|
||||
<template> |
||||
<BasicModal |
||||
v-bind="$attrs" |
||||
centered |
||||
title="重置密码" |
||||
minHeight="100px" |
||||
@ok="handleSubmit" |
||||
@register="registerModal" |
||||
> |
||||
<Form :model="modelRef" :rules="rulesRef"> |
||||
<FormItem name="newPassword" v-bind="validateInfos.newPassword"> |
||||
<StrengthMeter v-model:value="modelRef.newPassword" placeholder="请输入重置密码"/> |
||||
</FormItem> |
||||
</Form> |
||||
</BasicModal> |
||||
</template> |
||||
|
||||
<script lang="ts" setup> |
||||
/** |
||||
* 提供模板规范代码参考,请尽量保证编写代码风格跟模板规范代码一致 |
||||
* 采用ant-design-vue原生组件编写表单,采用 setup 写法 |
||||
* Copyright © 2023-2023 <a href="https://godolphinx.org">海豚生态开源社区</a> All rights reserved. |
||||
* author wangxiang4 |
||||
*/ |
||||
import { reactive } from 'vue'; |
||||
import { BasicModal, ModalProps, useModalInner } from '/@/components/Modal'; |
||||
import { StrengthMeter } from '/@/components/StrengthMeter'; |
||||
import { Form } from 'ant-design-vue'; |
||||
import { resetPwd } from '/@/api/platform/system/controller/user'; |
||||
import { useMessage } from '/@/hooks/web/useMessage'; |
||||
|
||||
/** 类型规范统一声明定义区域 */ |
||||
interface WindowState { |
||||
id: string; |
||||
newPassword: string; |
||||
} |
||||
|
||||
/** 通用变量统一声明区域 */ |
||||
const { createMessage } = useMessage(); |
||||
/** https://v3.cn.vuejs.org/api/options-data.html#emits */ |
||||
const emit = defineEmits(['success', 'register']); |
||||
const FormItem = Form.Item; |
||||
const useForm = Form.useForm; |
||||
const modelRef = reactive<WindowState>({ |
||||
id: '', |
||||
newPassword: '' |
||||
}); |
||||
const rulesRef = reactive<Recordable>({ |
||||
newPassword: [ |
||||
{ |
||||
required: true, |
||||
whitespace: true, |
||||
message: '请输入密码!', |
||||
}, |
||||
{ |
||||
pattern: new RegExp('[^\\u4e00-\\u9fa5]+'), |
||||
type: 'string', |
||||
message: '密码不能输入汉字!', |
||||
}, |
||||
{ |
||||
min: 6, |
||||
max: 32, |
||||
message: '长度必需在6-32之间!', |
||||
} |
||||
] |
||||
}); |
||||
const { resetFields, clearValidate, validate, validateInfos } = useForm(modelRef, rulesRef); |
||||
const [registerModal, { setModalProps, closeModal }] = useModalInner( (data: BoxPayload) => { |
||||
// 处理清除脏数据 |
||||
resetFields(); |
||||
clearValidate(); |
||||
// 处理设置数据 |
||||
modelRef.id = data.record?.id; |
||||
const props: Partial<ModalProps> = { confirmLoading: false }; |
||||
// 尾部:设置处理后的最终配置数据 |
||||
setModalProps(props); |
||||
}); |
||||
|
||||
/** 处理模态框提交 */ |
||||
async function handleSubmit() { |
||||
try { |
||||
// 验证数据 |
||||
await validate(); |
||||
// 处理提交之前逻辑 |
||||
setModalProps({ confirmLoading: true }); |
||||
if (!modelRef.id) return createMessage.error('用户编号ID不存在,请检查!'); |
||||
await resetPwd(modelRef); |
||||
// 处理提交完成之后逻辑 |
||||
closeModal(); |
||||
emit('success'); |
||||
} finally { |
||||
setModalProps({ confirmLoading: false }); |
||||
} |
||||
} |
||||
|
||||
</script> |
Loading…
Reference in new issue