url-to-path.js 2.64 KB
/**
 * url解析到path
 */
import { stringify, parse } from '../vendors/query-stringify';
const GO_DETAIL = 'go.productDetail';
const GO_H5 = 'go.h5';
const GO_MINIAPP = 'go.miniapp';
import Action from './actions'

function urlToPath(jumpUrl) {
  if (!jumpUrl) {
    return {};
  }
  let pathName = '';
  let openBy = {}
  if (typeof jumpUrl === 'string') { // 处理普通url
    let tempArr = jumpUrl.split('?');
    tempArr.splice(0, 1);
    let search = tempArr.join('?');

    // 排除search内rom_page_param参数的url参数
    search = search.replace(/"from_page_param":"[^,]+"/, '"from_page_param":""');
    search = search.replace(/'from_page_param':'[^,]+'/, "'from_page_param':''");

    const qs = parse(search);
    if (!qs['openby:yohobuy'])
      return {};
      openBy = JSON.parse(qs['openby:yohobuy']);

  } else if (typeof jumpUrl === 'object') { // 处理公共跳转页面path参数
    openBy = JSON.parse(jumpUrl['openby:yohobuy']);

  }
  let action = openBy.action;
  console.log(action)
  let query = openBy.params;
  let appId = openBy.appId || '';
  if (query && query.appId) {
    appId = query.appId;
  }

  let miniApp = openBy.miniApp;
  let pagePath = '';
  if (Action[action]) {
    pathName = Action[action]
    if (openBy.params) {
      if (openBy.params.actiontype) {
        let actionType = openBy.params.actiontype
        pathName = Action[action][actionType]
      }

      if (openBy.params.type) {
        let actionType = openBy.params.type
        pathName = Action[action][actionType]
      }
    }
  }

  if (GO_H5 === action) {
    //处理go.h5 url参数
    if (query && query.param) {
      if (query.url) {
        let urltemp = query.url;
        let urlparams = stringify(query.param, { encode: false });
        if (urltemp.indexOf('?') > -1) {
          urltemp = urltemp + "&" + encodeURIComponent(urlparams);
        } else {
          urltemp = urltemp + "?" + encodeURIComponent(urlparams);
        }
        query.url = encodeURIComponent(urltemp);
      }
    }
  } else if (GO_DETAIL === action) {
    // 参数兼容
    query.productSkn = query.product_skn || query.productSkn;
    query.activityId = query.activity_id || query.activityId;
  } else if (GO_MINIAPP === action) {
    //跳转到小程序必须有appid和path
    if (!query || !query.appId || query.appId.length === 0) { return }
    if (!query.path || query.path.length === 0) {
      pagePath = '';
    } else {
      pagePath = query.path;
    }
    if (pagePath) {
      if (pagePath.indexOf(0) != '/') {
        pagePath = '/' + pagePath;
      }
    }
  }

  return {
    appId,
    miniApp,
    pagePath,
    pathName,
    query
  };
}

export default urlToPath;