vue网络请求

post网络请求


import axios from 'axios'
import {ElMessage, ElLoading} from "element-plus"
import { nextTick } from "vue"
import JSONbig from 'json-bigint'
import { userToken } from "@/constants/Constant.js";

const defaultConfig = {
    baseURL: import.meta.env.VITE_BASE_URL,
    timeout: 2 * 60 * 1000,
    headers: {
        Accept: "application/json",
        "Content-Type": "application/json"
    },
    transitional: {
        forcedJSONParsing: false
    },
    transformResponse: (data) => {
        try {
            return JSONbig.parse(data)
        } catch (err) {
            return data
        }
    }
};

const instance = axios.create({
    ...defaultConfig,
})

instance.interceptors.request.use((config) => {
    // 实时动态设置登陆用户token
    config.headers.Authorization = userToken();
    return config;
});

export async function postReq(path, param, config, success, fail) {
    return await instance.post(
        path,
        param || {},
        config,
    ).then(success, fail)
}

/**
 * 
 * @param {*} callback 只返回成功后的数据
 */
export function basePostReq(path, param, callback) {
    sendPostReq(path, param, null, true, true, true, callback)
}

/**
 * 
 * @param {*} isShowLoading 是否显示loading
 * @param {*} isShowError 是否显示报错
 * @param {*} isOnlyData 是否只返回有用的数据
 * @param {*} callback 只返回成功后的数据
 */
export function sendPostReq(path, param, config, isShowLoading, isShowError, isOnlyData, callback) {
    var loading = null
    if (isShowLoading) {
        loading = ElLoading.service({
            lock: true,
            text: '请稍等...',  
            background: 'rgba(0, 0, 0, 0.7)'
        })
    }
    postReq(path, param, config, (res)=> {
        if (loading) {
            nextTick(() => {
                loading.close()
            })
        }
        if (res.data instanceof Blob) {
            if (res.data.type == 'application/json') {
                const reader = new FileReader()
                reader.readAsText(res.data, 'utf-8')
                reader.onload = function() {
                    try {
                        const blobData = JSON.parse(reader.result)
                        if (!blobData.code || blobData.code != 200) {
                            if (isShowError) {
                                ElMessage({
                                    showClose: true,
                                    message: blobData.message || '请求失败',
                                    duration: 0,
                                    type: 'error'
                                })
                            }
                            return
                        }
                        if (isOnlyData && blobData) {
                            if (callback) {
                                callback(blobData.data)
                            }
                        } else {
                            if (callback) {
                                callback(blobData)
                            }
                        }
                    } catch (e) {
                        ElMessage({
                            showClose: true,
                            message: "json解析失败:" + e.toString(),
                            duration: 0,
                            type: 'error'
                        })
                    }
                }
            } else {
                if (callback) {
                    callback(res.data)
                }
            }
            return
        }
        if (!res.data.code || res.data.code != 200) {
            if (isShowError) {
                ElMessage({
                    showClose: true,
                    message: res.data.message || '请求失败',
                    duration: 0,
                    type: 'error'
                })
            }
            return
        }
        if (isOnlyData && res.data) {
            if (callback) {
                callback(res.data.data)
            }
        } else {
            if (callback) {
                callback(res.data)
            }
        }
    }, (err)=> {
        if (loading) {
            nextTick(() => {
                loading.close()
            })
        }
        ElMessage({
            showClose: true,
            message: err.code +', ' + err.message,
            duration: 0,
            type: 'error'
        })
    })
}



  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值