Flyio簡介
Fly.js 通過在不同 JavaScript 運行時通過在底層切換不同的 Http Engine來實現多環境支持,但同時對用戶層提供統一、標準的Promise API。不僅如此,Fly.js還支持請求/響應攔截器、自動轉化JSON、請求轉發等功能,詳情請參考:https://github.com/wendux/fly 。
下面我們看看在微信小程序、mpvue中和中如何使用fly.
Flyio 官方地址
文檔
github地址
Flyio的一些特點
fly.js 是一個基于 promise 的,輕量且強大的Javascript http 網絡庫,它有如下特點:
在小程序中使用flyio請求,封裝代碼如下
一、src下新建utils/request.js文件
var Fly=require("flyio/dist/npm/wx") import { getCache } from '../utils' const request = new Fly() // 全局加載提示 - 設定時間 let ltime = 0; function closeLoading(param) { ltime-- } request.interceptors.request.use((request) => { // 全局加載提示 - 展示提示 // wx.showNavigationBarLoading() ltime++ let dataSource = getCache("dataSource") request.headers = { "Content-Type": "application/x-www-form-urlencoded", "source": "miniApp", "dataSource": dataSource ? dataSource : '' } // 沒用到 if (request.url.indexOf('getReviewInfo') != -1) { closeLoading() return request } // 登錄 console.log('這是token'); console.log(); let type = ''; if(request.url.indexOf("wxLogin") != -1) { type = request.body.loginType; } console.log(getCache("token")); console.log('這是token'); if (request.url.indexOf("wxLogin") == -1 || type == 'WORKBENCH') { // let storeId = getCache("storeId"); let storeCode = getCache("storeCode"); let inviter = getCache("inviter"); let token = getCache("token"); request.headers = { "Content-Type": "application/x-www-form-urlencoded", "source": "miniApp", "token": token, "storeCode": storeCode, "inviter": inviter } console.log('打印request'); console.log(request); console.log('打印request'); let dataSource = getCache("dataSource") if (dataSource) { request.headers['dataSource'] = dataSource } } return request }) request.interceptors.response.use((response, promise) => { closeLoading() // wx.hideNavigationBarLoading() // 微信運維統計 if (response.status) { wx.reportMonitor('0', +(response.status)) } if (response.headers.date) { let time = new Date().getTime() - new Date(response.headers.date).getTime() wx.reportMonitor('1', +(time)) } // 錯誤提示 if (response.status != 200) { wx.showToast({ title: '出錯啦!請稍后再試試哦~', icon: 'none', duration: 2000 }) } return promise.resolve(response.data) }, (err, promise) => { wx.hideNavigationBarLoading() return promise.resolve() } ) export default request
另外有需要云服務器可以了解下創新互聯scvps.cn,海內外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業上云的綜合解決方案,具有“安全穩定、簡單易用、服務可用性高、性價比高”等特點與優勢,專為企業上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。
分享標題:微信小程序中如何使用flyio封裝網絡請求-創新互聯
URL分享:http://m.kartarina.com/article16/ccjodg.html
成都網站建設公司_創新互聯,為您提供小程序開發、外貿網站建設、網站維護、軟件開發、定制開發、網站設計
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯