// 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 App from './App'
import router from './router'
import YDUI from 'vue-ydui'; /* 相当于import YDUI from 'vue-ydui/ydui.rem.js' */
import 'vue-ydui/dist/ydui.base.css';
import 'vue-ydui/dist/ydui.rem.css';
import 'vant/lib/index.css';
import 'vue-slider-component/theme/default.css'
import './assets/icon/iconfont.css'
import axios from 'axios'
import apiUrl from './global'



Vue.prototype.GLOBAL = apiUrl;
axios.defaults.baseURL=apiUrl.BASE_URL;
Vue.prototype.$http=axios;
/* 使用px：import 'vue-ydui/dist/ydui.px.css'; */
import { Confirm, Alert, Toast, Notify, Loading } from 'vue-ydui/dist/lib.rem/dialog';

/* 使用px：import { Confirm, Alert, Toast, Notify, Loading } from 'vue-ydui/dist/lib.px/dialog'; */
Vue.prototype.$dialog = {
    confirm: Confirm,
    alert: Alert,
    toast: Toast,
    notify: Notify,
    loading: Loading,
};
Vue.use(YDUI);









Vue.config.productionTip = false

/**
 * 客户、运营可同时登录
 * 
 * 默认跳转到对应角色首页
 *  - 客户：location
 *  - 运营：yys
 * 如未登录，跳转到：
 *  - 客户：first 
 *  - 运营：yys_login 
 */
router.beforeEach((to,from,next)=>{
  // console.log('to.....',to);

  //车牌扫码校验临时
  if(to.name == 'vehicle' || to.name == 'vehicledecode'){
    console.log('ddddddddddddddddd')
    
    next()
    return
  }

  // localStorage.setItem('openid', '2222222');


  let query = to.query || {};
  if (query.openid) {
    localStorage.setItem('openid', query.openid);
  }
  
  let openid = localStorage.getItem("openid");
  let loginFlag = false; // 登录标记
  
  // 运营商，本地保存 yys_userid
  if (to.meta.role === 'yys') {
    let yysUserid = localStorage.getItem('yysUserid');
    loginFlag = (openid && yysUserid);

    if (to.name === 'yys_login') {
      next();
    } else {
      if (loginFlag) {
        next();
      } else {
        next('/yys_login');
      }
    }

  } else {
    // 用户，本地保存 userid, userType, deviceType
    let userid = localStorage.getItem('userid');
    // openid =222222;//测试用

    loginFlag = (openid && userid );
    
  
    if (to.name === 'login') {
      next();
    } else if ( to.name === 'register' || to.name === 'reset_pwd') {

      // if (userType) {
        next();
      // } else {
      //   next('/');
      // }

    } else {
      if (!loginFlag) {
        next('/login');
      } else {
        next();
        
      }
    }

  }

});

// 保存用户设备标识
var u = navigator.userAgent;
var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
if (isAndroid) {
  localStorage.setItem('userAgent', 'android');
} else if (isiOS) {
  localStorage.setItem('userAgent', 'ios');
} else {
  localStorage.setItem('userAgent', 'other');
}
// 保存首次登录地址（IOS 微信 JS-SDK 需要用到）
localStorage.setItem('mainUrl', location.href.split('#')[0]);

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>'
})
