index.js 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 AuthPage from '@/components/Auth/AuthPage'
  10. import Spin from '@/components/Spin/Spin2'
  11. import FixedConsultant from '@/components/FixedConsultant'
  12. import FirstScreen from '@/components/FirstScreen'
  13. import ShareToCircle from '@/components/ShareToCircle'
  14. import { report as reportCustomer } from '@/utils/customer'
  15. import nav2Target from '@/utils/nav2Target'
  16. import { ROLE_CODE } from '@/constants/user'
  17. import useAuth from './useAuth'
  18. import useScreen from './useScreen'
  19. import { routes } from '../routes'
  20. import './style.scss'
  21. export default (ChildComponent) => (props) => {
  22. const mode = Taro.getStorageSync('mode')
  23. const isSinglePage = mode === 'singlePage'
  24. const consultant = useSelector(s => s.system.consultant)
  25. const city = useSelector(s => s.city.curCity)
  26. const { spinning, userInfo } = useSelector(s => s.user)
  27. const { person, ...extInfo } = userInfo || {}
  28. const router = useRouter()
  29. const page = routes.filter((r) => (router.path.indexOf(r.page) > -1))[0]
  30. const [loading, setLoading] = useState(false)
  31. const [authPhone, authAvatar, authPage] = useAuth(person, page)
  32. const [shareTimelineVisible, setShareTimelineVisible] = useState(false)
  33. const { id } = router.params
  34. const showConsultant = page.shortcut && page.shortcut.consultant
  35. // 页面分享
  36. const [shareContent, setShareContent] = useState({})
  37. // 页面埋点
  38. const [trackData, setTrackData] = useState({})
  39. // 开屏广告
  40. const [screenInfo, screenVisible, toggleShowScreen] = useScreen(city.id, person)
  41. const handleScreen = () => {
  42. toggleShowScreen()
  43. nav2Target(screenInfo)
  44. }
  45. // 分享朋友圈提示
  46. const showShareTimeline = (visible) => setShareTimelineVisible(visible)
  47. // 设置当前页标题
  48. const setNavigationBarTitle = title => {
  49. if (!title) return;
  50. Taro.nextTick(() => {
  51. Taro.setNavigationBarTitle({ title })
  52. })
  53. }
  54. // 报备客户
  55. useEffect(() => {
  56. if (isSinglePage) return;
  57. reportCustomer(person, consultant, false).catch(() => {})
  58. }, [person, consultant, isSinglePage])
  59. // 请求埋点设置
  60. useEffect(() => {
  61. if (isSinglePage) return;
  62. if (id) {
  63. setShareContent({})
  64. }
  65. } ,[id, page.type, isSinglePage])
  66. // 埋点数据
  67. useEffect(() => {
  68. if (isSinglePage) return;
  69. const consultantId = person?.personType === ROLE_CODE.CONSULTANT ? person.personId : undefined
  70. setTrackData({
  71. eventType: page.type,
  72. propertyName: page.name,
  73. consultantId,
  74. sharePersonId: person?.personId,
  75. targetId: id,
  76. })
  77. }, [person, page, id, isSinglePage])
  78. // 主要用于埋点
  79. useEffect(() => {
  80. Taro.setStorage({ key: 'page', data: page })
  81. }, [page])
  82. useEffect(() => {
  83. setLoading(!person || !person.personId)
  84. // eslint-disable-next-line react-hooks/exhaustive-deps
  85. }, [person])
  86. return (
  87. <>
  88. {/* 菊花转 */}
  89. <Overlay visible={loading} style={{background: '#fff'}}>
  90. <Loading />
  91. </Overlay>
  92. {/* 授权手机 */}
  93. <Overlay visible={authPhone && !isSinglePage} aligin='bottom'>
  94. <View className='auth-wrapper'>
  95. <AuthPhone consultant={consultant} router={router} page={page} />
  96. </View>
  97. </Overlay>
  98. {/* 授权头像 */}
  99. <Overlay visible={!authPhone && authAvatar && !isSinglePage} aligin='bottom'>
  100. <View className='auth-wrapper'>
  101. <AuthAvatar />
  102. </View>
  103. </Overlay>
  104. {/* 授权头像-全屏 */}
  105. { authPage && !isSinglePage && <AuthPage /> }
  106. {/* 显示分享朋友圈 */}
  107. <ShareToCircle visible={shareTimelineVisible && !isSinglePage} onClose={() => setShareTimelineVisible(false)} />
  108. {/* 页面内容 */}
  109. <Spin size={32} spinning={spinning} />
  110. {
  111. person && !!person.personId && (
  112. <ChildComponent
  113. person={person}
  114. router={router}
  115. consultant={consultant}
  116. page={page}
  117. city={city}
  118. mode={mode}
  119. shareContent={shareContent}
  120. trackData={trackData}
  121. showShareTimeline={showShareTimeline}
  122. setNavigationBarTitle={setNavigationBarTitle}
  123. {...props}
  124. {...extInfo}
  125. />
  126. )
  127. }
  128. {/* 当前置业顾问 */}
  129. {
  130. !!showConsultant && (<FixedConsultant consultant={consultant} />)
  131. }
  132. {/* 开屏广告 */}
  133. <FirstScreen
  134. info={screenInfo}
  135. visible={screenVisible && !isSinglePage}
  136. onClick={handleScreen}
  137. onClose={toggleShowScreen}
  138. />
  139. </>
  140. )
  141. }