index.jsx 1.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import { useMemo } from 'react'
  2. import Taro from '@tarojs/taro'
  3. import { useModel } from '@/store'
  4. import useRouter from '@/utils/hooks/useRouter'
  5. import Loading from './Loading'
  6. import { getPageBy, getIndexPageOf } from '../routes'
  7. export default (Child) => (props) => {
  8. const { person } = useModel('person')
  9. const router = useRouter()
  10. // 确保人员信息到位
  11. const loading = !person?.personRole;
  12. // 校验人员角色及页面是否对应
  13. const jumpPage = useMemo(() => {
  14. const personRole = person?.personRole;
  15. if (!personRole) return;
  16. const isNormalPerson = personRole === 'normal'
  17. const currentPage = getPageBy(router.path)
  18. let indexPage = getIndexPageOf(personRole)
  19. if (!isNormalPerson) {
  20. indexPage = `${personRole}/${indexPage.page}`
  21. } else {
  22. indexPage = `pages/index/index`
  23. }
  24. const isMatched = isNormalPerson ?
  25. // 如果是普通客户, page.root 应该是空的
  26. !currentPage.root :
  27. // 其他角色, 应该是对应的
  28. currentPage.root === personRole
  29. // 如果对应上, 就不跳转
  30. return isMatched ? personRole : indexPage
  31. }, [router.path, person?.personRole])
  32. if (jumpPage) {
  33. // 注意此处跳转是没有加上原来的页面参数的
  34. Taro.reLaunch({
  35. url: `/${jumpPage}`,
  36. })
  37. }
  38. return loading ? <Loading /> : <Child {...props} person={person} router={router} />
  39. }