微信小程序怎么实现音乐排行榜
导读:本文共7998.5字符,通常情况下阅读需要27分钟。同时您也可以点击右侧朗读,来听本文内容。按键盘←(左) →(右) 方向键可以翻页。
摘要: 排行页我们先在services/music.js里添加网络请求函数:function getTopMusic(callback){ var data = { format: 'json', g_tk: 5381, uin: 0, inCharset: 'utf-8', outChars... ...
目录
(为您整理了一些要点),点击可以直达。- (2): var data = {
- (3): format: 'jso
- (4): g_tk: 5381,
- (5): uin: 0,
- (6): inCharset: '
- (7): outCharset: '
- (8): notice: 0,
- (9): platform: 'h
- (10): needNewCode: 1,
- (11): _: Date.now()
- (12): };
- (13): wx.request({
- (15): data: data,
- (16): header: {
- (18): },
- (19): success: functio
- (20): if (res.status
- (21): callback(res.d
- (22): } else {
- (23): }
- (24): }
- (25): });
- (34): "code":
- (35): "subcode"
- (36): "message"
- (37): "default"
- (38): "data":
- (39): "topList"
- (40): {
- (41): "id"
- (44): "songList
- (45): {
- (48): },
- (49): {
- (52): },
- (53): {
- (56): }
- (57): ],
- (59): "type&quo
- (60): },
- (61): {
- (62): "id"
- (65): "songList
- (66): {
- (69): },
- (70): {
- (73): },
- (74): {
- (77): }
- (78): ],
- (80): "type&quo
- (81): },
- (82): ...
- (83): ]
- (84): }
- (89)://引用网络请求文件
- (91)://获取应用实例
- (94): data: {
- (95): indicatorDots: tru
- (96): autoplay: true,
- (97): interval: 5000,
- (98): duration: 1000,
- (99): radioList: [],
- (100): slider: [],
- (101): mainView: 1,
- (102): topList:[]
- (103): },
- (104): onLoad: function (
- (105): var that = this;
- (108): },
- (109): ...
- (110): initTopList: funct
- (111): var self = this;
- (112): if (data.code == 0
- (113): self.setData({
- (114): topList: data.
- (115): })
- (116): }
- (117): },
- (118): ...
- (122):<!-- 排行页 -->
- (125): ...
- (126):</view>
- (127):</view>
- (130):<!-- 排行页 -->
- (135): ...
- (136): </view>
- (137):</view>
- (138):</view>
- (143):<!-- 排行页 -->
- (151): </view>
- (152): </view>
- (153):</view>
- (154):</view>
- (157):.top-view {
- (158): background:#f7f7f7
- (159): padding:20rpx;
- (161):.top-item {
- (162): display:-webkit-bo
- (163): height:200rpx;
- (164): margin-bottom:20rp
- (165): background:#fff;
- (166): overflow: hidden;
- (168):.top-item-img {
- (169): display:block;
- (170): position:relative;
- (171): width:200rpx;
- (172): height:200rpx;
- (174):.top-item-info {
- (175): margin:0 10rpx 0 2
- (176): flex-direction:col
- (178):.top-item-list {
- (179): white-space:nowrap
- (181):.top-icon {
- (182): margin-top:16rpx;
- (183): width:40rpx;
- (184): height:40rpx;
- (186):.top-item-text {
- (187): margin-bottom: 10r
- (188): font-size:40rpx;
- (193): wx.navigateTo({
- (194): url: '../top
- (195): })
- (196): },
- (199)://app.js
- (204): logs.unshift(Date.
- (208): var that = this
- (209): if(this.globalData
- (211): }else{
- (212): //调用登录接口
- (213): wx.login({
- (214): success: function
- (215): wx.getUserInfo({
- (216): success: functio
- (219): }
- (220): })
- (221): }
- (222): })
- (223): }
- (225)://这里是我们添加的代码!!!
- (227): for(var n in obj)
- (228): this.globalData[n]
- (229): }
- (232): userInfo:null
- (240): topListId: _data
- (241): });
- (242): wx.navigateTo({
- (243): url: '../top
- (244): })
- (245): },
排行页
我们先在services/music.js里添加网络请求函数:
function getTopMusic(callback){
var data = {
format: 'json',
g_tk: 5381,
uin: 0,
inCharset: 'utf-8',
outCharset: 'utf-8',
notice: 0,
platform: 'h6',
needNewCode: 1,
_: Date.now()
};
wx.request({
url: 'http://c.y.qq.com/v8/fcg-bin/fcg_myqq_toplist.fcg',
data: data,
header: {
'Content-Type': 'application/json'
},
success: function (res) {
if (res.statusCode == 200) {
callback(res.data)
} else {
}
}
});
}
module.exports = {
getRecommendMusic:getRecommendMusic
getTopMusic:getTopMusic
}
复制代码
这里返回的JSON格式数据为:
{
"code": 0,
"subcode": 0,
"message": "",
"default": 0,
"data": {
"topList": [
{
"id": 4,
"listenCount": 20000000,
"picUrl": "https://qixn-bj.oss-cn-beijing.aliyuncs.com/seosjz/uploadfile/all/jpg/iydofdgkebm.jpg",
"songList": [
{
"singername": "赵雷",
"songname": "理想 (Live)"
},
{
"singername": "薛之谦/欧阳娜娜",
"songname": "小幸运 (Live)"
},
{
"singername": "迪玛希Dimash",
"songname": "秋意浓 (Live)"
}
],
"topTitle": "巅峰榜·流行指数",
"type": 0
},
{
"id": 26,
"listenCount": 19900000,
"picUrl": "https://qixn-bj.oss-cn-beijing.aliyuncs.com/seosjz/uploadfile/all/jpg/h04p53iojgf.jpg",
"songList": [
{
"singername": "李玉刚",
"songname": "刚好遇见你"
},
{
"singername": "周杰伦",
"songname": "告白气球"
},
{
"singername": "张杰",
"songname": "三生三世"
}
],
"topTitle": "巅峰榜·热歌",
"type": 0
},
...
]
}
}
复制代码
可以看到这里显示了两类排行榜:“巅峰榜·流行指数”与“巅峰榜·热歌”,篇幅原因省去了其他12类,所以实际返回的排行榜类别为14类,每一类包涵标题("topTitle"),该类的图标图片地址("picUrl"),以及前三位的歌曲列表("songList")。因此,我们最后要达成的页面应该为图所示。
同理上一节内容,我们新增topList数组,调用网络请求,使用回调函数为topList赋值。
//引用网络请求文件
var MusicService = require('../../services/music');
//获取应用实例
var app = getApp()
Page({
data: {
indicatorDots: true,
autoplay: true,
interval: 5000,
duration: 1000,
radioList: [],
slider: [],
mainView: 1,
topList:[]
},
onLoad: function () {
var that = this;
MusicService.getRecommendMusic(that.initPageData);
MusicService.getTopMusic(that.initTopList);
},
...
initTopList: function (data) {
var self = this;
if (data.code == 0) {
self.setData({
topList: data.data.topList
})
}
},
...
})
复制代码
排行页主要由列表组成,所以使用wx:for为topList每一项创建view,绑定每一项的id和点击事件topListTap。
<!-- 排行页 -->
<view class="top-view" hidden="{{currentView != 2}}">
<view class="top-item" wx:for="{{topList}}" wx:key="unique" data-id="{{item.id}}" bindtap="topListTap">
...
</view>
</view>
复制代码
列表的每一项由图片(数据源为picUrl)以及前三名歌曲列表(数据源为songList)组成。我们把这一部分加入到省略号位置。
<!-- 排行页 -->
<view class="top-view" hidden="{{currentView != 2}}">
<view class="top-item" wx:for="{{topList}}" wx:key="unique" data-id="{{item.id}}" bindtap="topListTap">
<image class="top-item-img" mode="aspectFit" src="{{item.picUrl}}" />
<view class="top-item-info">
...
</view>
</view>
</view>
复制代码
图片定义了属性aspectFit,即在保持纵横比的前提下,缩放图片,使图片在容器内都显示出来。
最后我们添加歌曲列表,每一项由文字(歌曲名-歌手)以及表示排名的图片组成。这个图片为本地图片,保存在resources/images下,名称为1.png,2.png,3.png。所以这部分最终的代码为:
<!-- 排行页 -->
<view class="top-view" hidden="{{currentView != 2}}">
<view class="top-item" wx:for="{{topList}}" wx:key="unique" data-id="{{item.id}}" bindtap="topListTap">
<image class="top-item-img" mode="aspectFit" src="{{item.picUrl}}" />
<view class="top-item-info">
<view class="top-item-list" wx:for="{{item.songList}}" wx:key="unique">
<image class="top-icon" src="../../resources/images/{{index+1}}.png" />
<text class="top-item-text">{{item.songname}}-{{item.singername}}</text>
</view>
</view>
</view>
</view>
复制代码
需要的格式文件代码为:
.top-view {
background:#f7f7f7;
padding:20rpx;
}
.top-item {
display:-webkit-box;
height:200rpx;
margin-bottom:20rpx;
background:#fff;
overflow: hidden;
}
.top-item-img {
display:block;
position:relative;
width:200rpx;
height:200rpx;
}
.top-item-info {
margin:0 10rpx 0 20rpx;
flex-direction:column;
}
.top-item-list {
white-space:nowrap;
}
.top-icon {
margin-top:16rpx;
width:40rpx;
height:40rpx;
}
.top-item-text {
margin-bottom: 10rpx;
font-size:40rpx;
}
复制代码
页面完成后,在index.js里添加点击事件的代码:
topListTap: function (e) {
wx.navigateTo({
url: '../toplist/toplist'
})
},
复制代码
这样在点击之后就进入了列表页面,但这样还不能完成我们的要求,因为这样列表页完全不知道我们点击了哪一类。为了让列表页获取这个信息,我们需要把类的id传过去,这就需要我们在app.js里添加一个全局变量。
//app.js
App({
onLaunch: function () {
//调用API从本地缓存中获取数据
var logs = wx.getStorageSync('logs') || []
logs.unshift(Date.now())
wx.setStorageSync('logs', logs)
},
getUserInfo:function(cb){
var that = this
if(this.globalData.userInfo){
typeof cb == "function" && cb(this.globalData.userInfo)
}else{
//调用登录接口
wx.login({
success: function () {
wx.getUserInfo({
success: function (res) {
that.globalData.userInfo = res.userInfo
typeof cb == "function" && cb(that.globalData.userInfo)
}
})
}
})
}
},
//这里是我们添加的代码!!!
setGlobalData: function (obj) {
for(var n in obj) {
this.globalData[n] = obj[n];
}
},
globalData:{
userInfo:null
}
})
复制代码
这里定义了一个方法,让我们可以向globalData里添加数据,之后我们只需在点击事件里调用这个方法就可以了:
topListTap: function (e) {
var _dataSet = e.currentTarget.dataset; //获取点击的view的数据
app.setGlobalData({ //将数据里的id传给globaldata
topListId: _dataSet.id
});
wx.navigateTo({
url: '../toplist/toplist'
})
},
</div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
微信小程序怎么实现音乐排行榜的详细内容,希望对您有所帮助,信息来源于网络。