商城系統(tǒng) 注冊

小程序調(diào)用豆瓣電影api

2020-09-27|HiShop
導(dǎo)讀:在瀏覽器中輸入豆瓣電影接口地址 http://api.douban.com/v2/movie/in_theaters 然后F12,打開調(diào)試窗口,選擇Console,輸入var a=接口返回的json串,如下圖所示: 然后回...

在瀏覽器中輸入豆瓣電影接口地址

http://api.douban.com/v2/movie/in_theaters

然后F12,打開調(diào)試窗口,選擇Console,輸入var a=接口返回的json串,如下圖所示:

 小程序調(diào)用豆瓣電影api

 

然后回車,再輸入a,再回車,即可看到已經(jīng)格式化的JSON對象,如下圖所示:

 小程序調(diào)用豆瓣電影api

4、從接口獲取數(shù)據(jù)進(jìn)行綁定

<block wx:for="{{movies}}">
    <view class="movie">
      <view class="pic">
        <image mode="aspectFit" src="{{item.images.medium}}">image>
      view>
      <view class="movie-info">
        <view class="base-info">
          <text>{{item.text}}text>
        view>
      view>
    view>
    <view class="hr">view>
  block>
// pages/movie/movie.js
Page({

  /**
   * 頁面的初始數(shù)據(jù)
   */
  data: {
    imgUrls: [
      '../../assets/img/001.jpg',
      '../../assets/img/002.jpg',
      '../../assets/img/003.jpg'
    ],
    indicatorDots: true,
    autoplay: true,
    interval: 3000,
    duration: 1000,
    movies:[],
    hidden:false
  },

  /**
   * 生命周期函數(shù)--監(jiān)聽頁面加載
   */
  onLoad: function (options) {
      this.loadMovie();
  },

  /**
   * 生命周期函數(shù)--監(jiān)聽頁面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函數(shù)--監(jiān)聽頁面顯示
   */
  onShow: function () {

  },

  /**
   * 生命周期函數(shù)--監(jiān)聽頁面隱藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函數(shù)--監(jiān)聽頁面卸載
   */
  onUnload: function () {

  },

  /**
   * 頁面相關(guān)事件處理函數(shù)--監(jiān)聽用戶下拉動作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 頁面上拉觸底事件的處理函數(shù)
   */
  onReachBottom: function () {

  },

  /**
   * 用戶點擊右上角分享
   */
  onShareAppMessage: function () {

  },
  /**
   * 加載電影
   */
  loadMovie:function(){
    var page = this;
    wx.request({
      url: 'http://api.douban.com/v2/movie/in_theaters',
      header:{
        'Content-Type':"application/json"
      },
      success:function(res){
      var subjects = res.data.subjects;
      processSubjects(subjects);
      page.setData({ movies: subjects, hidden:true});
      }
    })
  },
  /**
   * 
   */
  processSubjects: function (subjects) {
      //循環(huán)
    for (var i = 0; i < subjects.length;i++){
      var subject = subjects[i];
      this.processSubject(subject);
      }
  },
  /**
   * 
   */
  processSubject:function(subject){
      //名稱
      var title = subject.title;
      //導(dǎo)演
      var directors  = subject.directors;
      var directorStr = "";
      for (var index in directors){
        directorStr= directorStr+directors[index].name+" / ";
      }
      if(directorStr!=""){
        directorStr = directorStr.substring(0,directorStr.length-2);
      }
      //主演
      var casts = subject.casts;
      var castStr = "";
      for(var index in casts){
        castStr= castStr+casts[index].name+" / ";
      }
      if(castStr!=""){
        castStr= castStr.substring(0,castStr.length-2);
      }
      //類型
      var genres = subject.genres;
      var genresStr = "";
      for(var index in genres){
        genresStr = genresStr+genres[index]+" / ";
      }
      if(genresStr!=""){
        genresStr= genresStr.substring(0,genresStr.length-2);
      }
      //年份
      var year = subject.year;
      //拼接字符串
      var text = "名稱:"+title+"\n導(dǎo)演:"+directorStr+"\n主演:"+castStr+"\n類型:"+genresStr+"\n上映年份:"+year;
      subject.text = text;
  }

})

5、加載進(jìn)度條

電話咨詢 預(yù)約演示 0元開店