const DOMAIN_URL = 'https://www.xxx.com/'
const API_URL = 'https://api.xxx.com/'
import {
CryptoJS
} from 'cryptojs.js'
//格式化时间
const formatTime = date => {
const year = date.getFullYear()
const month = date.getMonth() + 1
const day = date.getDate()
const hour = date.getHours()
const minute = date.getMinutes()
const second = date.getSeconds()
return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
}
//数字补位
const formatNumber = n => {
n = n.toString()
return n[1] ? n : `0${n}`
}
//格式化号码串
const formatNo = (str, len) => {
let exp = RegExp('(.{' + len + '})', 'g')
return str.replace(/\s/g, '').replace(exp, "$1 ")
}
//格式化富文本
const formatRichText = html => {
let content = html.replace(/<img[^>]*>/gi, match => {
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
})
content = content.replace(/style="[^"]+"/gi, match => {
match = match.replace(/width:[^;]+;/gi, 'max-width:100%;').replace(/width:[^;]+;/gi, 'max-width:100%;')
return match
})
content = content.replace(/\<img/gi, '<img style="max-width:100%;height:auto;display:block;margin-top:0;margin-bottom:0;"')
return content
}
//删除左右两端的空格
const trim = str => {
return str.replace(/(^\s*)|(\s*$)/g, '')
}
//获取字符串长度
const strlen = str => {
let len = 0
let i, c
for (i = 0; i < str.length; i++) {
c = str.charCodeAt(i)
if ((c >= 0x0001 && c <= 0x007e) || (0xff60 <= c && c <= 0xff9f)) {
len++
} else {
len += 2
}
}
return len
}
//判断对象是否为空
const isEmpty = ob => {
const type = Object.prototype.toString.call(ob)
let flag = false
if ('[object Array]' == type) {
flag = 0 == ob.length
} else if ('[object Object]' == type) {
flag = 0 == Object.keys(ob).length
} else if ('[object String]' == type) {
flag = 0 == ob.replace('/s', '').length
} else if ('[object Number]' == type) {
flag = isNaN(ob)
} else if ('[object Null]' == type) {
flag = true
} else if ('[object Undefined]' == type) {
flag = true
} else if ('[object Boolean]' == type) {
flag = !ob
}
return flag
}
//对象转URL参数
const urlEncode = obj => {
let params = Object.keys(obj).map(key => {
return encodeURIComponent(key) + '=' + encodeURIComponent(obj[key])
}).join('&')
return params
}
//加密
const encrypt = (str, key) => {
let keyHex = CryptoJS.enc.Utf8.parse(key)
let encrypted = CryptoJS.DES.encrypt(str, keyHex, {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7
})
return encrypted.toString()
}
//解密
const decrypt = (str, key) => {
let keyHex = CryptoJS.enc.Utf8.parse(key)
let decrypted = CryptoJS.DES.decrypt(str, keyHex, {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7
})
return decrypted.toString(CryptoJS.enc.Utf8)
}
module.exports = {
domainUrl: DOMAIN_URL,
apiUrl: API_URL,
formatTime: formatTime,
formatNumber: formatNumber,
formatNo: formatNo,
formatRichText: formatRichText,
isEmpty: isEmpty,
trim: trim,
strlen: strlen,
urlEncode: urlEncode,
encrypt: encrypt,
decrypt: decrypt
}
微信小程序util.js常用自定义函数集
最新推荐文章于 2024-06-19 14:31:33 发布