feat(user): 改为动态查询 VIP 角色ID,避免写死角色ID

- RoleParam 类型新增 tenantId 字段,支持按租户过滤角色
- 新增 getCurrentTenantId 获取当前租户ID,支持登录态和默认租户回退
- 实现 getVipRoleId 函数,按 roleCode 与 tenantId 查询 VIP 角色ID
- assignVipRole 中动态获取 VIP 角色ID,避免硬编码
- 添加用户角色时带上 tenantId,确保多租户角色绑定正确
- 移除写死的 VIP_ROLE_ID,改用动态查询结果,提高跨租户兼容性
- 补充引入 listRoles 和 TenantId,完善相关依赖引用
This commit is contained in:
2026-07-21 21:07:08 +08:00
parent 43093b505b
commit eeb63efe9f
4 changed files with 56 additions and 18 deletions

View File

@@ -24,4 +24,6 @@ export interface RoleParam extends PageParam {
roleName?: string;
roleCode?: string;
comments?: string;
// 租户ID多租户隔离按租户查询角色
tenantId?: number | string;
}

View File

@@ -4,6 +4,8 @@ import Taro, { useDidShow } from '@tarojs/taro'
import { listShopDealerApply, updateShopDealerApply } from '@/api/shop/shopDealerApply'
import type { ShopDealerApply } from '@/api/shop/shopDealerApply/model'
import { addUserRole, listUserRole } from '@/api/system/userRole'
import { listRoles } from '@/api/system/role'
import { TenantId } from '@/config/app'
import { getMyClerk } from '@/api/shop/shopStoreUser'
definePageConfig({
@@ -20,10 +22,25 @@ const TABS: { key: TabKey; label: string; status: number }[] = [
]
// VIP 角色信息(对应 system/role 表)
const VIP_ROLE_ID = 2032
// 角色 ID 不写死,按 roleCode + 当前租户ID 动态查询
const VIP_ROLE_CODE = 'vip'
const VIP_ROLE_NAME = 'VIP会员'
// 获取当前租户 ID登录时写入 storage未登录时回退到默认租户
function getCurrentTenantId(): number | string | undefined {
return Taro.getStorageSync('TenantId') || TenantId
}
/**
* 按 roleCode=vip + 当前租户ID 动态查询 VIP 角色,返回 roleId
*/
async function getVipRoleId(): Promise<number | undefined> {
const tenantId = getCurrentTenantId()
const roles = await listRoles({ roleCode: VIP_ROLE_CODE, tenantId })
const vip = (roles || []).find(r => r.roleCode === VIP_ROLE_CODE)
return vip?.roleId
}
// 格式化时间戳
function formatTime(time?: number | string): string {
if (!time) return '-'
@@ -56,15 +73,23 @@ function formatDateTime(date: Date): string {
/**
* 给用户添加 VIP 角色
* 角色 ID 按 roleCode=vip + 当前租户ID 动态查询,避免写死;
* 先查询是否已有 VIP 角色,没有则新增,避免重复绑定
*/
async function assignVipRole(userId?: number): Promise<void> {
if (!userId) return
// 动态查询当前租户下的 VIP 角色
const vipRoleId = await getVipRoleId()
if (!vipRoleId) {
Taro.showToast({ title: '未找到 VIP 角色配置', icon: 'none' })
return
}
// 查询用户已有角色
const userRoles = await listUserRole({ userId })
const hasVip = (userRoles || []).some(
r => r.roleId === VIP_ROLE_ID || r.roleCode === VIP_ROLE_CODE
r => r.roleId === vipRoleId || r.roleCode === VIP_ROLE_CODE
)
if (hasVip) {
@@ -75,9 +100,10 @@ async function assignVipRole(userId?: number): Promise<void> {
// 新增 VIP 角色绑定
await addUserRole({
userId,
roleId: VIP_ROLE_ID,
roleId: vipRoleId,
roleCode: VIP_ROLE_CODE,
roleName: VIP_ROLE_NAME,
tenantId: getCurrentTenantId(),
})
}