123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- class CutString {
- constructor(string, limit) {
- // temparary node to parse the html tags in the string
- this.tempDiv = document.createElement('div')
- this.tempDiv.id = 'TempNodeForTest'
- this.tempDiv.innerHTML = string
- // while parsing text no of characters parsed
- this.charCount = 0
- this.limit = limit
- }
- cut() {
- var newDiv = document.createElement('div')
- this.searchEnd(this.tempDiv, newDiv)
- return newDiv.innerHTML
- }
- searchEnd(parseDiv, newParent) {
- var ele
- var newEle
- for (var j = 0; j < parseDiv.childNodes.length; j++) {
- ele = parseDiv.childNodes[j]
- // not text node
- if (ele.nodeType != 3) {
- newEle = ele.cloneNode(true)
- newParent.appendChild(newEle)
- if (ele.childNodes.length === 0) continue
- newEle.innerHTML = ''
- var res = this.searchEnd(ele, newEle)
- if (res) return res
- else {
- continue
- }
- }
- if (ele.nodeValue.length + this.charCount >= this.limit) {
- newEle = ele.cloneNode(true)
- newEle.nodeValue = ele.nodeValue.substr(0, this.limit - this.charCount)
- newParent.appendChild(newEle)
- return true
- }
- newEle = ele.cloneNode(true)
- newParent.appendChild(newEle)
- this.charCount += ele.nodeValue.length
- }
- return false
- }
- }
- export function defaults(newVal, oldVal, callback) {
- let value
- if (newVal === null || newVal === undefined) {
- value = oldVal
- } else {
- value = newVal
- }
- if (callback) {
- value = callback(value)
- }
- return value
- }
- /**
- * html字符串裁剪
- * @param {String} html
- * @param {Number} length
- */
- export function htmlCut(html, length) {
- return new CutString(html, length).cut()
- }
- export function timeFormat(second) {
- //秒
- var str = '' //不支持大于60分钟的时间哟
- var minute = parseInt(second / 60)
- if (minute < 10) str += '0'
- str += minute
- second = parseInt(second % 60) + ''
- if (second.length == 1) second = '0' + second
- str = str + ':' + second
- return str
- }
- export function monthDayFormat(date) {
- //秒
- if (date.indexOf('T') != -1) {
- return date.substring(5, 10)
- }
- }
- /**
- * html字符串转文本
- * @param {String} html
- */
- export const html2Text = (function () {
- const div = document.createElement('div')
- return function (html) {
- if (!html) {
- return ''
- }
- div.innerHTML = html
- return div.innerText
- }
- })()
- /**
- * 计算字符串长度(英文占1个字符,中文汉字占2个字符)
- * @param {*} str
- */
- export function getCharLength(str = '') {
- let len = 0
- for (let i = 0; i < str.length; i++) {
- if (str.charCodeAt(i) > 127 || str.charCodeAt(i) == 94) {
- len += 2
- } else {
- len++
- }
- }
- return len
- }
- /**
- * 添加url请求参数
- * @param {String} url 地址
- * @param {Object} params 参数列表
- */
- export function addQueryParams(url, params = {}) {
- if (!url) {
- return ''
- }
- let has = url.indexOf('?') > -1
- let arr = []
- for (let key in params) {
- if (!url.match('&' + key + '|\\?' + key)) {
- arr.push(`${key}=${params[key]}`)
- }
- }
- if (arr.length) {
- return `${url}${has ? '&' : '?'}${arr.join('&')}`
- }
- return url
- }
- export function randomWord(randomFlag, min, max) {
- let str = ''
- let range = min
- let arr = [
- '0',
- '1',
- '2',
- '3',
- '4',
- '5',
- '6',
- '7',
- '8',
- '9',
- 'a',
- 'b',
- 'c',
- 'd',
- 'e',
- 'f',
- 'g',
- 'h',
- 'i',
- 'j',
- 'k',
- 'l',
- 'm',
- 'n',
- 'o',
- 'p',
- 'q',
- 'r',
- 's',
- 't',
- 'u',
- 'v',
- 'w',
- 'x',
- 'y',
- 'z',
- 'A',
- 'B',
- 'C',
- 'D',
- 'E',
- 'F',
- 'G',
- 'H',
- 'I',
- 'J',
- 'K',
- 'L',
- 'M',
- 'N',
- 'O',
- 'P',
- 'Q',
- 'R',
- 'S',
- 'T',
- 'U',
- 'V',
- 'W',
- 'X',
- 'Y',
- 'Z',
- ]
- // 随机产生
- if (randomFlag) {
- range = Math.round(Math.random() * (max - min)) + min
- }
- for (var i = 0; i < range; i++) {
- let pos = Math.round(Math.random() * (arr.length - 1))
- str += arr[pos]
- }
- return str
- }
- /**
- * 密码加密
- * @param {String} pwd
- */
- export function password(pwd) {
- const NUM = 2
- const front = randomWord(false, 8)
- const middle = randomWord(false, 8)
- const end = randomWord(false, 8)
- let str = Base64.encode(pwd)
- let str1 = str.substring(0, NUM)
- let str2 = str.substring(NUM)
- return front + str2 + middle + str1 + end
- }
- /**
- * 删除输入法表情符号
- * @param {*} content
- */
- export function removeEmoji(content = '') {
- return content.replace(/[\ud800-\udbff][\udc00-\udfff]/g, function (char) {
- if (char.length === 2) {
- return ''
- } else {
- return char
- }
- })
- }
|