app.tsx 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import { Component } from 'react'
  2. import { Provider, ProviderProps } from 'mobx-react'
  3. import Taro from '@tarojs/taro'
  4. import './app.less'
  5. // ======store======
  6. import userInfoStore, { UserInfoStore } from './store/userInfo'
  7. import indexStore, { IndexStore } from './store/index'
  8. import Book, { BookStore } from './store/book'
  9. import classifyStore, { ClassifyStore } from './store/classIfy'
  10. import modalStore from './store/modalStore'
  11. // ======引入全局配置======
  12. import { initApp, setApp } from './config'
  13. // ======引入拦截器=========
  14. import './interceptor'
  15. import api from './server/index'
  16. import { getShortBookInfo } from './server/book/short'
  17. import { getUserInfo } from './server/wx/login'
  18. export interface Store {
  19. userInfoStore: UserInfoStore,
  20. indexStore: IndexStore,
  21. bookStore: BookStore,
  22. classifyStore: ClassifyStore,
  23. modalStore: {
  24. visible: boolean, // 弹窗开关
  25. showModal: () => void,
  26. hideModal: () => void
  27. }
  28. }
  29. const store: Store = {
  30. userInfoStore,
  31. indexStore,
  32. bookStore: Book,
  33. classifyStore,
  34. modalStore
  35. }
  36. class App extends Component {
  37. //每次打开小程序触发,后台切入不算
  38. onLaunch(options) {
  39. }
  40. componentDidMount() {
  41. console.log("APPcomponentDidMount")
  42. /**获取状态栏高度 */
  43. Taro.getSystemInfo({}).then(res => {
  44. let { system, statusBarHeight,platform } = res
  45. indexStore.setData({ navBarTop: statusBarHeight, system })//设置navbar高度和当前系统
  46. setApp({ system:platform })
  47. })
  48. }
  49. async componentDidShow(options) {
  50. console.log("appcomponentDidShow11111111111111111111111")
  51. let { scene, query ,path} = options//获取当前小程序进入的场景值
  52. let bookConfig = Taro.getStorageSync("bookConfig")
  53. if (bookConfig) {
  54. Book.setData({ bookConfig: JSON.parse(bookConfig) })
  55. }
  56. //阅读页面并且路径存在bookId请求数据
  57. if(query?.bookId && path === "pages/book/bookArticle/index"){
  58. getShortBookInfo(query?.bookId)
  59. }
  60. indexStore.setData({ scene, query })
  61. // 启动初始化
  62. console.log("初始化")
  63. initApp()//初始化全局变量
  64. let { appId, version, envVersion } = Taro.getAccountInfoSync().miniProgram//获取当前小程序的APPID
  65. setApp({ appId, proVersion: version, envVersion,pathParams:{ scene, ...query } })
  66. // 初始化登录,获取登录使用的token存放用于登录使用
  67. await api.loginInit(appId)
  68. }
  69. componentDidHide() { }
  70. componentDidCatchError() { }
  71. render() {
  72. console.log("store.userInfoStore.userInfo", store.userInfoStore.userInfo)
  73. return <Provider store={store}>
  74. {(this.props as ProviderProps).children}
  75. </Provider >
  76. }
  77. }
  78. export default App