import { Component } from 'react'
import Taro from '@tarojs/taro'
import preFetchData from '@/utils/preFetchData'
import store, { StoreRoot } from './store'
import './app.less'

class App extends Component {

  onLaunch(options) {
    const {setPerson, login} = store.getModel('person').getState()

    // 预拉取数据
    preFetchData().then((fetchData) => {
      setPerson({
        personId: fetchData.personId,
        openid: fetchData.openid,
      })

      // 登录获取人员信息
      Taro.login({
        success: (res) => {
          const { code } = res;
          login({ code, personId: fetchData.personId })
        }
      })
    }).catch(() => {
      Taro.showToast({
        title: '网络异常, 请退出重试',
        icon: 'none',
      })
    })
  }

  // this.props.children 是将要会渲染的页面
  render () {
    return (
      <StoreRoot>
        {this.props.children}
      </StoreRoot>
    )
  }
}

export default App
