feat(user): 优化用户验证页面的机构查询逻辑

- 添加 fetchUserInfo 方法到 useUser hook 用于刷新用户信息
- 实现 normalizeOrganizationIds 函数处理商户ID数组格式化
- 添加从本地存储获取用户商户信息的逻辑
- 当本地无商户信息时通过接口刷新用户数据
- 根据用户商户权限动态调整机构查询参数
- 修复组织ID数组去重和过滤空值问题
- 优化 useEffect 依赖数组避免无效重复加载
This commit is contained in:
2026-01-13 16:54:38 +08:00
parent 96b75c6cab
commit 07894f63a2
2 changed files with 61 additions and 18 deletions

View File

@@ -41,4 +41,5 @@ export interface OrganizationParam extends PageParam {
keywords?: string;
limit?: number;
parentId?: number;
organizationIds?: any;
}

View File

@@ -25,7 +25,7 @@ const UserVerifyAdmin: React.FC = () => {
const [keywords, setKeywords] = useState<string>('')
// const [showTextArea, setShowTextArea] = useState<boolean>(false)
const [refreshing, setRefreshing] = useState<boolean>(false)
const {user} = useUser()
const {user, fetchUserInfo} = useUser()
console.log(refreshing)
// 获取状态显示
@@ -52,25 +52,66 @@ const UserVerifyAdmin: React.FC = () => {
keywords: keywords.trim(),
}
// if (Taro.getStorageSync('RoleCode') == 'zhandian') {
// @ts-ignore
// where.organizationId = Taro.getStorageSync('OrganizationId')
// }
// if (Taro.getStorageSync('RoleCode') == 'kuaidi') {
// @ts-ignore
// where.OrganizationParentId = Taro.getStorageSync('OrganizationParentId')
// }
// 如果当前登录用户可管理商户存在则直接按商户ID过滤
const normalizeOrganizationIds = (value: any): any[] | undefined => {
if (value == null) return undefined
if (Array.isArray(value)) return value
if (typeof value === 'string') {
const trimmed = value.trim()
if (!trimmed) return []
try {
const parsed = JSON.parse(trimmed)
if (Array.isArray(parsed)) return parsed
} catch (_e) {}
return trimmed
.split(',')
.map(s => s.trim())
.filter(Boolean)
}
return [value]
}
const organizations = await listOrganizations({
parentId: Taro.getStorageSync('OrganizationId')
})
const organizationIds = organizations.map(item => item.organizationId)
// 把Taro.getStorageSync('OrganizationId')也放入数组
organizationIds.push(Taro.getStorageSync('OrganizationId'))
let merchants = user?.merchants
if (merchants == null) {
const storedUser = Taro.getStorageSync('User')
if (storedUser) {
if (typeof storedUser === 'string') {
try {
merchants = JSON.parse(storedUser)?.merchants
} catch (_e) {}
} else {
merchants = storedUser?.merchants
}
}
}
// 本地没有拿到 merchants 时,尝试从接口刷新一次用户信息
if (merchants == null && user?.userId) {
const latestUser = await fetchUserInfo()
merchants = latestUser?.merchants
}
// 查询机构列表:如果 merchants 存在,则用 organizationIds=merchants 来查
const organizationIdsParam = normalizeOrganizationIds(merchants)
const hasMerchants = Boolean(organizationIdsParam && organizationIdsParam.length > 0)
const organizations = await listOrganizations(
hasMerchants
? // @ts-ignore
{ organizationIds: organizationIdsParam }
: { parentId: Taro.getStorageSync('OrganizationId') }
)
const organizationIds = organizations.map(item => item.organizationId).filter(Boolean) as any[]
// 把 merchants/当前机构 也放入数组
if (hasMerchants) {
organizationIds.push(...(organizationIdsParam as any[]))
} else {
organizationIds.push(Taro.getStorageSync('OrganizationId'))
}
console.log(organizationIds,'organizationIds')
// @ts-ignore
where.organizationIds = organizationIds;
where.organizationIds = Array.from(new Set(organizationIds))
const res = await pageUserVerify(where)
@@ -168,8 +209,9 @@ const UserVerifyAdmin: React.FC = () => {
}
useEffect(() => {
if (!user?.userId) return
reload().then()
}, [])
}, [user?.userId])
return (
<>