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

  async update(to, from, next) {
    // 设置游客用户信息
    const { user_id: userId, student_id: studentId } = to.query
    if (userId && studentId) {
      store.commit('setGuestUser', { user_id: userId, student_id: studentId })
    }
    // 创建游客用户
    // await store.dispatch('createGuestUser')

    const isLogin = store.state.isLogin || (await store.dispatch('checkLogin'))
    isLogin && wx.miniProgram.postMessage({ data: { token: store.state.token } })

    if (to.meta.requiredLogin && !isLogin) {
      store.state.isWeapp ? wx.miniProgram.redirectTo({ url: '/pages/login/index' }) : next('/login')
      return
    }
    next()
  }
}
