微信小程序調用后台接口+熱點新聞滾動展示


1、微信JS文件,發送請求調用:  //將返回接口數據,寫入Page({data})里面

//獲取熱點新聞,這個也是寫在onload:function(){//code)里面的
           wx.request({
             url: 'https://m.xxiangfang.com/index.php/Home/Xiaoxxf/activity?is_hot=1',//熱點新聞
             data: {},
             method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT   
             header: {
               'Content-Type': 'application/json'
             },
             success: function (res) {
               console.log(res.data)
               that.setData({
                 notices: res.data   //一維數組,只取兩條數據
               })
             },
             fail: function () {
               // fail  
             },
             complete: function () {
               // complete  
             }
           })

2、后台php處理:

使用curl調用即可,記得傳參和token(key)標識

 

3、JS文件里面【熱點新聞滾動展示】:

onLoad: function (res) {
 startNotice: function() {
          var me = this;
              var notices = me.data.notices || [];
          //console.log(this.data.notices)    //就是這里有問題,數據還沒從接口返回就跑到這里了 xzz-6.2
              if( notices.length == 0 ) {
                //return;     //是這里的問題,數據沒加載過來,導致程序return;
              }

              var animation = me.animation;
              //animation.translateY( -12 ).opacity( 0 ).step();
              // animation.translateY( 0 ).opacity( 1 ).step( { duration: 0 });
              // me.setData( { animationNotice: animation.export() });

              var noticeIdx = me.data.noticeIdx + 1;
          console.log(notices.length);
              if( noticeIdx == notices.length ) {
                noticeIdx = 0;
              }
          
              // 更換數據
              setTimeout( function() {
                me.setData( {
                  noticeIdx: noticeIdx
                });
              }, 400 );

              // 啟動下一次動畫
              setTimeout( function() {
                me.startNotice();
              }, 5000 );
},
      
onShow: function() {
         this.startNotice();
},

4、wxml的前段代碼:

<span style="color:#999999">熱門活動</span><span style="color:#3273c3">{{notices[noticeIdx]}}</span>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM