<template>
  <router-view />
</template>

<script>
import Cookies from 'js-cookie';
export default {
  name: 'App',
  setup(props) {
    const clearCookie = ()=> {
      var keys = document.cookie.match(/[^ =;]+(?=\=)/g);
      if (keys) {
        for (var i = keys.length; i--; ) document.cookie = keys[i] + '=0;expires=' + new Date(0).toUTCString();
      }
    }
    const setUrlCookie = ()=> {
      let hrefUrl = window.location.href;
      console.log(hrefUrl, 'hrefUrl');
      let [, pluginListUrl] = hrefUrl.split('pluginListUrl=');
      if(!pluginListUrl){
        return 
      }
      var parse = pluginListUrl.substring(pluginListUrl.indexOf('?') + 1),
        params = parse.split('&'),
        len = params.length,
        item = [],
        param = {};
      for (var i = 0; i < len; i++) {
        item = params[i].split('=');
        param[item[0]] = item[1];
      }
      for (let key in param) {
        Cookies.set(key, param[key]);
      }
      Cookies.set('pluginListUrl', pluginListUrl);
      return param;
    }
    function keyboardFn(e) {
      if ( e.keyCode == 8 && document.querySelectorAll('input:focus').length === 0 && document.querySelectorAll('textarea:focus').length === 0 ) {
        console.log('键盘事件', e)
        e.preventDefault();
      }
    }
    window.addEventListener('keydown', keyboardFn, false);
    if (['production', 'test'].includes(process.env.NODE_ENV)) {
      let storageList = window.localStorage
      for(let key in storageList){
        key!=='installList'&&window.localStorage.removeItem(key)
      } //每次进入插件清空所有的缓存,保留插件列表的缓存
      clearCookie(); //添加之前先清空cookie，解决切换设备时cookie存了2份的问题
      setUrlCookie(); // 存储所有链接带过来的入参
    }
     return {
      clearCookie,
      keyboardFn,
      setUrlCookie
    };
  }
}
</script>

<style>

</style>
