| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343 |
- /**
- * 格式化时间
- * @param time
- * @param cFormat
- * @returns {string|null}
- */
- import { JSEncrypt } from 'jsencrypt'
- export function parseTime(time, cFormat) {
- if (arguments.length === 0) {
- return null
- }
- const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
- let date
- if (typeof time === 'object') {
- date = time
- } else {
- if (typeof time === 'string' && /^[0-9]+$/.test(time)) {
- time = parseInt(time)
- }
- if (typeof time === 'number' && time.toString().length === 10) {
- time = time * 1000
- }
- date = new Date(time)
- }
- const formatObj = {
- y: date.getFullYear(),
- m: date.getMonth() + 1,
- d: date.getDate(),
- h: date.getHours(),
- i: date.getMinutes(),
- s: date.getSeconds(),
- a: date.getDay(),
- }
- const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
- let value = formatObj[key]
- if (key === 'a') {
- return ['日', '一', '二', '三', '四', '五', '六'][value]
- }
- if (result.length > 0 && value < 10) {
- value = '0' + value
- }
- return value || 0
- })
- return time_str
- }
- /**
- * 格式化时间
- * @param time
- * @param option
- * @returns {string}
- */
- export function formatTime(time, option) {
- if (('' + time).length === 10) {
- time = parseInt(time) * 1000
- } else {
- time = +time
- }
- const d = new Date(time)
- const now = Date.now()
- const diff = (now - d) / 1000
- if (diff < 30) {
- return '刚刚'
- } else if (diff < 3600) {
- // less 1 hour
- return Math.ceil(diff / 60) + '分钟前'
- } else if (diff < 3600 * 24) {
- return Math.ceil(diff / 3600) + '小时前'
- } else if (diff < 3600 * 24 * 2) {
- return '1天前'
- }
- if (option) {
- return parseTime(time, option)
- } else {
- return (
- d.getMonth() +
- 1 +
- '月' +
- d.getDate() +
- '日' +
- d.getHours() +
- '时' +
- d.getMinutes() +
- '分'
- )
- }
- }
- /**
- * 将url请求参数转为json格式
- * @param url
- * @returns {{}|any}
- */
- export function paramObj(url) {
- const search = url.split('?')[1]
- if (!search) {
- return {}
- }
- const replacedSearch = decodeURIComponent(search)
- .replace(/"/g, '\\"')
- .replace(/&/g, '","')
- .replace(/=/g, '":"')
- .replace(/\+/g, ' ')
- return JSON.parse(`{${replacedSearch}}`)
- }
- /**
- * 父子关系的数组转换成树形结构数据
- * @param data
- * @returns {*}
- */
- export function translateDataToTree(data) {
- const parent = data.filter(
- (value) => value.parentId === 'undefined' || value.parentId == null
- )
- const children = data.filter(
- (value) => value.parentId !== 'undefined' && value.parentId != null
- )
- const translator = (parent, children) => {
- parent.forEach((parent) => {
- children.forEach((current, index) => {
- if (current.parentId === parent.id) {
- const temp = JSON.parse(JSON.stringify(children))
- temp.splice(index, 1)
- translator([current], temp)
- typeof parent.children !== 'undefined'
- ? parent.children.push(current)
- : (parent.children = [current])
- }
- })
- })
- }
- translator(parent, children)
- return parent
- }
- /**
- * 树形结构数据转换成父子关系的数组
- * @param data
- * @returns {[]}
- */
- export function translateTreeToData(data) {
- const result = []
- data.forEach((item) => {
- const loop = (data) => {
- result.push({
- id: data.id,
- name: data.name,
- parentId: data.parentId,
- })
- const child = data.children
- if (child) {
- for (let i = 0; i < child.length; i++) {
- loop(child[i])
- }
- }
- }
- loop(item)
- })
- return result
- }
- /**
- * 10位时间戳转换
- * @param time
- * @returns {string}
- */
- export function tenBitTimestamp(time) {
- const date = new Date(time * 1000)
- const y = date.getFullYear()
- let m = date.getMonth() + 1
- m = m < 10 ? '' + m : m
- let d = date.getDate()
- d = d < 10 ? '' + d : d
- let h = date.getHours()
- h = h < 10 ? '0' + h : h
- let minute = date.getMinutes()
- let second = date.getSeconds()
- minute = minute < 10 ? '0' + minute : minute
- second = second < 10 ? '0' + second : second
- return y + '年' + m + '月' + d + '日 ' + h + ':' + minute + ':' + second //组合
- }
- /**
- * 13位时间戳转换
- * @param time
- * @returns {string}
- */
- export function thirteenBitTimestamp(time) {
- const date = new Date(time / 1)
- const y = date.getFullYear()
- let m = date.getMonth() + 1
- m = m < 10 ? '' + m : m
- let d = date.getDate()
- d = d < 10 ? '' + d : d
- let h = date.getHours()
- h = h < 10 ? '0' + h : h
- let minute = date.getMinutes()
- let second = date.getSeconds()
- minute = minute < 10 ? '0' + minute : minute
- second = second < 10 ? '0' + second : second
- return y + '年' + m + '月' + d + '日 ' + h + ':' + minute + ':' + second //组合
- }
- /**
- * 获取随机id
- * @param length
- * @returns {string}
- */
- export function uuid(length = 32) {
- const num = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'
- let str = ''
- for (let i = 0; i < length; i++) {
- str += num.charAt(Math.floor(Math.random() * num.length))
- }
- return str
- }
- /**
- * m到n的随机数
- * @param m
- * @param n
- * @returns {number}
- */
- export function random(m, n) {
- return Math.floor(Math.random() * (m - n) + n)
- }
- /**
- * addEventListener
- * @type {function(...[*]=)}
- */
- export const on = (function () {
- return function (element, event, handler, useCapture = false) {
- if (element && event && handler) {
- element.addEventListener(event, handler, useCapture)
- }
- }
- })()
- /**
- * removeEventListener
- * @type {function(...[*]=)}
- */
- export const off = (function () {
- return function (element, event, handler, useCapture = false) {
- if (element && event) {
- element.removeEventListener(event, handler, useCapture)
- }
- }
- })()
- /**
- * 删除URL中指定search参数,会将参数值一起删除
- * @param {string} url 地址字符串
- * @param {array} aParam 要删除的参数key数组,如['name','age']
- * @return {string} 返回新URL字符串
- */
- export function ridUrlParam(url, aParam) {
- aParam.forEach((item) => {
- const fromindex = url.indexOf(`${item}=`) //必须加=号,避免参数值中包含item字符串
- if (fromindex !== -1) {
- // 通过url特殊符号,计算出=号后面的的字符数,用于生成replace正则
- const startIndex = url.indexOf('=', fromindex)
- const endIndex = url.indexOf('&', fromindex)
- const hashIndex = url.indexOf('#', fromindex)
- let reg
- if (endIndex !== -1) {
- // 后面还有search参数的情况
- const num = endIndex - startIndex
- reg = new RegExp(`${item}=.{${num}}`)
- url = url.replace(reg, '')
- } else if (hashIndex !== -1) {
- // 有hash参数的情况
- const num = hashIndex - startIndex - 1
- reg = new RegExp(`&?${item}=.{${num}}`)
- url = url.replace(reg, '')
- } else {
- // search参数在最后或只有一个参数的情况
- reg = new RegExp(`&?${item}=.+`)
- url = url.replace(reg, '')
- }
- }
- })
- const noSearchParam = url.indexOf('=')
- if (noSearchParam === -1) {
- url = url.replace(/\?/, '') // 如果已经没有参数,删除?号
- }
- return url
- }
- /**
- *
- *获取value的类型
- * @export
- * @param {*} value
- * @return {*} {String}
- */
- export function getType(value) {
- const type = Object.prototype.toString.call(value)
- return type.match(/\[object (.*)\]/)[1].toLowerCase()
- }
- /**
- *
- *是否无效值
- * @param {*} value
- */
- export const isVoid = (value) =>
- value === undefined || value === null || value === ''
- /**
- *
- *去除对象中的无效值
- * @param {*} object
- * @return {*}
- */
- export const validObject = (object) => {
- if (!object) {
- return {}
- }
- const result = { ...object }
- Object.keys(result).forEach((key) => {
- const value = result[key]
- if (isVoid(value)) {
- delete result[key]
- }
- })
- return result
- }
- export const encryptWithRsa = (string) => {
- let encrypt = new JSEncrypt()
- let key = window.eipRSAPublicKey
- if (!key) {
- key =
- 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCMnhQ99yP-eEU2jXdQWc6j-wWbqNLqOLinEGBY11WJUCmzHiEycDXPc6-3YMOvrdAiHZcjkMCzU_eRnBLUqkcNw9nhQrCak-sTpEVlAV21LskD6KMf-6PsfttUvpXeCO5g3Hg48F_vbLKxb8s_lcvQgCpKBIpsUdYRcp_PgSg8BQIDAQAB'
- }
- encrypt.setPublicKey(key)
- return encrypt.encrypt(string)
- }
|