// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.

import Vue from 'vue'
import { VueJsonp } from 'vue-jsonp'
import App from './App'
import router from './router'
import store from './store/store' // 引入store
import resource from 'vue-resource' // 引入vue-resource
import tabbar from './components/tabbar.vue'
import banner from './components/banner.vue'
import back from './components/back.vue'
import backIndex from './components/backindex.vue'
import Vant from 'vant'
import clipboard from 'clipboard'
import 'vant/lib/index.less'
import wx from 'weixin-js-sdk'
// import VConsole from 'vconsole';
import request from "@/utils/request"
import { getWxOpenId } from './script/wxGetOpenId.js'
import common from './script/common.js'
import '@/assets/iconfont/iconfont.css'

import { getUrlParam } from './utils/utils.js'

Vue.use(VueJsonp)
// new VConsole()
Vue.prototype.$axios = request
Vue.prototype.$wx = wx
Vue.component('tabbar', tabbar)
Vue.component('banner', banner)
Vue.component('back', back)
Vue.component('backindex', backIndex)
Vue.config.productionTip = false
Vue.use(Vant)
Vue.use(resource)
Vue.use(common)
Vue.prototype.clipboard = clipboard

// 时间戳过滤器
Vue.filter('dateFormat', (dataStr) => {
    var time = new Date(dataStr)

    function timeAdd0(str) {
        if (str < 10) {
            str = '0' + str
        }
        return str
    }
    var y = time.getFullYear()
    var m = time.getMonth() + 1
    var d = time.getDate()
    var h = time.getHours()
    var mm = time.getMinutes()
    var s = time.getSeconds()
    return y + '-' + timeAdd0(m) + '-' + timeAdd0(d) + ' ' + timeAdd0(h) + ':' + timeAdd0(mm) + ':' + timeAdd0(s)
})

import { checkIsLogin } from './api/login'

// import { getWxOpenId } from './script/wxGetOpenId.js'

        //     const res = await checkIsLogin();
        //     if (res.msg !== "success") {
        //         this.$router.push({ path: '/Login' })
        //     }
const whiteList = ['/Login']
router.beforeEach(async (to, from, next) => {
    // if (to.matched.some(record => record.meta.needLogin)) { // 判断该路由是否需要登录权限
    if (!whiteList.includes(to.path)) {
        console.log('有登录权限')
        
        // else
        if (!localStorage.getItem('openid') ) {
            console.log('无openId')
            getWxOpenId().then(() => {
                next()
            })
            console.log('overallExample.getOpenid()走完了')
            return;
        }
        if (!localStorage.getItem('userId')) { // 判断当前用户的登录信息userId是否存在
            console.log('无userId')
            next('/Login')
            return;
        }

        // 登录过期 重新登录
        const res = await checkIsLogin();
        if (res.msg!== "success") {
            localStorage.clear();
            getWxOpenId().then(() => {
                next()
            })
            return
        }


        next()
    } else {
        next();
    }
})


new Vue({
    el: '#app',
    router,
    store: store,
    components: { App },
    template: '<App/>',

})

