movies.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. var util = require('../../utils/util.js')
  2. var app = getApp();
  3. Page({
  4. data: {
  5. inTheaters: {},
  6. comingSoon: {},
  7. top250: {},
  8. searchResult: {},
  9. containerShow: true,
  10. searchPanelShow: false,
  11. searchText:""
  12. },
  13. onLoad: function (event) {
  14. // var inTheatersUrl = app.globalData.doubanBase +
  15. // "/v2/movie/in_theaters" + "?start=0&count=6";
  16. // var comingSoonUrl = app.globalData.doubanBase +
  17. // "/v2/movie/coming_soon" + "?start=0&count=6";
  18. // var top250Url = app.globalData.doubanBase +
  19. // "/v2/movie/top250" + "?start=0&count=6";
  20. // this.getMovieListData(inTheatersUrl, "inTheaters", "正在热映");
  21. // this.getMovieListData(comingSoonUrl, "comingSoon", "即将上映");
  22. // this.getMovieListData(top250Url, "top250", "豆瓣Top250");
  23. },
  24. onMoreTap: function (event) {
  25. var category = event.currentTarget.dataset.category;
  26. wx.navigateTo({
  27. url: "more-movie/more-movie?category=" + category
  28. })
  29. },
  30. open : function (e){
  31. }
  32. ,
  33. onMovieTap: function (event) {
  34. var movieId = event.currentTarget.dataset.movieid;
  35. wx.navigateTo({
  36. url: "movie-detail/movie-detail?id=" + movieId
  37. })
  38. },
  39. //onload方法中调用
  40. getMovieListData: function (url, settedKey, categoryTitle) {
  41. wx.showNavigationBarLoading()
  42. var that = this;
  43. wx.request({
  44. url: url,
  45. method: 'GET',
  46. header: {
  47. "Content-Type": "json"
  48. },
  49. success: function (res) {
  50. that.processDoubanData(res.data, settedKey, categoryTitle)
  51. },
  52. fail: function (error) {
  53. console.log(error)
  54. }
  55. })
  56. },
  57. processDoubanData: function (moviesDouban, settedKey, categoryTitle) {
  58. var movies = [];
  59. for (var idx in moviesDouban.subjects) {
  60. var subject = moviesDouban.subjects[idx];
  61. var title = subject.title;
  62. if (title.length >= 6) {
  63. title = title.substring(0, 6) + "...";
  64. }
  65. var score = subject.rating.average + "";
  66. var temp = {
  67. stars: util.convertToStarsArray(subject.rating.stars),
  68. title: title,
  69. average: score.length == 1 ? subject.rating.average + '.0' : subject.rating.average,
  70. coverageUrl: subject.images.large,
  71. movieId: subject.id
  72. }
  73. movies.push(temp)
  74. }
  75. var readyData = {};
  76. readyData[settedKey] = {
  77. categoryTitle: categoryTitle,
  78. movies: movies
  79. }
  80. this.setData(readyData);
  81. wx.hideNavigationBarLoading();
  82. },
  83. //点击搜索框,让container隐藏,搜索条显示
  84. onBindFocus: function (event) {
  85. this.setData({
  86. containerShow: false,
  87. searchPanelShow: true
  88. })
  89. },
  90. //搜索框失去焦点开始搜索
  91. onBindBlur: function (event) {
  92. var text = event.detail.value;
  93. var searchUrl = app.globalData.doubanBase + "/v2/movie/search?q=" + text;
  94. this.getMovieListData(searchUrl, "searchResult", "");
  95. },
  96. //点击输入框的X 关闭
  97. onCancelImgTap: function (event) {
  98. this.setData({
  99. containerShow: true,
  100. searchPanelShow: false,
  101. searchResult: {},
  102. searchText:""
  103. });
  104. },
  105. onShareAppMessage: function () {
  106. return {
  107. title: '豆瓣电影',
  108. desc: '一起看电影吧~'
  109. }
  110. }
  111. })