赚流量卷,晚点删

自己封装ajax方法

import ajax from '@/libs/ajax';
import qs from "qs";
import Config from '@/config';
import { getProtocolAndHostname } from '@/libs/util';

const AjaxPlugin = {};
// const baseUrl = process.env.NODE_ENV === 'development' ? Config.baseUrl.dev : Config.baseUrl.pro;
const baseUrl = getProtocolAndHostname() + "/api";
// console.log(baseUrl);
AjaxPlugin.install = async function (Vue, options) {
    //封装post请求以及界面反馈
    Vue.prototype.post = async function (url, param, notShowMessage = false) {
        let loadingCall = () => { }
        try {
            if (!notShowMessage) {
                loadingCall = this.$Message.loading({ content: '正在发送请求', duration: 10 });
            }

            const resp = await ajax.post(baseUrl + "/" + (url.substr(0, 1) === '/' ? url.substr(1) : url), param);
            if (resp.status > 400) {
                throw new Error('response status is not 200');
            }
            if (resp.data.__error__) {//后端返回的明确错误
                this.$Modal.warning({ title: '请求异常', content: resp.data.__error__ });
                let err = new Error(resp.data.__error__);
                err.name = 'apiError';
                throw err;
            }

            if (!notShowMessage) {
                this.$Message.info('请求成功');
            }
            return resp;
        } catch (e) {
            this.$Message.error('请求失败');
            throw e;
        } finally {
            loadingCall()
        }
    };
    Vue.prototype.get = async function (url, param, notShowMessage = false) {
        let loadingCall = () => { }
        try {
            if (!notShowMessage) {
                loadingCall = this.$Message.loading({ content: '正在发送请求', duration: 10 });
            }
            const resp = await ajax.get(baseUrl + "/" + (url.substr(0, 1) === '/' ? url.substr(1) : url) + "?1=1&" + qs.stringify(param));
            if (resp.status > 400) {
                throw new Error('response status is not 200');
            }
            if (resp.data.__error__) {//后端返回的明确错误
                this.$Modal.warning({ title: '请求异常', content: resp.data.__error__ });
                let err = new Error(resp.data.__error__);
                err.name = 'apiError';
                throw err;
            }
            this.$Message.info('请求成功');
            return resp;
        } catch (e) {
            this.$Message.error('请求失败');
            throw e;
        } finally {
            loadingCall()
        }
    }
}

export default AjaxPlugin;

最近更新

  1. TCP协议是安全的吗?

    2024-06-13 21:00:03       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-13 21:00:03       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-13 21:00:03       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-13 21:00:03       18 阅读

热门阅读

  1. A.计算圆周率——无穷级数法

    2024-06-13 21:00:03       8 阅读
  2. 【一个 Android 反编译神器jadx】

    2024-06-13 21:00:03       8 阅读
  3. 热门开源项目推荐:技术与地址概览

    2024-06-13 21:00:03       12 阅读
  4. Codeforces Round 952 (Div. 4)(实时更新)

    2024-06-13 21:00:03       10 阅读
  5. 算法设计与分析复习(第5章 回溯法)

    2024-06-13 21:00:03       8 阅读
  6. 2563. 统计公平数对的数目

    2024-06-13 21:00:03       6 阅读
  7. 笔记97:C++ 中 string / char 和 int 之间相互转化

    2024-06-13 21:00:03       8 阅读
  8. (2024最新)CentOS 7上在线安装MySQL 5.7|喂饭级教程

    2024-06-13 21:00:03       7 阅读
  9. cuda 架构设置

    2024-06-13 21:00:03       6 阅读
  10. 【npm如何发布自己的插件包】

    2024-06-13 21:00:03       6 阅读
  11. chat gpt基本原理解读

    2024-06-13 21:00:03       9 阅读
  12. 标准化数据

    2024-06-13 21:00:03       6 阅读
  13. 使用nmcli命令创建、删除bond

    2024-06-13 21:00:03       6 阅读
  14. 《阅读的方法》读后感——超越期待的收获

    2024-06-13 21:00:03       5 阅读