index.js 3.1KB

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