

import Vue from 'vue'
import Router from 'vue-router'
import Honghe from '../pages/Honghe.vue'
import SaveShare from '../pages/SaveShare.vue'
import { useModel } from '../store'
import { getOpenId } from '../utils/wx'
import { login } from '../services/person'
import { setToken } from '../utils/token'

Vue.use(Router)

const router = new Router({
  routes: [

    //面页
    {
      path: '/',
      name: 'Honghe',
      component: Honghe,

    },
    {
      path: '/SaveShare',
      name: 'SaveShare',
      component: SaveShare,

    },
  ]
})

router.beforeEach((to, from, next) => {

  const [person, setPerson] = useModel('person')

  // 未登录
  if (!person.personId) {
    getOpenId().then(openid => {
      login(openid).then(res => {
        const { user, token } = res;
        setPerson(user)
        setToken(token)
      })
    })
  }

  // 未登录的正常进行页面加载, 不需要进行页面控制
  next()
})

export default router;
