import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import './styles/index.scss'; // global css
import request from './utils/request'

import setWechatTitle from './utils/setWechatTitle'

Vue.config.productionTip = false

function getUrlParam(name) {
  var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
  var r = window.location.search.substr(1).match(reg);  //匹配目标参数
  if (r != null) return decodeURI(r[2]); return null; //返回参数值
}

router.beforeEach((to, from, next) => {
  if(!getUrlParam('token')){//地址不携带 token
    //本地没有存储 token 去请求授权 ，有存储token直接跳转
    if(!localStorage.getItem("token")){
      // let _url = document.location.href.split('#')[0]
      // location.href = _url+'api/MarketingToolApi/Wechat/index'
      location.href = 'api/MarketingToolApi/Wechat/index'
    }else{
      next()
    }
  }else{//地址携带 token 获取url上的值 并存储 再跳转
    localStorage.setItem("token",getUrlParam('token'))
    next()
  }
  
})

router.afterEach((transition) => {
  setWechatTitle(transition.meta.title)
})

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