Files
template-10579/src/api/credit/utils/data-scope.ts
赵忠林 31098f889b refactor(api): 重构信用模块API请求和数据作用域逻辑
- 将companyId API中的参数处理改为查询字符串拼接方式
- 优化数据作用域工具函数,改用Taro存储并增加H5降级方案
- 统一所有信用相关API的响应数据结构访问方式
- 将API请求方法调用格式统一为更简洁的形式
- 修改文件上传接口的headers字段名为header以适配Taro
- 更新批量删除接口的数据传递方式为直接传递数组参数
2026-03-05 12:13:46 +08:00

52 lines
1.1 KiB
TypeScript

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<T extends Record<string, any> | 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;
}