refactor权限控制逻辑

- 移除全局方法getUserBtnRole
- 使用hasPermission函数替代checkBtnPermission
- 优化权限判断逻辑,减少冗余代码
- 统一权限控制的实现方式
This commit is contained in:
郑彪辉
2025-04-26 09:50:55 +08:00
parent 07ac63b5bb
commit 69af422128
28 changed files with 238 additions and 239 deletions

View File

@@ -100,7 +100,7 @@
:safeAreaInsetBottom="true" @close="unitSheetShow = false" @select="updateSexUnit"></u-action-sheet>
<view class="w-full footer">
<view v-if="hasPermission('erp_product_add')" class="w-full footer">
<view
class="py-[var(--top-m)] px-[var(--sidebar-m)] footer w-full fixed bottom-0 left-0 right-0 box-border">
<button hover-class="none"
@@ -120,9 +120,9 @@
import { generatedCode } from '@/addon/erp/utils/common';
import {uploadImage } from '@/app/api/system'
import { redirect } from '@/utils/common';
import { checkBtnPermission } from '@/utils/auth';
import useMemberStore from '@/stores/member'
import usePermission from '@/utils/usePermission'
const { hasPermission } = usePermission()
const memberStore = useMemberStore()
interface FileItem {
url: string;
@@ -273,8 +273,6 @@
// 保存
const handSave = async() => {
const hasPermission = await checkBtnPermission(memberStore.btnPermission, 'erp_product_add')
if (!hasPermission) return
formRef.value.validate().then(() => {
if (loading.value) return
loading.value = true

View File

@@ -46,7 +46,7 @@
<view><up-button :text="t('info')" size="mini"
@click="redirect({ url: '/addon/erp/pages/product/product/detail',param:{id: item.id} })"></up-button>
</view>
<view><up-button type="error" @click="hanldeDelete(item.id)" :text="t('delete')"
<view v-if="hasPermission('erp_product_add')"><up-button type="error" @click="hanldeDelete(item.id)" :text="t('delete')"
size="mini"></up-button></view>
</view>
</view>
@@ -60,7 +60,7 @@
</mescroll-body>
<loading-page :loading="loading"></loading-page>
<view class="fixed bottom-4 right-4 z-2">
<view v-if="hasPermission('erp_product_delete')" class="fixed bottom-4 right-4 z-2">
<up-button @click="redirect({ url: '/addon/erp/pages/product/product/add'})" type="primary" shape="circle"
:customStyle="{borderRadius: '50%',padding: 0,width: '50px',height: '50px'}">
<u-icon size="30" color="#fff" name="plus-circle-fill"></u-icon>
@@ -80,7 +80,8 @@
import useMescroll from '@/components/mescroll/hooks/useMescroll.js';
import { getProductList, deleteProduct } from '@/addon/erp/api/product';
import { onShow, onPageScroll, onReachBottom } from '@dcloudio/uni-app';
import usePermission from '@/utils/usePermission'
const { hasPermission } = usePermission()
const { downCallback, mescrollInit, getMescroll } = useMescroll(onPageScroll, onReachBottom);

View File

@@ -15,7 +15,7 @@
</u-form-item>
</view>
</u-form>
<view class="w-full footer">
<view v-if="hasPermission('erp_product_type_add')" class="w-full footer">
<view class="py-[var(--top-m)] px-[var(--sidebar-m)] footer w-full fixed bottom-0 left-0 right-0 box-border">
<button hover-class="none" class="!bg-[var(--primary-color)] text-[#fff] h-[80rpx] leading-[80rpx] rounded-[100rpx] text-[26rpx] font-500"
@click="handSave" :disabled="loading" :class="{'opacity-50': loading}">{{t('save')}}</button>
@@ -35,9 +35,9 @@
import { onLoad } from '@dcloudio/uni-app';
import { getProductTypeSelect, getProductTypeInfo, addProductType, editProductType } from '@/addon/erp/api/product';
import { redirect } from '@/utils/common';
import { checkBtnPermission } from '@/utils/auth';
import useMemberStore from '@/stores/member'
import usePermission from '@/utils/usePermission'
const { hasPermission } = usePermission()
const memberStore = useMemberStore()
const formRef: any = ref(null)
@@ -107,8 +107,6 @@
// 保存
const handSave = async() => {
const hasPermission = await checkBtnPermission(memberStore.btnPermission, 'erp_product_type_add')
if (!hasPermission) return
formRef.value.validate().then(() => {
if (loading.value) return
loading.value = true

View File

@@ -22,7 +22,7 @@
</mescroll-body>
<loading-page :loading="loading"></loading-page>
<view class="fixed bottom-4 right-4 z-2">
<view v-if="hasPermission('erp_product_type_add')" class="fixed bottom-4 right-4 z-2">
<up-button @click="redirect({ url: '/addon/erp/pages/product/type/add'})" type="primary" shape="circle"
:customStyle="{borderRadius: '50%',padding: 0,width: '50px',height: '50px'}">
<u-icon size="30" color="#fff" name="plus-circle-fill"></u-icon>
@@ -41,12 +41,19 @@
import useMescroll from '@/components/mescroll/hooks/useMescroll.js';
import { getProductTypeList, deleteProductType } from '@/addon/erp/api/product';
import { onShow, onPageScroll, onReachBottom } from '@dcloudio/uni-app';
import usePermission from '@/utils/usePermission'
const { downCallback, mescrollInit, getMescroll } = useMescroll(onPageScroll, onReachBottom);
const { hasPermission } = usePermission()
const show = ref(false);
interface BtnOption {
text: string;
style: {
backgroundColor: string;
};
}
// 获取系统状态栏的高度
let menuButtonInfo : any = {};
// 如果是小程序,获取右上角胶囊的尺寸信息,避免导航栏右侧内容与胶囊重叠(支付宝小程序非本API尚未兼容)
@@ -95,18 +102,28 @@
})
}
const btnOptions = ref([{
text: t('edit'),
style: {
backgroundColor: '#3c9cff'
const btnOptions = ref<BtnOption[]>([]);
const initBtn = () => {
btnOptions.value = []
const hasEdit = hasPermission.value('erp_product_type_edit')
const hasDel = hasPermission.value('erp_product_type_delete')
if (hasEdit) {
btnOptions.value.push({
text: t('edit'),
style: {
backgroundColor: '#3c9cff'
}
})
}
}, {
text: t('delete'),
style: {
backgroundColor: '#f56c6c'
if (hasDel) {
btnOptions.value.push({
text: t('delete'),
style: {
backgroundColor: '#f56c6c'
}
})
}
}])
}
const swipeActionRefs = ref<any[]>([]);
const setSwipeActionRef = (index : number) => (el : any[] | null) => {
@@ -146,6 +163,7 @@
}
}).catch(() => { });
}
initBtn()
</script>
<style>

View File

@@ -8,7 +8,7 @@
</u-form-item>
</view>
</u-form>
<view class="w-full footer">
<view v-if="hasPermission('erp_product_unit_add')" class="w-full footer">
<view class="py-[var(--top-m)] px-[var(--sidebar-m)] footer w-full fixed bottom-0 left-0 right-0 box-border">
<button hover-class="none" class="!bg-[var(--primary-color)] text-[#fff] h-[80rpx] leading-[80rpx] rounded-[100rpx] text-[26rpx] font-500"
@click="handSave" :disabled="loading" :class="{'opacity-50': loading}">{{t('save')}}</button>
@@ -28,9 +28,9 @@
import { onLoad } from '@dcloudio/uni-app';
import { getProductUnitInfo, addProductUnit, editProductUnit } from '@/addon/erp/api/product';
import { redirect } from '@/utils/common';
import { checkBtnPermission } from '@/utils/auth';
import useMemberStore from '@/stores/member'
import usePermission from '@/utils/usePermission'
const { hasPermission } = usePermission()
const memberStore = useMemberStore()
const formRef: any = ref(null)
@@ -71,8 +71,6 @@
// 保存
const handSave = async() => {
const hasPermission = await checkBtnPermission(memberStore.btnPermission, 'erp_product_unit_add')
if (!hasPermission) return
formRef.value.validate().then(() => {
if (loading.value) return
loading.value = true

View File

@@ -22,7 +22,7 @@
</mescroll-body>
<loading-page :loading="loading"></loading-page>
<view class="fixed bottom-4 right-4 z-2">
<view v-if="hasPermission('erp_product_unit_add')" class="fixed bottom-4 right-4 z-2">
<up-button @click="redirect({ url: '/addon/erp/pages/product/unit/add'})" type="primary" shape="circle"
:customStyle="{borderRadius: '50%',padding: 0,width: '50px',height: '50px'}">
<u-icon size="30" color="#fff" name="plus-circle-fill"></u-icon>
@@ -41,12 +41,17 @@
import useMescroll from '@/components/mescroll/hooks/useMescroll.js';
import { getProductUnitList, deleteProductUnit } from '@/addon/erp/api/product';
import { onShow, onPageScroll, onReachBottom } from '@dcloudio/uni-app';
import usePermission from '@/utils/usePermission'
const { downCallback, mescrollInit, getMescroll } = useMescroll(onPageScroll, onReachBottom);
const { hasPermission } = usePermission()
const show = ref(false);
interface BtnOption {
text: string;
style: {
backgroundColor: string;
};
}
// 获取系统状态栏的高度
let menuButtonInfo : any = {};
// 如果是小程序,获取右上角胶囊的尺寸信息,避免导航栏右侧内容与胶囊重叠(支付宝小程序非本API尚未兼容)
@@ -95,17 +100,28 @@
})
}
const btnOptions = ref([{
text: t('edit'),
style: {
backgroundColor: '#3c9cff'
const btnOptions = ref<BtnOption[]>([]);
const initBtn = () => {
btnOptions.value = []
const hasEdit = hasPermission.value('erp_product_unit_edit')
const hasDel = hasPermission.value('erp_product_unit_delete')
if (hasEdit) {
btnOptions.value.push({
text: t('edit'),
style: {
backgroundColor: '#3c9cff'
}
})
}
}, {
text: t('delete'),
style: {
backgroundColor: '#f56c6c'
if (hasDel) {
btnOptions.value.push({
text: t('delete'),
style: {
backgroundColor: '#f56c6c'
}
})
}
}])
}
const swipeActionRefs = ref<any[]>([]);
const setSwipeActionRef = (index : number) => (el : any[] | null) => {
@@ -145,6 +161,7 @@
}
}).catch(() => { });
}
initBtn()
</script>
<style>