微信小程式--搜尋電影app(續)
阿新 • • 發佈:2019-02-11
熱門推薦頁面各個檔案程式碼如下:
recommendMovies.json:
這裡的頁面佈局基本同第一個頁面一致,熱門推薦的電影 api 介面地址為:https://api.douban.com/v2/movie/top250。
detail.wxss程式碼如下:
searchMovies.wxss 程式碼如下:
佈局基本都跟第一個頁面一樣,api 介面地址:https://api.douban.com/v2/movie/search?q=人名。
recommendMovies.wxml:
<view class="wrapper">
<view wx:for="{{topMovies}}" wx:for-item="item">
<view class="content">
<view class="picView">
<image class="pic" src="{{item.images.medium}}" id="{{item.id}}" bindtap="toDetail" />
</view>
<view class="info">
<view class="name">
名稱:{{item.title}}
</view>
<view class="score">{{item.rating.average}}分</view>
<view class="type">
型別:
<block wx:for="{{item.genres}}" wx:for-item="type">
{{type}},
<!--注意不要使用<view>,不然調不出效果。。-->
</block>
</view>
<view class="director">
導演:
<block wx:for="{{item.directors}}" wx:for-item="director">
{{director.name}},
</block>
</view>
<view class="actor">
演員:
<block wx:for="{{item.casts}}" wx:for-item="actor">
{{actor.name}},
</block>
</view>
<view class="time">年份: {{item.year}}</view>
</view>
</view>
</view>
</view>
recommendMovies.wxss:
.wrapper{
padding:0;
margin:0;
width:100%;
height:100%;
}
.slide-image{
width:750rpx;
height:100%;
}
.content{
width:100%;
height:300rpx;
padding:10rx;
display: flex;
flex-direction: row;
border-bottom: 2rpx solid #CCCCCC;
}
.picView{
float:left;
padding:20rpx 15rpx;
}
.pic{
width:210rpx;
height:260rpx;
}
.info{
float:left;
display: flex;
flex-direction: column;
color:#888888;
padding-top:20rpx;
font-size: 30rpx;
}
.name{
color:#000;
width:100%;
font-size: 32rpx;
margin-bottom: -19rpx;
}
.score{
position: relative;
width:80rpx;
float:right;
top:-18rpx;
color:#8C5A0D;
right:-433rpx;
}
.type{
display: flex;
flex-direction: row;
margin-bottom: 10rpx;
}
.director{
display: flex;
flex-direction: row;
margin-bottom: 10rpx;
}
.actor{
margin-bottom: 10rpx;
}
recommendMovies.js:
//recommendMovies.js
var util = require('../../utils/util.js')
Page({
data: {
},
onLoad: function () {
/*
var city = wx.getStorageSync('city');
console.log('只在初次進入此頁面時執行一次');
var topMovies = wx.getStorageSync('topMovies'+city);
if (!topMovies){
console.log('request');
this.requestTopMovies();
}else{
console.log('storage');
this.setData({
topMovies:topMovies
});
}
*/
},
onShow: function () {
/* var city = wx.getStorageSync('city'); */
console.log('每次進入此頁面都會執行此函式,適合放需要實時呼叫頁面邏輯的程式碼');
/*經測試,此處不需要加city特判,因為請求排行榜靠前的電影時不需要加city引數.
var topMovies = wx.getStorageSync('topMovies' + city);*/
var topMovies = wx.getStorageSync('topMovies');
if (!topMovies) {
console.log('request');
this.requestTopMovies();
} else {
console.log('storage');
this.setData({
topMovies: topMovies
});
}
},
requestTopMovies:function(){
/* console.log('city:' + wx.getStorageSync('city'));
var city = wx.getStorageSync('city');*/
var url = "https://api.douban.com/v2/movie/top250";
/**
* var data = {
city : city //貌似沒有這個引數需要傳遞,結果都一樣。
};*/
var that = this;
wx.request({
url:url,
data:'',//data,
header:{
'content-type':'json'//不要寫'aplication/json',會報400錯誤。
},
success:function(res){
console.log(res);
that.setData({
topMovies:res.data.subjects
});
that.saveData(res.data.subjects/*,city*/);
}
});
},
toDetail:function(event){
wx.navigateTo({//通過 id 請求詳情頁面
url: '/pages/detail/detail?id='+event.currentTarget.id,
})
},
saveData:function(res/*,city*/){
wx.setStorage({
key:'topMovies'/*+city*/,
data:res
});
}
})
recommendMovies.json:
{
"navigationBarTitleText": "熱門電影推薦"
}
這裡的頁面佈局基本同第一個頁面一致,熱門推薦的電影 api 介面地址為:https://api.douban.com/v2/movie/top250。
沒有city的引數,加上沒效果,已試過!可以檢視上面的截圖框。
要注意的一點時,請求 wx.request 時,header:{ 'conten-type':'json' }不能寫成 'application/json' 不然會一直報400錯誤,應該是版本問題。
詳情頁面各個檔案程式碼如下:
detail.wxml:
<!--detail.wxml-->
<image src="../../assets/imgs/bg.jpg" mode="scaleToFill" />
<view class="wrapper">
<view class="filmIntro">
<view class="filmPic">
<image src="{{filmDetail.images.medium}}" />
</view>
<view class="filmInfo">
<view class="name">名稱:{{filmDetail.title}}</view>
<view class="score">{{filmDetail.rating.average}}分</view>
<view class="type">
型別:<block wx:for="{{filmDetail.genres}}" wx:for-item="type">
{{type}},
</block>
</view>
<view class="director">
導演:<block wx:for="{{filmDetail.directors}}" wx:for-item="director">
{{director.name}},
</block>
</view>
<view class="actor">
演員:<block wx:for="{{filmDetail.casts}}" wx:for-item="cast">
{{cast.name}},
</block>
</view>
<view class="time">年份: {{filmDetail.year}}</view>
</view>
</view>
<view class="descrision">
{{filmDetail.summary}}
<!--<textarea value="{{filmDetail.summary}}" auto-height focus="true" />有一些文字出不來不知為啥所以沒用textarea-->
</view>
<view class="bottom">
<view class="title">主要參演人員</view>
<scroll-view scroll-x bindscrolltoupper="upper" bindscrolltolower="lower" bindscroll="scroll" class="casts">
<view wx:for="{{filmDetail.casts}}" wx:for-item="cast" class="castsItem">
<view class="castImg"><image src="{{cast.avatars.medium}}" /></view>
<view class="castName">{{cast.name}}</view>
</view>
<view wx:for="{{filmDetail.directors}}" wx:for-item="director" class="castsItem">
<view class="castImg"><image src="{{director.avatars.medium}}" /></view>
<view class="castName">{{director.name}}</view>
</view>
</scroll-view>
</view>
</view>
detail.wxss程式碼如下:
/* detail.wxss */
image{
width:750rpx;
height:350rpx;
}
.wrapper{
padding:0;
margin:0;
width:750rpx;
height:100%;
position: absolute;
left:0;
top:0;
}
.filmIntro{
width:100%;
height:350rpx;
}
.filmPic{
float:left;
width:175rpx;
height:300rpx;
padding:40rpx 15rpx 10rpx 40rpx;
}
.filmPic image{
width:175rpx;
height:240rpx;
border:2rpx solid #fff;
}
.filmInfo{
float:right;
position: relative;
width:507rpx;
left:-11rpx;
top:38rpx;
font-size: 0.8rem;
color:#fff;
}
.name{
float:left;
margin-bottom:10rpx;
font-size: 0.9rem;
}
.score{
position: absolute;
right: -10rpx;
color:aqua;
}
.type{
clear:both;
display: flex;
flex-direction: row;
margin-bottom:6rpx;
}
.director{
display: flex;
flex-direction: row;
margin-bottom:6rpx;
}
.actor{
display: flex;
flex-direction: row;
margin-bottom:6rpx;
}
.descrision{
clear:both;
width:700rpx;
height:346rpx;
overflow: hidden;
text-overflow: ellipsis;
display: -webkit-box;
-webkit-line-clamp: 11;
-webkit-box-orient: vertical;
padding:30rpx 30rpx;
font-size: 0.8rem;
color:#aaa;
border-bottom:18rpx solid #ccc;
}
.title{
width:100%;
font-size: 1rem;
padding:10rpx;
border-bottom: 2rpx solid #ccc;
}
.casts{
/*display: flex;
flex-direction: row;*/
width:750rpx;
white-space: nowrap;/*不換行(放在父元素)*/
border-bottom:22rpx solid #ccc;
}
.castsItem{
display: inline-block;/*內聯塊(放在子元素)*/
padding:26rpx;
text-align: center;/*文字居中*/
}
.castImg>image{
width:180rpx;
height:254rpx;
}
.castName{
font-size: 0.7rem;
color:#0ff;
}
textarea{
width:700rpx;
height:350rpx;
}
detail.js程式碼如下:
// detail.js
Page({
/**
* 頁面的初始資料
*/
data: {
},
/**
* 生命週期函式--監聽頁面載入
*/
onLoad: function (options) {
var that = this, data = null;
var filmId = options.id;
console.log('detail\'s id:' + filmId);//獲取在首頁點選的電影圖片的id
/*
一開始在首頁電影資料中獲取詳情頁資料,後來發現並沒有簡介的文字部分,所以上官網找到了相應的api來獲取詳情頁資料,通過id獲取的,方便許多!不需遍歷陣列了。
var hotMovies = wx.getStorageSync('hotMovies');
console.log(hotMovies.length);
if(hotMovies){
for(var i=0;i<hotMovies.length;i++){
if (hotMovies[i].id == filmId){//遍歷電影資料找到符合的就退出。
data = hotMovies[i];
break;
}
}
that.setData({
filmDetail: data
});
}
*/
var name = 'detailInfo' + filmId;
var detailInfo = wx.getStorageSync(name);/**獲取本地同步資料 */
console.log(detailInfo);
if (!detailInfo) {
console.log('request');
this.requestDetailInfo(filmId);
}else{
console.log('storage');
this.setData({
filmDetail: detailInfo
});
}
},
/**
* 生命週期函式--監聽頁面初次渲染完成
*/
onReady: function () {
},
/**
* 生命週期函式--監聽頁面顯示
*/
onShow: function () {
},
requestDetailInfo: function (filmId) {
var that = this;
var url = 'https://api.douban.com/v2/movie/subject/'+filmId;
wx.request({
url: url,
data: '',
header: {
'content-type': 'json',
},
success: function (res) {
console.log('detailInfo\'s summary is:' + res.data.summary);
console.log(res.data);
//return res.data;不起作用,因為是回撥函式,非同步性。
that.setData({
filmDetail: res.data
});
that.saveData(res.data,filmId);
}
});
},
saveData: function (data,id) {
wx.setStorage({
key: 'detailInfo'+id,
data: data,
})
}
})
佈局就基本同上,主要是底部滑鼠控制滑動圖片使用了元件 scroll-view 元件,這裡是橫向滑動,所以必須設定固定高度,還有就是要使得多個元素橫向排列不換行,使用之前flex佈局不起作用,得在 scroll-view 元件上加屬性: white-space: nowrap;/*不換行(放在父元素)*/,在其子元素(橫向排布的元素)加屬性:display: inline-block;/*內聯塊(放在子元素)*/。具體見程式碼。
搜尋頁面各個檔案程式碼如下:
searchMovies.wxml程式碼如下:
<view class="wrapper">
<view class="search">
<input bindinput="keyword" placeholder="請輸入主演名字" />
<button type="default" bindtap="searchMovies" data-keyword="{{keyword}}">搜尋</button><!--data-keyword向函式傳參keyword-->
</view>
<view class="searchText">您要搜尋:{{keyword}}</view>
<view class="horLine"></view>
<view wx:for="{{searchMovies}}" wx:for-item="item">
<view class="content">
<view class="picView">
<image class="pic" src="{{item.images.medium}}" id="{{item.id}}" bindtap="toDetail" />
</view>
<view class="info">
<view class="name">
名稱:{{item.title}}
</view>
<view class="score">{{item.rating.average}}分</view>
<view class="type">
型別:
<block wx:for="{{item.genres}}" wx:for-item="type">
{{type}},
<!--注意不要使用<view>,不然調不出效果。。-->
</block>
</view>
<view class="director">
導演:
<block wx:for="{{item.directors}}" wx:for-item="director">
{{director.name}},
</block>
</view>
<view class="actor">
演員:
<block wx:for="{{item.casts}}" wx:for-item="actor">
{{actor.name}},
</block>
</view>
<view class="time">年份: {{item.year}}</view>
</view>
</view>
</view>
</view>
searchMovies.wxss 程式碼如下:
.wrapper{
padding:0;
margin:0;
width:750rpx;
height:100%;
}
.search{
width:100%;
height:92rpx;
display: flex;
flex-direction: row;
}
input{
height:1rem;
flex-grow: 1; /**剩餘空間都給我*/
line-height: 70rpx;
border:2rpx solid #ccc;
margin:20rpx;
border-radius: 10rpx;
font-size: 0.8rem;
}
button{
width:130rpx;
height:50rpx;
line-height: 50rpx;
margin:24rpx 30rpx 0rpx 0rpx;
font-size: 0.8rem;
}
.searchText{
margin-left:20rpx;
font-size: 0.8rem;
padding-bottom:20rpx;
}
.horLine{
width:100%;
border:10rpx solid #ccc;
}
.content{
width:100%;
height:300rpx;
padding:10rx;
display: flex;
flex-direction: row;
border-bottom: 2rpx solid #CCCCCC;
}
.picView{
float:left;
padding:20rpx 15rpx;
}
.pic{
width:210rpx;
height:260rpx;
}
.info{
float:left;
display: flex;
flex-direction: column;
color:#888888;
padding-top:20rpx;
font-size: 30rpx;
}
.name{
color:#000;
width:100%;
font-size: 32rpx;
margin-bottom: -19rpx;
}
.score{
position: relative;
width:80rpx;
float:right;
top:-18rpx;
color:#8C5A0D;
right:-433rpx;
}
.type{
display: flex;
flex-direction: row;
margin-bottom: 10rpx;
}
.director{
display: flex;
flex-direction: row;
margin-bottom: 10rpx;
}
.actor{
margin-bottom: 10rpx;
}
searchMovies.js程式碼如下:
//searchMovies.js
var util = require('../../utils/util.js')
Page({
data: {
},
onLoad: function () {
},
searchMovies:function(event){
var that = this;
var keyword = event.currentTarget.dataset.keyword;
console.log(keyword);
var url = 'https://api.douban.com/v2/movie/search?q='+keyword;
wx.request({
url:url,
data:'',
header:{
'content-type':'json',
},
success:function(res){
console.log(res);
that.setData({
searchMovies:res.data.subjects
});
}
});
},
keyword:function(event){
var keyword = event.detail.value;/**獲取input輸入的值並設定到搜尋值 */
this.setData({
keyword:keyword
});
},
toDetail:function(event){
console.log(event.currentTarget.id);
wx.navigateTo({
url: '/pages/detail/detail?id='+event.currentTarget.id,
});
}
})
searchMovies.json程式碼如下:
{
"navigationBarTitleText": "電影搜尋"
}
佈局基本都跟第一個頁面一樣,api 介面地址:https://api.douban.com/v2/movie/search?q=人名。
這裡只選了一個引數即人物名字來搜尋,你可以自己擴充套件。
好了,詳情請自己參閱程式碼吧。這篇博文寫了好久啊啊。。
尷尬死了,中午寫到兩點多要釋出博文時居然把我部落格關閉了,後來聯絡之後解決之後又被鎖了,最後csdn負責人叫我改標題,果然解決!!!感謝!終於解決了,可以釋出了!