专辑的页面与我们刚刚完成的排行列表很相似,除了部分文字的变化外就是多了简介这一部分。
网络请求的函数为:
-
function getAlbumInfo(id,callback){
-
var data = {
-
albummid: albummid,
-
g_tk: 5381,
-
uin: 0,
-
format: 'json',
-
inCharset: 'utf-8',
-
outCharset: 'utf-8',
-
notice: 0,
-
platform: 'h5',
-
needNewCode: 1,
-
_: Date.now()
-
};
-
wx.request({
-
url: 'http://c.y.qq.com/v8/fcg-bin/fcg_v8_album_info_cp.fcg',
-
data: data,
-
header: {
-
'Content-Type': 'application/json'
-
},
-
success: function (res) {
-
console.log(res);
-
if (res.statusCode == 200) {
-
callback(res.data);
-
} else {
-
-
}
-
}
-
});
-
}
-
-
module.exports = {
-
...
-
getAlbumInfo:getAlbumInfo
-
}
复制代码
页面的布局代码为:
-
<view class="list-top">
-
<view class="top-info">
-
<view class="top-info-inner">
-
<view class="top-info-text">
-
<view class="top-info-title">{{albumInfo.name}}</view>
-
<view class="top-info-base">
-
<text>{{albumInfo.singername}}</text>
-
<text style="margin-left: 5px;">{{albumInfo.aDate}}</text>
-
<text style="margin-left:10px;">{{albumInfo.genre}}</text>
-
</view>
-
</view>
-
<view class="top-play"></view>
-
</view>
-
</view>
-
<image class="top-img" mode="aspectFit" src="{{coverImg}}"></image>
-
<view class="top-back"></view>
-
</view>
-
<view class="song-list" style="background:{{listBgColor}}">
-
<view class="song-item" wx:for="{{albumInfo.list}}" data-data="{{item.data}}" data-mid="{{item.songmid}}">
-
<text class="song-index">{{index+1}}</text>
-
<view class="song-item-title">{{item.songname}}</view>
-
<view class="song-item-text">
-
<block wx:for="{{item.singer}}">
-
<block wx:if="{{index!=0}}">|</block>
-
{{item.name}}
-
</block>
-
</view>
-
</view>
-
</view>
-
<view class="desc" style="background:{{listBgColor}}">
-
<view class="desc-title">简介</view>
-
<text>{{albumInfo.desc}}</text>
-
</view>
复制代码
简介部分的格式文件:
-
.desc {
-
box-sizing: border-box;
-
font-size: 14px;
-
padding: 40px 10px;
-
color: #fff;
-
line-height: 20px;
-
}
-
-
.desc-title {
-
text-align: center;
-
width: 100%;
-
font-size: 16px;
-
margin-bottom: 20px;
-
}
复制代码
加载数据的代码为:
-
var MusicService = require('../../services/music');
-
var app = getApp()
-
-
Page({
-
data: {
-
albumInfo: {},
-
coverImg: '',
-
},
-
onl oad: function (options) {
-
// 页面初始化 options为页面跳转所带来的参数
-
var mid = app.globalData.zhidaAlbummid;
-
MusicService.getAlbumInfo(mid, this.setPageData)
-
},
-
setPageData: function (data) {
-
if (data.code == 0) {
-
var albummid = data.data.mid;
-
var img = 'http://y.gtimg.cn/music/photo/mid_album_500/' + albummid.slice(-2, -1) + '/' + albummid.slice(-1) + '/' + albummid + '.jpg'
-
this.setData({albumInfo: data.data, coverImg: img});
-
}
-
},
-
})
复制代码
这里的点击事件与前文相同,就不再重复了。
另外,我们在首页里未完成的两个点击事件,现在也可以完成了。先看电台的点击事件,这个事件与我们刚刚完成的一样,具体代码为:
-
radioTap: function (e) {
-
var dataSet = e.currentTarget.dataset;
-
MusicService.getRadioMusicList(dataSet.id, function (data) {
-
wx.navigateTo({
-
url: '../play/play'
-
});
-
if (data.code == 0) {
-
var list = [];
-
var dataList = data.data;
-
for (var i = 0; i < dataList.length; i++) {
-
var song = {};
-
var item = dataList[i];
-
song.id = item.id;
-
song.mid = item.mid;
-
song.name = item.name;
-
song.title = item.title;
-
song.subTitle = item.subtitle;
-
song.singer = item.singer;
-
song.album = item.album
-
song.img = 'http://y.gtimg.cn/music/photo_new/T002R150x150M000' + item.album.mid + '.jpg?max_age=2592000'
-
list.push(song);
-
}
-
app.setGlobalData({
-
playList: list,
-
playIndex: 0
-
});
-
}
-
});
-
},
复制代码
这里面getRadioMusicList为网络请求,具体代码为:
-
function getRadioMusicList(id,callback){
-
var data = {
-
labelid: id,
-
g_tk: 5381,
-
uin: 0,
-
format: 'json',
-
inCharset: 'utf-8',
-
outCharset: 'utf-8',
-
notice: 0,
-
&nnbsp; platform: 'h5',
-
needNewCode: 1,
-
_: Date.now(),
-
}
-
wx.request({
-
url: 'https://c.y.qq.com/v8/fcg-bin/fcg_v8_radiosonglist.fcg',
-
data: data,
-
header: {
-
'Content-Type': 'application/json'
-
},
-
success: function (res) {
-
if (res.statusCode == 200) {
-
callback(res.data);
-
} else {
-
-
}
-
-
}
-
});
-
}
-
-
module.exports = {
-
...
-
getRadioMusicList:getRadioMusicList
-
}
复制代码
另一部分为搜索结果里歌曲的点击事件
-
musuicPlay: function (e) {
-
var dataSet = e.currentTarget.dataset;
-
var playingSongs = app.globalData.playList;
-
if (typeof dataSet.index !== 'undefined') {
-
var index = dataSet.index;
-
var item = this.data.searchSongs[index];
-
var song = {};
-
var album = {};
-
album.mid = item.albummid
-
album.id = item.albumid
-
album.name = item.albumname;
-
album.desc = item.albumdesc
-
-
song.id = item.songid;
-
song.mid = item.songmid;
-
song.name = item.songname;
-
song.title = item.songorig;
-
song.subTitle = '';
-
song.singer = item.singer;
-
song.album = album;
-
song.time_public = item.time_public;
-
song.img = 'http://y.gtimg.cn/music/photo_new/T002R150x150M000' + album.mid + '.jpg?max_age=2592000'
-
this.addPlayingSongs(song);
-
}
-
},
复制代码
前面的内容与我们写过的一样,最后我们没有直接更新全局变量而是调用了一个新方法,因为前文所有的点击事件都更新了整个播放列表,而我们点击某一首歌曲时,我们希望添加这首歌到已有的列表中,而不是先清空它。
-
addPlayingSongs: function (song) {
-
var playingSongs = app.globalData.playList; //获取当前的播放列表
-
var index = -1;
-
if (typeof playingSongs === 'undefined') { //判断列表是否为空
-
playingSongs = [];
-
playingSongs.push(song);
-
app.setGlobalData({ //如果是空的话,直接更新全局变量
-
playList: playingSongs,
-
playIndex: 0
-
});
-
} else { //不为空的话我们先判断当前列表是否包含选定歌曲
-
for (var i = 0; i < playingSongs.length; i++) { //遍历整个列表
-
var item = playingSongs[i];
-
if (item.mid == song.mid) { //如果发现有mid相同的(即同一首歌)
-
index = i; //获取这首歌在列表里的序号
-
break;
-
}
-
}
-
if (index != -1) { //歌曲已存在
-
app.setGlobalData({
-
playIndex: index //用我们获取的序号更新当前播放序号
-
});
-
} else { //不存在的情况
-
playingSongs.push(song);
-
index = playingSongs.length - 1; //将歌曲加入播放列表,播放序号改为列表最后一项
-
app.setGlobalData({
-
playList: playingSongs,
-
playIndex: index
-
});
-
}
-
}
-
wx.navigateTo({
-
url: '../play/play'
-
});
-
},
复制代码
上一节:微信小程序小白项目开发案例之音乐播放器-页面渲染
下一节:微信小程序小白项目开发案例之音乐播放器—音乐播放页