通过QQ音乐接口,做的一个简易微信小程序播放器

刚接触微信小程序开发,自己闲来无事做的一个音乐播放器。大概介绍一下其中使用的哪些东西。由于QQ音乐要求请求头中必须包含Referer这个设置(Referer:代表你是从哪个页面发送的请求)才能正确的响应数据,然而微信小程序的网路请求wx.request规定不能在请求头中设置Referer这里写图片描述,所以导致不能直接从小程序中去调用QQ音乐的接口。为了解决这个问题,开始我想采用java来做一个后台代理(就是给请求加上请求头),但是考虑到一个原因,java的服务占用内存比较大相对来说要重量一点。通过了解采用node.js来做这个功能再合适不过。于是就采用了node.js,搭建服务快,省去了java服务需要的很多配置。
node代码:

var express = require('express');
var request = require("request");
var app = express();

//获取推荐歌单列表
app.get('/haicome/getQMusicSongSheet', function (req, res) {
    const url = "https://c.y.qq.com/splcloud/fcgi-bin/fcg_get_diss_by_tag.fcg?" +
        "picmid=1&rnd=0.9948304561071419&g_tk=5381&jsonpCallback=getPlaylist&loginUin=0" +
        "&hostUin=0&format=jsonp&inCharset=utf8&outCharset=utf-8&notice=0&platform=yqq&" +
        "needNewCode=0&categoryId=10000000&sortId=2&sin=0&ein=29"
    var options = {
        url: url,
        headers: {
            referer: 'https://y.qq.com/portal/playlist.html'
        }
    };
    request(options, function (error, response, body) {
        var jsonResult = body.substring(body.indexOf("(")+1,body.length-1);
        res.send(jsonResult);
    })
});

//获取要播放的歌单的详情
app.get('/haicome/getQMusicSongSheetDetail', function (req, res) {
    const dissid = req.query.dissid;
    const url = "https://c.y.qq.com/qzone/fcg-bin/fcg_ucc_getcdinfo_byids_cp.fcg?" +
        "type=1&json=1&utf8=1&onlysong=0&disstid="+dissid +
        "&format=jsonp&g_tk=5381&jsonpCallback=playlistinfoCallback&loginUin=0&hostUin=0&" +
        "format=jsonp&inCharset=utf8&outCharset=utf-8&notice=0&platform=yqq&needNewCode=0"
    var options = {
        url: url,
        headers: {
            referer: 'https://y.qq.com/portal/playlist.html'
        }
    };
    request(options, function (error, response, body) {
        var jsonResult = body.substring(body.indexOf("(")+1,body.length-1);
        res.send(jsonResult);
    })
});

//获取指定歌曲信息
app.get('/haicome/getQMusicSongByAlbummid', function (req, res) {
    const albummid = req.query.albummid;
    const url = "https://c.y.qq.com/v8/fcg-bin/fcg_v8_album_info_cp.fcg?" +
        "albummid="+albummid +
        "&g_tk=5381&jsonpCallback=albuminfoCallback&loginUin=0&hostUin=0&" +
        "format=jsonp&inCharset=utf8&outCharset=utf-8&notice=0&platform=yqq&needNewCode=0"
    var options = {
        url: url,
        headers: {
            referer: 'https://y.qq.com/portal/playlist.html'
        }
    };
    request(options, function (error, response, body) {
        var jsonResult = body.substring(body.indexOf("(")+1,body.length-1);
        res.send(jsonResult);
    })
});

//获取指定歌曲vkey  这个vkey在获取资源的时候需要使用
app.get('/haicome/getQMusicSongVkeyById', function (req, res) {
    const id = req.query.id;
    const url = "https://c.y.qq.com/base/fcgi-bin/fcg_music_express_mobile3.fcg?" +
        "g_tk=5381&jsonpCallback=MusicJsonCallback8307607419317575&loginUin=0&hostUin=0&" +
        "format=json&inCharset=utf8&outCharset=utf-8&notice=0&platform=yqq&needNewCode=0&" +
        "cid=205361747&callback=MusicJsonCallback8307607419317575&uin=0&songmid="+id+"&filename=C400"+id+".m4a&guid=890047478"
    var options = {
        url: url,
        headers: {
            referer: 'https://y.qq.com/portal/playlist.html'
        }
    };
    request(options, function (error, response, body) {
        const result = JSON.stringify(body);
        const key = result.substring(result.indexOf("vkey")+9,result.length-8) ;
        res.send(key);
    })
});

//查询歌单
app.get('/haicome/getQMusicSongSearchList', function (req, res) {
    var search = req.query.search;
    search = encodeURIComponent(search);
    console.log(search);
    const url = "https://c.y.qq.com/soso/fcgi-bin/client_music_search_songlist?" +
        "remoteplace=txt.yqq.playlist&searchid=111594735142470005&flag_qc=0&page_no=0&num_per_page=50" +
        "&query="+search +
        "&g_tk=5381&jsonpCallback=MusicJsonCallback321112164733657&loginUin=0&hostUin=0&format=jsonp" +
        "&inCharset=utf8&outCharset=utf-8&notice=0&platform=yqq&needNewCode=0"
    console.log(url);
    var options = {
        url: url,
        headers: {
            referer: 'https://y.qq.com/portal/playlist.html',
            contentType:'application/x-javascript;charset=utf-8',
            vary:'Accept-Encoding'
        }
    };
    request(options, function (error, response, body) {
        var jsonResult = body.substring(body.indexOf("(")+1,body.length-1);
        console.log(jsonResult);
        res.send(jsonResult);
    })
});

app.listen(8088, function () {
    console.log('Example app listening on port 8088!');
});

微信小程序代码请移步码云下载:https://gitee.com/haicome/wechat_applet_play

  • 3
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值