微信小程序处理接口数据
util.js代码:
function get_index_data(callback) {
wx.request({
url: 'https://c.y.qq.com/musichall/fcgi-bin/fcg_yqqhomepagerecommend.fcg', //仅为示例,并非真实的接口地址
data: {
g_tk: 5381,
uin: 0,
format: 'json',
inCharset: 'utf - 8',
outCharset: 'utf - 8',
notice: 0,
platform: 'h5',
needNewCode: 1,
_: new Date().getTime() //清除缓存
},
header: {
'content-type': 'application/json' // 默认值
},
success: function (res) {
//console.log(res)
//回调函数
callback(res)
}
})
};
//排行榜
function get_toplist(callback){
wx.request({
url: 'https://c.y.qq.com/v8/fcg-bin/fcg_myqq_toplist.fcg',
data: {
g_tk: 5381,
uin: 0,
format: 'json',
inCharset: 'utf - 8',
outCharset: 'utf - 8',
notice: 0,
platform: 'h5',
needNewCode: 1,
_: new Date().getTime() //清除缓存
},
header: {
'content-type': 'application/json' // 默认值
},
success: function (res) {
//console.log(res)
//回调函数
callback(res)
}
})
}
//热门搜索
function get_hot_key(callback){
wx.request({
url: 'https://c.y.qq.com/splcloud/fcgi-bin/gethotkey.fcg',
data: {
g_tk: 5381,
uin: 0,
format: 'json',
inCharset: 'utf - 8',
outCharset: 'utf - 8',
notice: 0,
platform: 'h5',
needNewCode: 1,
_: new Date().getTime() //清除缓存
},
header: {
'content-type': 'application/json' // 默认值
},
success: function (res) {
//console.log(res)
//回调函数
if(res.statusCode == 200){
callback(res.data)
}
}
})
}
function get_toplist_detail(id,callback){
wx.request({
url: 'https://c.y.qq.com/v8/fcg-bin/fcg_v8_toplist_cp.fcg',
data: {
g_tk: 5381,
uin: 0,
format: 'json',
inCharset: 'utf-8',
outCharset: 'utf-8',
notice: 0,
platform: 'h5',
needNewCode: 1,
tpl: 3,
page: 'detail',
type: 'top',
topid: id,
_: new Date().getTime() //清除缓存
},
header: {
'content-type': 'application/json' // 默认值
},
success: function (res) {
//console.log(res)
//回调函数
if (res.statusCode == 200) {
callback(res.data)
}
}
})
}
//暴露接口
module.exports={
get_index_data,
get_toplist,
get_hot_key,
get_toplist_detail
}
暴露接口之后还需要在页面的js文件中进行处理。
代码:
onLoad: function(options) {
var that = this
util.get_toplist(function(res) {
let data = res.data.data.topList;
that.setData({
topList: data
})
})
},
(这里只处理了上面几个接口中的一个,做个栗子嘛~)
把那些接口放到浏览器中打开,你就知道topList是怎么来的了。
然后就可以将数据渲染到页面上了,例如:
<view class='item' wx:for="{{topList}}" wx:key="" bindtap='opentoplist' data-topid="{{item.id}}">
<image src='{{item.picUrl}}'></image>
<view class='left'>
<text class='title'>{{item.topTitle}}</text>
<view class='left-wrap' wx:for="{{item.songList}}" wx:key="">
<text>{{index+1}}</text>
<text>{{item.songname}}</text>
<text>{{item.singername}}</text>
</view>
</view>
<text class='fbiao'>></text>
</view>
就这么多,感谢阅读,么么哒~