123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import React, { useState, useEffect } from 'react'
  2. import Taro, { useDidShow, useRouter } from '@tarojs/taro'
  3. import { useSelector } from 'react-redux'
  4. import { View } from '@tarojs/components'
  5. import Overlay from '@/components/Overlay'
  6. import Loading from '@/components/Loading'
  7. import AuthAvatar from '@/components/Auth/AuthAvatar'
  8. import AuthPhone from '@/components/Auth/AuthPhone'
  9. import Spin from '@/components/Spin/Spin2'
  10. import FixedConsultant from '@/components/FixedConsultant'
  11. import { ROLE_CODE } from '@/constants/user'
  12. import useAuth from './useAuth'
  13. import { routes } from '../routes'
  14. import './style.scss'
  15. export default (ChildComponent) => (props) => {
  16. const consultant = useSelector(s => s.system.consultant)
  17. const { spinning, userInfo } = useSelector(s => s.user)
  18. const { person, ...extInfo } = userInfo || {}
  19. const router = useRouter()
  20. const page = routes.filter((r) => (router.path.indexOf(r.page) > -1))[0]
  21. const [loading, setLoading] = useState(false)
  22. const [authPhone, authAvatar] = useAuth(person, page)
  23. const { id } = router.params
  24. const showConsultant = page.shortcut && page.shortcut.consultant
  25. // 页面分享
  26. const [shareContent, setShareContent] = useState({})
  27. // 页面埋点
  28. const [trackData, setTrackData] = useState({})
  29. // 请求埋点设置
  30. useEffect(() => {
  31. if (id) {
  32. setShareContent({})
  33. }
  34. } ,[id, page.type])
  35. // 埋点数据
  36. useEffect(() => {
  37. const consultantId = person?.personType === ROLE_CODE.CONSULTANT ? person.personId : undefined
  38. setTrackData({
  39. eventType: page.type,
  40. propertyName: page.name,
  41. consultantId,
  42. sharePersonId: person?.personId,
  43. targetId: id,
  44. })
  45. }, [person, page, id])
  46. // 主要用于埋点
  47. useEffect(() => {
  48. Taro.setStorage({ key: 'page', data: page })
  49. }, [page])
  50. useEffect(() => {
  51. setLoading(!person || !person.personId)
  52. // eslint-disable-next-line react-hooks/exhaustive-deps
  53. }, [person])
  54. useDidShow(() => {
  55. console.log('componentDidShow')
  56. })
  57. return (
  58. <>
  59. <Overlay visible={loading} style={{background: '#fff'}}>
  60. <Loading />
  61. </Overlay>
  62. <Overlay visible={authPhone} aligin='bottom'>
  63. <View className='auth-wrapper'>
  64. <AuthPhone consultant={consultant} router={router} page={page} />
  65. </View>
  66. </Overlay>
  67. <Overlay visible={!authPhone && authAvatar} aligin='bottom'>
  68. <View className='auth-wrapper'>
  69. <AuthAvatar />
  70. </View>
  71. </Overlay>
  72. <Spin size={32} spinning={spinning} />
  73. {
  74. person && !!person.personId && (
  75. <ChildComponent
  76. person={person}
  77. router={router}
  78. consultant={consultant}
  79. page={page}
  80. shareContent={shareContent}
  81. trackData={trackData}
  82. {...props}
  83. {...extInfo}
  84. />
  85. )
  86. }
  87. {
  88. !!showConsultant && (<FixedConsultant consultant={consultant} />)
  89. }
  90. </>
  91. )
  92. }