侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

微信小程序中如何使用flyio封装网络请求

微信(小程序)  /  管理员 发布于 3年前   213

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

二、src下新建utils/api.js文件

export const baseUrlApi = 'http://192.168.128.242:8080'//---开发调试环境//export const baseUrlApi = 'https://test.mini.com'//---测试环境https//export const baseUrlApi = 'https://product.mini.com'//---生产环境https

这个里面可以写不同环境或者调试的接口地址

三、src下新建service文件夹

在这个下面不同的模块简历不同的js文件,例如:login-service.js,order-service.js

里面代码示例如下

import { baseUrlApi } from '../utils/api'import request from '../utils/request'export default { // 登录  wxLogin: (data) =>    request.post(`/store-miniApp-web/external/interface/wechat/wxLogin`, data, { baseURL: baseUrlApi }), // 收藏 addCollect: (goodId, status) =>  request.get(`/store-miniApp-web/store/member/addCollect?goodId=${goodId}&status=${status}`,   null, {    baseURL: baseUrlApi   }),}

四、接口请求的使用

import loginApi from "@/service/login-service"; methods: {//-登录  clickLoginBtn() {   var data = {    phone: '18709090909',    password: "123456",   };   console.log("登录参数==", data);   loginApi.wxLogin(data).then(    data => {     if (!data) {      this.$toast(data.msg);      return;     }     if (data.code==0) {      console.log("登录成功", data);       }    },    err => {    }   );  },  //-收藏  collect() {   let isCollect = "1"; //1收藏 0取消   let goodId = "4343434";   loginApi.addCollect(goodsId, isCollect).then(data => {    if (data.code != 0) {     console.log("收藏失败", data);     return;    }    if (isCollect == 1) {     this.$toast("取消成功");    } else {     this.$toast("收藏成功");    }   });  } }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

您可能感兴趣的文章:

  • 详解微信小程序网络请求接口封装实例
  • 微信小程序网络请求封装示例
  • 微信小程序之网络请求简单封装实例详解
  • 微信小程序网络请求wx.request详解及实例
  • 微信小程序 网络请求(post请求,get请求)
  • 微信小程序 POST请求(网络请求)详解及实例代码
  • 微信小程序 网络请求API详解
  • 微信小程序网络请求实现过程解析


  • 上一条:
    使用微信SDK自定义分享的方法
    下一条:
    微信小程序在线客服自动回复功能(基于node)
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 小程序开发之跳转微信直播示例api(0个评论)
    • 在uni_app中开发小程序之常用功能示例代码汇总(0个评论)
    • 小程序开发之多端框架:taro(0个评论)
    • 微信小程序前端使用七牛云官方SDK上传七牛云代码示例(0个评论)
    • 百度小程序审核未通过,真机审核存在点击返回键退出小程序...解决方式之一tabBar(0个评论)
    • 近期文章
    • windows系统中安装FFMpeg及在phpstudy环境php7.3 + php-ffmpeg扩展的使用流程步骤(0个评论)
    • 在go语言中对浮点的数组、切片(slice)进行正向排序和反向排序(0个评论)
    • 在go语言中对整数数组、切片(slice)进行排序和反向排序(0个评论)
    • 在go语言中对字符串数组、切片(slice)进行排序和反向排序(0个评论)
    • 最新国内免注册ChatGPT体验站_ChatGPT镜像站访问链接地址2023/3/28持续更新(0个评论)
    • 在Laravel项目中的实现无密码认证之:发送邮箱链接授权(0个评论)
    • 在go语言中使用GoRoutines实现高性能并发批量调用api示例(0个评论)
    • Docker撤回受争议的收费方案,又可以继续使用docker了(0个评论)
    • 在go语言生成唯一ID之SnowFlake算法(0个评论)
    • ChatGPT再出新功能,推出插件功能,能联网、搜索了(0个评论)
    • 近期评论
    • 博主 在

      2023年国务院办公厅春节放假通知:1月21日起休7天中评论 @ xiaoB 你只管努力,剩下的叫给天意;天若有情天亦老,..
    • xiaoB 在

      2023年国务院办公厅春节放假通知:1月21日起休7天中评论 会不会春节放假后又阳一次?..
    • BUG4 在

      你翻墙过吗?国内使用vpn翻墙可能会被网警抓,你需了解的事中评论 不是吧?..
    • 博主 在

      go语言+beego框架中获取get,post请求的所有参数中评论 @ t1  直接在router.go文件中配就ok..
    • Jade 在

      如何在MySQL查询中获得当月记录中评论 Dear zongscan.com team, We can skyroc..
    • 2017-10
    • 2018-01
    • 2020-03
    • 2021-06
    • 2021-10
    • 2022-03
    • 2023-02
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客