import Taro from '@tarojs/taro'; import { hasRole } from '@/utils/permission'; function isSuperAdmin(): boolean { try { return hasRole('superAdmin'); } catch { return false; } } function getCurrentUserId(): number | undefined { try { const raw = Taro.getStorageSync('UserId'); const n = Number(raw); if (Number.isFinite(n) && n > 0) return n; } catch { // ignore } // H5 fallback try { const raw = typeof localStorage !== 'undefined' ? localStorage.getItem('UserId') : null; if (!raw) return undefined; const n = Number(raw); return Number.isFinite(n) && n > 0 ? n : undefined; } catch { return undefined; } } /** * Credit module data scope: * - superAdmin: see all data * - others: only see data published by themselves (userId) */ export function withCreditUserScope | undefined>( params: T ): T { if (isSuperAdmin()) { return params; } const userId = getCurrentUserId(); if (!userId) { return params; } if (!params) { return ({ userId } as unknown) as T; } return { ...(params as any), userId } as T; }