lx-music-desktop/src/renderer/utils/index.js
2019-08-18 20:36:17 +08:00

206 lines
5.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import fs from 'fs'
import { shell, remote } from 'electron'
import path from 'path'
import os from 'os'
/**
* 获取两个数之间的随机整数大于等于min小于max
* @param {*} min
* @param {*} max
*/
export const getRandom = (min, max) => Math.floor(Math.random() * (max - min)) + min
export const sizeFormate = size => {
// https://gist.github.com/thomseddon/3511330
if (!size) return '0 b'
let units = ['b', 'kB', 'MB', 'GB', 'TB']
let number = Math.floor(Math.log(size) / Math.log(1024))
return `${(size / Math.pow(1024, Math.floor(number))).toFixed(2)} ${units[number]}`
}
export const formatPlayTime = time => {
let m = parseInt(time / 60)
let s = parseInt(time % 60)
return m === 0 && s === 0 ? '--/--' : (m < 10 ? '0' + m : m) + ':' + (s < 10 ? '0' + s : s)
}
export const formatPlayTime2 = time => {
let m = parseInt(time / 60)
let s = parseInt(time % 60)
return (m < 10 ? '0' + m : m) + ':' + (s < 10 ? '0' + s : s)
}
export const b64DecodeUnicode = str => {
// Going backwards: from bytestream, to percent-encoding, to original string.
return decodeURIComponent(window.atob(str).split('').map(function(c) {
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2)
}).join(''))
}
export const decodeName = str => str.replace(/&apos;/g, '\'')
export const scrollTo = (element, to, duration = 300, fn = function() {}) => {
const start = element.scrollTop || element.scrollY
const change = to - start
const increment = 10
if (!change) {
fn()
return
}
let currentTime = 0; let val
const easeInOutQuad = (t, b, c, d) => {
t /= d / 2
if (t < 1) return (c / 2) * t * t + b
t--
return (-c / 2) * (t * (t - 2) - 1) + b
}
const animateScroll = () => {
currentTime += increment
val = parseInt(easeInOutQuad(currentTime, start, change, duration))
if (element.scrollTo) {
element.scrollTo(0, val)
} else {
element.scrollTop = val
}
if (currentTime < duration) {
setTimeout(animateScroll, increment)
} else {
fn()
}
}
animateScroll()
}
/**
* 检查路径是否存在
* @param {*} path
*/
export const checkPath = path => fs.existsSync(path)
/**
* 在资源管理器中打开目录
* @param {*} 选项
*/
export const openSelectDir = options => remote.dialog.showOpenDialog(remote.getCurrentWindow(), options)
/**
* 在资源管理器中打开目录
* @param {*} 选项
*/
export const openSaveDir = options => remote.dialog.showSaveDialog(remote.getCurrentWindow(), options)
/**
* 在资源管理器中打开目录
* @param {*} dir
*/
export const openDirInExplorer = dir => {
shell.showItemInFolder(dir)
}
export const checkVersion = (currentVer, targetVer) => {
// console.log(currentVer)
// console.log(targetVer)
currentVer = currentVer.split('.')
targetVer = targetVer.split('.')
let maxLen = Math.max(currentVer.length, targetVer.length)
if (currentVer.length < maxLen) {
for (let index = 0, len = maxLen - currentVer.length; index < len; index++) {
currentVer.push(0)
}
}
if (targetVer.length < maxLen) {
for (let index = 0, len = maxLen - targetVer.length; index < len; index++) {
targetVer.push(0)
}
}
for (let index = 0; index < currentVer.length; index++) {
if (parseInt(currentVer[index]) < parseInt(targetVer[index])) return true
if (parseInt(currentVer[index]) > parseInt(targetVer[index])) return false
}
return false
}
export const isObject = item => item && typeof item === 'object' && !Array.isArray(item)
/**
* 对象深度合并
* 注意:循环引用的对象会出现死循环
* @param {} target 要合并源对象
* @param {} source 要合并目标对象
*/
export const objectDeepMerge = (target, source) => {
let base = {}
Object.keys(source).forEach(item => {
if (Array.isArray(source[item])) {
let arr = Array.isArray(target[item]) ? target[item] : []
target[item] = arr.concat(source[item])
return
} else if (isObject(source[item])) {
if (!isObject(target[item])) target[item] = {}
objectDeepMerge(target[item], source[item])
return
}
base[item] = source[item]
})
Object.assign(target, base)
}
/**
* 判断是否父子元素
* @param {*} parent
* @param {*} children
*/
export const isChildren = (parent, children) => {
return children.parentNode ? children.parentNode === parent ? true : isChildren(parent, children.parentNode) : false
}
export const updateSetting = setting => {
const defaultVersion = '1.0.2'
const defaultSetting = {
version: defaultVersion,
player: {
togglePlayMethod: 'listLoop',
highQuality: false,
},
list: {
isShowAlbumName: true,
},
download: {
savePath: path.join(os.homedir(), 'Desktop'),
fileName: '歌名 - 歌手',
},
leaderboard: {
source: 'kw',
tabId: 'kwbiaosb',
},
themeId: 0,
sourceId: 'kw',
apiSource: 'messoer',
randomAnimate: true,
}
const overwriteSetting = {
version: defaultVersion,
sourceId: 'kw',
}
if (!setting) {
setting = defaultSetting
} else if (checkVersion(setting.version, defaultSetting.version)) {
objectDeepMerge(defaultSetting, setting)
objectDeepMerge(defaultSetting, overwriteSetting)
setting = defaultSetting
}
return setting
}
/**
* 在浏览器打开URL
* @param {*} url
*/
export const openUrl = url => {
shell.openExternal(url)
}