import store from '@/store'
export default class Before {
  constructor(opt) {
    this.opt = opt || {}
  }

  async update(to, from, next) {
    // 创建游客用户
    await store.dispatch('createGuestUser')

    if (to.meta.requiredLogin) {
      const isLogin = store.state.isLogin || (await store.dispatch('checkLogin'))
      if (isLogin) {
        wx.miniProgram.postMessage({ data: { token: store.state.token } })
        next()
      } else {
        store.state.isWeapp ? wx.miniProgram.redirectTo({ url: '/pages/login/index' }) : next('/login')
      }
      return
    }
    next()
  }
}
