yyzone
Version:
yyzone vue components and utils
138 lines (124 loc) • 4.27 kB
JavaScript
import md5 from './md5'
import { t } from '../locale/'
export function getCookie(name) {
const cookieArray = window.document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)"))
return cookieArray != null ? unescape(cookieArray[2]) : null
}
export function getColor(str) {
const colors = [
'#E45D58',
'#35C5AF',
'#19CCE8',
'#FFB319',
'#7465C6',
]
str = str || ''
let char = md5(str).charAt(0).toLowerCase()
return colors['abcdefghijklmnopqrstuvwxyz0123456789'.indexOf(char) % colors.length]
}
export function getQuery(string, name) {
if (typeof string != 'string') return null
String.prototype.getQuery = function (name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)")
var r = this.substr(this.indexOf("\?") + 1).match(reg)
if (r != null) return unescape(r[2])
return null
}
return string.getQuery(name)
}
export function formatTime(date, fmt = 'YYYY-MM-DD HH:mm:ss') {
const o = {
'M+': date.getMonth() + 1,
'D+': date.getDate(),
'h+': date.getHours() % 12 === 0 ? 12 : date.getHours() % 12,
'H+': date.getHours(),
'm+': date.getMinutes(),
's+': date.getSeconds(),
'q+': Math.floor((date.getMonth() + 3) / 3),
'S': date.getMilliseconds()
}
const week = {
'0': '\u65e5',
'1': '\u4e00',
'2': '\u4e8c',
'3': '\u4e09',
'4': '\u56db',
'5': '\u4e94',
'6': '\u516d'
}
if (/(Y+)/.test(fmt)) {
fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
}
if (/(E+)/.test(fmt)) {
fmt = fmt.replace(RegExp.$1, ((RegExp.$1.length > 1) ? (RegExp.$1.length > 2 ? '\u661f\u671f' : '\u5468') : '') + week[date.getDay() + ''])
}
for (let k in o) {
if (new RegExp('(' + k + ')').test(fmt)) {
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)))
}
}
return fmt
}
export function friendlyTime(time) {
let date = (typeof time === 'number') ? new Date(time) : new Date((time || '').replace(/-/g, '/')),
diff = (((new Date()).getTime() - date.getTime()) / 1000),
dayDiff = Math.floor(diff / 86400)
let isValidDate = Object.prototype.toString.call(date) === '[object Date]' && !isNaN(date.getTime())
if (!isValidDate) {
console.error('not a valid date')
}
const formatDate = function (date) {
let today = new Date(date),
year = today.getFullYear(),
month = ('0' + (today.getMonth() + 1)).slice(-2),
day = ('0' + today.getDate()).slice(-2),
hour = ('0' + today.getHours()).slice(-2),
minute = ('0' + today.getMinutes()).slice(-2),
second = today.getSeconds()
return `${year}-${month}-${day} ${hour}:${minute}`
}
if (isNaN(dayDiff) || dayDiff < 0 || dayDiff >= 31) {
return formatDate(date);
}
return dayDiff === 0 && (
diff < 60 && t('friendlyTime.just') ||
diff < 120 && t('friendlyTime.oneMinuteAgo') ||
diff < 3600 && Math.floor(diff / 60) + t('friendlyTime.minutesAgo') ||
diff < 7200 && t('friendlyTime.oneHourAgo') ||
diff < 86400 && Math.floor(diff / 3600) + t('friendlyTime.hoursAgo')) ||
dayDiff === 1 && t('friendlyTime.yesterday') ||
dayDiff < 7 && dayDiff + t('friendlyTime.daysAgo') ||
dayDiff < 31 && Math.ceil(dayDiff / 7) + t('friendlyTime.weeksAgo')
}
/**
* @description: 函数节流
* @param {type}
* @return {type}
*/
export function throttle(fn, interval = 500){
let run = true
return () => {
if(!run){
return false
}
run = false
setTimeout(() => {
fn.apply(this, arguments)
run = true
}, interval)
}
}
/**
* @description: 函数防抖
* @param {type}
* @return {type}
*/
export function debounce(fn, interval = 500){
let timeout = null
return () => {
clearTimeout(timeout)
timeout = setTimeout(() => {
fn.apply(this, arguments)
}, interval)
}
}