2021-11-11 16:56:54 +08:00
|
|
|
import Cookies from 'js-cookie'
|
2022-07-13 16:17:56 +08:00
|
|
|
export function timeSection(date, type, labelFormat = 'yyyy-MM-dd') {
|
2021-05-21 12:19:32 +08:00
|
|
|
if (!date) {
|
|
|
|
return null
|
|
|
|
}
|
2021-12-01 11:31:37 +08:00
|
|
|
if (!(date instanceof Date)) {
|
|
|
|
date = new Date(date)
|
|
|
|
}
|
2021-05-21 12:19:32 +08:00
|
|
|
const timeRanger = new Array(2)
|
2021-03-03 15:06:52 +08:00
|
|
|
|
2022-07-13 16:17:56 +08:00
|
|
|
const formatArr = labelFormat ? labelFormat.split(' ') : []
|
|
|
|
const methods = ['setHours', 'setMinutes', 'setSeconds', 'setMilliseconds']
|
|
|
|
let methodsLen = methods.length
|
|
|
|
if (type === 'datetime' && formatArr.length > 1) {
|
|
|
|
const childArr = formatArr[1] ? formatArr[1].split(':') : []
|
|
|
|
const childArrLength = childArr ? childArr.length : 0
|
|
|
|
|
|
|
|
while (--methodsLen >= childArrLength) {
|
|
|
|
date[methods[methodsLen]](0)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
methods.forEach(m => date[m](0))
|
|
|
|
}
|
|
|
|
|
2021-05-21 12:19:32 +08:00
|
|
|
const end = new Date(date)
|
|
|
|
if (type === 'year') {
|
|
|
|
date.setDate(1)
|
|
|
|
date.setMonth(0)
|
|
|
|
end.setFullYear(date.getFullYear() + 1)
|
|
|
|
timeRanger[1] = end.getTime() - 1
|
|
|
|
}
|
|
|
|
if (type === 'month') {
|
|
|
|
date.setDate(1)
|
|
|
|
const currentMonth = date.getMonth()
|
|
|
|
if (currentMonth === 11) {
|
|
|
|
end.setFullYear(date.getFullYear() + 1)
|
|
|
|
end.setMonth(0)
|
|
|
|
} else {
|
|
|
|
end.setMonth(date.getMonth() + 1)
|
|
|
|
}
|
|
|
|
timeRanger[1] = end.getTime() - 1
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type === 'date') {
|
|
|
|
end.setHours(23)
|
|
|
|
end.setMinutes(59)
|
|
|
|
end.setSeconds(59)
|
|
|
|
end.setMilliseconds(999)
|
|
|
|
timeRanger[1] = end.getTime()
|
|
|
|
}
|
2022-07-13 16:17:56 +08:00
|
|
|
|
|
|
|
if (type === 'datetime') {
|
|
|
|
methodsLen = methods.length
|
|
|
|
if (formatArr.length > 1) {
|
|
|
|
const childArr = formatArr[1] ? formatArr[1].split(':') : []
|
|
|
|
const childArrLength = childArr ? childArr.length : 0
|
|
|
|
|
|
|
|
while (--methodsLen >= childArrLength) {
|
|
|
|
end[methods[methodsLen]](methodsLen === 0 ? 23 : methodsLen === 3 ? 999 : 59)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
while (--methodsLen >= 0) {
|
|
|
|
end[methods[methodsLen]](methodsLen === 0 ? 23 : methodsLen === 3 ? 999 : 59)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
timeRanger[1] = end.getTime()
|
|
|
|
}
|
2021-05-21 12:19:32 +08:00
|
|
|
timeRanger[0] = date.getTime()
|
|
|
|
|
|
|
|
return timeRanger
|
|
|
|
}
|
2021-05-20 22:14:07 +08:00
|
|
|
export function dateFormat(date, fmt) {
|
|
|
|
let ret
|
|
|
|
const opt = {
|
|
|
|
'y+': date.getFullYear().toString(), // 年
|
|
|
|
'M+': (date.getMonth() + 1).toString(), // 月
|
|
|
|
'd+': date.getDate().toString(), // 日
|
|
|
|
'H+': date.getHours().toString(), // 时
|
|
|
|
'm+': date.getMinutes().toString(), // 分
|
|
|
|
's+': date.getSeconds().toString() // 秒
|
|
|
|
// 有其他格式化字符需求可以继续添加,必须转化成字符串
|
|
|
|
}
|
|
|
|
for (const k in opt) {
|
|
|
|
ret = new RegExp('(' + k + ')').exec(fmt)
|
|
|
|
if (ret) {
|
|
|
|
fmt = fmt.replace(ret[1], (ret[1].length === 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, '0')))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fmt
|
|
|
|
}
|
|
|
|
|
2021-03-03 15:06:52 +08:00
|
|
|
/**
|
|
|
|
* Parse the time to string
|
|
|
|
* @param {(Object|string|number)} time
|
|
|
|
* @param {string} cFormat
|
|
|
|
* @returns {string | null}
|
|
|
|
*/
|
|
|
|
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(/{([ymdhisa])+}/g, (result, key) => {
|
|
|
|
const value = formatObj[key]
|
|
|
|
// Note: getDay() returns 0 on Sunday
|
2021-12-29 10:57:46 +08:00
|
|
|
if (key === 'a') {
|
|
|
|
return ['日', '一', '二', '三', '四', '五', '六'][value]
|
|
|
|
}
|
2021-03-03 15:06:52 +08:00
|
|
|
return value.toString().padStart(2, '0')
|
|
|
|
})
|
|
|
|
return time_str
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if an element has a class
|
|
|
|
* @param {HTMLElement} elm
|
|
|
|
* @param {string} cls
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
export function hasClass(ele, cls) {
|
|
|
|
return !!ele.className.match(new RegExp('(\\s|^)' + cls + '(\\s|$)'))
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-12-29 10:57:46 +08:00
|
|
|
* Add class to element
|
|
|
|
* @param {HTMLElement} elm
|
|
|
|
* @param {string} cls
|
|
|
|
*/
|
2021-03-03 15:06:52 +08:00
|
|
|
export function addClass(ele, cls) {
|
|
|
|
if (!hasClass(ele, cls)) ele.className += ' ' + cls
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-12-29 10:57:46 +08:00
|
|
|
* Remove class from element
|
|
|
|
* @param {HTMLElement} elm
|
|
|
|
* @param {string} cls
|
|
|
|
*/
|
2021-03-03 15:06:52 +08:00
|
|
|
export function removeClass(ele, cls) {
|
|
|
|
if (hasClass(ele, cls)) {
|
|
|
|
const reg = new RegExp('(\\s|^)' + cls + '(\\s|$)')
|
|
|
|
ele.className = ele.className.replace(reg, ' ')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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 {string} url
|
|
|
|
* @returns {Object}
|
|
|
|
*/
|
|
|
|
export function param2Obj(url) {
|
|
|
|
const search = url.split('?')[1]
|
|
|
|
if (!search) {
|
|
|
|
return {}
|
|
|
|
}
|
|
|
|
return JSON.parse(
|
|
|
|
'{"' +
|
2021-12-29 10:57:46 +08:00
|
|
|
decodeURIComponent(search)
|
|
|
|
.replace(/"/g, '\\"')
|
|
|
|
.replace(/&/g, '","')
|
|
|
|
.replace(/=/g, '":"')
|
|
|
|
.replace(/\+/g, ' ') +
|
|
|
|
'"}'
|
2021-03-03 15:06:52 +08:00
|
|
|
)
|
|
|
|
}
|
2021-03-10 18:26:53 +08:00
|
|
|
|
|
|
|
export function formatCondition(param) {
|
|
|
|
if (!param) {
|
|
|
|
return null
|
|
|
|
}
|
2021-12-29 10:57:46 +08:00
|
|
|
const result = {
|
|
|
|
conditions: []
|
|
|
|
}
|
2021-06-24 14:31:01 +08:00
|
|
|
// eslint-disable-next-line no-unused-vars
|
2021-03-11 22:58:05 +08:00
|
|
|
for (const [key, value] of Object.entries(param)) {
|
|
|
|
result.conditions.push(value)
|
2021-03-10 18:26:53 +08:00
|
|
|
}
|
2021-03-11 22:58:05 +08:00
|
|
|
return result
|
2021-03-10 18:26:53 +08:00
|
|
|
}
|
2021-06-02 13:52:01 +08:00
|
|
|
/**
|
|
|
|
* 驼峰转下划线
|
|
|
|
* @param {*} name
|
|
|
|
* @returns
|
|
|
|
*/
|
|
|
|
export function toLine(name) {
|
|
|
|
return name.replace(/([A-Z])/g, '_$1').toLowerCase()
|
|
|
|
}
|
|
|
|
export function addOrder(order, orders) {
|
|
|
|
order.field = toLine(order.field)
|
|
|
|
if (order.value.startsWith('desc')) {
|
|
|
|
order.value = 'desc'
|
|
|
|
} else {
|
|
|
|
order.value = 'asc'
|
|
|
|
}
|
|
|
|
orders = orders || []
|
|
|
|
for (let index = 0; index < orders.length; index++) {
|
|
|
|
const element = orders[index]
|
|
|
|
if (order.field === element.field) {
|
|
|
|
orders[index] = order
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
orders.push(order)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function formatOrders(orders) {
|
|
|
|
return orders.map(order => order.field + ' ' + order.value)
|
|
|
|
}
|
2021-03-25 18:58:05 +08:00
|
|
|
|
2021-05-19 17:25:07 +08:00
|
|
|
export function formatQuickCondition(param, quickField) {
|
|
|
|
let quickObj = null
|
|
|
|
if (!param || !(quickObj = param.quick) || !quickField) {
|
|
|
|
quickObj && delete param.quick
|
|
|
|
return param
|
|
|
|
}
|
|
|
|
param[quickField] = {
|
|
|
|
field: quickField,
|
|
|
|
operator: 'like',
|
|
|
|
value: quickObj.value
|
|
|
|
}
|
|
|
|
delete param.quick
|
|
|
|
return param
|
|
|
|
}
|
|
|
|
|
2021-03-25 18:58:05 +08:00
|
|
|
export function getQueryVariable(variable) {
|
2021-11-11 16:56:54 +08:00
|
|
|
let query = window.location.search.substring(1)
|
|
|
|
if (!query) {
|
|
|
|
query = Cookies.get(variable)
|
|
|
|
}
|
2021-12-29 10:57:46 +08:00
|
|
|
if (query !== undefined) {
|
2021-12-19 14:46:33 +08:00
|
|
|
const vars = query.split('&')
|
|
|
|
for (var i = 0; i < vars.length; i++) {
|
|
|
|
const pair = vars[i].split('=')
|
2021-12-29 10:57:46 +08:00
|
|
|
if (pair[0] === variable) {
|
|
|
|
return pair[1]
|
|
|
|
}
|
2021-12-19 14:46:33 +08:00
|
|
|
}
|
2021-03-25 18:58:05 +08:00
|
|
|
}
|
|
|
|
return (false)
|
|
|
|
}
|
|
|
|
|
2021-12-29 10:57:46 +08:00
|
|
|
export function isMobile() {
|
|
|
|
const flag = navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i)
|
|
|
|
return flag
|
|
|
|
}
|
2022-06-29 16:46:11 +08:00
|
|
|
|
|
|
|
export const isSameVueObj = (source, target) => {
|
|
|
|
if (!source && !target) return true
|
|
|
|
if (!!source && !!target) {
|
|
|
|
return JSON.stringify(source) === JSON.stringify(target)
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2022-08-10 14:08:03 +08:00
|
|
|
|
|
|
|
export const changeFavicon = link => {
|
|
|
|
let $favicon = document.querySelector('link[rel="icon"]')
|
|
|
|
if ($favicon !== null) {
|
|
|
|
$favicon.href = link
|
|
|
|
} else {
|
|
|
|
$favicon = document.createElement('link')
|
|
|
|
$favicon.rel = 'icon'
|
|
|
|
$favicon.href = link
|
|
|
|
document.head.appendChild($favicon)
|
|
|
|
}
|
|
|
|
}
|
2022-11-15 16:36:05 +08:00
|
|
|
|
|
|
|
export const mergeCustomSortOption = (customSortList, sourceList) => {
|
|
|
|
if (!customSortList?.length) return sourceList?.length ? sourceList : []
|
|
|
|
|
|
|
|
if (!sourceList?.length) return customSortList?.length ? customSortList : []
|
|
|
|
|
|
|
|
const result = [...customSortList, ...sourceList]
|
|
|
|
return [...new Set(result)]
|
|
|
|
}
|