123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 FirstScreen from '@/components/FirstScreen'
  12. import { report as reportCustomer } from '@/utils/customer'
  13. import nav2Target from '@/utils/nav2Target'
  14. import { ROLE_CODE } from '@/constants/user'
  15. import useAuth from './useAuth'
  16. import useScreen from './useScreen'
  17. import { routes } from '../routes'
  18. import './style.scss'
  19. export default (ChildComponent) => (props) => {
  20. const consultant = useSelector(s => s.system.consultant)
  21. const city = useSelector(s => s.city.curCity)
  22. const { spinning, userInfo } = useSelector(s => s.user)
  23. const { person, ...extInfo } = userInfo || {}
  24. const router = useRouter()
  25. const page = routes.filter((r) => (router.path.indexOf(r.page) > -1))[0]
  26. const [loading, setLoading] = useState(false)
  27. const [authPhone, authAvatar] = useAuth(person, page)
  28. const { id } = router.params
  29. const showConsultant = page.shortcut && page.shortcut.consultant
  30. // 页面分享
  31. const [shareContent, setShareContent] = useState({})
  32. // 页面埋点
  33. const [trackData, setTrackData] = useState({})
  34. // 开屏广告
  35. const [screenInfo, screenVisible, toggleShowScreen] = useScreen(city.id, person)
  36. const handleScreen = () => {
  37. toggleShowScreen()
  38. nav2Target(screenInfo)
  39. }
  40. // 报备客户
  41. useEffect(() => {
  42. reportCustomer(person, consultant, false).catch(() => {})
  43. }, [person, consultant])
  44. // 请求埋点设置
  45. useEffect(() => {
  46. if (id) {
  47. setShareContent({})
  48. }
  49. } ,[id, page.type])
  50. // 埋点数据
  51. useEffect(() => {
  52. const consultantId = person?.personType === ROLE_CODE.CONSULTANT ? person.personId : undefined
  53. setTrackData({
  54. eventType: page.type,
  55. propertyName: page.name,
  56. consultantId,
  57. sharePersonId: person?.personId,
  58. targetId: id,
  59. })
  60. }, [person, page, id])
  61. // 主要用于埋点
  62. useEffect(() => {
  63. Taro.setStorage({ key: 'page', data: page })
  64. }, [page])
  65. useEffect(() => {
  66. setLoading(!person || !person.personId)
  67. // eslint-disable-next-line react-hooks/exhaustive-deps
  68. }, [person])
  69. return (
  70. <>
  71. <Overlay visible={loading} style={{background: '#fff'}}>
  72. <Loading />
  73. </Overlay>
  74. <Overlay visible={authPhone} aligin='bottom'>
  75. <View className='auth-wrapper'>
  76. <AuthPhone consultant={consultant} router={router} page={page} />
  77. </View>
  78. </Overlay>
  79. <Overlay visible={!authPhone && authAvatar} aligin='bottom'>
  80. <View className='auth-wrapper'>
  81. <AuthAvatar />
  82. </View>
  83. </Overlay>
  84. <Spin size={32} spinning={spinning} />
  85. {
  86. person && !!person.personId && (
  87. <ChildComponent
  88. person={person}
  89. router={router}
  90. consultant={consultant}
  91. page={page}
  92. city={city}
  93. shareContent={shareContent}
  94. trackData={trackData}
  95. {...props}
  96. {...extInfo}
  97. />
  98. )
  99. }
  100. {
  101. !!showConsultant && (<FixedConsultant consultant={consultant} />)
  102. }
  103. <FirstScreen
  104. info={screenInfo}
  105. visible={false}
  106. onClick={handleScreen}
  107. onClose={toggleShowScreen}
  108. />
  109. </>
  110. )
  111. }