movies.js
var util = require('../../utils/util.js')
var app = getApp();
Page({
// RESTFul API JSON
// SOAP XML
//粒度 不是 力度
data: {
inTheaters: {},
comingSoon: {},
top250: {},
searchResult: {},
//九宫格
containerShow: true,
//
searchPanelShow: false,
},
//https://api.douban.com/v2/movie/in_theaters?start=0&count=3
onLoad: function (event) {
var inTheatersUrl = app.globalData.doubanBase +"/v2/movie/in_theaters" + "?start=1&count=6";
var comingSoonUrl = app.globalData.doubanBase +"/v2/movie/coming_soon" + "?start=1&count=6";
var top250Url = app.globalData.doubanBase +"/v2/movie/top250" + "?start=0&count=6";
var inTheatersUrl = app.globalData.doubanBase + "/v2/movie/in_theaters" + "?start=5&count=6";
var comingSoonUrl = app.globalData.doubanBase + "/v2/movie/coming_soon" + "?start=6&count=6";
var top250Url = app.globalData.doubanBase + "/v2/movie/top250" + "?start=6&count=6";
this.getMovieListData(inTheatersUrl, "inTheaters", "正在热映");
this.getMovieListData(comingSoonUrl, "comingSoon", "即将上映");
this.getMovieListData(top250Url, "top250", "豆瓣Top250");
this.getMovieListData(inTheatersUrl, "inTheaters", "正在热映");
this.getMovieListData(comingSoonUrl, "comingSoon", "即将上映");
this.getMovieListData(top250Url, "top250", "豆瓣Top250");
},
getMovieListData: function (url, settedKey, categoryTitle) {
var that = this;
//后台请求数据
wx.request({
//调接口
url: url,
//数据格式
method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
//数据返回格式
header: {
"Content-Type": "json"
},
success: function (res) {
that.processDoubanData(res.data, settedKey, categoryTitle)
},
fail: function (error) {
// fail
console.log(error)
}
})
},
onCancelImgTap: function (event) {
this.setData({
containerShow: true,
searchPanelShow: false,
searchResult: {}
}
)
},
onBindFocus: function (event) {
this.setData({
containerShow: false,
searchPanelShow: true
})
},
onBindBlur: function (event) {
var text = event.detail.value;
var searchUrl = app.globalData.doubanBase + "/v2/movie/search?q=" + text;
this.getMovieListData(searchUrl, "searchResult", "");
},
processDoubanData: function (moviesDouban, settedKey, categoryTitle) {
var movies = [];
for (var idx in moviesDouban.subjects) {
var subject = moviesDouban.subjects[idx];
var title = subject.title;
//字超过六个显示...
if (title.length >= 6) {
//substring
title = title.substring(0, 6) + "...";
}
// [1,1,1,1,1] [1,1,1,0,0]
var temp = {
stars: util.convertToStarsArray(subject.rating.stars),
title: title,
average: subject.rating.average,
coverageUrl: subject.images.large,
movieId: subject.id
}
movies.push(temp)
}
var readyData = {};
readyData[settedKey] = {
categoryTitle: categoryTitle,
movies: movies
}
//readyData将替换掉 data 中的三个变量 inTheaters、comingSoon、top250
this.setData(readyData);
},
onMoreTap: function (event) {
var category = event.currentTarget.dataset.category;
wx.navigateTo({
url: "more-movie/more-movie?category=" + category
})
},
onMovieTap: function (event) {
var movieId = event.currentTarget.dataset.movieid;
wx.navigateTo({
url: "movie-detail/movie-detail?id=" + movieId
})
}
})