/*
 * @Author: your name
 * @Date: 2021-12-21 11:05:37
 * @LastEditTime: 2023-03-23 16:49:49
 * @LastEditors: code4eat awesomedema@gmail.com
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: /KC-MiddlePlatform/src/global.tsx
 */

import { history } from 'umi';
import { logout } from './service/login';

interface EventType extends Event {
  key?: string;
}

const loginPath = '/login';

export const logoutHandle = async () => {
  // await logout();
  localStorage.removeItem('userData');
  localStorage.removeItem('initialState');
  localStorage.removeItem('currentSelectedTab');
  //localStorage.removeItem('isChildShowMenu');
  localStorage.removeItem('selectedKeys');
  localStorage.removeItem('openKeys');
  // localStorage.removeItem('visitedTabs');
};

(function () {
  let originalRemoveItem = localStorage.removeItem;
  //重写removeItem函数
  localStorage.removeItem = function (key) {
    let event: EventType = new Event('removeLocalItemEvent');
    event.key = key;
    //提交事件
    window.dispatchEvent(event);
    //执行原removeItem函数
    originalRemoveItem.apply(this, [`${key}`]);
  };
})();

//添加removeLocalItemEvent监听事件
//用于子应用token过期触发登录
window.addEventListener('removeLocalItemEvent', function (e: EventType) {
  if (e.key == 'userData') {
    const hospSign = localStorage.getItem('hospSign');
    history.replace(`${loginPath}?hospSign=${hospSign}`);
  }
});
