refactor(api): 重构信用模块API请求和数据作用域逻辑
- 将companyId API中的参数处理改为查询字符串拼接方式 - 优化数据作用域工具函数,改用Taro存储并增加H5降级方案 - 统一所有信用相关API的响应数据结构访问方式 - 将API请求方法调用格式统一为更简洁的形式 - 修改文件上传接口的headers字段名为header以适配Taro - 更新批量删除接口的数据传递方式为直接传递数组参数
This commit is contained in:
63
src/utils/permission.ts
Normal file
63
src/utils/permission.ts
Normal file
@@ -0,0 +1,63 @@
|
||||
import Taro from '@tarojs/taro'
|
||||
|
||||
type StoredRole = { roleCode?: string; roleName?: string }
|
||||
type StoredUser = {
|
||||
userId?: number
|
||||
roleCode?: string
|
||||
roleName?: string
|
||||
roles?: StoredRole[]
|
||||
isAdmin?: boolean | number | string
|
||||
isSuperAdmin?: boolean | number | string
|
||||
}
|
||||
|
||||
const safeParseJSON = <T,>(v: any): T | null => {
|
||||
try {
|
||||
if (!v) return null
|
||||
if (typeof v === 'object') return v as T
|
||||
if (typeof v === 'string') return JSON.parse(v) as T
|
||||
return null
|
||||
} catch (_e) {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
export function getStoredUser(): StoredUser | null {
|
||||
try {
|
||||
const raw = Taro.getStorageSync('User')
|
||||
return safeParseJSON<StoredUser>(raw)
|
||||
} catch (_e) {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
export function hasRole(roleCode: string): boolean {
|
||||
const code = String(roleCode || '').trim()
|
||||
if (!code) return false
|
||||
|
||||
const user = getStoredUser()
|
||||
if (!user) return false
|
||||
|
||||
if (String(user.roleCode || '').trim() === code) return true
|
||||
if (Array.isArray(user.roles) && user.roles.some(r => String(r?.roleCode || '').trim() === code)) return true
|
||||
|
||||
// Fallback: some backends only expose roleName; keep it permissive for common admin codes.
|
||||
const name = String(user.roleName || '').trim()
|
||||
if (code === 'superAdmin' && (user as any)?.isSuperAdmin) return true
|
||||
if (code === 'admin' && (user as any)?.isAdmin) return true
|
||||
if (name && name.toLowerCase().includes(code.toLowerCase())) return true
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
export function isAdmin(): boolean {
|
||||
const user = getStoredUser()
|
||||
const v: any = user?.isAdmin
|
||||
return v === true || v === 1 || v === '1'
|
||||
}
|
||||
|
||||
export function isSuperAdmin(): boolean {
|
||||
const user = getStoredUser()
|
||||
const v: any = user?.isSuperAdmin
|
||||
return v === true || v === 1 || v === '1'
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user