router.service.ts 3.0 KB
export interface Params {
  [key: string]: any;
}

class RouterService {
  /**
   * @description: 跨应用跳转
   * @author ChenRui
   * @date 2020/9/28 14:23
   */
  push(subapp: string): void {
    history.pushState(null, subapp, subapp);
  }

  /**
   * @description: 获取纯路劲
   * @author ChenRui
   * @date 2020/10/12 21:06
   */
  getPathFromUrl(url: string): string {
    let path = url;
    if (url.indexOf("?") !== -1) {
      path = url.split("?")[0];
    }
    return path;
  }

  /**
   * @description: 获取请求参数
   * @author ChenRui
   * @date 2020/10/12 21:06
   */
  getParamsFromUrl(url: string): Params | undefined {
    let parameter: Params | undefined;
    if (url.indexOf("?") !== -1) {
      parameter = this.urlParameterToParams(url.split("?")[1]);
    }
    return parameter;
  }

  /**
   * @description: url地址参数转换成路由参数对象
   * @author ChenRui
   * @date 2020/10/12 21:07
   */
  urlParameterToParams(parameter: string): Params | undefined {
    const params: Params = {};
    const arr: string[] = parameter.split("&");
    arr.forEach((val, idx, array) => {
      const kv: string[] = val.split("=");
      if (kv.length > 1) {
        params[kv[0]] = decodeURIComponent(kv[1]);
      } else {
        params[kv[0]] = "";
      }
    });
    return params;
  }

  /**
   * @description: 地址参数拼接
   * @author ChenRui
   * @date 2021/2/19 11:40
   */
  combinationPathAndParameter(url: string, parameter?: { [key: string]: any }): string {
    let connectiveSymbol = "";
    if (url.indexOf("?") !== -1) {
      connectiveSymbol = "&";
    } else {
      connectiveSymbol = "?";
    }
    if (parameter && Object.keys(parameter).length > 0) {
      let mark = 0;
      Object.keys(parameter).forEach((item) => {
        if (mark === 0) {
          if (parameter[item] != null && parameter[item] !== "null" && parameter[item] !== "undefined") {
            url += connectiveSymbol + item + "=" + parameter[item];
          } else {
            url += connectiveSymbol + item + "=";
          }
        } else {
          if (parameter[item] != null && parameter[item] !== "null" && parameter[item] !== "undefined") {
            url += "&" + item + "=" + parameter[item];
          } else {
            url += "&" + item + "=";
          }
        }
        mark++;
      });
    }
    return url;
  }
  /**
   * 切换路由动画
   * @param to
   * @param from
   * @param component
   */
  animateName: (to: any, from: any, component: any) => string = (to: any, from: any, component: any) => {
    const toPath = to.path.split("/").join("_");
    const frPath = from.path.split("/").join("_");
    if (!component.pathMap[toPath]) {
      component.pathMap[toPath] = +new Date() + 1;
    }
    if (!component.pathMap[frPath]) {
      component.pathMap[frPath] = +new Date();
    }
    if (component.pathMap[toPath] > component.pathMap[frPath]) {
      return "left";
    } else {
      return "right";
    }
  };
}
const routerService = new RouterService();
export default routerService;