Post请求Loading及封装

Post请求Loading及封装

1.组件

  • axios

  • sweetalert

2.目标

对post请求进行封装的目标及最终实现如下:

  • 实现在请求时弹出遮罩层

  • 在header中设置Authorization

  • 发生错误弹出错误提示信息

  • 鉴权失败后跳转到登录页面

3.loading代码

使用 JS 代码实现在页面上显示遮罩层及图片,代码如下:

/*
 * GUID
 */
function guid () {
  function S4 () {
    return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1)
  }
  return (S4() + S4() + '-' + S4() + '-' + S4() + '-' + S4() + '-' + S4() + S4() + S4())
}
​
/*
 * 显示loading遮罩层
 */
function loading () {
  var id = guid()
​
  var maskBg = document.createElement('div')
  maskBg.id = 'mask_bg_' + id
  maskBg.style.position = 'absolute'
  maskBg.style.top = '0px'
  maskBg.style.left = '0px'
  maskBg.style.width = '100%'
  maskBg.style.height = '100%'
  maskBg.style.backgroundColor = '#777'
  maskBg.style.opacity = 0.6
  maskBg.style.zIndex = 10001
  document.body.appendChild(maskBg)
​
  var maskMsg = document.createElement('div')
  maskMsg.style.position = 'absolute'
  maskMsg.style.top = '43%'
  maskMsg.style.left = '43%'
  maskMsg.style.textAlign = 'center'
  maskMsg.style.zIndex = 10002
​
  var image = document.createElement('img')
  image.src = '/images/rotate_loader.svg' // 加载图片
  maskMsg.appendChild(image)
​
  maskBg.appendChild(maskMsg)
​
  return maskBg.id
}
/*
 * 关闭遮罩层
 */
function loaded (id) {
  var maskBg = document.getElementById(id)
  if (maskBg != null) {
    maskBg.parentNode.removeChild(maskBg)
  }
}
​
export {loading, loaded}

4.post请求封装

代码如下:

import axios from 'axios'
import swal from 'sweetalert'
import {loading, loaded} from './loading'
​
const config = {
  baseURL: '',
  timeout: 60000,
  headers: {
    'Content-Type': 'application/json'
  },
  responseType: 'json'
}
​
const postRequest = (url, data) => {
  let _id = loading()
  // eslint-disable-next-line
  const promise = new Promise((resolve, reject) => {
    // 设置Headers的Authorization,鉴权Token
    config.headers['Authorization'] =
        localStorage.getItem('token_type') + ' ' + localStorage.getItem('access_token')
​
    axios.post(
      url, data, config
    ).then((response) => {
      loaded(_id)
      resolve(response)
    }).catch((error) => {
      loaded(_id)
      console.log(error)
      let status = error.response.status
      swal(status.toString(), error.message, 'error')
​
      if (status === 401) {
        swal({
          title: '鉴权失败',
          text: error.message,
          icon: 'error',
          button: "确定",
        }).then((value) => {
          console.log(value)
          // 路由跳转
        })
      }
      // reject(error)
    })
  })
​
  return promise
}

5.Vue中使用

import {postRequest} from "@/assets/utils/utils"
​
let _this = this
postRequest(
    'url',  // url
    data // Object
).then((response) => {
    ......
})
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Janeb1018

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值