module.exports = { /** * 消息格式化处理 * @param text * @param userid * @returns {string|*} */ textMsgFormat(text, userid) { if (!text) { return "" } const atReg = new RegExp(``, "g") text = text.trim().replace(/(\n\x20*){3,}/g, "\n\n"); text = text.replace(/ /g, ' ') text = text.replace(/

<\/p>/g, '


') text = text.replace(/\{\{RemoteURL\}\}/g, $A.apiUrl('../')) text = text.replace(atReg, ``) // 处理内容连接 if (/https*:\/\//.test(text)) { text = text.split(/(<[^>]*>)/g).map(string => { if (string && !/<[^>]*>/.test(string)) { string = string.replace(/(https*:\/\/)((\w|=|\?|\.|\/|&|-|:|\+|%|;)+)/g, "$1$2") } return string; }).join("") } // 处理图片显示尺寸 const array = text.match(/]*?>/g); if (array) { const widthReg = new RegExp("width=\"(\\d+)\""), heightReg = new RegExp("height=\"(\\d+)\"") array.some(res => { const widthMatch = res.match(widthReg), heightMatch = res.match(heightReg); if (widthMatch && heightMatch) { const width = parseInt(widthMatch[1]), height = parseInt(heightMatch[1]), maxSize = res.indexOf("emoticon") > -1 ? 150 : 220; const scale = $A.scaleToScale(width, height, maxSize, maxSize); const value = res .replace(widthReg, `original-width="${width}" width="${scale.width}"`) .replace(heightReg, `original-height="${height}" height="${scale.height}"`) text = text.replace(res, value) } }) } return text; } }