12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- <template>
- <view class="container main">
- <view class="one">
- <rich-text :nodes="formatRichText(config.brief)"></rich-text>
- </view>
- </view>
- </template>
- <script>
- export default {
- components: {},
- data() {
- return {
- config: {},
- }
- },
- onLoad: async function(e) {
- const that = this;
- that.searchConfig();
- },
- methods: {
- searchConfig() {
- const that = this;
- try {
- const res = uni.getStorageSync('config');
- if (res) that.$set(that, `config`, res);
- } catch (e) {}
- },
- // 处理富文本
- formatRichText(html) {
- if (html) {
- html = html.replace(/<table[^>]*>/gi, match => {
- // 如果已有 style 属性,替换为新的样式;如果没有,添加新的 style 属性
- return match.replace(/style="[^"]+"/gi,
- `style="border-collapse: collapse; width: 100%;text-align: center;"`)
- .replace(/<table/gi,
- `<table style="border-collapse: collapse; width: 100%;text-align: center;"`);
- });
- html = html.replace(/<th[^>]*>/gi, match => {
- // 如果已有 style 属性,替换为新的样式;如果没有,添加新的 style 属性
- return match.replace(/style="[^"]+"/gi,
- `style="border: 1px solid #ddd; padding: 8px; background-color: #f2f2f2;text-align: center;"`
- )
- .replace(/<table/gi,
- `<table style="border: 1px solid #ddd; padding: 8px; background-color: #f2f2f2;text-align: center;"`
- );
- });
- html = html.replace(/<td[^>]*>/gi, match => {
- // 如果已有 style 属性,替换为新的样式;如果没有,添加新的 style 属性
- return match.replace(/style="[^"]+"/gi,
- `style="border: 1px solid #ddd; padding: 8px;text-align: center;"`)
- .replace(/<table/gi,
- `<table style="border: 1px solid #ddd; padding: 8px;text-align: center;"`);
- });
- // 富文本内容格式化
- return html && html.replace(/<img[^>]*>/gi, function(match, capture) {
- // 查找所有的 img 元素
- return match.replace(/style=".*"/gi, '').replace(/style='.*'/gi,
- '')
- // 删除找到的所有 img 元素中的 style 属性
- }).replace(/\<img/gi, '<img style="width:100%;"') // 对 img 元素增加 style 属性,并设置宽度为 100%
- }
- },
- }
- }
- </script>
- <style lang="scss" scoped>
- .main {
- .one {
- padding: 2vw 4vw;
- .rich-img {
- width: 100% !important;
- display: block;
- }
- }
- }
- </style>
|