// 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 axios from 'axios'
import store from './vuex/store'
import crypto from 'crypto'
import qs from 'qs'
import { Button, Swipe, SwipeItem, Field, Lazyload, Toast, Loadmore, Header, Cell, Radio, InfiniteScroll, Picker, DatetimePicker, Tabbar, TabItem, Popup, Switch } from 'mint-ui'
import 'mint-ui/lib/style.min.css'
import './assets/css/common.scss'
import './assets/css/my-mint.scss'
import { getStore, removeStore } from './utils/storage'
import VideoPlayer from 'vue-video-player'
import { FontAwesomeIcon } from '@fortawesome/vue-fontawesome'
import { faUser } from '@fortawesome/free-solid-svg-icons'
import 'font-awesome/css/font-awesome.min.css'
import { library } from '@fortawesome/fontawesome-svg-core'
import { log } from 'console'
import "core-js/stable";
import "regenerator-runtime/runtime";

// import wx from 'weixin-js-sdk'
let wx = require('weixin-js-sdk')
Vue.config.productionTip = false


// mintui模块
Vue.component(Button.name, Button)
Vue.component(Swipe.name, Swipe);
Vue.component(SwipeItem.name, SwipeItem);
Vue.component(Field.name, Field);
Vue.component(Loadmore.name, Loadmore);
Vue.component(Header.name, Header);
Vue.component(Cell.name, Cell);
Vue.component(Radio.name, Radio);
Vue.component(Button.name, Button);
Vue.component(Picker.name, Picker);
Vue.component(DatetimePicker.name, DatetimePicker);
Vue.component(Tabbar.name, Tabbar);
Vue.component(TabItem.name, TabItem);
Vue.component(Popup.name, Popup);
Vue.component(Switch.name, Switch);
Vue.use(Lazyload);
Vue.use(InfiniteScroll);
// axios
Vue.prototype.$http = axios
axios.defaults.baseURL = '/api'

library.add(
  faUser,
)
Vue.component('font-awesome-icon', FontAwesomeIcon)

// md5加密处理
Vue.prototype.md5 = (params) => {
  var seckey = 'victor_2017@DU^^&JGK_((*&gjGH';
  var str = '';
  for (let k in params) {
    let md5 = crypto.createHash('md5');
    md5.update(seckey + params[k]);
    str += md5.digest('hex');
  }
  let md5 = crypto.createHash('md5');
  md5.update(seckey + str + seckey);
  str = md5.digest('hex');
  params.apitoken = str
  return params;
}


const service = axios.create({
  baseURL: process.env.BASE_API, // api的base_url
  timeout: 5000 // 请求超时时间
})
service.interceptors.response.use(
  response => response,
  error => {
    Toast({
      message: error.message,
      position: "bottom",
      duration: 5 * 1000
    })
    return Promise.reject(error)
  })

// 封装axios获取数据
Vue.prototype.getData = (url, params) => {
  params = Vue.prototype.md5(params)
  params = qs.stringify(params)
  return new Promise((resolve, reject) => {
    service.post(url, params)
      .then(data => {
        resolve(data.data)
      })
      .catch(err => reject(err))
  })
}
// 时间过滤器
Vue.filter('format', (timestamp) => {
  const date = new Date(parseInt(timestamp) * 1000)
  const Y = date.getFullYear(),
    M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1),
    D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate(),
    h = date.getHours() < 10 ? '0' + date.getHours() : date.getHours(),
    m = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes(),
    s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
  return Y + '-' + M + '-' + D + ' ' + h + ':' + m;
})

//--------------------手机号正则验证-------------------
Vue.prototype.checkPhone = (phone) => {
  const myreg = /^(1[3-9][0-9])\d{8}$/;
  if (!myreg.test(phone)) {
    return false;
  } else {
    return true;
  }
}
//---------------------手机号位数限制 ---------------------
Vue.prototype.checkLength = (length, phone, ele) => {
  if (phone.length > length) {
    Toast("超过手机号最大位数");
    ele.readonly = "readonly";
  } else {
    ele.readonly = ''
  }
}
// -------------------身份证号正则验证----------------
Vue.prototype.checkCard = (idcard) => {
  const myreg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
  if (!myreg.test(idcard)) {
    return false;
  } else {
    return true;
  }
}
//邮箱正则验证
Vue.prototype.checkEmail = (email) => {
  const myreg = /^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$/;
  if (!myreg.test(email)) {
    return false;
  } else {
    return true;
  }
}


//----------------------6位数字密码正则验证--------------------
Vue.prototype.checkPwd = (password) => {
  const myreg = /^[0-9a-zA-Z_]{6,}$/;
  if (!myreg.test(password)) {
    return false;
  } else {
    return true;
  }
}
//-----------------------存入cookie的函数
Vue.prototype.setCookie = (objname) => {
  let expires = new Date();//设置过期时间
  expires.setTime(expires.getTime() + 100 * 365 * 24 * 60 * 60);//永不过期
  document.cookie = "usertoken=" + objname + ';+expires=' + expires.toGMTString() + ";path=/";//用户token和过期信息存到cookie
}
//获取指定名称的cookie的值
Vue.prototype.getCookie = (objname) => {
  var arrstr = document.cookie.split("; ");
  for (var i = 0; i < arrstr.length; i++) {
    var temp = arrstr[i].split("=");
    if (temp[0] == objname) {
      return unescape(temp[1]);
    }
  }
}
// Vue.prototype.sysTime = 0
//登录状态判断
const whiteList = ['/login', '/login2', '/daily', '/qrcode', '/signin', '/wxerror', '/video', '/videocontrol'] //不需要登陆的页面
router.beforeEach(function (to, from, next) {
  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)
  }
  let params = {
    token: getStore('token')
  }

  Vue.prototype.getData("/Member/checklogin", params).then(
    data => {
      if (data.code == 400) { // 没登录
        removeStore('token')
        if (whiteList.indexOf(to.path) !== -1) { // 白名单
          next()
        } else {
          next('/login2')
        }
      }
      // else if(data.code == 301){     //为绑定手机号
      //   store.commit('addInfo',data.data)
      //   Toast("首次使用请先绑定手机号");
      //   if (to.path === '/user/bind') {
      //     next()
      //   } else {
      //     next('/user/bind')
      //   }
      // }
      else {
        store.commit('addInfo', data.data)
        if (to.path === '/login' || to.path === '/login2') { //  跳转到
          next({ path: '/' })
        }
        next()
      }
    }, err => {
    })
  window.scrollTo(0, 0)
})

Vue.prototype.setFooter = (bodyHeight) => {
  let footer = document.getElementsByClassName('footer')[0];
  if (bodyHeight < window.innerHeight) {
    footer.style.position = "fixed"
    footer.style.bottom = 0
    footer.style.left = 0
    footer.style.width = "100%"
  } else if (bodyHeight > window.innerHeight) {
    footer.style.position = 'static'
  }
}




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