SOURCE

const blocks = [{
    "blockID": "airecommend_collection",
    "blockName": "智能推荐",
    "title": "",
    "text": "为家人添置意外保障,全家放心",
    "imageURL": "",
    "link": "",
    "option": "{\n\t\"isShow\": false\n}"
},
{
    "blockID": "airecommend_default_PA92",
    "blockName": "智能推荐列表",
    "title": "境内旅行无忧险",
    "text": "意外、财产保障全面,国内出游首选",
    "imageURL": "https://resource.wesure100.com/AT-Config/config/d5957333286cb1be458b367ae6f2b8aa.png",
    "link": "/pages/template/base/productDetail/index?productCode=PA92&channel=ae43dfb25a",
    "option": "{\n\t\"buttonText\": \"¥13起  去看看\"\n}"
},
{
    "blockID": "airecommend_default_PA71",
    "blockName": "智能推荐列表",
    "title": "运动意外险",
    "text": "可按天购买,覆盖滑雪等高风险运动",
    "imageURL": "https://resource.wesure100.com/AT-Config/config/53cca8841e1fc9a1b33b7dab3a4de388.png",
    "link": "/pages/template/base/productDetail/index?productCode=PA71&channel=ae43dfb25a",
    "option": "{\n\t\"buttonText\": \"¥1起  去看看\"\n}"
},
{
    "blockID": "airecommend_default_PA62",
    "blockName": "智能推荐列表",
    "title": "家庭意外险",
    "text": "小投入高保额,一张保单可保9人",
    "imageURL": "https://resource.wesure100.com/AT-Config/config/bccca54c9d95e77d9d0f97340edf379b.png",
    "link": "/pages/template/base/productDetail/index?productCode=PA62&channel=ae43dfb25a",
    "option": "{\n\t\"buttonText\": \"¥9.9/月   去看看\"\n}"
},
{
    "blockID": "airecommend_insured_PA92",
    "blockName": "智能推荐列表",
    "title": "",
    "text": "",
    "imageURL": "https://resource.wesure100.com/AT-Config/config/518566382141fbf2ce75b1b53e69e3fe.png",
    "link": "/pages/product/riskAssessment/home/index?channel=ae43dfb25a",
    "option": ""
}]

// 根据blockID,判断block类型
const getBlockType = (blockID) => {
    if (blockID.startsWith('product_collection')) {
        const [t, key, namespace] = blockID.split('_');
        return {
            type: 'product-main',
            namespace,
            t,
            key,
        };
    }

    if (blockID.startsWith('product_')) {
        const [blockName, namespace, key] = blockID.split('_');
        return {
            type: 'product-detail',
            namespace,
            key,
            blockName,
        };
    }

    if (blockID.startsWith('airecommend_collection')) {
        const [blockName, namespace] = blockID.split('_');
        console.log(blockName, namespace)
        return {
            type: 'airecommend-main',
            blockName,
            namespace,
        };
    }

    if (blockID.startsWith('airecommend_')) {
        const [blockName, namespace, key] = blockID.split('_');
        console.log(blockName, namespace, key)
        return {
            type: 'airecommend-detail',
            namespace,
            key,
            blockName,
        };
    }

    if (blockID.search('-') !== -1) {
        const [namespace, key] = blockID.split('-');
        return {
            type: 'array',
            namespace,
            key,
        };
    }

    if (blockID.search('/') !== -1) {
        const [namespace, key] = blockID.split('/');
        return {
            type: 'object',
            namespace,
            key,
        };
    }

    return {
        type: 'default',
        namespace: blockID,
        key: blockID,
    };
};

const getProductDetail = (block) => {
    const productCode = block.blockID.split('_')[2];
    let option = {};
    try {
        option = option && JSON.parse(block.option || '{}');
    } catch (error) {
        option = { error };
    }

    return {
        ...option,
        productName: block.title,
        productCode,
        link: block.link,
        text: block.text,
        imageURL: block.imageURL,
    };
};

// 解析config
const parseBlocks = (blocks) => {
    const pageData = {};
    const airecommendData = {};
    const products = [];

    blocks.forEach((block) => {
        const { blockID } = block;
        const { type: blockType, namespace, key } = getBlockType(blockID);

        // 解析产品列表
        if (blockType === 'product-main') {
            products.push({
                namespace,
                info: {
                    ...getBlockData(block),
                },
                productList: [],
            });
            return;
        }

        // 解析产品卡片
        if (blockType === 'product-detail') {
            const target = products.find((p) => p.namespace === namespace);
            const detail = getProductDetail(block);
            target.productList.push({
                ...detail,
            });
            return;
        }

        if (blockType === 'airecommend-main') {
            Object.assign(airecommendData, {
                ...getBlockData(block),
                recItemList: []
            });
            return;
        }

        if (blockType === 'airecommend-detail') {
            airecommendData.recItemList.push(getBlockData(block))
            Object.assign(airecommendData, {
                ...getBlockData(block),
                recItemList: []
            });
            return;
        }

        // 初始化为数组[], 模块名-数组对象
        if (blockType === 'array') {
            const target = pageData[namespace] || (pageData[namespace] = []);
            target.push({
                key,
                ...getBlockData(block),
            });
        }

        // 初始化为对象{}, 模块名/子模块名
        if (blockType === 'object') {
            const target = pageData[namespace] || (pageData[namespace] = {});
            if (key === 'index') {
                Object.assign(target, getBlockData(block));
            } else {
                target[key] = getBlockData(block);
            }
        }

        if (blockType === 'default') {
            pageData[namespace] = getBlockData(block);
        }
    });

    return { ...pageData, products };
};

const getBlockData = (block) => {
    let option = {};

    try {
        option = option && JSON.parse(block.option || '{}');
    } catch (error) {
        option = { error };
    }
    return {
        blockName: block.blockName,
        title: block.title,
        text: block.text,
        imageURL: block.imageURL,
        link: block.link,
        ...option,
    };
};

parseBlocks(blocks)
console 命令行工具 X clear

                    
>
console