【原生小程序-如何让rich-text里面的图片长按保存或分享】

<rich-text nodes="{{info.content}}" catchtap="richTextClick"></rich-text>
var $=getApp();
Page({
  data:{
    info:{},
    next:{},
    prev:{}
  },
  onLoad:function(o){
  //对样式做调整
    var t=this;
    $.http('请求接口',{传参},function(r){
      r.data.info.addtime=$.date('Y-m-d',r.data.info.addtime),
      r.data.info.content = r.data.info.content
      // .replace(/<p([\s\w"=\/\.:;]+)((?:(style="[^"]+")))/ig, '<p')
      .replace(/<p>/ig, '<p style="font-size: 16px; line-height: 25px;margin-bottom:5px;">')
      .replace(/<img([\s\w"-=\/\.:;]+)((?:(height="[^"]+")))/ig, '<img$1')
      .replace(/<img([\s\w"-=\/\.:;]+)((?:(width="[^"]+")))/ig, '<img$1')
      .replace(/<img([\s\w"-=\/\.:;]+)((?:(style="[^"]+")))/ig, '<img$1')
      .replace(/<img([\s\w"-=\/\.:;]+)((?:(alt="[^"]+")))/ig, '<img$1')
      // .replace(/\<img/gi, '<img style="width:200px;height:auto;display:block;margin: 0 auto; "')
      .replace(/<img([\s\w"-=\/\.:;]+)/ig, '<img style="max-width: 100%;display:block;height:auto;" $1');
    })
  },
  //长按预览图片下载保存图片
  richTextClick:function() {
     // 富文本
     const richContent = this.data.info.content;
     // 判断含有图片
     if (richContent.indexOf("src") >= 0) {
       const imgs = [];
       richContent.replace(/]*src=['"]([^'"]+)[^>]*>/gi, function (match, capture) {
         imgs.push(capture);
       })
       wx.previewImage({
         current: imgs[0], // 当前显示图片的http链接
         showmenu:true,//可以进行下载或者分享
         urls: imgs
       })
     }
  }
})
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值