import 'babel-polyfill'
import Vue from 'vue'
import App from './App.vue'
import router from './router.js'
import cookie from 'cookie'
import store from './store'
import 'timeago.js'
import 'normalize.css'
import '../public/css/bootstrap.vue.min.css'
import '../public/css/icomoon.css'
import 'github-markdown-css'
import './app.scss'

Vue.config.debug = true

Vue.http.interceptors.push(function (request, next) {
    if (cookie.parse(document.cookie)['XSRF-TOKEN']) {
        request.headers.set('X-XSRF-TOKEN', cookie.parse(document.cookie)['XSRF-TOKEN'])
    }
    next(response => {
        if (response.status === 401) {
            if (!['/auth/login', '/auth/register', '/api/user'].includes(response.url)) {
                store.commit('OFFLINE')
                router.push({
                    name: 'login'
                })
            }
        } else if (response.status !== 200) {
            if (response.data.message) {
                store.commit('ERROR', {
                    message:   response.data.message,
                    timeoutId: setTimeout(() => {
                        store.commit('CLEAR_ERROR')
                        store.commit('CLEAR_ERROR_TIMER')
                    }, 3000)
                })
            } else {
                // 通常为 500 错误
                store.commit('ERROR', {
                    message:   '服务器开小差了',
                    timeoutId: setTimeout(() => {
                        store.commit('CLEAR_ERROR')
                        store.commit('CLEAR_ERROR_TIMER')
                    }, 3000)
                })
            }
            setTimeout(() => {
                // TODO
                router.back()
            }, 1500)
        }
    })
})

new Vue({
    router: router,
    store,
    render: h => h(App)
}).$mount('#app')

@@ -613,281 +613,8 @@
 tus 
-=== 401) %7B%0A            if (!%5B'/auth/login', '/auth/register', '/api/user'%5D.includes(response.url)) %7B%0A                store.commit('OFFLINE')%0A                router.push(%7B%0A                    name: 'login'%0A                %7D)%0A            %7D%0A        %7D else if (response.status 
 !== 
@@ -1334,101 +1334,303 @@
     
-setTimeout(() =%3E %7B%0A                // TODO%0A                router.back()%0A            %7D, 1500)
+// TODO %E5%A4%84%E7%90%86%E9%94%99%E8%AF%AF%E7%9A%84%E9%A1%B5%E9%9D%A2%E8%B7%B3%E8%BD%AC%E9%97%AE%E9%A2%98%0A        %7D%0A        if (response.status === 401) %7B%0A            if (!%5B'/auth/login', '/auth/register', '/api/user'%5D.includes(response.url)) %7B%0A                store.commit('OFFLINE')%0A                router.push(%7B%0A                    name: 'login'%0A                %7D)%0A            %7D
 %0A   

fix error handel when 401