|
在微信小程序中實(shí)現(xiàn)網(wǎng)絡(luò)請求相對于Android來說感覺簡單很多,我們只需要使用其提供的API就可以解決網(wǎng)絡(luò)請求問題。
為了數(shù)據(jù)安全,微信小程序網(wǎng)絡(luò)請求只支持https,當(dāng)然各個(gè)參數(shù)的含義就不在細(xì)說,不熟悉的話可以;可以去閱讀官方文檔的網(wǎng)絡(luò)請求api,當(dāng)我們使用request時(shí)header的content-typ默認(rèn)是application/json,在文檔中指出method 的value必須是大寫,不過經(jīng)過測試,小寫也能請求成功。request默認(rèn)的超時(shí)時(shí)間是60s,如果我們想自定義超時(shí)時(shí)間,我們可以在app.json中加入下面代碼片段,分別設(shè)置request,socket,和上傳文件及下載文件的超時(shí)時(shí)間。
"networkTimeout": {
"request": 5000,
"connectSocket": 5000,
"uploadFile": 5000,
"downloadFile": 5000
}
設(shè)置過超時(shí)時(shí)間,我們就開始封裝網(wǎng)絡(luò)請求,平時(shí)我們所接觸的網(wǎng)絡(luò)請求,一般會(huì)分為兩類,一類是在后臺(tái)運(yùn)行的,沒有加載對話框提示,另一種就是有提示,如提示正在加載數(shù)據(jù),,那么我們就以此為線索來進(jìn)行封裝。先創(chuàng)建一個(gè)network的網(wǎng)絡(luò)請求工具類,然后
// 展示進(jìn)度條的網(wǎng)絡(luò)請求
// url:網(wǎng)絡(luò)請求的url
// params:請求參數(shù)
// message:進(jìn)度條的提示信息
// success:成功的回調(diào)函數(shù)
// fail:失敗的回調(diào)
function requestLoading(url, params, message, success, fail) {
console.log(params)
wx.showLoading({
title: message,
})
wx.request({
url: url,
data: params,
header: {
'content-type': 'application/x-www-form-urlencoded'
},
method: 'post',
success: function (res) {
//console.log(res.data)
wx.hideLoading()
if (res.statusCode == 200) {
success(res.data)
} else {
fail()
}
},
fail: function (res) {
wx.hideLoading()
fail()
},
complete: function (res) {
},
})
}
上面函數(shù)很好理解,參數(shù)的含義已在代碼中解釋,在網(wǎng)絡(luò)請求開始前,先展示Loading對話框,提示用戶當(dāng)前網(wǎng)絡(luò)正在請求數(shù)據(jù),當(dāng)網(wǎng)絡(luò)請求成功或者失敗后調(diào)用wx.hideLoading()取消提示框的展示。在api中還提供了wx.showNavigationBarLoading()用于顯示當(dāng)前頁面的導(dǎo)航條加載動(dòng)畫,那么如果我們想展示這個(gè)動(dòng)畫可以在requestLoading執(zhí)行開始調(diào)用wx.showNavigationBarLoading(),然后在網(wǎng)絡(luò)請求成功或者失敗后調(diào)用wx.hideNavigationBarLoading()隱藏導(dǎo)航欄加載動(dòng)畫。
當(dāng)網(wǎng)絡(luò)請求成功并且狀態(tài)碼為200時(shí),將請求到的數(shù)據(jù)回調(diào)通過su
//不顯示對話框的請求
function request(url, params, success, fail) {
this.requestLoading(url, params, "", success, fail)
}
我們看到我們最終還是調(diào)用的requestLoading,那么我們可以在該函數(shù)作下判斷,如果提示信息message==''就不顯示對話框。
function request(url, params, success, fail) {
this.requestLoading(url, params, "", success, fail)
}
// 展示進(jìn)度條的網(wǎng)絡(luò)請求
// url:網(wǎng)絡(luò)請求的url
// params:請求參數(shù)
// message:進(jìn)度條的提示信息
// success:成功的回調(diào)函數(shù)
// fail:失敗的回調(diào)
function requestLoading(url, params, message, success, fail) {
console.log(params)
wx.showNavigationBarLoading()
if (message != "") {
wx.showLoading({
title: message,
})
}
wx.request({
url: url,
data: params,
header: {
//'Content-Type': 'application/json'
'content-type': 'application/x-www-form-urlencoded'
},
method: 'post',
success: function (res) {
//console.log(res.data)
wx.hideNavigationBarLoading()
if (message != "") {
wx.hideLoading()
}
if (res.statusCode == 200) {
success(res.data)
} else {
fail()
}
},
fail: function (res) {
wx.hideNavigationBarLoading()
if (message != "") {
wx.hideLoading()
}
fail()
},
complete: function (res) {
},
})
}
module.exports = {
request: request,
requestLoading: requestLoading
}
使用就很簡單了,如下
//路徑根據(jù)自己項(xiàng)目路徑修改
var network = require("/utils/network.js")
getData:function(){
network.requestLoading(URL.MY_SCORE, that.data.params, '正在加載數(shù)據(jù)', function (res) {
//res就是我們請求接口返回的數(shù)據(jù)
console.log(res)
}, function () {
wx.showToast({
title: '加載數(shù)據(jù)失敗',
})
})
}
|