- micro-channel applet acquired network state

    Phone to connect to the Internet in several ways: Wifi, 2G, 3G, 4G, including the soon arrival of 5G, great upload speed and download speed of each mode differences, differences in their billing methods also led to the use of the Internet user when services have different habits.

    With respect to the Wifi several other network connection, which will be faster. Wifi are generally free for users, is the need to charge based on the use of mobile data traffic through the network. We consider such a scenario, the applet will need to download some documents, and then by the ability of small programs to preview the document, these documents may file size is relatively large, for some users, they do not want to spend too much data traffic to preview documents. Considering this situation, we can provide the ability to program gets through a small network status and do some more friendly experience tips.

    Using the acquired network state codes wx.getNetworkType

Code:

  // Preview document 
  lookFile () { 
    wx.getNetworkType ({ 
      Success: function (RES) { 
        // NetworkType field Valid values:
         // wifi / 2g / 3G / 4G / Unknown (less common type of network under Android) / none (non-network) 
        IF (res.networkType == ' WiFi ' ) {
           // download pdf document from a network 
          wx.downloadFile ({ 
            URL: ' https://pic25.nipic.com/20121205/10197997_003647426000_2.jpg ' , 
            Success : function (RES) { 
              // preview the document after a successful download 
              wx.openDocument ({ 
                filePath: res.tempFilePath 
              })
            }
          }) 
        } The else { 
          wx.showModal ({ 
            title: ' prompt ' , 
            Content: ' The current non-Wifi environment, determine download it? ' , 
            ConfirmText: ' OK ' , 
            CancelText: ' Cancel ' , 
            Success: function (RES) { 
              IF (res.confirm) { 
                the console.log ( ' determination operation ' ) 
                wx.downloadFile ({ 
                  URL: 'https://pic25.nipic.com/20121205/10197997_003647426000_2.jpg ' , 
                  Success: function (RES) { 
                    // preview document after a successful download 
                    wx.openDocument ({ 
                      filePath: res.tempFilePath 
                    }) 
                  } 
                }) 
              } the else  IF (res.cancel) { 
                the console.log ( ' cancel ' ) 
                wx.showToast ({ 
                  title: ' successfully canceled ' 
                }) 
              } 
            }  
          })
        }
      }
    })
  }

    In some cases, our phone is connected to a network dynamically change the way, for example, a phone device connected to the jitters Wifi hotspot, often cause the phone to switch from Wifi mobile data network. Applet host environment also provides an interface wx.onNetworkStatusChange a dynamically monitor network status changes, allows developers to adjust in time to experience a small program based on network conditions, wx.onNetworkStatusChange this interface usage scenarios left to the reader to think.

 

 

 

 

.

Guess you like

Origin www.cnblogs.com/jianxian/p/11130432.html