miniapp-path-rules.js 2.25 KB
const _isPlainObject = (obj) => {
    if (typeof(obj) !== 'object' || obj.nodeType || obj !== null && obj !== undefined && obj === obj.window) { // eslint-disable-line
        return false;
    }

    if (obj.constructor &&
        !Object.prototype.hasOwnProperty.call(obj.constructor.prototype, 'isPrototypeOf')) {
        return false;
    }

    return true;
};

const _qsStringfy = (params) => {
    let paramsArr = [];

    if (params && _isPlainObject(params)) {
        for (let key in params) {
            if (params.hasOwnProperty(key)) {
                paramsArr.push(`${key}=${params[key]}`);
            }
        }
    } else {
        console.info('params is:', params);
    }

    return paramsArr.length ? `?${paramsArr.join('&')}` : '';
};


// 有货跳转规则转化为有货精选路由
const transToMiniappPath = (href) => {
    let path = '';
    let openbyParamsArr = href.split(/openby:yohobuy(=|=)/);
    let paramsStr = openbyParamsArr[openbyParamsArr.length - 1];
    let paramsObj = {};

    try {
        paramsObj = JSON.parse(paramsStr);
    } catch (error) {
        console.error('paramsStr is: ', paramsStr);
    }

    switch (paramsObj.action) {
        case 'go.productDetail': // 商品详情页
            if (paramsObj.params && paramsObj.params.product_skn) {
                path = `/pages/goodsDetail/goodsDetail?productSkn=${paramsObj.params.product_skn}&page_name=home`;
            }
            break;
        case 'go.list': // 列表页、专区
            path = `/pages/goodsList/goodsList${_qsStringfy(paramsObj.params)}`;
            break;
        case 'go.poollist': // 商品池
            path = `/pages/goodsList/productPool${_qsStringfy(paramsObj.params)}`;
            break;
        case 'go.h5':
            if (paramsObj.params && paramsObj.params.url) {
                path = `/pages/webview/webview?page_name=home&url=${paramsObj.params.url}${_qsStringfy(paramsObj.params.param)}`; // eslint-disable-line
            }
            break;
        case 'go.shop':
            path = `/pages/goodsList/brandStore?shopId=${paramsObj.params.shop_id}`;
            break;
        default:
            break;
    }

    return {
        path: path,
        action: paramsObj.action
    };
};

module.exports = {
    transToMiniappPath
};