前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >vue.js客服系统实时聊天项目开发(十九)使用正则将消息格式替换为产品卡片信息

vue.js客服系统实时聊天项目开发(十九)使用正则将消息格式替换为产品卡片信息

作者头像
唯一Chat
发布2023-02-17 13:05:11
6800
发布2023-02-17 13:05:11
举报
文章被收录于专栏:陶士涵的菜地

我们客服系统的消息列表中,会有产品卡片展示,这个是怎么实现的呢

产品信息其实就是下面这个字符串

代码语言:javascript
复制
product[{"title":"纯坚果零食大礼包一整箱干果类网红爆款解馋小吃休闲食品送女友","price":"¥9.9","img":"https://img.alicdn.com/bao/uploaded/i1/2201453915278/O1CN01dZolFu1orN8dFeKAj_!!0-item_pic.jpg_200x200q90.jpg_.webp","url":"https://www.taobao.com"}] 

只需要把上面的字符串解析成html格式就可以了

代码语言:javascript
复制
//转换产品卡片
function replaceProduct(str){
    return str.replace(/product\[([^\[\]]+?)\]/g, function (product) {
        console.log(str,arguments);
        if(!arguments[1]){
            return;
        }
        let jsonStr=arguments[1].replace(/\'/g,'"');
        try{
            let info=JSON.parse(jsonStr);
            if(typeof info=="undefined"){
                return;
            }
            if(!info.title) info.title="智能客服系统产品卡片";
            if(!info.price) info.price="¥0";
            if(info.img){
                info.img="<img src='"+info.img+"'/>";
            }else{
                info.img="";
            }
            if(!info.url) info.url="/";
            let html= `
                    <a class="productCard" href="`+info.url+`" target="_blank"/>
                    `+info.img+`

                    <div class="productCardTitle">
                        <p class="productCardTitle">`+info.title+`</p>
                        <p class="productCardPrice">`+info.price+`</p>
                    </div>
                    </a>
            `;
            return html;
        }catch (e) {
            return jsonStr;
        }
    });
}

然后给转换后的html增加相应的样式

代码语言:javascript
复制
.productCard{
        cursor: pointer;
        background: #fff;
        padding: 5px;
        border-radius: 2px;
        width: auto;
        max-width: 730px;
        display: flex;
        border-radius: 5px;
    }
    .productCard img{
        width: 100px;
        height: 100px;
        margin-right: 15px;
    }
    .productCard .productCardPrice{
        color: #ff7736;
        font-weight: bold;
        font-size: 16px;
        margin-top: 10px;
    }
    .productCard .productCardTitle{
        color: #333;
    }
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2023-02-04,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
腾讯企点客服
腾讯企点客服(Tencent QiDian Customer Service)是为销售和客服提供多渠道商机转化与客户服务的智能客服平台。基于腾讯云的即时通讯、音视频、人工智能、大数据、云呼叫中心等技术,以个性化服务和精准客户洞察撬动销售转化与复购增购。企点客服全面覆盖了从销售到服务的全链路业务场景,帮助企业构建一体化的客户运营体系,极大提升了企业获客、待客、留客的效率。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档