/**
 * Created by jin.zhouhang on 17-8-10 下午2:55.
 *
 * 拦截器
 */
import router from '../router'

const DEFAULT_PATH = '/login'

const Filter = {
  install(Vue) {
    /** 修改title */
    const changeTitle = (to) => {
      if (to.meta.title !== undefined) {
        document.title = to.meta.title
      }
    }

    /** 拦截器 */
    const filter = (to, next) => {

      if (to.meta.filter) {
        Vue.prototype.$$get('/user/isLogin')
          .then(res => {
            if (res.data.code === '200') {
              next()
            } else {
              alert('您的账号已在其他设备上登录，您将被强行退出。请确保账号安全')
              next(DEFAULT_PATH)
            }
          })
          .catch(err => {
            Vue.prototype.$message.error(err)
            Vue.nextTick(() => finish())
            next(false)
          })
      } else {
        next()
      }
    }

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

    })

    router.afterEach(() => {
      Vue.nextTick(() => {
      })
    })
  }
}

export default Filter
