You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
307 lines
8.5 KiB
307 lines
8.5 KiB
/**
|
|
* 权限判断
|
|
* @param {*} key
|
|
*/
|
|
export function hasPermission(key) {
|
|
// return true
|
|
return window.SITE_CONFIG['permissions'].indexOf(key) !== -1 || false
|
|
}
|
|
|
|
/**
|
|
* 获取字典数据列表
|
|
* @param dictType 字典类型
|
|
*/
|
|
export function getDictDataList(dictType) {
|
|
const type = window.SITE_CONFIG['dictList'].find((element) => (element.dictType === dictType))
|
|
if (type) {
|
|
return type.dataList
|
|
} else {
|
|
return []
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取字典名称
|
|
* @param dictType 字典类型
|
|
* @param dictValue 字典值
|
|
*/
|
|
export function getDictLabel(dictType, dictValue) {
|
|
const type = window.SITE_CONFIG['dictList'].find((element) => (element.dictType === dictType))
|
|
if (type) {
|
|
const val = type.dataList.find((element) => (element.dictValue === dictValue + ''))
|
|
if (val) {
|
|
return val.dictLabel
|
|
} else {
|
|
return dictValue
|
|
}
|
|
} else {
|
|
return dictValue
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取uuid
|
|
*/
|
|
export function getUUID() {
|
|
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
|
|
return (c === 'x' ? (Math.random() * 16 | 0) : ('r&0x3' | '0x8')).toString(16)
|
|
})
|
|
}
|
|
|
|
/**
|
|
* 获取svg图标(id)列表
|
|
*/
|
|
export function getIconList() {
|
|
var res = []
|
|
var list = document.querySelectorAll('svg symbol')
|
|
for (var i = 0; i < list.length; i++) {
|
|
res.push(list[i].id)
|
|
}
|
|
|
|
return res
|
|
}
|
|
|
|
/**
|
|
* 树形数据转换
|
|
* @param {*} data
|
|
* @param {*} id
|
|
* @param {*} pid
|
|
*/
|
|
export function treeDataTranslate(data, id = 'id', pid = 'pid') {
|
|
var res = []
|
|
var temp = {}
|
|
for (var i = 0; i < data.length; i++) {
|
|
temp[data[i][id]] = data[i]
|
|
}
|
|
for (var k = 0; k < data.length; k++) {
|
|
if (!temp[data[k][pid]] || data[k][id] === data[k][pid]) {
|
|
res.push(data[k])
|
|
continue
|
|
}
|
|
if (!temp[data[k][pid]]['children']) {
|
|
temp[data[k][pid]]['children'] = []
|
|
}
|
|
temp[data[k][pid]]['children'].push(data[k])
|
|
data[k]['_level'] = (temp[data[k][pid]]._level || 0) + 1
|
|
}
|
|
return res
|
|
}
|
|
|
|
/**
|
|
* 树形数据转换,移除空children
|
|
* @param {*} data
|
|
*/
|
|
export function treeDataTranslateChildren(data) {
|
|
// 循环遍历json数据
|
|
for (var i = 0; i < data.length; i++) {
|
|
if (data[i].children.length < 1) {
|
|
// children若为空数组,则将children设为undefined
|
|
data[i].children = undefined
|
|
} else {
|
|
treeDataTranslateChildren(data[i].children)
|
|
}
|
|
}
|
|
return data
|
|
}
|
|
|
|
// 获取范围内随机数据
|
|
export const genRandom = (min, max) => (Math.random() * (max - min + 1) | 0) + min
|
|
|
|
/**
|
|
* Parse the time to string
|
|
* @param {(Object|string|number)} time 时间戳
|
|
* @param {string} cFormat
|
|
* @returns {string}
|
|
*/
|
|
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]
|
|
// Note: getDay() returns 0 on Sunday
|
|
if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value] }
|
|
if (result.length > 0 && value < 10) {
|
|
value = '0' + value
|
|
}
|
|
return value || 0
|
|
})
|
|
return time_str
|
|
}
|
|
|
|
/**
|
|
* @param {number} time 时间戳
|
|
* @param {string} 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() +
|
|
'分'
|
|
)
|
|
}
|
|
}
|
|
/**
|
|
* 时间转日期
|
|
* @param {Date} dateTime
|
|
* @param {string} formatStr
|
|
*/
|
|
export function formatDate(dateTime, formatStr) {
|
|
var converted = Date.parse(dateTime)
|
|
var thisDateTime = new Date(converted)
|
|
if (thisDateTime) {
|
|
let str = formatStr
|
|
const Week = ['日', '一', '二', '三', '四', '五', '六']
|
|
|
|
str = str.replace(/yyyy|YYYY/, thisDateTime.getFullYear())
|
|
str = str.replace(/yy|YY/, (thisDateTime.getYear() % 100) > 9 ? (thisDateTime.getYear() % 100).toString() : '0' + (thisDateTime.getYear() % 100))
|
|
|
|
str = str.replace(/MM/, (thisDateTime.getMonth() + 1) > 9 ? (thisDateTime.getMonth() + 1).toString() : '0' + (thisDateTime.getMonth() + 1))
|
|
str = str.replace(/M/g, thisDateTime.getMonth())
|
|
|
|
str = str.replace(/w|W/g, Week[thisDateTime.getDay()])
|
|
|
|
str = str.replace(/dd|DD/, thisDateTime.getDate() > 9 ? thisDateTime.getDate().toString() : '0' + thisDateTime.getDate())
|
|
str = str.replace(/d|D/g, thisDateTime.getDate())
|
|
|
|
str = str.replace(/hh|HH/, thisDateTime.getHours() > 9 ? thisDateTime.getHours().toString() : '0' + thisDateTime.getHours())
|
|
str = str.replace(/h|H/g, thisDateTime.getHours())
|
|
str = str.replace(/mm/, thisDateTime.getMinutes() > 9 ? thisDateTime.getMinutes().toString() : '0' + thisDateTime.getMinutes())
|
|
str = str.replace(/m/g, thisDateTime.getMinutes())
|
|
|
|
str = str.replace(/ss|SS/, thisDateTime.getSeconds() > 9 ? thisDateTime.getSeconds().toString() : '0' + thisDateTime.getSeconds())
|
|
str = str.replace(/s|S/g, thisDateTime.getSeconds())
|
|
|
|
return str
|
|
} else { return formatStr }
|
|
}
|
|
|
|
// 获取时间区间内的所有【月份】列表
|
|
export function getAllDateMonthList(begin, end) {
|
|
begin = begin.split('-')
|
|
var res = []
|
|
var year = +begin[0]
|
|
var month = +begin[1]
|
|
var date = new Date(year, month, 0)
|
|
var endTime = new Date(end).getTime()
|
|
while (date.getTime() <= endTime) {
|
|
res.push(year + '-' + month.toString().replace(/^(\d)$/, '0$1'))
|
|
// 加一天,强制到下一月份
|
|
date = new Date(date.getTime() + 24 * 60 * 60 * 1000)
|
|
year = date.getFullYear()
|
|
month = date.getMonth() + 1
|
|
date = new Date(year, month, 0)
|
|
}
|
|
res.push(end)
|
|
return res
|
|
}
|
|
|
|
/**
|
|
* @param strBirthday:指的是出生日期,格式为"1990-01-01"
|
|
*/
|
|
export function getAge(strBirthday) {
|
|
var returnAge
|
|
var strBirthdayArr = strBirthday.split('-')
|
|
var birthYear = strBirthdayArr[0]
|
|
var birthMonth = strBirthdayArr[1]
|
|
var birthDay = strBirthdayArr[2]
|
|
var d = new Date()
|
|
var nowYear = d.getFullYear()
|
|
var nowMonth = d.getMonth() + 1
|
|
var nowDay = d.getDate()
|
|
if (nowYear === birthYear) {
|
|
returnAge = 0// 同年 则为0周岁
|
|
} else {
|
|
var ageDiff = nowYear - birthYear // 年之差
|
|
if (ageDiff > 0) {
|
|
if (nowMonth === birthMonth) {
|
|
var dayDiff = nowDay - birthDay// 日之差
|
|
if (dayDiff < 0) {
|
|
returnAge = ageDiff - 1
|
|
} else {
|
|
returnAge = ageDiff
|
|
}
|
|
} else {
|
|
var monthDiff = nowMonth - birthMonth// 月之差
|
|
if (monthDiff < 0) {
|
|
returnAge = ageDiff - 1
|
|
} else {
|
|
returnAge = ageDiff
|
|
}
|
|
}
|
|
} else {
|
|
returnAge = -1// 返回-1 表示出生日期输入错误 晚于今天
|
|
}
|
|
}
|
|
return returnAge// 返回周岁年龄
|
|
}
|
|
|
|
/**
|
|
* 字段脱敏处理
|
|
* @param {String} field 未脱敏字段
|
|
* @param {Int} before 开头未脱敏字符数
|
|
* @param {Int} after 结尾未脱敏字符数
|
|
* @return {String} 已脱敏字段
|
|
*/
|
|
export function desensitizeField(field, before = 3, after = 4, middle = '***') {
|
|
if (!field) { return '' }
|
|
field = String(field)
|
|
// 匹配中文、英文、数字
|
|
const regItem = '[\u4e00-\u9fa5a-zA-Z0-9]'
|
|
const regExp = `(${regItem}{${before}})${regItem}*(${regItem}{${after}})`
|
|
const reg = new RegExp(regExp)
|
|
|
|
return field.replace(reg, `$1${middle}$2`)
|
|
}
|