refactor(auth): 统一权限校验并重构权限指令为响应式实现 - #668
Open
Airomeo wants to merge 1 commit into
Open
Conversation
将分散在组件与指令中的权限判断收敛至 utils/authFunction,修复历史不一致与无法响应更新的问题。 - stores/userInfo: 新增 authBtnSet getter,对 authBtnList 统一 trim/toLowerCase/过滤空值并缓存为 Set,指令与组件复用同一实例,避免每按钮重复遍历 - utils/authFunction: 重构 auth/auths/authAll,统一处理 isDebug 直放、unknown 入参校验、大小写不敏感及去空格,authAll 对空数组返回 false(原 judementSameArr 返回 true),移除对 arrayOperation 的依赖 - directive/authDirective: 以 hidden 替代 removeChild,支持权限及 isDebug 变更后实时更新;集中 Map 管理所有指令元素,共享单一 watch,unmounted 时清理并在无元素时释放 watcher;保留 wasInitiallyHidden 语义,初始 hidden 的元素保持隐藏避免权限越权显示 - components/auth: auth/auths/authAll 组件移除直连 store 的重复逻辑,改为调用统一工具函数,computed 自动追踪权限变化 - 修正指令注释中 :v-auths 错误写法为 v-auths BREAKING CHANGE: 权限校验现统一大小写不敏感;authAll([]) 由 true 改为 false 源提交: be41bae04d382616606fc68429d14de22b816a6f (MES) 适用分支: dotnet8
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
背景
当前
WalkingTec.Mvvm.Vue3Demo前端权限逻辑存在以下问题:校验分散不一致:
v-auth指令使用toLowerCase()大小写不敏感auth/auths/authAll组件与utils/authFunction使用严格相等===v-auths未做大小写处理,v-auth-all依赖judementSameArr(大小写敏感)无法响应式更新:指令在
mounted后通过removeChild直接删除 DOM,后续authBtnList/isDebug变化不会触发重新显示/隐藏;且每个指令未监听binding.value变化。性能问题:每个按钮都遍历
authBtnList,未缓存;auths/authAll存在双层map嵌套。健壮性:未对
undefined/null/非字符串入参做保护;authAll([])误返回true。本 PR 将修复完整移植到 WTM 的
demo/WalkingTec.Mvvm.Vue3Demo,适用分支dotnet8。变更内容
涉及文件(6 个):
demo/WalkingTec.Mvvm.Vue3Demo/ClientApp/src/stores/userInfo.tsdemo/WalkingTec.Mvvm.Vue3Demo/ClientApp/src/utils/authFunction.tsdemo/WalkingTec.Mvvm.Vue3Demo/ClientApp/src/directive/authDirective.tsdemo/WalkingTec.Mvvm.Vue3Demo/ClientApp/src/components/auth/auth.vuedemo/WalkingTec.Mvvm.Vue3Demo/ClientApp/src/components/auth/auths.vuedemo/WalkingTec.Mvvm.Vue3Demo/ClientApp/src/components/auth/authAll.vue1.
stores/userInfo.ts新增
authBtnSetgetter:authBtnList变化时重建trim+toLowerCase+ 过滤空值,所有校验复用同一Set,O(1)查找2.
utils/authFunction.tsarrayOperation.judementSameArr的依赖unknown,增加类型守卫,非法入参直接返回falseisDebug === true直放auth/auths/authAll均基于authBtnSet实现大小写不敏感、去空格authAll修正:空数组返回false(原true)3.
directive/authDirective.tshidden替代removeChild,支持权限/调试模式变更后实时更新显隐Map<HTMLElement, State>管理所有指令元素,共享单一watch([isDebug, authBtnSet])updated钩子同步binding.value变化unmounted时清理Map,无元素时释放watcher,避免内存泄漏wasInitiallyHidden语义,初始hidden的元素始终保持隐藏,防止越权显示4.
components/auth/*auth.vue/auths.vue/authAll.vue移除直连store的重复遍历逻辑,改为调用utils/authFunction的统一函数,computed自动追踪authBtnSet/isDebug变化。BREAKING CHANGE
authAll([])由true改为false测试
vue-tsc/eslint(如有)isDebug=true时所有v-auth*均显示authBtnList大小写、首尾空格后指令与组件实时更新v-auth="Btn.Add"与authBtnList=["btn.add"]匹配成功authAll([])/auth(undefined)返回falsehidden元素不会因权限通过而被显示