vue 音乐配置

286 阅读2分钟

ajax.js

import axios from "axios";
import qs from "qs";
import store from "../store/" // 引入vuex

// axios.defaults.baseURL = '/api';
// 是否添加请求头 // 全局请求头添加
// axios.defaults.headers.common['Authorization'] = '1234';
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';


const loadingQueue = {}
    // 请求发送前数据拦截拦截器   => 全局请求拦截
axios.interceptors.request.use(function(config) {
    /* TODOS使用store进行loading添加 */
    // store.state.isShow = true
    config.data = qs.stringify(config.data)
        // console.log('loading开始');
    if (Object.keys(loadingQueue).length == 0) {
        store.state.isShowLoading = true
    } // 判读是否让他显示,数据没请求完成就让他为true 让正在加载图片显示

    loadingQueue[config.url] = true // 给loadingQueue添加一个属性告诉浏览器此时此刻我的对象不为空,ajax请求数据已完成 ,config.url是请求数据的url
    return config;
}, function(error) {
    return Promise.reject(error);
})

// 请求成功数据拦截器      =>   全局请求拦截
axios.interceptors.response.use(function(response) {
    /* TODOS 使用store进行loading方法解除 */
    // console.log('loading结束');  
    delete loadingQueue[response.config.url] // 请求完成以后删除这个属性,方便做下面判断
    if (Object.keys(loadingQueue).length == 0) {
        store.state.isShowLoading = false
    } // 判读是否让他显示,数据请求成功了,让正在加载图片消失
    return response.data;
}, function(error) {
    return Promise.reject(error);
})


export default function({ params = null, url = '', type = 'get' }) {
    let data = (type == 'get' || type == 'delete') ? { params } : params;

    return new Promise((resolve, reject) => {
        axios[type](url, data).then(resolve).catch(reject)
    })
}

index.js 音乐配置

import ajax from "./ajax";

export const getRecommendSwiper = () => ajax({
    url: "/api/musichall/fcgi-bin/fcg_yqqhomepagerecommend.fcg"
}) // 轮播图数据

export const getDiscList = () => ajax({
    url: '/api/getDiscList',
    params: {
        platform: 'yqq',
        hostUin: 0,
        sin: 0,
        ein: 29,
        sortId: 5,
        needNewCode: 0,
        categoryId: 10000000,
        rnd: Math.random(),
        format: 'json',
        notice: 0,
        outCharset: "utf-8",
        inCharset: "utf-8",
        g_tk: "1928093487"
    }
}) // 全部歌单数据


export const getSingerList = () => ajax({
    url: "/api/v8/fcg-bin/v8.fcg",
    params: {
        channel: 'singer',
        page: 'list',
        key: 'all_all_all',
        pagesize: 100,
        pagenum: 1,
        hostUin: 0,
        needNewCode: 0,
        platform: 'yqq',
        g_tk: 1928093487,
        notice: 0,
        format: "jsonp",
        inCharset: "utf-8",
        outCharset: "utf-8"
    }

}) //全部歌手数据

export const getSingerDetail = () => ajax({
    url: "/api/v8/fcg-bin/fcg_v8_singer_track_cp.fcg",
    params: {
        inCharset: "utf-8",
        outCharset: "utf-8",
        notice: 0,
        pagesize: 100,
        pagenum: 1,
        hostUin: 0,
        needNewCode: 0,
        platform: 'yqq',
        g_tk: 1928093487,
        notice: 0,
        format: "jsonp",
        order: "listen",
        begin: 0,
        num: 80,
        songstatus: 1,
        singermid: "003Nz2So3XXYek"
    }
})

vue.cogfig.js

const path = require("path");
const axios = require("axios")
module.exports = {
    css: {
        loaderOptions: {
            stylus: {
                'resolve url': true,
                'import': [
                    './src/theme',
                    "./src/common/stylus/mixin.styl",
                    "./src/common/stylus/variable.styl"
                ]
            }
        }
    },
    pluginOptions: {
        'cube-ui': {
            postCompile: true,
            theme: true
        }
    },
    configureWebpack: {
        resolve: {
            alias: {
                '@': path.join(__dirname, 'src'),
                'components': path.join(__dirname, 'src/components'),
                'common': path.join(__dirname, 'src/common'),
                "views": path.join(__dirname, "src/views"),
                "api": path.join(__dirname, "src/api"),
                "base": path.join(__dirname, "src/base")
            }
        }
    }, // 配置绝对路径
    devServer: { //跨域配置
        proxy: {
            '/api': {
                target: "https://c.y.qq.com/",
                ws: true,
                changOrigin: true,
                pathRewrite: {
                    '^/api': ''
                }
            }

        },
        before(app) {
            app.get('/api/getDiscList', (req, res) => {
                var url = 'https://c.y.qq.com/splcloud/fcgi-bin/fcg_get_diss_by_tag.fcg'
                axios.get(url, {
                    headers: {
                        referer: 'https://c.y.qq.com/',
                        host: 'c.y.qq.com'
                    },
                    params: req.query
                }).then((response) => {
                    res.json(response.data)
                }).catch((e) => {
                    // console.log(e)
                })
            })

        } //配置全部歌单代理

    }

}