actions.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import {
  2. clearHistoryList,
  3. setHistoryList,
  4. removeHistoryList,
  5. setMode,
  6. setUserId
  7. } from '@/utils/storage'
  8. import * as types from './mutation-types'
  9. function findIndex(list, music) {
  10. return list.findIndex(item => {
  11. return item.id === music.id
  12. })
  13. }
  14. export default {
  15. // 设置播放列表
  16. setPlaylist: function ({ commit }, { list }) {
  17. commit(types.SET_PLAYLIST, list)
  18. commit(types.SET_ORDERLIST, list)
  19. },
  20. // 选择播放(会更新整个播放列表)
  21. selectPlay: function ({ commit }, { list, index }) {
  22. commit(types.SET_PLAYLIST, list)
  23. commit(types.SET_ORDERLIST, list)
  24. commit(types.SET_CURRENTINDEX, index)
  25. commit(types.SET_PLAYING, true)
  26. },
  27. // 选择播放(会插入一条到播放列表)
  28. selectAddPlay: function ({ commit, state }, music) {
  29. let list = [...state.playlist]
  30. // 查询当前播放列表是否有代插入的音乐,并返回其索引值
  31. let index = findIndex(list, music)
  32. // 当前播放列表有待插入的音乐时,直接改变当前播放音乐的索引值
  33. if (index > -1) {
  34. commit(types.SET_CURRENTINDEX, index)
  35. } else {
  36. list.unshift(music)
  37. commit(types.SET_PLAYLIST, list)
  38. commit(types.SET_ORDERLIST, list)
  39. commit(types.SET_CURRENTINDEX, 0)
  40. }
  41. commit(types.SET_PLAYING, true)
  42. },
  43. // 清空播放列表
  44. clearPlayList: function ({ commit }) {
  45. commit(types.SET_PLAYING, false)
  46. commit(types.SET_CURRENTINDEX, -1)
  47. commit(types.SET_PLAYLIST, [])
  48. commit(types.SET_ORDERLIST, [])
  49. },
  50. // 删除正在播放列表中的歌曲
  51. removerPlayListItem: function (
  52. { commit, state },
  53. { list, index }
  54. ) {
  55. let currentIndex = state.currentIndex
  56. if (index < state.currentIndex || list.length === state.currentIndex) {
  57. currentIndex--
  58. commit(types.SET_CURRENTINDEX, currentIndex)
  59. }
  60. commit(types.SET_PLAYLIST, list)
  61. commit(types.SET_ORDERLIST, list)
  62. if (!list.length) {
  63. commit(types.SET_PLAYING, false)
  64. } else {
  65. commit(types.SET_PLAYING, true)
  66. }
  67. },
  68. // 设置播放历史
  69. setHistory: function ({ commit }, music) {
  70. commit(types.SET_HISTORYLIST, setHistoryList(music))
  71. },
  72. // 删除播放历史
  73. removeHistory: function ({ commit }, music) {
  74. commit(types.SET_HISTORYLIST, removeHistoryList(music))
  75. },
  76. // 清空播放历史
  77. clearHistory: function ({ commit }) {
  78. commit(types.SET_HISTORYLIST, clearHistoryList())
  79. },
  80. // 设置播放模式
  81. setPlayMode: function ({ commit }, mode) {
  82. commit(types.SET_PLAYMODE, setMode(mode))
  83. },
  84. // 设置网易云用户UID
  85. setUid: function ({ commit }, uid) {
  86. commit(types.SET_UID, setUserId(uid))
  87. }
  88. }