国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 熱點 > 微信 > 正文

微信小程序中如何使用flyio封裝網絡請求

2024-07-22 01:18:05
字體:
來源:轉載
供稿:網友

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 網絡庫,它有如下特點:

提供統一的 Promise API。 瀏覽器環境下,輕量且非常輕量 。 支持多種JavaScript 運行環境 支持請求/響應攔截器。 自動轉換 JSON 數據。 支持切換底層 Http Engine,可輕松適配各種運行環境。 瀏覽器端支持全局Ajax攔截 。 H5頁面內嵌到原生 APP 中時,支持將 http 請求轉發到 Native。支持直接請求圖片。

在小程序中使用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            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 郑州市| 开平市| 阳朔县| 公主岭市| 林口县| 建湖县| 常州市| 庆安县| 石棉县| 松桃| 巍山| 灌云县| 普兰店市| 林芝县| 连山| 甘泉县| 平顶山市| 九龙城区| 弥勒县| 休宁县| 华池县| 恭城| 北川| 禄劝| 鄂伦春自治旗| 连云港市| 邵阳市| 辉南县| 微山县| 潜山县| 昌江| 蒙阴县| 西乌珠穆沁旗| 辽阳县| 格尔木市| 丰都县| 武山县| 河津市| 凤凰县| 紫阳县| 香港|