app.jsx 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import { Component } from 'react'
  2. import { Provider } from 'react-redux'
  3. import Taro from '@tarojs/taro'
  4. import { getLocation, getRouterParams } from '@/utils/tools'
  5. import login from '@/utils/login'
  6. import im from '@/utils/im'
  7. import store from './store'
  8. import trackUserSource from '@/utils/tracking/userSource'
  9. import './app.scss'
  10. class App extends Component {
  11. // 是否登录
  12. logged;
  13. // 更新埋点
  14. updateTracking;
  15. componentDidMount () {}
  16. componentDidShow (options) {
  17. // 保留初始场景
  18. Taro.setStorage({ key: 'scene', data: options.scene })
  19. // 埋点
  20. if (this.logged) {
  21. trackUserSource(options).then((res) => (this.updateTracking = res))
  22. }
  23. }
  24. componentDidHide () {
  25. this.updateTracking()
  26. }
  27. componentDidCatchError () {}
  28. loadFontFace() {
  29. Taro.loadFontFace({
  30. global: true,
  31. family: 'iconfont',
  32. // eslint-disable-next-line no-undef
  33. source: `url("${ICON_FONT}")`,
  34. success() {},
  35. fail: function(res) {
  36. console.error('加载远程字体失败:', res)
  37. }
  38. });
  39. }
  40. onLaunch (options) {
  41. // 加载远程字体
  42. this.loadFontFace()
  43. // 当前设备相关
  44. Taro.getSystemInfo({
  45. success: (data) => {
  46. Taro.setStorage({ key: 'systemInfo', data })
  47. }
  48. })
  49. getLocation().then((loc) => {
  50. getRouterParams(options).then(router => {
  51. Taro.setStorageSync('router', router)
  52. const { query: payload } = router
  53. payload.path = router.path
  54. payload.scene = router.scene
  55. if (loc) {
  56. payload.lon = loc.lon
  57. payload.lat = loc.lat
  58. }
  59. // login
  60. login(payload).then((res) => {
  61. const { person: { personId } } = res
  62. // 初始化 im
  63. im.init(personId)
  64. this.logged = true
  65. // 埋点
  66. trackUserSource(options).then((x) => (this.updateTracking = x))
  67. })
  68. })
  69. })
  70. }
  71. // 在 App 类中的 render() 函数没有实际作用
  72. // 请勿修改此函数
  73. render () {
  74. return (
  75. <Provider store={store}>
  76. {this.props.children}
  77. </Provider>
  78. )
  79. }
  80. }
  81. export default App