1 Star 1 Fork 0

GPCSoftbase / mini-axios

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

mini-axios(只适用于原生小程序开发)

基于 axios 封装的微信小程序请求工具,完全使用Promise,并提供了请求和响应的拦截器。

引入到项目中

  • 第一步, npm i mini-axios
  • 第二步 import axios from 'mini-axios'
  • 第三步, 复制 dist 目录下的 mini-axios.min.js 到项目中 //如果小程序安装了npm环境可跳过 三、四步
  • 第四步, import axios from './libs/mini-axios.min.js'

快速使用

// http.js
import axios from './libs/mini-axios.min.js'
axios.get(url).then(res => {
    console.log(res )
})
// 或者
axios.defaults.baseURL = 'https://xxx.com/'   // 配置默认地址
axios.get(url).then(res => {
    console.log(res)
})

一次导入到处使用

==默认已经绑定App,Page,Component 与导入的axios是同一个实例==

//只需要在app.js中导入就好
--app.js

    import axios from './libs/mini-axios.min.js'
    App({
        onLaunch: function () {
             this.$axios.get(url).then(res => {
                console.log(res)
             })
        }
    })


--pages/index/index.js
//不需要再导入了 直接使用
     Page({
        onLoad:function(){
             this.$axios.post(url).then(res => {
                console.log(res)
             })
        }
     })

追加参数

let data = {
    userId: 1,
    username:"张三"
};
axios.get(url, data).then(res=> {
    console.log(res)
})

多个请求

function getUserInfo(){
    return axios.get(url)
}
function getList(){
    return axios.get(url)
}
axios.all([getUserInfo, getList]).then(res=> {
    console.log(res)
}).catch(error => {
    console.log(error)
})
axios.race([getUserInfo, getList]).then(res=> {
    console.log(res)
}).catch(error => {
    console.log(error)
})

MiniAxios API

axios(config)

axios({
  method: 'POST',
  url: url,
  data: {
    userId:1
  }
}).then(res=> {
  console.log(res)
}).catch(err => {
  console.log(err)
})

// 或
axios.defaults.baseURL = 'https://xxx.com'
axios({
  method: 'POST',
  url: url,
  data: {
      userId:1
  }
}).then(res=> {
  console.log(res)
})

axios.(url[,config])

axios(url, {
  method: 'POST',
  data: {
      userId:1
  }
}).then(res=> {
  console.log(res)
})

小程序中定义了8种请求类型:微信小程序请求方式

  • ==OPTIONS== HTTP 请求 OPTIONS
  • ==GET== HTTP 请求 GET
  • ==HEAD== HTTP 请求 HEAD
  • ==POST== HTTP 请求 POST
  • ==PUT== HTTP 请求 PUT
  • ==DELETE== HTTP 请求 DELETE
  • ==TRACE== HTTP 请求 TRACE
  • ==CONNECT== HTTP 请求 CONNEC
axios.get(url[, config])
axios.post(url[, config])
axios.put(url[, config])
axios.delete(url[, config])
axios.options(url[, config])
axios.head(url[, config])
axios.trace(url[, config])
axios.connect(url[, config])

创建实例

let server1 = axios.create({
    baseURL: 'https://xxx.com'
})
let server2 = axios.create({
    baseURL: 'https://xxx.com'
})

server1.get(url).then(res => {
    console.log(res)
})

请求配置 axios.defaults

部分配置参数和微信小程序的配置一样 微信小程序 request 请求参数列表

参数 说明 类型 默认值
baseURL 默认地址 baseURL 将自动加在 url 前面 string ''
url url 是用于请求的服务器 URL string ''
data data 是作为请求主体被发送的数据 object {}
header headers 是即将被发送的自定义请求头 object 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8'
method method 是创建请求时使用的方法 string 'GET'
dataType dataType 是返回的数据类型 string 'json'
responseType responseType 是响应的数据类型 string 'text'
axios.defaults.baseURL = 'https://xxx.com'

拦截器 Interceptors

mini-axios 也提供了和 axios 一样的请求拦截和响应拦截,并且可以配置多个

request

// 1.第一个 request 的拦截器
axios.interceptors.request.use(config => {
    config.data.companyId = 1
    // ...do something
    return config
})
// 2.第二个 request 的拦截器,
axios.interceptors.request.use(config => {
    config.data.token = wx.getStorageSync('token')
    // ...do something
    return config
})

response

// 1.第一个 response 的拦截器
axios.interceptors.response.use(response => {
   if (response.statusCode === 200) {
      // ...do something
   }
   console.log(response.header);
    return response
})
// 2.第二个 response 的拦截器,
axios.interceptors.response.use(function (response) {
    if (response.status === 400) {
      // ...do something
   }
    return response
})

License

MIT

MIT License Copyright (c) 2020 偏差 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

基于 axios 封装的微信小程序请求工具,完全使用Promise,并提供了请求和响应的拦截器。 展开 收起
JavaScript
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
JavaScript
1
https://gitee.com/gpcsoftbase/mini-axios.git
git@gitee.com:gpcsoftbase/mini-axios.git
gpcsoftbase
mini-axios
mini-axios
master

搜索帮助