前端常用的工具函数解决方案、Front-end common tool function solutions
import querystring from 'querystring'
export const cutOutText = (str, limit) => {
const arr = str.split(' ')
const matchRes = str.match(/\r?\n/g)
const lineFeedCount = matchRes ? matchRes.length : 0
const result = arr.slice(0, limit + lineFeedCount).join(' ')
return result
}
export const getWordCount = (str) => {
const arr = str.replace(/\r?\n/g, ' ').split(' ')
let count = 0
arr.forEach((item) => {
if (item) {
count += 1
}
})
return count
}
export function HTML2Text(htmlStr = '') {
if (!htmlStr) {
return htmlStr
}
return htmlStr.replace(/<[^>]*>/g, '').trim().replace(/ /g, ' ')
}
export function HTML2TextExceptImg(htmlStr = '') {
if (!htmlStr) {
return htmlStr
}
return htmlStr.replace(/<[^(>|img)]*>/g, '').trim().replace(/ /g, ' ')
}
export const isWeixin = () => /MicroMessenger/i.test(navigator.userAgent)
export const isQQ = () => /QQ/i.test(navigator.userAgent)
let barOffset
export function getBarOffset() {
if (barOffset) return Promise.resolve(barOffset)
return new Promise((resolve) => {
HJSDK.invoke('getStatusBarHeight', ({ height }) => resolve(height || 0))
})
}
}
export function isIPhoneX() {
return /iPhone/gi.test(navigator.userAgent) && (screen.height === 812 && screen.width === 375)
}
export const isHJIPadApp = () => isHJApp() && /iPad/i.test(navigator.userAgent)
export const isIos = () => /iPhone|iPad|iPod/i.test(navigator.userAgent)
export const isHjIosApp = () => isHJApp() && /iPhone|iPad|iPod/i.test(navigator.userAgent)
export const isMobile = () => {
let useragent = navigator.userAgent.toLowerCase()
return useragent.indexOf('mobile') > -1 || useragent.indexOf('hjapp') > -1 || useragent.indexOf('android') > -1
}
export const isChrome = () => {
let useragent = navigator.userAgent.toLowerCase()
return useragent.indexOf('chrome') > -1
}
export const isFirefox = () => {
let useragent = navigator.userAgent.toLowerCase()
return useragent.indexOf('firefox') > -1
}
export const setCursorPosition = (pos, target) => { if (target.setSelectionRange) { target.focus() target.setSelectionRange(pos, pos) } }
export const getCursorStatus = (target) => { const cursor = { text: '', start: 0, end: 0 } if (target.setSelectionRange) { cursor.start = target.selectionStart cursor.end = target.selectionEnd cursor.operateText = (cursor.start !== cursor.end) ? target.value.substring(cursor.start, cursor.end) : '' } return cursor } export const isFromHJClass = () => { // 网校的请求 const query = querystring.parse(window.location.search.replace('?', '')) // qa环境和线上环境appId const hjClassAppId = ['2017071019372507', '2017073115243401'] return query.appId && hjClassAppId.indexOf(query.appId) !== -1 }
export const getPublicParameter = (key) => { let ret = {} const query = querystring.parse(window.location.search.replace('?', '')) if (query._p) { ret = querystring.parse(query._p) } if (key) { return ret[key] } return ret }
export const checkAudioContext = () => !!(typeof window.AudioContext !== 'undefined' || typeof webkitAudioContext !== 'undefined' || typeof window.mozAudioContext !== 'undefined' )
export const checkGetUserMedia = () => !!(window.navigator.getUserMedia || window.navigator.webkitGetUserMedia || window.navigator.mozGetUserMedia || window.navigator.msGetUserMedia)
// QQ 浏览器 会抛出异常需要在catch里面检测 const checkCatchMedia = () => navigator.mediaDevices.getUserMedia({ audio: true }).then(() => true).catch(() => false)
// 判断当前是什么环境 export const checkRecordEnv = () => { let record if (isWeixin()) { record = '微信环境' } else if (isHJApp()) { record = 'APP环境' } else { record = '普通浏览器环境' } return record }
const checkAppleWebkit = () => { let isSupport = true const isSafari = navigator.userAgent.indexOf('AppleWebKit') > -1 if (isSafari && isIos()) { const reAppleWebKit = new RegExp('AppleWebKit/(\d+(?:\.\d*)?)') reAppleWebKit.test(navigator.userAgent) if (parseFloat(RegExp.$1) < 605) { isSupport = false } } return isSupport }
// 检测是否支持录音 export const isRecord = () => new Promise((resolve, reject) => { let res = true if (!isHJApp() && !isWeixin()) { res = checkAudioContext() && checkGetUserMedia() && checkCatchMedia() // && checkAppleWebkit() } resolve(res) }) export const isVersion4 = () => { // 后端接口v4版本,导致了很多逻辑跟原来不同,需要添加兼容处理 const query = querystring.parse(window.location.search.replace('?', '')) // 如果url里有key参数,代表使用v4版本接口 return !!query.key }
export const isRedo = () => { const query = querystring.parse(window.location.search.replace('?', '')) return !!query.redo }
// location 为类似window.location的结构 export const transformLocation = (location) => { const query = querystring.parse(location.search.replace('?', '')) return { ...location, query, } } export const removeSpace = (str) => { const tempArr = [] str.split('&&&').forEach((s) => { tempArr.push(s.trim()) }) return tempArr.join('&&&') }
export const enableVConsole = () => { new window.VConsole({ onReady: () => { const times = 6 // 两秒钟内连续点击次数,到次数后显示vConsole const consoleDom = document.getElementById('__vconsole') consoleDom.style.display = 'none' let clickedTime = 0 document.body.addEventListener('click', () => { clickedTime += 1 if (clickedTime === 1) { setTimeout(() => { clickedTime = 0 }, 2000); } else if (clickedTime >= times) { consoleDom.style.display = 'block' } }) }, }) }
export const http2https = (str) => {
if (typeof str !== 'string') return str
if (str.substring(0, 5) === 'https') {
return str
} if (str.substring(0, 4) === 'http') {
return https${str.slice(4)}
}
return str
}
export const isWindows = () => navigator.userAgent.toLowerCase().indexOf('windows') >= 0
export function getSecondSpan(timeSpan = 0) { return (timeSpan / 1000).toFixed(1) }
/**
- 图片转base64格式 */ export function img2Base64(url, crossOrigin) { return new Promise((resolve) => { const img = new Image(); img.onload = () => { const c = document.createElement('canvas'); c.width = img.naturalWidth; c.height = img.naturalHeight; const cxt = c.getContext('2d'); cxt.drawImage(img, 0, 0); // 得到图片的base64编码数据 resolve(c.toDataURL('image/png')); }; crossOrigin && img.setAttribute('crossOrigin', 'anonymous'); img.src = url; }); }
export function preFetchImg(url, crossOrigin) { return new Promise((resolve) => { const img = new Image(); img.onload = () => { resolve(img); }; crossOrigin && img.setAttribute('crossOrigin', 'anonymous'); img.src = url; }); }
export const delaySkip = (url) => { setTimeout(() => { location.href = url }, 100) }
export const isChinese = (str) => { if (escape(str).indexOf('%u') < 0) return false return true }
export const emoj2str = (str) => { return unescape(escape(str).replace(/%uD.{3}/g, '')) }
export const safeText = (str) => {
let res = emoj2str(str)
if (isChinese(res)) {
res = res.length > 4 ? ${res.slice(0, 4)}...
: res
} else {
res = res.length > 7 ? ${res.slice(0, 7)}...
: res
}
return res
}
/**
- 序列化Error,
- 因为 error 直接 JSON.stringify 会返回 '{}', 所以需要序列化一下 */ export const serializeError = (err = {}) => { return JSON.stringify({ name: err.name, message: err.message, description: err.description, fileName: err.fileName, lineNumber: err.lineNumber, columnNumber: err.columnNumber, stack: err.stack, }) }
/**
- numFixed(*, 2)
- 数据格式化 97 => 97 97.3232 => 97.32 */ export const numFixed = (n = 0, m = 0) => { return n === parseInt(n, 10) ? parseInt(n, 10) : parseFloat(n).toFixed(m) }