小程序頁(yè)面為空白怎么解決

小新
1317
2020-12-16 08:56:34
欄目: 云計(jì)算

小程序頁(yè)面為空白怎么解決

小程序頁(yè)面為空白,可以設(shè)計(jì)一個(gè)“臨時(shí)加載頁(yè)”來(lái)解決,方法如下:

1.在app.json中,創(chuàng)建一個(gè)新的page,命名為welcome,代碼:"pages/welcome/welcome"(注意該page要放置在進(jìn)入小程序的第一個(gè)頁(yè)面)

2.在welcome.js中,將具體請(qǐng)求操作寫入到onload中,代碼:

// pages/welcome/welcome.js

Page({

  data:{},

  onLoad:function(options){

    // 頁(yè)面初始化 options為頁(yè)面跳轉(zhuǎn)所帶來(lái)的參數(shù)

    var that = getApp()

     try {

     //首先檢查緩存中是否有我們需要請(qǐng)求的數(shù)據(jù),如果沒(méi)有,我們?cè)俑?span id="ssyekqg" class="in-link" data-id="53">服務(wù)器連接,獲取數(shù)據(jù)

     //首次登陸肯定是沒(méi)有的

     that.globalData.userInfo = wx.getStorageSync('userInfo')

     if(wx.getStorageSync('userInfo')!=''){

     //如果緩存不為空,即已經(jīng)存在數(shù)據(jù),我們不用再跟服務(wù)器交互了,那么直接跳轉(zhuǎn)到首頁(yè)

       wx.switchTab({

        url: '../index/index',

     })

     }

     if (value) {

        // Do something with return value

        console.log(that.globalData.userInfo)

      }

    } catch(e){

      // Do something when catch error

      //當(dāng)try中的緩存數(shù)據(jù)不存在時(shí),將跳到這步,這步中,我們將與服務(wù)器進(jìn)行連接,并獲取數(shù)據(jù)

      if(that.globalData.userInfo == ''){

      wx.login({

            success: function(res) {

            //獲取用戶code,轉(zhuǎn)發(fā)到我們的服務(wù)器,再在我們服務(wù)器與微信交互,獲取openid

                var code = res.code

                wx.getUserInfo({

                    success: function(userInfo) {

                        var encryptedData = userInfo.encryptedData

                        var iv = userInfo.iv

                        //我們服務(wù)器請(qǐng)求地址

                        var url = that.apiHost + 'index/login'

                        var userinfo = userInfo.userInfo

                        var username = userinfo.nickName

                        var useravatar =userinfo.avatarUrl

                        var usersex=userinfo.gender

                        wx.request({

                            url: url,

                            method: 'POST',

                            data: {

                                'code': code,

                                'username':username,

                                'useravatar':useravatar,

                                'usersex':usersex

                            },

                            header: {

                          "Content-Type": "application/x-www-form-urlencoded"

                        },

               success:function(response) {

               //數(shù)據(jù)交互成功后,我們將服務(wù)器返回的數(shù)據(jù)寫入全局變量與緩存中

             console.log(response.data)

             //寫入全局變量

             that.globalData.userInfo = response.data

             wx.hideToast()

             //寫入緩存

             wx.setStorage({

                key: 'userInfo',

                data: that.globalData.userInfo,

                success: function(res){

                     console.log("insert success")

                 },

                fail: function() {

                  // fail

                 },

                complete: function() {

                  // complete

                 }

                 })

            //寫入后,我們將跳轉(zhuǎn)到主頁(yè)

            wx.switchTab({

              url: '../index/index',

             })

          },

      failure: function(error) {

               console.log(error)

                            },

                        })

                    }

                })

            }

        })}

    } 

  },

  onReady:function(){

    // 頁(yè)面渲染完成

  },

  onShow:function(){

    // 頁(yè)面顯示

  },

  onHide:function(){

    // 頁(yè)面隱藏

  },

  onUnload:function(){

    // 頁(yè)面關(guān)閉

  },

  redirect:function(){

    wx.switchTab({

      url: '../index/index',

    })

  }

})

3.寫好發(fā)布后,首次登陸時(shí),將進(jìn)入我們的歡迎界面,當(dāng)用戶確認(rèn)公開信息后,頁(yè)面將自動(dòng)跳轉(zhuǎn)到首頁(yè),首頁(yè)數(shù)據(jù)就會(huì)正常顯示。

0