美文网首页
Vue2.x 通过后端接口代理方法获取PC端推荐歌单

Vue2.x 通过后端接口代理方法获取PC端推荐歌单

作者: Jay_Chen | 来源:发表于2018-05-07 14:46 被阅读536次
    在QQ音乐api接口有时候不能直接通过Jsonp访问,可通过后端代理获取,这里是通过axios获取

    这里是: axios GitHub地址,具体可访问学习,本文仅记录Vue2.x 通过后端接口代理方法获取PC端推荐歌单。

    1. npm install axios -S
    2. build/webpack.dev.conf.js文件
    // 开始引入 axios
    const axios = require('axios') 
    // devServer 里添加
      before(app) {
          app.get('/getDiscList', function (req, res) {
            var url = 'https://c.y.qq.com/splcloud/fcgi-bin/fcg_get_diss_by_tag.fcg'
            axios.get(url, {
              headers: {
                referer: 'https://y.qq.com/'
              },
              params: req.query
            }).then((response) => {
              res.json(response.data)
            }).catch((e) => {
              console.log(e)
            })
          })
        }
    
    3. 在api里的js文件里:将方法里的url替换成步骤2里自定义的接口,即 '/getDiscList',再通过axios获取返回的数据。
    // 文件路径:api/recommend
    export function getDiscList() {
      const url = '/getDiscList'
      const data = Object.assign({}, commonParams, {
        platform: 'yqq',
        picmid: 1,
        hostUin: 0,
        sin: 0,
        ein: 29,
        sortId: 5,
        needNewCode: 0,
        categoryId: 10000000,
        rnd: Math.random(),
        format: 'json'
      })
    
      return axios.get(url, {
        params: data
      }).then((res) => {
        return Promise.resolve(res.data)
      })
    }
    
    4. 组件里的调用
     import {getDiscList} from 'api/recommend'
    
     export default {
       data() {
          return {
            discList: []
          }
        },
        created() {
          this._getDiscList()
        },
        methods: {
          _getDiscList() {
             getDiscList().then((res) => {
              if (res.code === ERR_OK) {
               //  console.log(res.data.list)
                this.discList = res.data.list
              }
            })
          }      
        }
      }
    

    相关文章

      网友评论

          本文标题:Vue2.x 通过后端接口代理方法获取PC端推荐歌单

          本文链接:https://www.haomeiwen.com/subject/mogmrftx.html