项目中遇到问题,后台配置富文本内容,小程序渲染的时候出现图片显示问题和视频节点不显示,排查过程中发现图片显示没问题,返回数据就是这么大的尺寸,那就是怎么适配了(下面直接上代码),视频节点不显示问题,看了下微信文档,这个组件是不支持vedio标签的,说了这么多直接上代码吧!
escape(html) {
return String(html)
// .replace(/&(?!\w+;)/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, "'");
},
convert (htmlText) {
let str = htmlText.replace(/<img[^>]*>/gi, function (match, capture) {
// return match.replace(/(<img[^>]*)(\/?>)/gi, "$1width='100%' $2") // 添加width="100%"
return match.replace(/style\s*?=\s*?([‘"])[\s\S]*?\1/ig, 'style="max-width:100%;height:auto;"') // 替换style
})
return str
},
// 富文本渲染修复
const callback = this.escape(res.rows.content)
const artDelContent = this.convert(callback).replace(/\<img/gi, '<img style="max-width:100%;height:auto;display:block;"')
let videoList = [];
let videoReg = /<video.*?(?:>|\/>)/gi; //匹配到字符串中的 video 标签
let srcReg = /src=[\'\"]?([^\'\"]*)[\'\"]?/i; //匹配到字符串中的 video 标签 的路径
let arr = artDelContent.match(videoReg) || []; // arr 为包含所有video标签的数组
let articleList = artDelContent.split('</video>') // 把字符串 从视频标签分成数组
arr.forEach((item,index)=>{
var src = item.match(srcReg);
videoList.push(src[1]) //所要显示的字符串中 所有的video 标签 的路径
})
let needArticleList = [];
articleList.forEach((item,index)=>{
if(item != "" && item != undefined){ // 常见的标签渲染
needArticleList.push({
type:'rich-text',
value:item+"</video>"
});
}
let articleListLength = articleList.length; // 插入到原有video 标签位置
if(index<articleListLength && videoList[index] != undefined){
needArticleList.push({
type:'video',
value:videoList[index]
})
}
})
this.setData({
artDelContent: needArticleList,
});
``
<view class="paragragh" wx:for="{{artDelContent}}" wx:key="index">
<rich-text wx:if="{{item.type=='rich-text'?true:false}}" class="imgsty" nodes="{{item.value}}"></rich-text>
<video wx:if="{{item.type=='video' && item.value != '' ?true:false}}" src="{{item.value}}" style="width:100%;height: 150px" frameborder="0"></video>
</view>
在这里插入代码片
欢迎大家沟通交流,这里是我的参考
[视频节点不显示问题](https://blog.csdn.net/forest_fire/article/details/121072470)
[图片适配问题](https://blog.csdn.net/qq_41909079/article/details/120453487)