This repository has been archived on 2023-08-30. You can view files and clone it, but cannot push or open issues/pull-requests.
appcat
/
sonist
Archived
1
0
Fork 0
sonist/js/app.js

417 lines
9.5 KiB
JavaScript
Raw Normal View History

2018-12-26 23:58:24 +08:00
/**
* {sonist app}
* @author yutent<yutent@doui.cc>
* @date 2018/12/16 17:15:57
*/
2019-01-04 16:24:42 +08:00
import '/lib/anot.next.js'
import layer from '/lib/layer/index.js'
import store from '/lib/store/index.js'
import AudioPlayer from '/lib/audio/index.js'
2019-01-04 19:28:03 +08:00
import Lyrics from '/lib/lyrics/index.js'
2018-12-26 23:58:24 +08:00
import Api from '/js/api.js'
import Artist from '/js/modules/artist.js'
import Local from '/js/modules/local.js'
2018-12-27 03:02:37 +08:00
import Profile from '/js/modules/profile.js'
2018-12-26 23:58:24 +08:00
const log = console.log
const fs = require('iofs')
const path = require('path')
2018-12-28 18:47:45 +08:00
const { remote } = require('electron')
const WIN = remote.getCurrentWindow()
const HOME_PATH = remote.app.getPath('appData')
2018-12-26 23:58:24 +08:00
const APP_INI_PATH = path.join(HOME_PATH, 'app.ini')
2018-12-28 22:19:29 +08:00
const LYRICS_PATH = path.join(HOME_PATH, 'lyrics')
2018-12-26 23:58:24 +08:00
const PLAY_MODE = {
0: 'all',
1: 'single',
2: 'random'
}
2018-12-29 20:00:19 +08:00
const COLORS = [
{
title: '#62778d',
lrc: '#98acae',
bar1: '#dae1e9',
bar2: '#3fc2a7'
},
{
title: '#fff',
lrc: '#d7d8db',
bar1: '#454545',
bar2: '#fff'
}
]
2018-12-26 23:58:24 +08:00
const FONTS_NAME =
' Helvetica, Arial,"WenQuanYi Micro Hei","PingFang SC","Hiragino Sans GB","Segoe UI", "Microsoft Yahei", sans-serif'
// 本地音乐和试用音乐列表
window.LS = store.collection('local')
window.TS = store.collection('temp')
// 音乐播放器
window.SONIST = new AudioPlayer()
2019-01-04 19:28:03 +08:00
window.LYRICS = new Lyrics()
2018-12-26 23:58:24 +08:00
let appInit = fs.cat(APP_INI_PATH)
2018-12-28 18:47:45 +08:00
Anot.ss('app-init', appInit + '')
2018-12-26 23:58:24 +08:00
appInit = JSON.parse(appInit)
Anot({
$id: 'app',
state: {
2018-12-28 22:19:29 +08:00
theme: appInit.theme || 1, // 1:macos, 2: deepin
2018-12-28 18:47:45 +08:00
winFocus: false,
2018-12-26 23:58:24 +08:00
mod: 'local',
playMode: Anot.ls('play-mode') >>> 0, // 0:all | 1:single | 2:random
2018-12-29 20:00:19 +08:00
ktvMode: 0,
2018-12-26 23:58:24 +08:00
isPlaying: false,
2018-12-28 18:47:45 +08:00
optBoxShow: false,
2018-12-28 22:19:29 +08:00
volumeCtrlShow: false,
volume: Anot.ls('volume') || 70,
2018-12-26 23:58:24 +08:00
curr: {
id: '',
title: '',
artist: '',
album: '',
time: 0,
duration: 0
2019-01-04 19:28:03 +08:00
},
ctrlLrc: '暂无歌词...',
lrc: {
l: { bg: '', txt: '' },
r: { bg: '', txt: '' }
2018-12-28 22:19:29 +08:00
}
2018-12-26 23:58:24 +08:00
},
skip: [],
computed: {
views() {
if (!this.mod) {
return
}
return '/views/' + this.mod + '.htm'
2018-12-29 20:00:19 +08:00
},
coverBG() {
if (this.curr.cover) {
return `url(${this.curr.cover})`
} else {
return 'none'
}
2018-12-26 23:58:24 +08:00
}
},
watch: {
mod(val) {
this.activeModule(val)
}
},
mounted() {
let canvas = this.$refs.player
// 画布放大4倍, 以解决模糊的问题
this.__WIDTH__ = canvas.clientWidth * 4
this.__HEIGHT__ = canvas.clientHeight * 4
canvas.width = this.__WIDTH__
canvas.height = this.__HEIGHT__
this.__CTX__ = canvas.getContext('2d')
2018-12-30 02:26:07 +08:00
this.draw(true)
2018-12-28 18:47:45 +08:00
2018-12-26 23:58:24 +08:00
// 修改歌曲进度
canvas.addEventListener(
'click',
ev => {
if (!this.curr.id) {
return
}
2018-12-26 23:58:24 +08:00
let rect = canvas.getBoundingClientRect()
let aw = rect.width
let ax = ev.pageX - rect.left
let ay = ev.pageY - rect.top
if (ax < 80) {
2018-12-29 20:00:19 +08:00
this.ktvMode = this.ktvMode ^ 1
return
}
2018-12-26 23:58:24 +08:00
if (ax > 124 && ay > 55 && ay < 64) {
let pp = (ax - 124) / (aw - 124)
this.curr.time = pp * this.curr.duration
SONIST.seek(this.curr.time)
2019-01-04 19:28:03 +08:00
LYRICS.seek(this.curr.time)
if (!this.isPlaying) {
this.draw()
}
2018-12-26 23:58:24 +08:00
}
},
2018-12-30 00:58:04 +08:00
false
2018-12-26 23:58:24 +08:00
)
// 设置循环模式
SONIST.mode = PLAY_MODE[this.playMode]
2018-12-29 02:17:46 +08:00
SONIST.volume = this.volume
2018-12-26 23:58:24 +08:00
SONIST.on('play', time => {
this.curr.time = time
2019-01-04 19:28:03 +08:00
LYRICS.update(time)
2018-12-26 23:58:24 +08:00
})
SONIST.on('end', time => {
this.nextSong(1)
})
2019-01-04 19:28:03 +08:00
// 控制条的单行歌词
LYRICS.on('ctrl-lrc', lrc => {
this.ctrlLrc = lrc
})
// ktv模式的歌词
LYRICS.on('ktv-lrc', lrc => {
this.lrc = lrc
})
2018-12-26 23:58:24 +08:00
this.activeModule(this.mod)
2018-12-28 18:47:45 +08:00
remote.app.on('browser-window-focus', _ => {
this.winFocus = true
})
remote.app.on('browser-window-blur', _ => {
this.winFocus = false
})
2018-12-26 23:58:24 +08:00
},
methods: {
2018-12-28 18:47:45 +08:00
quit(force) {
if (force) {
remote.app.exit()
} else {
if (appInit.allowPlayOnBack) {
WIN.hide()
} else {
remote.app.exit()
}
}
},
2018-12-26 23:58:24 +08:00
minimize() {},
maximize() {},
activeModule(mod) {
switch (mod) {
case 'artist':
Artist.__init__()
break
case 'local':
Local.__init__()
break
2018-12-27 03:02:37 +08:00
case 'profile':
Profile.__init__()
break
2018-12-26 23:58:24 +08:00
default:
break
}
},
2018-12-28 18:47:45 +08:00
toggleOptBox() {
this.optBoxShow = !this.optBoxShow
},
2018-12-26 23:58:24 +08:00
toggleModule(mod) {
2018-12-29 02:14:00 +08:00
if ('mv' === mod) {
2018-12-26 23:58:24 +08:00
return
}
2018-12-28 18:47:45 +08:00
this.optBoxShow = false
2018-12-26 23:58:24 +08:00
this.mod = mod
},
togglePlayMode() {
let mod = this.playMode
mod++
if (mod > 2) {
mod = 0
}
this.playMode = mod
SONIST.mode = PLAY_MODE[mod]
Anot.ls('play-mode', mod)
},
2018-12-28 22:19:29 +08:00
// 修改音量
changeValume(ev) {
2019-01-04 16:24:42 +08:00
let volume = 575 - ev.pageY
2018-12-28 22:19:29 +08:00
if (volume < 0) {
volume = 0
}
if (volume > 100) {
volume = 100
}
this.volume = volume
SONIST.volume = volume
Anot.ls('volume', volume)
},
2018-12-30 02:26:07 +08:00
__draw__() {
2018-12-26 23:58:24 +08:00
let play = this.isPlaying
let rx = (play ? 112 : 40) + this.__HEIGHT__ / 2 // 旋转唱片的圆心坐标X
let ry = this.__HEIGHT__ / 2 // 旋转唱片的圆心坐标Y
let pw = this.__WIDTH__ - this.__HEIGHT__ - 180 // 进度条总长度
let wl = this.__HEIGHT__ + 180 // 文字的坐标X
2018-12-29 20:00:19 +08:00
2018-12-30 02:26:07 +08:00
let { time, duration, title, artist } = this.curr
2019-01-04 19:28:03 +08:00
let lrc = this.ctrlLrc
2018-12-30 02:26:07 +08:00
let pp = time / duration // 进度百分比
time = Anot.filters.time(time)
duration = Anot.filters.time(duration)
this.__CTX__.clearRect(0, 0, this.__WIDTH__, this.__HEIGHT__)
this.__CTX__.save()
// 将原点移到唱片圆心, 旋转完再回到初始值
this.__CTX__.translate(rx, ry)
this.__CTX__.rotate(this.__DEG__ * Math.PI)
this.__CTX__.translate(-rx, -ry)
this.__CTX__.drawImage(
this.__img1__,
play ? 112 : 40,
0,
this.__HEIGHT__,
this.__HEIGHT__
)
this.__CTX__.restore()
this.__CTX__.drawImage(
this.__img2__,
0,
0,
this.__HEIGHT__,
this.__HEIGHT__
)
// 歌曲标题和歌手
this.__CTX__.fillStyle = COLORS[this.ktvMode].title
this.__CTX__.font = '56px' + FONTS_NAME
this.__CTX__.fillText(`${title} - ${artist}`, wl, 100)
// 时间
this.__CTX__.fillStyle = COLORS[this.ktvMode].lrc
this.__CTX__.font = '48px' + FONTS_NAME
this.__CTX__.fillText(`${time} / ${duration}`, this.__WIDTH__ - 280, 100)
// 歌词
this.__CTX__.fillStyle = COLORS[this.ktvMode].lrc
this.__CTX__.font = '48px' + FONTS_NAME
2019-01-04 19:28:03 +08:00
this.__CTX__.fillText(lrc, wl, 180)
2018-12-30 02:26:07 +08:00
// 进度条
this.__CTX__.fillStyle = COLORS[this.ktvMode].bar1
this.__CTX__.fillRect(wl, 230, pw, 16)
this.__CTX__.fillStyle = COLORS[this.ktvMode].bar2
this.__CTX__.fillRect(wl, 230, pw * pp, 16)
this.__DEG__ += 0.01
},
2018-12-26 23:58:24 +08:00
2018-12-30 02:26:07 +08:00
draw(force) {
if (force) {
this.__img1__ = new Image()
this.__img2__ = new Image()
let p1 = Promise.defer()
let p2 = Promise.defer()
this.__img1__.onload = p1.resolve
this.__img2__.onload = p2.resolve
this.__img1__.src = '/images/disk.png'
this.__img2__.src = this.curr.cover || '/images/album.png'
Promise.all([p1.promise, p2.promise]).then(_ => {
clearInterval(this.timer)
this.__DEG__ = 0.01
if (this.isPlaying) {
this.timer = setInterval(_ => {
this.__draw__()
}, 20)
} else {
this.__draw__()
}
})
} else {
2018-12-26 23:58:24 +08:00
clearInterval(this.timer)
2018-12-30 02:26:07 +08:00
if (this.isPlaying) {
this.timer = setInterval(_ => {
this.__draw__()
2018-12-26 23:58:24 +08:00
}, 20)
} else {
2018-12-30 02:26:07 +08:00
this.__draw__()
2018-12-26 23:58:24 +08:00
}
2018-12-30 02:26:07 +08:00
}
2018-12-26 23:58:24 +08:00
},
nextSong(step) {
let _p = null
if (step > 0) {
_p = SONIST.next()
} else {
_p = SONIST.prev()
}
this.isPlaying = false
_p.then(it => {
2018-12-28 22:19:29 +08:00
if (this.mod === 'local') {
Local.__updateSong__(it)
2018-12-26 23:58:24 +08:00
}
// 通知子模块歌曲已经改变
this.$fire('child!curr', it.id)
2018-12-28 22:19:29 +08:00
this.play(it)
2018-12-26 23:58:24 +08:00
})
},
pause() {
this.isPlaying = false
},
2018-12-28 22:19:29 +08:00
updateCurr(obj) {
let old = this.curr.$model
this.curr = Object.assign(old, obj)
},
2018-12-26 23:58:24 +08:00
play(song) {
// 有参数的,说明是播放回调通知
// 此时仅更新播放控制条的信息即可
if (song) {
2018-12-28 22:19:29 +08:00
song.time = 0
2019-01-04 19:28:03 +08:00
this.ctrlLrc = '暂无歌词...'
2018-12-28 22:19:29 +08:00
this.updateCurr(song)
2018-12-26 23:58:24 +08:00
this.isPlaying = true
2018-12-30 02:26:07 +08:00
this.draw(true)
2019-01-04 19:28:03 +08:00
LYRICS.__init__(song.lyrics)
2018-12-26 23:58:24 +08:00
} else {
if (SONIST.stat === 'ready') {
2018-12-30 02:26:07 +08:00
let played = this.isPlaying
this.isPlaying = !this.isPlaying
2018-12-29 20:43:49 +08:00
if (this.curr.id) {
2018-12-30 02:26:07 +08:00
if (played) {
2018-12-29 20:43:49 +08:00
SONIST.pause()
} else {
SONIST.play()
}
2018-12-30 02:26:07 +08:00
this.draw()
2018-12-26 23:58:24 +08:00
} else {
2018-12-29 20:43:49 +08:00
let lastPlay = Anot.ls('last-play') || 0
SONIST.play(lastPlay).then(it => {
it.time = 0
2019-01-04 19:28:03 +08:00
this.ctrlLrc = '暂无歌词...'
2018-12-29 20:43:49 +08:00
this.updateCurr(it)
2018-12-30 02:26:07 +08:00
this.draw(true)
2018-12-30 00:58:07 +08:00
// this.ktvMode = 1
2019-01-04 19:28:03 +08:00
LYRICS.__init__(it.lyrics)
2018-12-29 20:43:49 +08:00
})
2018-12-26 23:58:24 +08:00
}
}
}
}
}
})