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

import waterfall from 'vue-waterfall2'
Vue.use(waterfall)

import VueCropper from 'vue-cropper'
Vue.use(VueCropper)

import VueQuickCropper from 'vue-quick-cropper';
Vue.use(VueQuickCropper)

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'
      // next()
    }else{
      var u = navigator.userAgent;
      var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
      // XXX: 修复iOS版微信HTML5 History兼容性问题
      if (isiOS && to.path !== location.pathname) {
        // 此处不可使用location.replace
        location.assign(to.fullPath)
      } else {
        next()
      }
    }
  }else{//地址携带 token 获取url上的值 并存储 再跳转
    localStorage.setItem("token",getUrlParam('token'))
    var u = navigator.userAgent;
    var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
    // XXX: 修复iOS版微信HTML5 History兼容性问题
    if (isiOS && to.path !== location.pathname) {
      // 此处不可使用location.replace
      location.assign(to.fullPath)
    } else {
      next()
    }
  }
  
})

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

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