vue封装http请求

import axios from 'axios'
import isObject from 'lodash/isObject'


const http = function (api, data = {}, withoutToken = false) {
    const { url, method } = isObject(api) ? api : { url: api, method: 'post' }
    const accessToken=localStorage.getItem('accessToken')
    const platform='pc'
    const v='2'

    return axios({
        url: withoutToken ? `${url}?platform=${platform}&v=${v}` : `${url}?accessToken=${accessToken}&platform=${platform}&v=${v}`,
        method,
        data,
    }).catch(e => {
        throw new Error('网络异常')
    }).then(res => {
        if(res.data.code===2001||res.data.code===2002){
            this.$confirm(res.data.message[0].details, '鲸保网提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
              }).then(() => {
                this.$router.push({path:'/my/order/list'})
              }).catch(() => {
                this.$message({
                  type: 'info',
                  message: '已取消'
                });
              });
        }else if (res.data.code === 502) {
            this.$message({
                message: '您现在是未登录状态,请前去登录',
                type: 'warning'
              });
            this.$router.replace({ path: '/login' })
        } else if (res.data.code == 500) {
            throw new Error(res.data.message[0].details)
        }
        return res.data
    })
}

export default http


posted @   李美玲  阅读(4920)  评论(0)    收藏  举报
编辑推荐:
· 为什么PostgreSQL不自动缓存执行计划?
· 于是转身独立开发者
· C#.Net筑基-泛型T & 协变逆变
· dotnet 代码调试方法
· DbContext是如何识别出实体集合的
阅读排行:
· 10亿订单如何分库分表?
· 【Cursor保姆级教程】零基础小白从安装到实战,手把手教你玩转AI编程神器!
· 如何基于three.js(webgl)引擎架构,实现3D医院、3D园区导航,3D科室路径导航
· C# 的深度强化学习框架RL_Matrix
· MySQL查询执行顺序:一张图看懂SQL是如何工作的
点击右上角即可分享
微信分享提示