• 获取并封装歌手歌曲的数据


    1.在api/song.js中发送jsonp请求来请求数据

     1 import {commonParams} from './config'
     2 import axios from 'axios'
     3 
     4 export function getLyric(mid) {
     5   const url = '/api/lyric'
     6 
     7   const data = Object.assign({}, commonParams, {
     8     songmid: mid,
     9     platform: 'yqq',
    10     hostUin: 0,
    11     needNewCode: 0,
    12     categoryId: 10000000,
    13     pcachetime: +new Date(),
    14     format: 'json'
    15   })
    16 
    17   return axios.get(url, {
    18     params: data
    19   }).then((res) => {
    20     return Promise.resolve(res.data)
    21   })
    22 }

    2.封装api/common/js/song.js的song类,来获取想要的信息

     1 // 格式化处理歌星的歌曲数据,将我们想要的数据封装成一个类,方便进行调用
     2 export default class Song {
     3   constructor({id, mid, singer, name, album, duration, image, url}) {
     4     this.id = id
     5     this.mid = mid
     6     this.singer = singer
     7     this.name = name
     8     this.album = album
     9     this.duration = duration
    10     this.image = image
    11     this.url = url
    12   }
    13 }
    14 
    15 
    16 export function createSong(musicData){//创建一个歌曲的实例,得到的是一个歌星的歌曲信息
    17   return new Song({
    18     id: musicData.songid,
    19     mid: musicData.songmid,
    20     singer: filterSinger(musicData.singer),
    21     name: musicData.songname,
    22     album: musicData.albumname,
    23     duration: musicData.interval,
    24     image: `https://y.gtimg.cn/music/photo_new/T002R300x300M000${musicData.albummid}.jpg?max_age=2592000`,
    25     url: `http://ws.stream.qqmusic.qq.com/${musicData.songid}.m4a?fromtag=46`
    26   })
    27 }
    28 
    29 // 只有当点击歌星singer.vue页面才会跳转到歌曲页面,获得歌曲的信息,因此当没有歌星信息的时候,就让其跳转到singer.vue页面
    30 function filterSinger(singer) {
    31   let ret = []
    32   if (!singer) {
    33     return ''
    34   }
    35   singer.forEach((s) => {
    36     ret.push(s.name)
    37   })
    38   return ret.join('/')
    39 }

    3.在sing-detail.vue中请求数据;

     1    _getDetail() {
     2         if (!this.singer.id) {
     3           this.$router.push('/singer')
     4           return
     5         }
     6         getSingerDetail(this.singer.id).then((res) => {
     7           if (res.code === ERR_OK) {
     8             console.log(res)
     9             this.songs = this._normalizeSongs(res.data.list)
    10             console.log(this.songs)
    11           }
    12         })
    13       },

    4.引入封装的song类,并处理请求到的数据:

     1   _normalizeSongs(list){
     2         let ret= []
     3         list.forEach((item)=>{//这里遍历的是createSong里面的值,songmid和albumid是必传的;item就是遍历的value
     4           let {musicData} = item
     5           if(musicData.songmid && musicData.albumid){
     6             ret.push(createSong(musicData))//这里调用createSong,就不用每次都写一大堆;重复的代码;
     7           }
     8         })
     9         return ret;
    10       }



    import {createSong} from '@/common/js/song'//注意:这里调用需要用对象的方式
  • 相关阅读:
    [UWP]如何使用Fluent Design System (下)
    [UWP]如何使用Fluent Design System (上)
    [UWP]了解IValueConverter
    [UWP]了解TypeConverter
    [UWP]本地化入门
    [WPF]本地化入门
    [UWP]创建一个进度按钮
    [UWP]分享一个基于HSV色轮的调色板应用
    [UWP]使用Writeable​Bitmap创建HSV色轮
    [UWP]理解及扩展Expander
  • 原文地址:https://www.cnblogs.com/yangguoe/p/9460497.html
Copyright © 2020-2023  润新知