mirror of
https://gitee.com/java110/WechatOwnerService.git
synced 2026-06-11 06:07:24 +08:00
优化代码
This commit is contained in:
parent
92e1580473
commit
140094cb1d
@ -44,6 +44,10 @@
|
|||||||
import {
|
import {
|
||||||
loadActivites
|
loadActivites
|
||||||
} from '../../api/index/indexApi.js'
|
} from '../../api/index/indexApi.js'
|
||||||
|
|
||||||
|
import conf from '../../conf/config.js'
|
||||||
|
|
||||||
|
import {replaceImgSrc} from '../../utils/ImageUtil.js'
|
||||||
export default {
|
export default {
|
||||||
data() {
|
data() {
|
||||||
return {
|
return {
|
||||||
@ -122,7 +126,7 @@
|
|||||||
_that.src = _activites.src;
|
_that.src = _activites.src;
|
||||||
_that.userName = _activites.userName;
|
_that.userName = _activites.userName;
|
||||||
_that.startTime = _activites.startTime;
|
_that.startTime = _activites.startTime;
|
||||||
_that.context = _activites.context;
|
_that.context = replaceImgSrc(_activites.context,conf.baseUrl);
|
||||||
_that.readCount = _activites.readCount;
|
_that.readCount = _activites.readCount;
|
||||||
_that.likeCount = _activites.likeCount;
|
_that.likeCount = _activites.likeCount;
|
||||||
});
|
});
|
||||||
|
|||||||
@ -18,6 +18,8 @@
|
|||||||
/** detail.js **/
|
/** detail.js **/
|
||||||
const context = require("../../../context/Java110Context.js");
|
const context = require("../../../context/Java110Context.js");
|
||||||
const constant = context.constant; //获取app实例
|
const constant = context.constant; //获取app实例
|
||||||
|
import conf from '../../../conf/config.js'
|
||||||
|
import {replaceImgSrc} from '../../../utils/ImageUtil.js'
|
||||||
//获取app实例
|
//获取app实例
|
||||||
const app = getApp().globalData;
|
const app = getApp().globalData;
|
||||||
|
|
||||||
@ -60,7 +62,7 @@
|
|||||||
console.log(res);
|
console.log(res);
|
||||||
let notice = res.data.notices[0]
|
let notice = res.data.notices[0]
|
||||||
notice.timeStr = notice.startTime.replace(/:\d{1,2}$/, ' ');
|
notice.timeStr = notice.startTime.replace(/:\d{1,2}$/, ' ');
|
||||||
|
notice.context = replaceImgSrc(notice.context,conf.baseUrl);
|
||||||
that.notice = notice;
|
that.notice = notice;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
54
utils/ImageUtil.js
Normal file
54
utils/ImageUtil.js
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
import {
|
||||||
|
isEmpty
|
||||||
|
} from './StringUtil'
|
||||||
|
/**
|
||||||
|
* 处理富文本里的图片宽度自适应
|
||||||
|
* 1.去掉img标签里的style、width、height属性
|
||||||
|
* 2.img标签添加style属性:max-width:100%;height:auto
|
||||||
|
* 3.修改所有style里的width属性为max-width:100%
|
||||||
|
* 4.去掉<br/>标签
|
||||||
|
* @param html
|
||||||
|
* @returns {void|string|*}
|
||||||
|
*/
|
||||||
|
export function formatRichText(html) {
|
||||||
|
let newContent = html.replace(/<img[^>]*>/gi, function(match, capture) {
|
||||||
|
match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '');
|
||||||
|
match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '');
|
||||||
|
match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '');
|
||||||
|
return match;
|
||||||
|
});
|
||||||
|
newContent = newContent.replace(/style="[^"]+"/gi, function(match, capture) {
|
||||||
|
match = match.replace(/width:[^;]+;/gi, 'max-width:100%;').replace(/width:[^;]+;/gi, 'max-width:100%;');
|
||||||
|
return match;
|
||||||
|
});
|
||||||
|
newContent = newContent.replace(/<br[^>]*\/>/gi, '');
|
||||||
|
newContent = newContent.replace(/\<img/gi, '<img style="max-width:100%;height:auto;display:block;margin:10px 0;"');
|
||||||
|
return newContent;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* 替换 图片src
|
||||||
|
* @param {Object} _content 富文本
|
||||||
|
* @param {Object} _url 图片前缀
|
||||||
|
*/
|
||||||
|
export function replaceImgSrc(_content, _url) {
|
||||||
|
console.log('_content', typeof(_content))
|
||||||
|
if (isEmpty(_content)) {
|
||||||
|
return _content;
|
||||||
|
}
|
||||||
|
// a 为富文本的字符串内容,为了测试,只写了img标签
|
||||||
|
let b = /<img [^>]*src=['"]([^'"]+)[^>]*>/g; // img 标签取src里面内容的正则
|
||||||
|
let s = _content.match(b); // 取到所有img标签 放到数组 s里面
|
||||||
|
if (!isEmpty(s)) {
|
||||||
|
for (let i = 0; i < s.length; i++) {
|
||||||
|
let srcImg = s[i].replace(b, '$1'); //取src面的内容
|
||||||
|
if (srcImg.slice(0, 4) == 'http' || srcImg.slice(0, 5) == 'https') {
|
||||||
|
//若src前4位置或者前5位是http、https则不做任何修改
|
||||||
|
} else {
|
||||||
|
//修改富文本字符串内容 img标签src 相对路径改为绝对路径
|
||||||
|
_content = _content.replace(srcImg, _url + srcImg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return formatRichText(_content);
|
||||||
|
}
|
||||||
@ -8,10 +8,32 @@
|
|||||||
* 判断是否为空
|
* 判断是否为空
|
||||||
* @param {Object} _value 字符串
|
* @param {Object} _value 字符串
|
||||||
*/
|
*/
|
||||||
export function isEmpty(_value){
|
export function isEmpty(_value) {
|
||||||
if(_value == undefined || _value == null || _value.trim() == ''){
|
let type = typeof _value;
|
||||||
return true;
|
|
||||||
|
switch (type) {
|
||||||
|
case 'number':
|
||||||
|
if (isFinite(_value)) {
|
||||||
|
if (_value == 0) {
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
case 'object':
|
||||||
|
for (let i in _value) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
case 'string':
|
||||||
|
if (_value.length == 0) {
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user