"use strict";

exports.__esModule = true;
exports.default = void 0;

var _ksUtils = require("../ks-utils");

var _filter = require("../ks-utils/filter");

// import './index.css'
var homeMenuId = '2820215716730110';

var __vue_render__ = function __vue_render__() {
  var _vm = this;

  var _h = _vm.$createElement;

  var _c = _vm._self._c || _h;

  return _c('div', {
    staticClass: "ks-user-access"
  }, [_vm.status && _vm.isEntry ? _c('a', {
    staticClass: "log-disabled light",
    class: {
      dark: _vm.dark
    },
    on: {
      "click": _vm.handleClick
    }
  }, [_vm._v(_vm._s(_vm.total)), _c('span', {
    staticClass: "gap"
  }, [_vm._v("/")]), _vm._v(_vm._s(_vm.rate) + "%")]) : _vm._e()]);
};

var __vue_staticRenderFns__ = [];
var _default2 = {
  render: __vue_render__,
  staticRenderFns: __vue_staticRenderFns__,
  name: 'ks-user-access',
  props: {
    dark: Boolean,
    authId: String,
    showCommentIcon: {
      type: Boolean,
      default: true
    },
    whitelist: {
      type: Array,
      default: function _default() {
        return [];
      }
    }
  },
  data: function data() {
    return {
      status: false,
      total: 0,
      rate: 0
    };
  },
  mounted: function mounted() {
    var _this = this;

    this.checkInit();

    _ksUtils.bus.$on('first-report', function (res) {
      console.log('[user report]: 从订阅触发', res);
      _this.total = res.accessTotal || 0;
      _this.rate = res.accessPercent || 0;
    });
  },
  computed: {
    isEntry: function isEntry() {
      if (!this.$router || this.$route.meta.home === true) {
        return true;
      }

      return this.$route.path === '/' ? true : false;
    },
    showInH5APP: function showInH5APP() {
      return navigator.userAgent.indexOf('pingjia') === -1;
    }
  },
  methods: {
    addEvent: function addEvent() {
      var _this2 = this;

      // 当窗口可见时，刷新统计
      // eslint-disable-next-line
      document.addEventListener("visibilitychange", function (_) {
        if (document.visibilityState === 'visible' && _this2.status) {
          console.log('[user report]:窗口可见时刷新统计');

          _this2.getAccessStatistics();
        }
      });
    },
    // 检查request有没有加载
    checkInit: function checkInit() {
      var _this3 = this;

      if (window.ks && window.ks.request) {
        this.getHomePagePermission();
      } else {
        setTimeout(function () {
          if (window.ks && window.ks.request) {
            _this3.getHomePagePermission();
          } else {
            _this3.checkInit();
          }
        }, 300);
      }
    },
    getHomePagePermission: function getHomePagePermission() {
      var _this4 = this;

      var headers = {
        // 'X-menu-id': this.authId || '2820215716730110',
        $config: {
          _tips: false
        }
      };
      (0, _ksUtils.http)('auth.menu.resources.userPermissionCode', 10132, {
        ids: [homeMenuId]
      }, {
        headers: headers
      }).then(function (data) {
        if (data[homeMenuId]) {
          var hasPermission = data[homeMenuId].includes('mcs.user.menu.access.report');

          if (hasPermission) {
            // 确认有权限时，添加窗口可见事件
            _this4.addEvent();

            var cacheKey = 'report-' + window.ksui.menu.id + '-' + (0, _filter.date)(new Date(), 'YYYYMMDD');

            if (typeof window[cacheKey] === 'object') {
              console.info('[user report]: 从上报取统计结果');
              _this4.total = window[cacheKey].accessTotal || 0;
              _this4.rate = window[cacheKey].accessPercent || 0;
              _this4.status = true;
            } else {
              _this4.getAccessStatistics();
            }
          }
        }
      }).catch(function () {
        // eslint-disable-next-line
        console.error('[user report]: 菜单权限获取失败');
      });
    },
    store: function store(menuId, data) {
      if (!menuId) return;
      var key = 'access-report' + menuId;
      localStorage.setItem(key, JSON.stringify(data));
    },
    getStore: function getStore(menuId) {
      if (!menuId) return;
      var key = 'access-report' + menuId;
      var result = localStorage.getItem(key);
      if (!result) return false;
      return JSON.parse(result);
    },
    getAccessStatistics: function getAccessStatistics() {
      var _this5 = this;

      var menuId = window.ksui && window.ksui.menu.id; // let cache = this.getStore(menuId)
      // if (typeof cache === 'object') {
      //   this.total = cache.total || 0
      //   this.rate = cache.rate || 0
      //   // eslint-disable-next-line
      //   console.log('[访问统计]取缓存', cache)
      // }

      var headers = {
        'X-menu-id': this.authId || '2820215716730110',
        $config: {
          _tips: false
        }
      };
      var vm = this;
      (0, _ksUtils.http)('mcs.user.menu.access.report', 50178, {
        menuId: menuId
      }, {
        headers: headers
      }).then(function (data) {
        if (data.accessTotal && data.accessTotal >= 1000) {
          data.accessTotal = (data.accessTotal / 1000).toFixed(1) + 'k';
        }

        console.log('从report接口获取');
        _this5.total = data.accessTotal;
        _this5.rate = data.accessPercent;
        _this5.status = true; // this.store(menuId, {total:this.total, rate:this.rate})
      }).catch(function (e) {
        vm.status = false; // eslint-disable-next-line

        console.error('用户统计获取失败: ', e);
      });
    },
    handleClick: function handleClick() {
      this.getAccessStatistics();
      var menu = window.ksui && window.ksui.menu || {};
      var title = menu.title;
      ks.openWorkApp({
        payload: JSON.stringify({
          type: 'userAccessStatistics',
          data: {
            menuId: menu.id,
            menuName: title
          }
        }),
        moduleId: '90942479f43d4a49aa13f32cf2cc7c1b'
      });
    },
    handleCommentClick: function handleCommentClick() {
      var menu = window.ksui && window.ksui.menu || {};
      var title = menu.title;
      ks.openWorkApp({
        payload: JSON.stringify({
          type: 'commentModule',
          data: {
            menuId: menu.id,
            appName: title
          }
        }),
        moduleId: '0b34151891e44d7e8874a18748ac55da'
      });
    }
  }
};
exports.default = _default2;