微信小程序音樂(lè)播放器,音樂(lè)播放器檢索頁(yè)制作(中)
這部分也是列表樣式,每一項(xiàng)由前面的時(shí)鐘圖標(biāo),中間的文字以及最后表示刪除的“X”組成,最下方為清空整個(gè)列表的選項(xiàng)。
- <view class="search-hi" wx:if="{{showSearchPanel == 2}}">
- <block wx:if="{{historySearchs.length > 0}}">
- <view class="search-hi-item" wx:for="{{historySearchs}}" wx:key="unique">
- <view class="hi-icon"></view>
- <text class="hi-text" data-key="{{item}}" bindtap="historysearchTap">{{item}}</text>
- <view class="hi-close" data-index="{{index}}" bindtap="delHistoryItem"></view>
- </view>
- <view class="clear-serach" bindtap="clearHistorySearchs">清除搜索記錄</view>
- </block>
- </view>
這一部分只有當(dāng)showSearchPanel=2才顯示。
具體的內(nèi)容,我們首先要判斷是否有歷史數(shù)據(jù)。使用historySearchs存儲(chǔ)所以歷史記錄,只有當(dāng)其長(zhǎng)度大于0時(shí),才渲染這一部分。
block里的內(nèi)容就如我們所要的一致,然后我們?yōu)槲淖?,結(jié)尾的“X”標(biāo)志,和最后的“清除搜索記錄”添加點(diǎn)擊事件。
格式文件為:
- .search-hi-item {
- height:88rpx;
- line-height:88rpx;
- border-bottom:2rpx solid #ededed;
- }
- .hi-icon {
- position:absolute;
- left:20rpx;
- display:inline-block;
- width:88rpx;
- height:88rpx;
- background:url('../../resources/images/clock_ic.png') no-repeat center center;
- background-size:40rpx;
- }
- .hi-text {
- position:absolute;
- left:108rpx;
- right:108rpx;
- display:block;
- white-space:nowrap;
- text-overflow:ellipsis;
- overflow:hidden;
- font-size:32rpx;
- }
- .hi-close {
- width:88rpx;
- height:88rpx;
- position:absolute;
- right:20rpx;
- background:url('../../resources/images/cancel.png') no-repeat center center;
- }
- .clear-serach {
- font-size:24rpx;
- text-align:center;
- color:#47c88a;
- line-height:88rpx;
- }
background里加載了分別加載了兩個(gè)本地圖片并為其設(shè)置了不平鋪與居中的屬性。
很顯然,在我們每次點(diǎn)擊確定按鈕后,輸入框內(nèi)的值除了發(fā)送到服務(wù)器請(qǐng)求搜索結(jié)果,還要添加到historySearchs里。
所以我們先寫搜索結(jié)果列表,然后一起處理確定按鈕的點(diǎn)擊事件。
首先是網(wǎng)絡(luò)請(qǐng)求的函數(shù):
- function getSearchMusic(word,callback){ //word為傳入的關(guān)鍵字,callback為回調(diào)函數(shù)
- var data = {
- g_tk: 5381,
- uin: 0,
- format: 'json',
- inCharset: 'utf-8',
- outCharset: 'utf-8',
- notice: 0,
- platform: 'h5',
- needNewCode: 1,
- w: word,
- zhidaqu: 1,
- catZhida: 1,
- t: 0,
- flag: 1,
- ie: 'utf-8',
- sem: 1,
- aggr: 0,
- perpage: 20,
- n: 20,
- p: 1,
- remoteplace: 'txt.mqq.all',
- _: Date.now()
- };
- wx.request({
- url: 'http://c.y.qq.com/soso/fcgi-bin/search_for_qq_cp',
- data: data,
- header: {
- 'Content-Type': 'application/json'
- },
- success: function (res) {
- if (res.statusCode == 200) {
- callback(res.data);
- } else {
- }
- }
- });
- }
- ...
- module.exports = {
- ...
- getSearchMusic:getSearchMusic
- }
返回的JSON數(shù)據(jù)為:
- {
- "code": 0,
- "data": {
- "keyword": "那片星空那片海 ",
- "priority": 0,
- "qc": [],
- "semantic": {
- "curnum": 0,
- "curpage": 1,
- "list": [],
- "totalnum": 0
- },
- "song": {
- "curnum": 6,
- "curpage": 1,
- "list": [
- {
- "albumid": 1829165,
- "albummid": "000cvc411fKPIC",
- "albumname": "那片星空那片海 電視原聲帶",
- "albumname_hilight": "<span class=\"c_tx_highlight\">那片星空那片海</span> 電視原聲帶",
- "alertid": 100007,
- "chinesesinger": 0,
- "docid": "8313377529987609872",
- "format": "qqhq;common;mp3common;wmacommon",
- "grp": [],
- "interval": 227,
- "isonly": 1,
- "lyric": "《那片星空那片海》電視劇片頭曲",
- &nnbsp; "lyric_hilight": "《<span class=\"c_tx_highlight\">那片星空那片海</span>》電視劇<span class=\"c_tx_highlight\">片</span>頭曲",
- "msgid": 13,
- "nt": 3247935513,
- "pay": {
- "payalbum": 1,
- "payalbumprice": 500,
- "paydownload": 1,
- "payinfo": 1,
- "payplay": 1,
- "paytrackmouth": 0,
- "paytrackprice": 0
- },
- "preview": {
- "trybegin": 0,
- "tryend": 0,
- "trysize": 0
- },
- "pubtime": 1486483200,
- "pure": 0,
- "singer": [
- {
- "id": 12111,
- "mid": "004YXxql1sSr2o",
- "name": "金志文",
- "name_hilight": "金志文"
- }
- ],
- "size128": 3649005,
- "size320": 9122181,
- "sizeape": 23599481,
- "sizeflac": 23676261,
- "sizeogg": 4916985,
- "songid": 200552006,
- "songmid": "001lggCh2Nw7us",
- "songname": "那片星空那片海",
- "songname_hilight": "<span class=\"c_tx_highlight\">那片星空那片海</span>",
- "songurl": "http://y.qq.com/#type=song&id=200552006",
- "stream": 5,
- "switch": 594177,
- "t": 0,
- "tag": 10,
- "type": 0,
- "ver": 0,
- "vid": ""
- },
- ...
- ],
- "totalnum": 6
- },
- "totaltime": 0,
- "zhida": {
- "albumid": 1829165,
- "albummid": "000cvc411fKPIC",
- "albumname": "那片星空那片海 電視原聲帶",
- "singername": "華語(yǔ)群星",
- "type": 3
- }
- },
- "message": "",
- "notice": "",
- "subcode": 0,
- "time": 1487063618,
- "tips": ""
- }
有了數(shù)據(jù)之后,我們需要在列表里顯示的內(nèi)容為歌曲名稱(songname)與歌手名稱(singer.name)。
上一節(jié):微信小程序小白項(xiàng)目開發(fā)案例之音樂(lè)播放器—檢索頁(yè)(上)
下一節(jié):微信小程序小白項(xiàng)目開發(fā)案例之音樂(lè)播放器—檢索頁(yè)(下)
第二部分:如何開通一個(gè)小商店